Passed
Branch master (f2d2e3)
by Michael
18:45
created

path::normalize()   A

Complexity

Conditions 6
Paths 20

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 14
c 1
b 1
f 0
nc 20
nop 1
dl 0
loc 22
rs 9.2222
1
<?php
2
3
/** This file is part of KCFinder project
4
  *
5
  *      @desc Path helper class
6
  *   @package KCFinder
7
  *   @version 3.12
8
  *    @author Pavel Tzonkov <[email protected]>
9
  * @copyright 2010-2014 KCFinder Project
10
  *   @license http://opensource.org/licenses/GPL-3.0 GPLv3
11
  *   @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12
  *      @link http://kcfinder.sunhater.com
13
  */
14
15
namespace kcfinder;
16
17
class path {
18
19
/** Get the absolute URL path of the given one. Returns FALSE if the URL
20
  * is not valid or the current directory cannot be resolved (getcwd())
21
  * @param string $path
22
  * @return string */
23
24
    static function rel2abs_url($path) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
        if (substr($path, 0, 1) == "/") return $path;
26
        $dir = @getcwd();
27
28
        if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false))
29
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
30
31
        $dir = self::normalize($dir);
32
        $doc_root = self::normalize(realpath($_SERVER['DOCUMENT_ROOT']));
33
34
        if (substr($dir, 0, strlen($doc_root)) != $doc_root)
35
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
36
37
        $return = self::normalize(substr($dir, strlen($doc_root)) . "/$path");
38
        if (substr($return, 0, 1) !== "/")
39
            $return = "/$return";
40
41
        return $return;
42
    }
43
44
/** Resolve full filesystem path of given URL. Returns FALSE if the URL
45
  * cannot be resolved
46
  * @param string $url
47
  * @return string */
48
49
    static function url2fullPath($url) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
        $url = self::normalize($url);
51
52
        $uri = isset($_SERVER['SCRIPT_NAME'])
53
            ? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF'])
54
            ? $_SERVER['PHP_SELF']
55
            : false);
56
57
        $uri = self::normalize($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri can also be of type false; however, parameter $path of kcfinder\path::normalize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $uri = self::normalize(/** @scrutinizer ignore-type */ $uri);
Loading history...
58
59
        if (substr($url, 0, 1) !== "/") {
60
            if ($uri === false) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
61
            $url = dirname($uri) . "/$url";
62
        }
63
64
        if (isset($_SERVER['DOCUMENT_ROOT'])) {
65
            return self::normalize(realpath($_SERVER['DOCUMENT_ROOT']) . "/$url");
66
67
        } else {
68
            if ($uri === false) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
69
70
            if (isset($_SERVER['SCRIPT_FILENAME'])) {
71
                $scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']);
72
                return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url");
73
            }
74
75
            $count = count(explode('/', $uri)) - 1;
76
            for ($i = 0, $chdir = ""; $i < $count; $i++)
77
                $chdir .= "../";
78
            $chdir = self::normalize($chdir);
79
80
            $dir = getcwd();
81
            if (($dir === false) || !@chdir($chdir))
82
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
83
            $rdir = getcwd();
84
            chdir($dir);
85
            return ($rdir !== false) ? self::normalize($rdir . "/$url") : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rdir !== false ?...dir . '/'.$url) : false could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
86
        }
87
    }
88
89
/** Normalize the given path. On Windows servers backslash will be replaced
90
  * with slash. Removes unnecessary double slashes and double dots. Removes
91
  * last slash if it exists. Examples:
92
  * path::normalize("C:\\any\\path\\") returns "C:/any/path"
93
  * path::normalize("/your/path/..//home/") returns "/your/home"
94
  * @param string $path
95
  * @return string */
96
97
    static function normalize($path) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
99
        // Backslash to slash convert
100
        if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
101
            $path = preg_replace('/([^\\\])\\\+([^\\\])/s', "$1/$2", $path);
102
            if (substr($path, -1) == "\\") $path = substr($path, 0, -1);
103
            if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1);
104
        }
105
106
        $path = preg_replace('/\/+/s', "/", $path);
107
108
        $path = "/$path";
109
        if (substr($path, -1) != "/")
110
            $path .= "/";
111
112
        $expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s';
113
        while (preg_match($expr, $path))
114
            $path = preg_replace($expr, "/", $path);
115
116
        $path = substr($path, 0, -1);
117
        $path = substr($path, 1);
118
        return $path;
119
    }
120
121
/** Encode URL Path
122
  * @param string $path
123
  * @return string */
124
125
    static function urlPathEncode($path) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
126
        $path = self::normalize($path);
127
        $encoded = "";
128
        foreach (explode("/", $path) as $dir)
129
            $encoded .= rawurlencode($dir) . "/";
130
        $encoded = substr($encoded, 0, -1);
131
        return $encoded;
132
    }
133
134
/** Decode URL Path
135
  * @param string $path
136
  * @return string */
137
138
    static function urlPathDecode($path) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
        $path = self::normalize($path);
140
        $decoded = "";
141
        foreach (explode("/", $path) as $dir)
142
            $decoded .= rawurldecode($dir) . "/";
143
        $decoded = substr($decoded, 0, -1);
144
        return $decoded;
145
    }
146
147
}
148