Passed
Push — develop ( 0cf906...cb779b )
by Jens
06:50
created

functions.php ➔ getRelativePath()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 32
nop 2
dl 0
loc 36
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/** @noinspection PhpDocSignatureInspection */
3
4
/**
5
 * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
6
 * Dies afterwards
7
 *
8
 * @param mixed ...    The data to be displayed
9
 */
10
function dump()
11
{
12
    $debug_backtrace = current(debug_backtrace());
13
    if (PHP_SAPI == 'cli') {
14
        echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
15
        foreach (func_get_args() as $data) {
16
            var_dump($data);
17
        }
18
    } else {
19
        ob_clean();
20
        echo <<<END
21
<!DOCTYPE html>
22
<html>
23
<head>
24
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
25
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
26
<script>hljs.initHighlightingOnLoad();</script>
27
<style>code.php.hljs{margin: -0.4em 0;}code.active{background-color:#e0e0e0;}code:before{content:attr(data-line);}code.active:before{color:#c00;font-weight:bold;}</style>
28
</head>
29
<body>
30
END;
31
32
        echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
33
        echo '<pre>';
34
        foreach (func_get_args() as $data) {
35
            echo "<code>";
36
            var_dump($data);
37
            echo "</code>";
38
        }
39
        echo '</pre>';
40
        echo <<<END
41
</body>
42
</html>
43
END;
44
    }
45
    die;
46
}
47
48
/**
49
 * Minify the html for the outputbuffer
50
 *
51
 * @param $buffer
52
 * @return mixed
53
 */
54
function sanitize_output($buffer)
55
{
56
    if (!isset($_GET['unsanitized'])) {
57
        $search = array(
58
            '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
59
            '/[^\S ]+\</s',     // strip whitespaces before tags, except space
60
            '/(\s)+/s',         // shorten multiple whitespace sequences
61
            '/<!--(.|\s)*?-->/' // Remove HTML comments
62
        );
63
64
        $replace = array(
65
            '>',
66
            '<',
67
            '\\1',
68
            ''
69
        );
70
71
        $buffer = preg_replace($search, $replace, $buffer);
72
73
        return $buffer;
74
    } else {
75
        return $buffer;
76
    }
77
}
78
79
80
/**
81
 * Convert all values of an array to utf8
82
 *
83
 * @param $array
84
 * @return array
85
 */
86
function utf8Convert($array)
87
{
88
    array_walk_recursive($array, function (&$item) {
89
        if (!mb_detect_encoding($item, 'utf-8', true)) {
90
            $item = utf8_encode($item);
91
        }
92
    });
93
94
    return $array;
95
}
96
97
/**
98
 * Calculate the relative path from $from to $to
99
 * Derived from https://stackoverflow.com/a/2638272/
100
 *
101
 * @param $from
102
 * @param $to
103
 * @return string
104
 */
105
function getRelativePath($from, $to)
106
{
107
    // some compatibility fixes for Windows paths
108
    $from = is_dir($from) ? rtrim($from, '\/') . DIRECTORY_SEPARATOR : $from;
109
    $to = is_dir($to) ? rtrim($to, '\/') . DIRECTORY_SEPARATOR : $to;
110
    $from = str_replace('\\', DIRECTORY_SEPARATOR, $from);
111
    $to = str_replace('\\', DIRECTORY_SEPARATOR, $to);
112
113
    $from = explode(DIRECTORY_SEPARATOR, $from);
114
    $to = explode(DIRECTORY_SEPARATOR, $to);
115
    $relPath = $to;
116
117
    foreach ($from as $depth => $dir) {
118
        // find first non-matching dir
119
        if ($dir === $to[$depth]) {
120
            // ignore this directory
121
            array_shift($relPath);
122
        } else {
123
            // get number of remaining dirs to $from
124
            $remaining = count($from) - $depth;
125
            if ($remaining > 1) {
126
                // add traversals up to first matching dir
127
                $padLength = (count($relPath) + $remaining - 1) * -1;
128
                $relPath = array_pad($relPath, $padLength, '..');
129
                break;
130
            } else {
131
                $relPath[0] = '.' . DIRECTORY_SEPARATOR . $relPath[0];
132
            }
133
        }
134
    }
135
    $relPath = implode(DIRECTORY_SEPARATOR, $relPath);
136
    while (strpos($relPath, '.' . DIRECTORY_SEPARATOR . '.' . DIRECTORY_SEPARATOR) !== false) {
137
        $relPath = str_replace('.' . DIRECTORY_SEPARATOR . '.' . DIRECTORY_SEPARATOR, '.' . DIRECTORY_SEPARATOR, $relPath);
138
    }
139
    return $relPath;
140
}