Project

General

Profile

1
<?php
2
/*
3
 * replace all "[wblink{page_id}]" with real links
4
 * @copyright       Manuela v.d.Decken <manuela@isteam.de>
5
 * @author          Manuela v.d.Decken <manuela@isteam.de>
6
 * @param string $content : content with tags
7
 * @return string content with links
8
 */
9
    function doFilterWbLink($content)
10
    {
11
        $aFilterSettings = getOutputFilterSettings();
12
        $key = preg_replace('=^.*?filter([^\.\/\\\\]+)(\.[^\.]+)?$=is', '\1', __FILE__);
13
        if ($aFilterSettings[$key]) {}
14
        global $database;
15
            $pattern = '/\[wblink([0-9]+)\]/isU';
16
            if (preg_match_all($pattern, $content, $aMatches, PREG_SET_ORDER))
17
            {
18
                $aSearchReplaceList = array();
19
                foreach ($aMatches as $aMatch) {
20
                     // collect matches formatted like '[wblink123]' => 123
21
                    $aSearchReplaceList[strtolower($aMatch[0])] = $aMatch[1];
22
                }
23
                // build list of PageIds for SQL query
24
                $sPageIdList = implode(',', $aSearchReplaceList); // '123,124,125'
25
                // replace all PageIds with '#' (stay on page death link)
26
                array_walk($aSearchReplaceList, function(&$value, $index){ $value = '#'; });
27
                $sql = 'SELECT `page_id`, `link` FROM `'.TABLE_PREFIX.'pages` '
28
                     . 'WHERE `page_id` IN('.$sPageIdList.')';
29
                if (($oPages = $database->query($sql))) {
30
                    while (($aPage = $oPages->fetchRow(MYSQLI_ASSOC))) {
31
                        $aPage['link'] = ($aPage['link']
32
                                         ? PAGES_DIRECTORY.$aPage['link'].PAGE_EXTENSION
33
                                         : '#');
34
                        // collect all search-replace pairs with valid links
35
                        if (is_readable(WB_PATH.$aPage['link'])) {
36
                            // replace death link with found and valide link
37
                            $aSearchReplaceList['[wblink'.$aPage['page_id'].']'] =
38
                                WB_URL.$aPage['link'];
39
                        }
40
                    }
41
                }
42
                // replace all found [wblink**] tags with their urls
43
                $content = str_ireplace(
44
                    array_keys($aSearchReplaceList),
45
                    $aSearchReplaceList,
46
                    $content
47
                );
48
            }
49

    
50
        return $content;
51
    }
(12-12/12)