PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/webkod3r/tripolis
PHP | 517 lines | 339 code | 30 blank | 148 comment | 26 complexity | 547828f43eeae0068c00dbffe98c03fd MD5 | raw file
  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. * @access public
  21. *
  22. * @param mixed $arg ignored argument
  23. */
  24. public function __construct($arg) {
  25. $this->method = 'direct';
  26. $this->errors = new WP_Error();
  27. }
  28. /**
  29. * Reads entire file into a string
  30. *
  31. * @access public
  32. *
  33. * @param string $file Name of the file to read.
  34. * @return string|bool The function returns the read data or false on failure.
  35. */
  36. public function get_contents($file) {
  37. return @file_get_contents($file);
  38. }
  39. /**
  40. * Reads entire file into an array
  41. *
  42. * @access public
  43. *
  44. * @param string $file Path to the file.
  45. * @return array|bool the file contents in an array or false on failure.
  46. */
  47. public function get_contents_array($file) {
  48. return @file($file);
  49. }
  50. /**
  51. * Write a string to a file
  52. *
  53. * @access public
  54. *
  55. * @param string $file Remote path to the file where to write the data.
  56. * @param string $contents The data to write.
  57. * @param int $mode Optional. The file permissions as octal number, usually 0644.
  58. * Default false.
  59. * @return bool False upon failure, true otherwise.
  60. */
  61. public function put_contents( $file, $contents, $mode = false ) {
  62. $fp = @fopen( $file, 'wb' );
  63. if ( ! $fp )
  64. return false;
  65. mbstring_binary_safe_encoding();
  66. $data_length = strlen( $contents );
  67. $bytes_written = fwrite( $fp, $contents );
  68. reset_mbstring_encoding();
  69. fclose( $fp );
  70. if ( $data_length !== $bytes_written )
  71. return false;
  72. $this->chmod( $file, $mode );
  73. return true;
  74. }
  75. /**
  76. * Gets the current working directory
  77. *
  78. * @access public
  79. *
  80. * @return string|bool the current working directory on success, or false on failure.
  81. */
  82. public function cwd() {
  83. return @getcwd();
  84. }
  85. /**
  86. * Change directory
  87. *
  88. * @access public
  89. *
  90. * @param string $dir The new current directory.
  91. * @return bool Returns true on success or false on failure.
  92. */
  93. public function chdir($dir) {
  94. return @chdir($dir);
  95. }
  96. /**
  97. * Changes file group
  98. *
  99. * @access public
  100. *
  101. * @param string $file Path to the file.
  102. * @param mixed $group A group name or number.
  103. * @param bool $recursive Optional. If set True changes file group recursively. Default false.
  104. * @return bool Returns true on success or false on failure.
  105. */
  106. public function chgrp($file, $group, $recursive = false) {
  107. if ( ! $this->exists($file) )
  108. return false;
  109. if ( ! $recursive )
  110. return @chgrp($file, $group);
  111. if ( ! $this->is_dir($file) )
  112. return @chgrp($file, $group);
  113. // Is a directory, and we want recursive
  114. $file = trailingslashit($file);
  115. $filelist = $this->dirlist($file);
  116. foreach ($filelist as $filename)
  117. $this->chgrp($file . $filename, $group, $recursive);
  118. return true;
  119. }
  120. /**
  121. * Changes filesystem permissions
  122. *
  123. * @access public
  124. *
  125. * @param string $file Path to the file.
  126. * @param int $mode Optional. The permissions as octal number, usually 0644 for files,
  127. * 0755 for dirs. Default false.
  128. * @param bool $recursive Optional. If set True changes file group recursively. Default false.
  129. * @return bool Returns true on success or false on failure.
  130. */
  131. public function chmod($file, $mode = false, $recursive = false) {
  132. if ( ! $mode ) {
  133. if ( $this->is_file($file) )
  134. $mode = FS_CHMOD_FILE;
  135. elseif ( $this->is_dir($file) )
  136. $mode = FS_CHMOD_DIR;
  137. else
  138. return false;
  139. }
  140. if ( ! $recursive || ! $this->is_dir($file) )
  141. return @chmod($file, $mode);
  142. // Is a directory, and we want recursive
  143. $file = trailingslashit($file);
  144. $filelist = $this->dirlist($file);
  145. foreach ( (array)$filelist as $filename => $filemeta)
  146. $this->chmod($file . $filename, $mode, $recursive);
  147. return true;
  148. }
  149. /**
  150. * Changes file owner
  151. *
  152. * @access public
  153. *
  154. * @param string $file Path to the file.
  155. * @param mixed $owner A user name or number.
  156. * @param bool $recursive Optional. If set True changes file owner recursively.
  157. * Default false.
  158. * @return bool Returns true on success or false on failure.
  159. */
  160. public function chown($file, $owner, $recursive = false) {
  161. if ( ! $this->exists($file) )
  162. return false;
  163. if ( ! $recursive )
  164. return @chown($file, $owner);
  165. if ( ! $this->is_dir($file) )
  166. return @chown($file, $owner);
  167. // Is a directory, and we want recursive
  168. $filelist = $this->dirlist($file);
  169. foreach ($filelist as $filename) {
  170. $this->chown($file . '/' . $filename, $owner, $recursive);
  171. }
  172. return true;
  173. }
  174. /**
  175. * Gets file owner
  176. *
  177. * @access public
  178. *
  179. * @param string $file Path to the file.
  180. * @return string|bool Username of the user or false on error.
  181. */
  182. public function owner($file) {
  183. $owneruid = @fileowner($file);
  184. if ( ! $owneruid )
  185. return false;
  186. if ( ! function_exists('posix_getpwuid') )
  187. return $owneruid;
  188. $ownerarray = posix_getpwuid($owneruid);
  189. return $ownerarray['name'];
  190. }
  191. /**
  192. * Gets file permissions
  193. *
  194. * FIXME does not handle errors in fileperms()
  195. *
  196. * @access public
  197. *
  198. * @param string $file Path to the file.
  199. * @return string Mode of the file (last 3 digits).
  200. */
  201. public function getchmod($file) {
  202. return substr( decoct( @fileperms( $file ) ), -3 );
  203. }
  204. /**
  205. * @access public
  206. *
  207. * @param string $file
  208. * @return string|false
  209. */
  210. public function group($file) {
  211. $gid = @filegroup($file);
  212. if ( ! $gid )
  213. return false;
  214. if ( ! function_exists('posix_getgrgid') )
  215. return $gid;
  216. $grouparray = posix_getgrgid($gid);
  217. return $grouparray['name'];
  218. }
  219. /**
  220. * @access public
  221. *
  222. * @param string $source
  223. * @param string $destination
  224. * @param bool $overwrite
  225. * @param int $mode
  226. * @return bool
  227. */
  228. public function copy($source, $destination, $overwrite = false, $mode = false) {
  229. if ( ! $overwrite && $this->exists($destination) )
  230. return false;
  231. $rtval = copy($source, $destination);
  232. if ( $mode )
  233. $this->chmod($destination, $mode);
  234. return $rtval;
  235. }
  236. /**
  237. * @access public
  238. *
  239. * @param string $source
  240. * @param string $destination
  241. * @param bool $overwrite
  242. * @return bool
  243. */
  244. public function move($source, $destination, $overwrite = false) {
  245. if ( ! $overwrite && $this->exists($destination) )
  246. return false;
  247. // Try using rename first. if that fails (for example, source is read only) try copy.
  248. if ( @rename($source, $destination) )
  249. return true;
  250. if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
  251. $this->delete($source);
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. /**
  258. * @access public
  259. *
  260. * @param string $file
  261. * @param bool $recursive
  262. * @param string $type
  263. * @return bool
  264. */
  265. public function delete($file, $recursive = false, $type = false) {
  266. if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  267. return false;
  268. $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
  269. if ( 'f' == $type || $this->is_file($file) )
  270. return @unlink($file);
  271. if ( ! $recursive && $this->is_dir($file) )
  272. return @rmdir($file);
  273. // At this point it's a folder, and we're in recursive mode
  274. $file = trailingslashit($file);
  275. $filelist = $this->dirlist($file, true);
  276. $retval = true;
  277. if ( is_array( $filelist ) ) {
  278. foreach ( $filelist as $filename => $fileinfo ) {
  279. if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
  280. $retval = false;
  281. }
  282. }
  283. if ( file_exists($file) && ! @rmdir($file) )
  284. $retval = false;
  285. return $retval;
  286. }
  287. /**
  288. * @access public
  289. *
  290. * @param string $file
  291. * @return bool
  292. */
  293. public function exists($file) {
  294. return @file_exists($file);
  295. }
  296. /**
  297. * @access public
  298. *
  299. * @param string $file
  300. * @return bool
  301. */
  302. public function is_file($file) {
  303. return @is_file($file);
  304. }
  305. /**
  306. * @access public
  307. *
  308. * @param string $path
  309. * @return bool
  310. */
  311. public function is_dir($path) {
  312. return @is_dir($path);
  313. }
  314. /**
  315. * @access public
  316. *
  317. * @param string $file
  318. * @return bool
  319. */
  320. public function is_readable($file) {
  321. return @is_readable($file);
  322. }
  323. /**
  324. * @access public
  325. *
  326. * @param string $file
  327. * @return bool
  328. */
  329. public function is_writable($file) {
  330. return @is_writable($file);
  331. }
  332. /**
  333. * @access public
  334. *
  335. * @param string $file
  336. * @return int
  337. */
  338. public function atime($file) {
  339. return @fileatime($file);
  340. }
  341. /**
  342. * @access public
  343. *
  344. * @param string $file
  345. * @return int
  346. */
  347. public function mtime($file) {
  348. return @filemtime($file);
  349. }
  350. /**
  351. * @access public
  352. *
  353. * @param string $file
  354. * @return int
  355. */
  356. public function size($file) {
  357. return @filesize($file);
  358. }
  359. /**
  360. * @access public
  361. *
  362. * @param string $file
  363. * @param int $time
  364. * @param int $atime
  365. * @return bool
  366. */
  367. public function touch($file, $time = 0, $atime = 0) {
  368. if ($time == 0)
  369. $time = time();
  370. if ($atime == 0)
  371. $atime = time();
  372. return @touch($file, $time, $atime);
  373. }
  374. /**
  375. * @access public
  376. *
  377. * @param string $path
  378. * @param mixed $chmod
  379. * @param mixed $chown
  380. * @param mixed $chgrp
  381. * @return bool
  382. */
  383. public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  384. // Safe mode fails with a trailing slash under certain PHP versions.
  385. $path = untrailingslashit($path);
  386. if ( empty($path) )
  387. return false;
  388. if ( ! $chmod )
  389. $chmod = FS_CHMOD_DIR;
  390. if ( ! @mkdir($path) )
  391. return false;
  392. $this->chmod($path, $chmod);
  393. if ( $chown )
  394. $this->chown($path, $chown);
  395. if ( $chgrp )
  396. $this->chgrp($path, $chgrp);
  397. return true;
  398. }
  399. /**
  400. * @access public
  401. *
  402. * @param string $path
  403. * @param bool $recursive
  404. * @return bool
  405. */
  406. public function rmdir($path, $recursive = false) {
  407. return $this->delete($path, $recursive);
  408. }
  409. /**
  410. * @access public
  411. *
  412. * @param string $path
  413. * @param bool $include_hidden
  414. * @param bool $recursive
  415. * @return bool|array
  416. */
  417. public function dirlist($path, $include_hidden = true, $recursive = false) {
  418. if ( $this->is_file($path) ) {
  419. $limit_file = basename($path);
  420. $path = dirname($path);
  421. } else {
  422. $limit_file = false;
  423. }
  424. if ( ! $this->is_dir($path) )
  425. return false;
  426. $dir = @dir($path);
  427. if ( ! $dir )
  428. return false;
  429. $ret = array();
  430. while (false !== ($entry = $dir->read()) ) {
  431. $struc = array();
  432. $struc['name'] = $entry;
  433. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  434. continue;
  435. if ( ! $include_hidden && '.' == $struc['name'][0] )
  436. continue;
  437. if ( $limit_file && $struc['name'] != $limit_file)
  438. continue;
  439. $struc['perms'] = $this->gethchmod($path.'/'.$entry);
  440. $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
  441. $struc['number'] = false;
  442. $struc['owner'] = $this->owner($path.'/'.$entry);
  443. $struc['group'] = $this->group($path.'/'.$entry);
  444. $struc['size'] = $this->size($path.'/'.$entry);
  445. $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
  446. $struc['lastmod'] = date('M j',$struc['lastmodunix']);
  447. $struc['time'] = date('h:i:s',$struc['lastmodunix']);
  448. $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
  449. if ( 'd' == $struc['type'] ) {
  450. if ( $recursive )
  451. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  452. else
  453. $struc['files'] = array();
  454. }
  455. $ret[ $struc['name'] ] = $struc;
  456. }
  457. $dir->close();
  458. unset($dir);
  459. return $ret;
  460. }
  461. }