Project

General

Profile

1
<?php
2
/**
3
 * Convert full qualified, local URLs into relative URLs
4
 * @copyright       Manuela v.d.Decken <manuela@isteam.de>
5
 * @author          Manuela v.d.Decken <manuela@isteam.de>
6
 * @param string $sContent
7
 * @return string
8
 */
9
    function doFilterRelUrl($sContent) {
10
        $aFilterSettings = getOutputFilterSettings();
11
        $key = preg_replace('=^.*?filter([^\.\/\\\\]+)(\.[^\.]+)?$=is', '\1', __FILE__);
12
        if ($aFilterSettings[$key]) {
13
            $sAppUrl  = rtrim(str_replace('\\', '/', WB_URL), '/').'/';
14
            $sAppPath = rtrim(str_replace('\\', '/', WB_PATH), '/').'/';
15
            $sAppRel  = preg_replace('/^https?:\/\/[^\/]*(.*)$/is', '$1', $sAppUrl);
16
            $sDocRoot = preg_replace('/^(.*?)'.preg_quote($sAppRel, '/').'$/', '$1', $sAppUrl);
17
            $sContent = preg_replace_callback(
18
                '/((?:href|src|action)\s*=\s*")([^\?\"]+?)/isU',
19
                function ($aMatches) use ($sAppUrl, $sAppPath, $sAppRel) {
20
                    $aMatches[2] = str_replace('\\', '/', $aMatches[2]);
21
                    if ($aMatches[2][0] ='/') { $aMatches[2] = rtrim($sAppUrl, '/').$aMatches[2]; }
22
                    $aMatches[2] = preg_replace('/^'.preg_quote($sAppUrl, '/').'/is', '', $aMatches[2]);
23
                    $aMatches[2] = preg_replace('/(\.+\/)|(\/+)/', '/', $aMatches[2]);
24
                    if (!is_readable($sAppPath.$aMatches[2])) {
25
                    // in case of death link show original link
26
                        return $aMatches[0];
27
                    } else {
28
                        return $aMatches[1].$sAppRel.$aMatches[2];
29
                    }
30
                },
31
                $sContent
32
            );
33
/* Original SP7 Manu Fix */
34
            // restore canonical relation links
35
            $sContent = preg_replace_callback(
36
                '/<link\s[^>]*?\"(canonical|alternate)\"[^>]*?>/isU',
37
                function($aMatches) use ($sDocRoot) {
38
                    return preg_replace(
39
                        '/(href\s*=\s*\")([^\"]*?)/siU',
40
                        '\1'.rtrim($sDocRoot, '/').'\2',
41
                        $aMatches[0]
42
                    );
43
                },
44
                $sContent
45
            );
46
        }
47
        return $sContent;
48
    }
(8-8/12)