helper_path.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Path helper class
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Pavel Tzonkov <sunhater@sunhater.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. namespace kcfinder;
  14. class path {
  15. /** Get the absolute URL path of the given one. Returns FALSE if the URL
  16. * is not valid or the current directory cannot be resolved (getcwd())
  17. * @param string $path
  18. * @return string */
  19. static function rel2abs_url($path) {
  20. if (substr($path, 0, 1) == "/") return $path;
  21. $dir = @getcwd();
  22. if (!isset($_SERVER['DOCUMENT_ROOT']) || ($dir === false))
  23. return false;
  24. $dir = self::normalize($dir);
  25. $doc_root = self::normalize($_SERVER['DOCUMENT_ROOT']);
  26. if (substr($dir, 0, strlen($doc_root)) != $doc_root)
  27. return false;
  28. $return = self::normalize(substr($dir, strlen($doc_root)) . "/$path");
  29. if (substr($return, 0, 1) !== "/")
  30. $return = "/$return";
  31. return $return;
  32. }
  33. /** Resolve full filesystem path of given URL. Returns FALSE if the URL
  34. * cannot be resolved
  35. * @param string $url
  36. * @return string */
  37. static function url2fullPath($url) {
  38. $url = self::normalize($url);
  39. $uri = isset($_SERVER['SCRIPT_NAME'])
  40. ? $_SERVER['SCRIPT_NAME'] : (isset($_SERVER['PHP_SELF'])
  41. ? $_SERVER['PHP_SELF']
  42. : false);
  43. $uri = self::normalize($uri);
  44. if (substr($url, 0, 1) !== "/") {
  45. if ($uri === false) return false;
  46. $url = dirname($uri) . "/$url";
  47. }
  48. if (isset($_SERVER['DOCUMENT_ROOT'])) {
  49. return self::normalize($_SERVER['DOCUMENT_ROOT'] . "/$url");
  50. } else {
  51. if ($uri === false) return false;
  52. if (isset($_SERVER['SCRIPT_FILENAME'])) {
  53. $scr_filename = self::normalize($_SERVER['SCRIPT_FILENAME']);
  54. return self::normalize(substr($scr_filename, 0, -strlen($uri)) . "/$url");
  55. }
  56. $count = count(explode('/', $uri)) - 1;
  57. for ($i = 0, $chdir = ""; $i < $count; $i++)
  58. $chdir .= "../";
  59. $chdir = self::normalize($chdir);
  60. $dir = getcwd();
  61. if (($dir === false) || !@chdir($chdir))
  62. return false;
  63. $rdir = getcwd();
  64. chdir($dir);
  65. return ($rdir !== false) ? self::normalize($rdir . "/$url") : false;
  66. }
  67. }
  68. /** Normalize the given path. On Windows servers backslash will be replaced
  69. * with slash. Removes unnecessary double slashes and double dots. Removes
  70. * last slash if it exists. Examples:
  71. * path::normalize("C:\\any\\path\\") returns "C:/any/path"
  72. * path::normalize("/your/path/..//home/") returns "/your/home"
  73. * @param string $path
  74. * @return string */
  75. static function normalize($path) {
  76. // Backslash to slash convert
  77. if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
  78. $path = preg_replace('/([^\\\])\\\+([^\\\])/s', "$1/$2", $path);
  79. if (substr($path, -1) == "\\") $path = substr($path, 0, -1);
  80. if (substr($path, 0, 1) == "\\") $path = "/" . substr($path, 1);
  81. }
  82. $path = preg_replace('/\/+/s', "/", $path);
  83. $path = "/$path";
  84. if (substr($path, -1) != "/")
  85. $path .= "/";
  86. $expr = '/\/([^\/]{1}|[^\.\/]{2}|[^\/]{3,})\/\.\.\//s';
  87. while (preg_match($expr, $path))
  88. $path = preg_replace($expr, "/", $path);
  89. $path = substr($path, 0, -1);
  90. $path = substr($path, 1);
  91. return $path;
  92. }
  93. /** Encode URL Path
  94. * @param string $path
  95. * @return string */
  96. static function urlPathEncode($path) {
  97. $path = self::normalize($path);
  98. $encoded = "";
  99. foreach (explode("/", $path) as $dir)
  100. $encoded .= rawurlencode($dir) . "/";
  101. $encoded = substr($encoded, 0, -1);
  102. return $encoded;
  103. }
  104. /** Decode URL Path
  105. * @param string $path
  106. * @return string */
  107. static function urlPathDecode($path) {
  108. $path = self::normalize($path);
  109. $decoded = "";
  110. foreach (explode("/", $path) as $dir)
  111. $decoded .= rawurldecode($dir) . "/";
  112. $decoded = substr($decoded, 0, -1);
  113. return $decoded;
  114. }
  115. }
  116. ?>