OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / kcfinder-2.51 / lib / helper_path.php
1 <?php
2
3 /** This file is part of KCFinder project
4   *
5   *      @desc Path helper class
6   *   @package KCFinder
7   *   @version 2.51
8   *    @author Pavel Tzonkov <pavelc@users.sourceforge.net>
9   * @copyright 2010, 2011 KCFinder Project
10   *   @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
11   *   @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
12   *      @link http://kcfinder.sunhater.com
13   */
14
15 class path {
16
17   /** Get the absolute URL path of the given one. Returns FALSE if the URL
18     * is not valid or the current directory cannot be resolved (getcwd())
19     * @param string $path
20     * @return string */
21
22     static function rel2abs_url($path) {
23         if (substr($path, 0, 1) == "/") return $path;
24         $dir = @getcwd();
25
26         if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false))
27             return false;
28
29         $dir = self::normalize($dir);
30         $doc_root = self::normalize($_SERVER['DOCUMENT_ROOT']);
31
32         if (substr($dir, 0, strlen($doc_root)) != $doc_root)
33             return false;
34
35         $return = self::normalize(substr($dir, strlen($doc_root)) . "/$path");
36         if (substr($return, 0, 1) !== "/")
37             $return = "/$return";
38
39         return $return;
40     }
41
42   /** Resolve full filesystem path of given URL. Returns FALSE if the URL
43     * cannot be resolved
44     * @param string $url
45     * @return string */
46
47     static function url2fullPath($url) {
48         $url = self::normalize($url);
49
50         $uri = isset($_SERVER['SCRIPT_NAME'])
51             ? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF'])
52             ? $_SERVER['PHP_SELF']
53             : false);
54
55         $uri = self::normalize($uri);
56
57         if (substr($url, 0, 1) !== "/") {
58             if ($uri === false) return false;
59             $url = dirname($uri) . "/$url";
60         }
61
62         if (isset($_SERVER['DOCUMENT_ROOT'])) {
63             return self::normalize($_SERVER['DOCUMENT_ROOT'] . "/$url");
64
65         } else {
66             if ($uri === false) return false;
67
68             if (isset($_SERVER['SCRIPT_FILENAME'])) {
69                 $scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']);
70                 return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url");
71             }
72
73             $count = count(explode('/', $uri)) - 1;
74             for ($i = 0, $chdir = ""; $i < $count; $i++)
75                 $chdir .= "../";
76             $chdir = self::normalize($chdir);
77
78             $dir = getcwd();
79             if (($dir === false) || !@chdir($chdir))
80                 return false;
81             $rdir = getcwd();
82             chdir($dir);
83             return ($rdir !== false) ? self::normalize($rdir . "/$url") : false;
84         }
85     }
86
87   /** Normalize the given path. On Windows servers backslash will be replaced
88     * with slash. Remobes unnecessary doble slashes and double dots. Removes
89     * last slash if it exists. Examples:
90     * path::normalize("C:\\any\\path\\") returns "C:/any/path"
91     * path::normalize("/your/path/..//home/") returns "/your/home"
92     * @param string $path
93     * @return string */
94
95     static function normalize($path) {
96         if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
97             $path = preg_replace('/([^\\\])\\\([^\\\])/', "$1/$2", $path);
98             if (substr($path, -1) == "\\") $path = substr($path, 0, -1);
99             if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1);
100         }
101
102         $path = preg_replace('/\/+/s', "/", $path);
103
104         $path = "/$path";
105         if (substr($path, -1) != "/")
106             $path .= "/";
107
108         $expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s';
109         while (preg_match($expr, $path))
110             $path = preg_replace($expr, "/", $path);
111
112         $path = substr($path, 0, -1);
113         $path = substr($path, 1);
114         return $path;
115     }
116
117   /** Encode URL Path
118     * @param string $path
119     * @return string */
120
121     static function urlPathEncode($path) {
122         $path = self::normalize($path);
123         $encoded = "";
124         foreach (explode("/", $path) as $dir)
125             $encoded .= rawurlencode($dir) . "/";
126         $encoded = substr($encoded, 0, -1);
127         return $encoded;
128     }
129
130   /** Decode URL Path
131     * @param string $path
132     * @return string */
133
134     static function urlPathDecode($path) {
135         $path = self::normalize($path);
136         $decoded = "";
137         foreach (explode("/", $path) as $dir)
138             $decoded .= rawurldecode($dir) . "/";
139         $decoded = substr($decoded, 0, -1);
140         return $decoded;
141     }
142 }
143
144 ?>