PageRenderTime 278ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/filesystem/path.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 301 lines | 159 code | 35 blank | 107 comment | 32 complexity | 84ab4de21a29880546a5e0adfc4c137d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage FileSystem
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. // Define a boolean constant as true if a Windows based host
  11. define('JPATH_ISWIN', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
  12. // Define a boolean constant as true if a Mac based host
  13. define('JPATH_ISMAC', (strtoupper(substr(PHP_OS, 0, 3)) === 'MAC'));
  14. if (!defined('DS'))
  15. {
  16. // Define a string constant shortcut for the DIRECTORY_SEPARATOR define
  17. define('DS', DIRECTORY_SEPARATOR);
  18. }
  19. if (!defined('JPATH_ROOT'))
  20. {
  21. // Define a string constant for the root directory of the file system in native format
  22. define('JPATH_ROOT', JPath::clean(JPATH_SITE));
  23. }
  24. /**
  25. * A Path handling class
  26. *
  27. * @package Joomla.Platform
  28. * @subpackage FileSystem
  29. * @since 11.1
  30. */
  31. class JPath
  32. {
  33. /**
  34. * Checks if a path's permissions can be changed.
  35. *
  36. * @param string $path Path to check.
  37. *
  38. * @return boolean True if path can have mode changed.
  39. *
  40. * @since 11.1
  41. */
  42. public static function canChmod($path)
  43. {
  44. $perms = fileperms($path);
  45. if ($perms !== false)
  46. {
  47. if (@chmod($path, $perms ^ 0001))
  48. {
  49. @chmod($path, $perms);
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * Chmods files and directories recursively to given permissions.
  57. *
  58. * @param string $path Root path to begin changing mode [without trailing slash].
  59. * @param string $filemode Octal representation of the value to change file mode to [null = no change].
  60. * @param string $foldermode Octal representation of the value to change folder mode to [null = no change].
  61. *
  62. * @return boolean True if successful [one fail means the whole operation failed].
  63. *
  64. * @since 11.1
  65. */
  66. public static function setPermissions($path, $filemode = '0644', $foldermode = '0755')
  67. {
  68. // Initialise return value
  69. $ret = true;
  70. if (is_dir($path))
  71. {
  72. $dh = opendir($path);
  73. while ($file = readdir($dh))
  74. {
  75. if ($file != '.' && $file != '..')
  76. {
  77. $fullpath = $path . '/' . $file;
  78. if (is_dir($fullpath))
  79. {
  80. if (!JPath::setPermissions($fullpath, $filemode, $foldermode))
  81. {
  82. $ret = false;
  83. }
  84. }
  85. else
  86. {
  87. if (isset($filemode))
  88. {
  89. if (!@ chmod($fullpath, octdec($filemode)))
  90. {
  91. $ret = false;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. closedir($dh);
  98. if (isset($foldermode))
  99. {
  100. if (!@ chmod($path, octdec($foldermode)))
  101. {
  102. $ret = false;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. if (isset($filemode))
  109. {
  110. $ret = @ chmod($path, octdec($filemode));
  111. }
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * Get the permissions of the file/folder at a give path.
  117. *
  118. * @param string $path The path of a file/folder.
  119. *
  120. * @return string Filesystem permissions.
  121. *
  122. * @since 11.1
  123. */
  124. public static function getPermissions($path)
  125. {
  126. $path = JPath::clean($path);
  127. $mode = @ decoct(@ fileperms($path) & 0777);
  128. if (strlen($mode) < 3)
  129. {
  130. return '---------';
  131. }
  132. $parsed_mode = '';
  133. for ($i = 0; $i < 3; $i++)
  134. {
  135. // read
  136. $parsed_mode .= ($mode{$i} & 04) ? "r" : "-";
  137. // write
  138. $parsed_mode .= ($mode{$i} & 02) ? "w" : "-";
  139. // execute
  140. $parsed_mode .= ($mode{$i} & 01) ? "x" : "-";
  141. }
  142. return $parsed_mode;
  143. }
  144. /**
  145. * Checks for snooping outside of the file system root.
  146. *
  147. * @param string $path A file system path to check.
  148. * @param string $ds Directory separator (optional).
  149. *
  150. * @return string A cleaned version of the path or exit on error.
  151. *
  152. * @since 11.1
  153. */
  154. public static function check($path, $ds = DIRECTORY_SEPARATOR)
  155. {
  156. if (strpos($path, '..') !== false)
  157. {
  158. // Don't translate
  159. JError::raiseError(20, 'JPath::check Use of relative paths not permitted');
  160. jexit();
  161. }
  162. $path = JPath::clean($path);
  163. if ((JPATH_ROOT != '') && strpos($path, JPath::clean(JPATH_ROOT)) !== 0)
  164. {
  165. // Don't translate
  166. JError::raiseError(20, 'JPath::check Snooping out of bounds @ ' . $path);
  167. jexit();
  168. }
  169. return $path;
  170. }
  171. /**
  172. * Function to strip additional / or \ in a path name.
  173. *
  174. * @param string $path The path to clean.
  175. * @param string $ds Directory separator (optional).
  176. *
  177. * @return string The cleaned path.
  178. *
  179. * @since 11.1
  180. */
  181. public static function clean($path, $ds = DIRECTORY_SEPARATOR)
  182. {
  183. $path = trim($path);
  184. if (empty($path))
  185. {
  186. $path = JPATH_ROOT;
  187. }
  188. else
  189. {
  190. // Remove double slashes and backslashes and convert all slashes and backslashes to DIRECTORY_SEPARATOR
  191. $path = preg_replace('#[/\\\\]+#', $ds, $path);
  192. }
  193. return $path;
  194. }
  195. /**
  196. * Method to determine if script owns the path.
  197. *
  198. * @param string $path Path to check ownership.
  199. *
  200. * @return boolean True if the php script owns the path passed.
  201. *
  202. * @since 11.1
  203. */
  204. public static function isOwner($path)
  205. {
  206. jimport('joomla.filesystem.file');
  207. $tmp = md5(JUserHelper::genRandomPassword(16));
  208. $ssp = ini_get('session.save_path');
  209. $jtp = JPATH_SITE . '/tmp';
  210. // Try to find a writable directory
  211. $dir = is_writable('/tmp') ? '/tmp' : false;
  212. $dir = (!$dir && is_writable($ssp)) ? $ssp : false;
  213. $dir = (!$dir && is_writable($jtp)) ? $jtp : false;
  214. if ($dir)
  215. {
  216. $test = $dir . '/' . $tmp;
  217. // Create the test file
  218. $blank = '';
  219. JFile::write($test, $blank, false);
  220. // Test ownership
  221. $return = (fileowner($test) == fileowner($path));
  222. // Delete the test file
  223. JFile::delete($test);
  224. return $return;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Searches the directory paths for a given file.
  230. *
  231. * @param mixed $paths An path string or array of path strings to search in
  232. * @param string $file The file name to look for.
  233. *
  234. * @return mixed The full path and file name for the target file, or boolean false if the file is not found in any of the paths.
  235. *
  236. * @since 11.1
  237. */
  238. public static function find($paths, $file)
  239. {
  240. settype($paths, 'array'); //force to array
  241. // Start looping through the path set
  242. foreach ($paths as $path)
  243. {
  244. // Get the path to the file
  245. $fullname = $path . '/' . $file;
  246. // Is the path based on a stream?
  247. if (strpos($path, '://') === false)
  248. {
  249. // Not a stream, so do a realpath() to avoid directory
  250. // traversal attempts on the local file system.
  251. $path = realpath($path); // needed for substr() later
  252. $fullname = realpath($fullname);
  253. }
  254. // The substr() check added to make sure that the realpath()
  255. // results in a directory registered so that
  256. // non-registered directories are not accessible via directory
  257. // traversal attempts.
  258. if (file_exists($fullname) && substr($fullname, 0, strlen($path)) == $path)
  259. {
  260. return $fullname;
  261. }
  262. }
  263. // Could not find the file in the set of paths
  264. return false;
  265. }
  266. }