PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/wp-admin/includes/class-wp-filesystem-direct.php

https://bitbucket.org/greg_gallant/cd-blog
PHP | 363 lines | 230 code | 35 blank | 98 comment | 56 complexity | 5a174f0c1d05d39cd489b6161180e04d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * WordPress Direct Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for direct PHP file and folder manipulation.
  10. *
  11. * @since 2.5
  12. * @package WordPress
  13. * @subpackage Filesystem
  14. * @uses WP_Filesystem_Base Extends class
  15. */
  16. class WP_Filesystem_Direct extends WP_Filesystem_Base {
  17. var $errors = null;
  18. /**
  19. * constructor
  20. *
  21. * @param mixed $arg ignored argument
  22. */
  23. function __construct($arg) {
  24. $this->method = 'direct';
  25. $this->errors = new WP_Error();
  26. }
  27. /**
  28. * connect filesystem.
  29. *
  30. * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
  31. */
  32. function connect() {
  33. return true;
  34. }
  35. /**
  36. * Reads entire file into a string
  37. *
  38. * @param string $file Name of the file to read.
  39. * @return string|bool The function returns the read data or false on failure.
  40. */
  41. function get_contents($file) {
  42. return @file_get_contents($file);
  43. }
  44. /**
  45. * Reads entire file into an array
  46. *
  47. * @param string $file Path to the file.
  48. * @return array|bool the file contents in an array or false on failure.
  49. */
  50. function get_contents_array($file) {
  51. return @file($file);
  52. }
  53. /**
  54. * Write a string to a file
  55. *
  56. * @param string $file Remote path to the file where to write the data.
  57. * @param string $contents The data to write.
  58. * @param int $mode (optional) The file permissions as octal number, usually 0644.
  59. * @return bool False upon failure.
  60. */
  61. function put_contents($file, $contents, $mode = false ) {
  62. if ( ! ($fp = @fopen($file, 'w')) )
  63. return false;
  64. @fwrite($fp, $contents);
  65. @fclose($fp);
  66. $this->chmod($file, $mode);
  67. return true;
  68. }
  69. /**
  70. * Gets the current working directory
  71. *
  72. * @return string|bool the current working directory on success, or false on failure.
  73. */
  74. function cwd() {
  75. return @getcwd();
  76. }
  77. /**
  78. * Change directory
  79. *
  80. * @param string $dir The new current directory.
  81. * @return bool Returns true on success or false on failure.
  82. */
  83. function chdir($dir) {
  84. return @chdir($dir);
  85. }
  86. /**
  87. * Changes file group
  88. *
  89. * @param string $file Path to the file.
  90. * @param mixed $group A group name or number.
  91. * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
  92. * @return bool Returns true on success or false on failure.
  93. */
  94. function chgrp($file, $group, $recursive = false) {
  95. if ( ! $this->exists($file) )
  96. return false;
  97. if ( ! $recursive )
  98. return @chgrp($file, $group);
  99. if ( ! $this->is_dir($file) )
  100. return @chgrp($file, $group);
  101. //Is a directory, and we want recursive
  102. $file = trailingslashit($file);
  103. $filelist = $this->dirlist($file);
  104. foreach ($filelist as $filename)
  105. $this->chgrp($file . $filename, $group, $recursive);
  106. return true;
  107. }
  108. /**
  109. * Changes filesystem permissions
  110. *
  111. * @param string $file Path to the file.
  112. * @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
  113. * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
  114. * @return bool Returns true on success or false on failure.
  115. */
  116. function chmod($file, $mode = false, $recursive = false) {
  117. if ( ! $mode ) {
  118. if ( $this->is_file($file) )
  119. $mode = FS_CHMOD_FILE;
  120. elseif ( $this->is_dir($file) )
  121. $mode = FS_CHMOD_DIR;
  122. else
  123. return false;
  124. }
  125. if ( ! $recursive || ! $this->is_dir($file) )
  126. return @chmod($file, $mode);
  127. //Is a directory, and we want recursive
  128. $file = trailingslashit($file);
  129. $filelist = $this->dirlist($file);
  130. foreach ( (array)$filelist as $filename => $filemeta)
  131. $this->chmod($file . $filename, $mode, $recursive);
  132. return true;
  133. }
  134. /**
  135. * Changes file owner
  136. *
  137. * @param string $file Path to the file.
  138. * @param mixed $owner A user name or number.
  139. * @param bool $recursive (optional) If set True changes file owner recursively. Defaults to False.
  140. * @return bool Returns true on success or false on failure.
  141. */
  142. function chown($file, $owner, $recursive = false) {
  143. if ( ! $this->exists($file) )
  144. return false;
  145. if ( ! $recursive )
  146. return @chown($file, $owner);
  147. if ( ! $this->is_dir($file) )
  148. return @chown($file, $owner);
  149. //Is a directory, and we want recursive
  150. $filelist = $this->dirlist($file);
  151. foreach ($filelist as $filename) {
  152. $this->chown($file . '/' . $filename, $owner, $recursive);
  153. }
  154. return true;
  155. }
  156. /**
  157. * Gets file owner
  158. *
  159. * @param string $file Path to the file.
  160. * @return string Username of the user.
  161. */
  162. function owner($file) {
  163. $owneruid = @fileowner($file);
  164. if ( ! $owneruid )
  165. return false;
  166. if ( ! function_exists('posix_getpwuid') )
  167. return $owneruid;
  168. $ownerarray = posix_getpwuid($owneruid);
  169. return $ownerarray['name'];
  170. }
  171. /**
  172. * Gets file permissions
  173. *
  174. * FIXME does not handle errors in fileperms()
  175. *
  176. * @param string $file Path to the file.
  177. * @return string Mode of the file (last 4 digits).
  178. */
  179. function getchmod($file) {
  180. return substr(decoct(@fileperms($file)),3);
  181. }
  182. function group($file) {
  183. $gid = @filegroup($file);
  184. if ( ! $gid )
  185. return false;
  186. if ( ! function_exists('posix_getgrgid') )
  187. return $gid;
  188. $grouparray = posix_getgrgid($gid);
  189. return $grouparray['name'];
  190. }
  191. function copy($source, $destination, $overwrite = false, $mode = false) {
  192. if ( ! $overwrite && $this->exists($destination) )
  193. return false;
  194. $rtval = copy($source, $destination);
  195. if ( $mode )
  196. $this->chmod($destination, $mode);
  197. return $rtval;
  198. }
  199. function move($source, $destination, $overwrite = false) {
  200. if ( ! $overwrite && $this->exists($destination) )
  201. return false;
  202. // try using rename first. if that fails (for example, source is read only) try copy
  203. if ( @rename($source, $destination) )
  204. return true;
  205. if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
  206. $this->delete($source);
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. }
  212. function delete($file, $recursive = false, $type = false) {
  213. if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  214. return false;
  215. $file = str_replace('\\', '/', $file); //for win32, occasional problems deleting files otherwise
  216. if ( 'f' == $type || $this->is_file($file) )
  217. return @unlink($file);
  218. if ( ! $recursive && $this->is_dir($file) )
  219. return @rmdir($file);
  220. //At this point its a folder, and we're in recursive mode
  221. $file = trailingslashit($file);
  222. $filelist = $this->dirlist($file, true);
  223. $retval = true;
  224. if ( is_array($filelist) ) //false if no files, So check first.
  225. foreach ($filelist as $filename => $fileinfo)
  226. if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
  227. $retval = false;
  228. if ( file_exists($file) && ! @rmdir($file) )
  229. $retval = false;
  230. return $retval;
  231. }
  232. function exists($file) {
  233. return @file_exists($file);
  234. }
  235. function is_file($file) {
  236. return @is_file($file);
  237. }
  238. function is_dir($path) {
  239. return @is_dir($path);
  240. }
  241. function is_readable($file) {
  242. return @is_readable($file);
  243. }
  244. function is_writable($file) {
  245. return @is_writable($file);
  246. }
  247. function atime($file) {
  248. return @fileatime($file);
  249. }
  250. function mtime($file) {
  251. return @filemtime($file);
  252. }
  253. function size($file) {
  254. return @filesize($file);
  255. }
  256. function touch($file, $time = 0, $atime = 0) {
  257. if ($time == 0)
  258. $time = time();
  259. if ($atime == 0)
  260. $atime = time();
  261. return @touch($file, $time, $atime);
  262. }
  263. function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  264. // safe mode fails with a trailing slash under certain PHP versions.
  265. $path = untrailingslashit($path);
  266. if ( empty($path) )
  267. return false;
  268. if ( ! $chmod )
  269. $chmod = FS_CHMOD_DIR;
  270. if ( ! @mkdir($path) )
  271. return false;
  272. $this->chmod($path, $chmod);
  273. if ( $chown )
  274. $this->chown($path, $chown);
  275. if ( $chgrp )
  276. $this->chgrp($path, $chgrp);
  277. return true;
  278. }
  279. function rmdir($path, $recursive = false) {
  280. return $this->delete($path, $recursive);
  281. }
  282. function dirlist($path, $include_hidden = true, $recursive = false) {
  283. if ( $this->is_file($path) ) {
  284. $limit_file = basename($path);
  285. $path = dirname($path);
  286. } else {
  287. $limit_file = false;
  288. }
  289. if ( ! $this->is_dir($path) )
  290. return false;
  291. $dir = @dir($path);
  292. if ( ! $dir )
  293. return false;
  294. $ret = array();
  295. while (false !== ($entry = $dir->read()) ) {
  296. $struc = array();
  297. $struc['name'] = $entry;
  298. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  299. continue;
  300. if ( ! $include_hidden && '.' == $struc['name'][0] )
  301. continue;
  302. if ( $limit_file && $struc['name'] != $limit_file)
  303. continue;
  304. $struc['perms'] = $this->gethchmod($path.'/'.$entry);
  305. $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
  306. $struc['number'] = false;
  307. $struc['owner'] = $this->owner($path.'/'.$entry);
  308. $struc['group'] = $this->group($path.'/'.$entry);
  309. $struc['size'] = $this->size($path.'/'.$entry);
  310. $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
  311. $struc['lastmod'] = date('M j',$struc['lastmodunix']);
  312. $struc['time'] = date('h:i:s',$struc['lastmodunix']);
  313. $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
  314. if ( 'd' == $struc['type'] ) {
  315. if ( $recursive )
  316. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  317. else
  318. $struc['files'] = array();
  319. }
  320. $ret[ $struc['name'] ] = $struc;
  321. }
  322. $dir->close();
  323. unset($dir);
  324. return $ret;
  325. }
  326. }