PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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