PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-filesystem-base.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 321 lines | 137 code | 26 blank | 158 comment | 31 complexity | e136b927844fea2dd33177ea9c1c3144 MD5 | raw file
  1. <?php
  2. /**
  3. * Base WordPress Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * Base WordPress Filesystem class for which Filesystem implementations extend
  10. *
  11. * @since 2.5
  12. */
  13. class WP_Filesystem_Base {
  14. /**
  15. * Whether to display debug data for the connection.
  16. *
  17. * @since 2.5
  18. * @access public
  19. * @var bool
  20. */
  21. var $verbose = false;
  22. /**
  23. * Cached list of local filepaths to maped remote filepaths.
  24. *
  25. * @since 2.7
  26. * @access private
  27. * @var array
  28. */
  29. var $cache = array();
  30. /**
  31. * The Access method of the current connection, Set automatically.
  32. *
  33. * @since 2.5
  34. * @access public
  35. * @var string
  36. */
  37. var $method = '';
  38. /**
  39. * Returns the path on the remote filesystem of ABSPATH
  40. *
  41. * @since 2.7
  42. * @access public
  43. * @return string The location of the remote path.
  44. */
  45. function abspath() {
  46. $folder = $this->find_folder(ABSPATH);
  47. //Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
  48. if ( ! $folder && $this->is_dir('/wp-includes') )
  49. $folder = '/';
  50. return $folder;
  51. }
  52. /**
  53. * Returns the path on the remote filesystem of WP_CONTENT_DIR
  54. *
  55. * @since 2.7
  56. * @access public
  57. * @return string The location of the remote path.
  58. */
  59. function wp_content_dir() {
  60. return $this->find_folder(WP_CONTENT_DIR);
  61. }
  62. /**
  63. * Returns the path on the remote filesystem of WP_PLUGIN_DIR
  64. *
  65. * @since 2.7
  66. * @access public
  67. *
  68. * @return string The location of the remote path.
  69. */
  70. function wp_plugins_dir() {
  71. return $this->find_folder(WP_PLUGIN_DIR);
  72. }
  73. /**
  74. * Returns the path on the remote filesystem of the Themes Directory
  75. *
  76. * @since 2.7
  77. * @access public
  78. *
  79. * @return string The location of the remote path.
  80. */
  81. function wp_themes_dir() {
  82. return $this->wp_content_dir() . '/themes';
  83. }
  84. /**
  85. * Locates a folder on the remote filesystem.
  86. *
  87. * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
  88. *
  89. * @since 2.5
  90. * @deprecated 2.7
  91. * @access public
  92. *
  93. * @param string $base The folder to start searching from
  94. * @param bool $echo True to display debug information
  95. * @return string The location of the remote path.
  96. */
  97. function find_base_dir($base = '.', $echo = false) {
  98. _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
  99. $this->verbose = $echo;
  100. return $this->abspath();
  101. }
  102. /**
  103. * Locates a folder on the remote filesystem.
  104. *
  105. * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
  106. *
  107. * @since 2.5
  108. * @deprecated 2.7
  109. * @access public
  110. *
  111. * @param string $base The folder to start searching from
  112. * @param bool $echo True to display debug information
  113. * @return string The location of the remote path.
  114. */
  115. function get_base_dir($base = '.', $echo = false) {
  116. _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
  117. $this->verbose = $echo;
  118. return $this->abspath();
  119. }
  120. /**
  121. * Locates a folder on the remote filesystem.
  122. *
  123. * Assumes that on Windows systems, Stripping off the Drive letter is OK
  124. * Sanitizes \\ to / in windows filepaths.
  125. *
  126. * @since 2.7
  127. * @access public
  128. *
  129. * @param string $folder the folder to locate
  130. * @return string The location of the remote path.
  131. */
  132. function find_folder($folder) {
  133. if ( strpos($this->method, 'ftp') !== false ) {
  134. $constant_overrides = array( 'FTP_BASE' => ABSPATH, 'FTP_CONTENT_DIR' => WP_CONTENT_DIR, 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR );
  135. foreach ( $constant_overrides as $constant => $dir )
  136. if ( defined($constant) && $folder === $dir )
  137. return trailingslashit(constant($constant));
  138. } elseif ( 'direct' == $this->method ) {
  139. $folder = str_replace('\\', '/', $folder); //Windows path sanitiation
  140. return trailingslashit($folder);
  141. }
  142. $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
  143. $folder = str_replace('\\', '/', $folder); //Windows path sanitiation
  144. if ( isset($this->cache[ $folder ] ) )
  145. return $this->cache[ $folder ];
  146. if ( $this->exists($folder) ) { //Folder exists at that absolute path.
  147. $folder = trailingslashit($folder);
  148. $this->cache[ $folder ] = $folder;
  149. return $folder;
  150. }
  151. if ( $return = $this->search_for_folder($folder) )
  152. $this->cache[ $folder ] = $return;
  153. return $return;
  154. }
  155. /**
  156. * Locates a folder on the remote filesystem.
  157. *
  158. * Expects Windows sanitized path
  159. *
  160. * @since 2.7
  161. * @access private
  162. *
  163. * @param string $folder the folder to locate
  164. * @param string $base the folder to start searching from
  165. * @param bool $loop if the function has recursed, Internal use only
  166. * @return string The location of the remote path.
  167. */
  168. function search_for_folder($folder, $base = '.', $loop = false ) {
  169. if ( empty( $base ) || '.' == $base )
  170. $base = trailingslashit($this->cwd());
  171. $folder = untrailingslashit($folder);
  172. $folder_parts = explode('/', $folder);
  173. $last_path = $folder_parts[ count($folder_parts) - 1 ];
  174. $files = $this->dirlist( $base );
  175. foreach ( $folder_parts as $key ) {
  176. if ( $key == $last_path )
  177. continue; //We want this to be caught by the next code block.
  178. //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
  179. // If its found, change into it and follow through looking for it.
  180. // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
  181. // If it reaches the end, and still cant find it, it'll return false for the entire function.
  182. if ( isset($files[ $key ]) ){
  183. //Lets try that folder:
  184. $newdir = trailingslashit(path_join($base, $key));
  185. if ( $this->verbose )
  186. printf( __('Changing to %s') . '<br/>', $newdir );
  187. if ( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
  188. return $ret;
  189. }
  190. }
  191. //Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
  192. if (isset( $files[ $last_path ] ) ) {
  193. if ( $this->verbose )
  194. printf( __('Found %s') . '<br/>', $base . $last_path );
  195. return trailingslashit($base . $last_path);
  196. }
  197. if ( $loop )
  198. return false; //Prevent tihs function looping again.
  199. //As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
  200. return $this->search_for_folder($folder, '/', true);
  201. }
  202. /**
  203. * Returns the *nix style file permissions for a file
  204. *
  205. * From the PHP documentation page for fileperms()
  206. *
  207. * @link http://docs.php.net/fileperms
  208. * @since 2.5
  209. * @access public
  210. *
  211. * @param string $file string filename
  212. * @return int octal representation of permissions
  213. */
  214. function gethchmod($file){
  215. $perms = $this->getchmod($file);
  216. if (($perms & 0xC000) == 0xC000) // Socket
  217. $info = 's';
  218. elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
  219. $info = 'l';
  220. elseif (($perms & 0x8000) == 0x8000) // Regular
  221. $info = '-';
  222. elseif (($perms & 0x6000) == 0x6000) // Block special
  223. $info = 'b';
  224. elseif (($perms & 0x4000) == 0x4000) // Directory
  225. $info = 'd';
  226. elseif (($perms & 0x2000) == 0x2000) // Character special
  227. $info = 'c';
  228. elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
  229. $info = 'p';
  230. else // Unknown
  231. $info = 'u';
  232. // Owner
  233. $info .= (($perms & 0x0100) ? 'r' : '-');
  234. $info .= (($perms & 0x0080) ? 'w' : '-');
  235. $info .= (($perms & 0x0040) ?
  236. (($perms & 0x0800) ? 's' : 'x' ) :
  237. (($perms & 0x0800) ? 'S' : '-'));
  238. // Group
  239. $info .= (($perms & 0x0020) ? 'r' : '-');
  240. $info .= (($perms & 0x0010) ? 'w' : '-');
  241. $info .= (($perms & 0x0008) ?
  242. (($perms & 0x0400) ? 's' : 'x' ) :
  243. (($perms & 0x0400) ? 'S' : '-'));
  244. // World
  245. $info .= (($perms & 0x0004) ? 'r' : '-');
  246. $info .= (($perms & 0x0002) ? 'w' : '-');
  247. $info .= (($perms & 0x0001) ?
  248. (($perms & 0x0200) ? 't' : 'x' ) :
  249. (($perms & 0x0200) ? 'T' : '-'));
  250. return $info;
  251. }
  252. /**
  253. * Converts *nix style file permissions to a octal number.
  254. *
  255. * Converts '-rw-r--r--' to 0644
  256. * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
  257. *
  258. * @link http://docs.php.net/manual/en/function.chmod.php#49614
  259. * @since 2.5
  260. * @access public
  261. *
  262. * @param string $mode string *nix style file permission
  263. * @return int octal representation
  264. */
  265. function getnumchmodfromh($mode) {
  266. $realmode = '';
  267. $legal = array('', 'w', 'r', 'x', '-');
  268. $attarray = preg_split('//', $mode);
  269. for ($i=0; $i < count($attarray); $i++)
  270. if ($key = array_search($attarray[$i], $legal))
  271. $realmode .= $legal[$key];
  272. $mode = str_pad($realmode, 9, '-');
  273. $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
  274. $mode = strtr($mode,$trans);
  275. $newmode = '';
  276. $newmode .= $mode[0] + $mode[1] + $mode[2];
  277. $newmode .= $mode[3] + $mode[4] + $mode[5];
  278. $newmode .= $mode[6] + $mode[7] + $mode[8];
  279. return $newmode;
  280. }
  281. /**
  282. * Determines if the string provided contains binary characters.
  283. *
  284. * @since 2.7
  285. * @access private
  286. *
  287. * @param string $text String to test against
  288. * @return bool true if string is binary, false otherwise
  289. */
  290. function is_binary( $text ) {
  291. return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)
  292. }
  293. }
  294. ?>