PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/Deployment/includes/filesystems/DirectFilesystem.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 500 lines | 432 code | 24 blank | 44 comment | 13 complexity | 1bf22f6efc43a6f74098166170b96363 MD5 | raw file
  1. <?php
  2. /**
  3. * File holding the DirectFilesystem class.
  4. *
  5. * @file DirectFilesystem.php
  6. * @ingroup Deployment
  7. * @ingroup Filesystem
  8. *
  9. * @author Jeroen De Dauw
  10. */
  11. if ( !defined( 'MEDIAWIKI' ) ) {
  12. die( 'Not an entry point.' );
  13. }
  14. /**
  15. * Filesystem class for direct PHP file and folder manipulation.
  16. *
  17. * @author Jeroen De Dauw
  18. */
  19. class DirectFilesystem extends Filesystem {
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct() {
  24. parent::__construct();
  25. // TODO
  26. }
  27. /**
  28. * @see Filesystem::connect
  29. */
  30. public function connect() {
  31. return true;
  32. }
  33. /**
  34. * @see Filesystem::changeDir
  35. */
  36. public function changeDir( $dir ) {
  37. wfSuppressWarnings();
  38. $result = (bool)chdir( $dir );
  39. wfRestoreWarnings();
  40. return $result;
  41. }
  42. /**
  43. * @see Filesystem::changeFileGroup
  44. */
  45. public function changeFileGroup( $file, $group, $recursive = false ) {
  46. if ( !$this->exists( $file ) ) {
  47. return false;
  48. }
  49. // Not recursive, so just use chgrp.
  50. if ( !$recursive || !$this->isDir( $file ) ) {
  51. wfSuppressWarnings();
  52. $result = chgrp( $file, $group );
  53. wfRestoreWarnings();
  54. return $result;
  55. }
  56. // Recursive approach required.
  57. $file = rtrim( $file, '/' ) . '/';
  58. $files = $this->listDir( $file );
  59. foreach ( $files as $fileName ) {
  60. $this->changeFileGroup( $file . $fileName, $group, $recursive );
  61. }
  62. return true;
  63. }
  64. /**
  65. * @see Filesystem::chmod
  66. */
  67. public function chmod( $file, $mode = false, $recursive = false ) {
  68. // TODO: refactor up?
  69. if ( !$mode ) {
  70. if ( $this->isFile( $file ) ) {
  71. $mode = FS_CHMOD_FILE;
  72. }
  73. elseif ( $this->isDir( $file ) ) {
  74. $mode = FS_CHMOD_DIR;
  75. }
  76. else {
  77. return false;
  78. }
  79. }
  80. // Not recursive, so just use chmod.
  81. if ( !$recursive || !$this->isDir( $file ) ) {
  82. wfSuppressWarnings();
  83. $result = (bool)chmod( $file, $mode );
  84. wfRestoreWarnings();
  85. return $result;
  86. }
  87. // Recursive approach required.
  88. $file = rtrim( $file, '/' ) . '/';
  89. $files = $this->listDir( $file );
  90. foreach ( $files as $fileName ) {
  91. $this->chmod( $file . $fileName, $mode, $recursive );
  92. }
  93. return true;
  94. }
  95. /**
  96. * @see Filesystem::chown
  97. */
  98. public function chown( $file, $owner, $recursive = false ) {
  99. if ( !$this->exists( $file ) ) {
  100. return false;
  101. }
  102. // Not recursive, so just use chown.
  103. if ( !$recursive || !$this->isDir( $file ) ) {
  104. wfSuppressWarnings();
  105. $result = (bool)chown( $file, $owner );
  106. wfRestoreWarnings();
  107. return $result;
  108. }
  109. // Recursive approach required.
  110. $file = rtrim( $file, '/' ) . '/';
  111. $files = $this->listDir( $file );
  112. foreach ( $files as $fileName ) {
  113. $this->chown( $file . $fileName, $owner, $recursive );
  114. }
  115. return true;
  116. }
  117. /**
  118. * @see Filesystem::delete
  119. */
  120. public function delete( $path, $recursive = false ) {
  121. if ( empty( $path ) ) {
  122. return false;
  123. }
  124. // For win32, occasional problems deleteing files otherwise.
  125. $path = str_replace( '\\', '/', $path );
  126. if ( $this->isFile( $path ) ) {
  127. wfSuppressWarnings();
  128. $result = (bool)unlink( $path );
  129. wfRestoreWarnings();
  130. return $result;
  131. }
  132. if ( !$recursive && $this->isDir( $path ) ) {
  133. wfSuppressWarnings();
  134. $result = (bool)rmdir( $path );
  135. wfRestoreWarnings();
  136. return $result;
  137. }
  138. // Recursive approach required.
  139. $path = rtrim( $path, '/' ) . '/';
  140. $files = $this->listDir( $path );
  141. $success = true;
  142. foreach ( $files as $fileName ) {
  143. if ( !$this->delete( $path . $fileName, $recursive ) ) {
  144. $success = false;
  145. }
  146. }
  147. if ( $success && file_exists( $path ) ) {
  148. wfSuppressWarnings();
  149. $rmdirRes = rmdir( $path );
  150. wfRestoreWarnings();
  151. if ( !$rmdirRes ) {
  152. $success = false;
  153. }
  154. }
  155. return $success;
  156. }
  157. /**
  158. * @see Filesystem::doCopy
  159. */
  160. protected function doCopy( $from, $to ) {
  161. return copy( $from, $to );
  162. }
  163. /**
  164. * @see Filesystem::doMove
  165. */
  166. protected function doMove( $from, $to, $overwrite ) {
  167. // try using rename first. if that fails (for example, source is read only) try copy and delete.
  168. wfSuppressWarnings();
  169. $renameRes = rename( $from, $to);
  170. wfRestoreWarnings();
  171. if ( $renameRes ) {
  172. return true;
  173. }
  174. if ( $this->copy( $from, $to, $overwrite ) && $this->exists( $to ) ) {
  175. $this->delete( $from );
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. /**
  182. * @see Filesystem::exists
  183. */
  184. public function exists( $file ) {
  185. wfSuppressWarnings();
  186. $result = file_exists( $file );
  187. wfRestoreWarnings();
  188. return $result;
  189. }
  190. /**
  191. * @see Filesystem::getChmod
  192. */
  193. public function getChmod( $file ) {
  194. wfSuppressWarnings();
  195. $fileperms = fileperms( $file );
  196. wfRestoreWarnings();
  197. return substr( decoct( $fileperms ), 3 );
  198. }
  199. /**
  200. * @see Filesystem::getContents
  201. */
  202. public function getContents( $file ) {
  203. wfSuppressWarnings();
  204. $result = file_get_contents( $file );
  205. wfRestoreWarnings();
  206. return $result;
  207. }
  208. /**
  209. * @see Filesystem::getCurrentWorkingDir
  210. */
  211. public function getCurrentWorkingDir() {
  212. wfSuppressWarnings();
  213. $result = getcwd();
  214. wfRestoreWarnings();
  215. return $result;
  216. }
  217. /**
  218. * @see Filesystem::getGroup
  219. */
  220. public function getGroup( $file ) {
  221. wfSuppressWarnings();
  222. $gid = filegroup( $file );
  223. wfRestoreWarnings();
  224. if ( !$gid ) {
  225. return false;
  226. }
  227. if ( function_exists( 'posix_getgrgid' ) ) {
  228. $groupArray = posix_getgrgid( $gid );
  229. return $groupArray['name'];
  230. }
  231. return $gid;
  232. }
  233. /**
  234. * @see Filesystem::getModificationTime
  235. */
  236. public function getModificationTime( $file ) {
  237. wfSuppressWarnings();
  238. $result = filemtime( $file );
  239. wfRestoreWarnings();
  240. return $result;
  241. }
  242. /**
  243. * @see Filesystem::getOwner
  244. */
  245. public function getOwner( $file ) {
  246. wfSuppressWarnings();
  247. $owneruid = fileowner( $file );
  248. wfRestoreWarnings();
  249. if ( !$owneruid ) {
  250. return false;
  251. }
  252. if ( function_exists( 'posix_getpwuid' ) ) {
  253. $ownerArray = posix_getpwuid( $owneruid );
  254. return $ownerArray['name'];
  255. }
  256. return $owneruid;
  257. }
  258. /**
  259. * @see Filesystem::getSize
  260. */
  261. public function getSize( $file ) {
  262. wfSuppressWarnings();
  263. $result = filesize( $file );
  264. wfRestoreWarnings();
  265. return $result;
  266. }
  267. /**
  268. * @see Filesystem::isDir
  269. */
  270. public function isDir( $path ) {
  271. wfSuppressWarnings();
  272. $result = (bool)is_dir( $path );
  273. wfRestoreWarnings();
  274. return $result;
  275. }
  276. /**
  277. * @see Filesystem::isFile
  278. */
  279. public function isFile( $path ) {
  280. wfSuppressWarnings();
  281. $result = (bool)is_file( $path );
  282. wfRestoreWarnings();
  283. return $result;
  284. }
  285. /**
  286. * @see Filesystem::isReadable
  287. */
  288. public function isReadable( $file ) {
  289. wfSuppressWarnings();
  290. $result = (bool)is_readable( $file );
  291. wfRestoreWarnings();
  292. return $result;
  293. }
  294. /**
  295. * @see Filesystem::isWritable
  296. */
  297. public function isWritable( $file ) {
  298. wfSuppressWarnings();
  299. $result = (bool)is_writable( $file );
  300. wfRestoreWarnings();
  301. return $result;
  302. }
  303. /**
  304. * @see Filesystem::listDir
  305. */
  306. public function listDir( $path, $includeHidden = true, $recursive = false ) {
  307. if ( $this->isFile( $path ) ) {
  308. $limit_file = basename( $path );
  309. $path = dirname( $path );
  310. } else {
  311. $limit_file = false;
  312. }
  313. if ( !$this->isDir( $path ) ) {
  314. return false;
  315. }
  316. wfSuppressWarnings();
  317. $dir = dir( $path );
  318. wfRestoreWarnings();
  319. if ( !$dir ) {
  320. return false;
  321. }
  322. $ret = array();
  323. while ( false !== ( $entry = $dir->read() ) ) {
  324. $struc = array();
  325. $struc['name'] = $entry;
  326. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  327. continue;
  328. if ( ( !$includeHidden && '.' == $struc['name'][0] ) || ( $limit_file && $struc['name'] != $limit_file ) ) {
  329. continue;
  330. }
  331. $entryPath = "$path/$entry";
  332. $struc['perms'] = $this->getChmod( $entryPath );
  333. $struc['number'] = false;
  334. $struc['owner'] = $this->getOwner( $entryPath );
  335. $struc['group'] = $this->getGroup( $entryPath );
  336. $struc['size'] = $this->getSize( $entryPath );
  337. $struc['lastmodunix']= $this->getModificationTime( $entryPath );
  338. $struc['lastmod'] = date( 'M j', $struc['lastmodunix'] );
  339. $struc['time'] = date( 'h:i:s', $struc['lastmodunix'] );
  340. $struc['type'] = $this->isDir( $entryPath ) ? 'd' : 'f';
  341. if ( $struc['type'] == 'd' ) {
  342. if ( $recursive ) {
  343. $struc['files'] = $this->listDir( $path . '/' . $struc['name'], $includeHidden, $recursive );
  344. }
  345. else {
  346. $struc['files'] = array();
  347. }
  348. }
  349. $ret[$struc['name']] = $struc;
  350. }
  351. $dir->close();
  352. unset($dir);
  353. return $ret;
  354. }
  355. /**
  356. * @see Filesystem::makeDir
  357. */
  358. public function makeDir( $path, $chmod = false, $chown = false, $chgrp = false ) {
  359. // Safe mode fails with a trailing slash under certain PHP versions.
  360. $path = rtrim( $path, '/' );
  361. if ( empty( $path ) ) {
  362. $path = '/';
  363. }
  364. if ( !$chmod ) {
  365. $chmod = FS_CHMOD_DIR;
  366. }
  367. wfSuppressWarnings();
  368. $mkdir = mkdir($path);
  369. wfRestoreWarnings();
  370. if ( !$mkdir ) {
  371. return false;
  372. }
  373. $this->chmod( $path, $chmod );
  374. if ( $chown ) {
  375. $this->chown( $path, $chown );
  376. }
  377. if ( $chgrp ) {
  378. $this->changeFileGroup( $path, $chgrp );
  379. }
  380. return true;
  381. }
  382. /**
  383. * @see Filesystem::touch
  384. */
  385. public function touch( $file, $time = 0, $atime = 0 ) {
  386. if ( $time == 0 ) {
  387. $time = time();
  388. }
  389. if ( $atime == 0 ) {
  390. $atime = time();
  391. }
  392. wfSuppressWarnings();
  393. $result = (bool)touch( $file, $time, $atime );
  394. wfRestoreWarnings();
  395. return $result;
  396. }
  397. /**
  398. * @see Filesystem::writeToFile
  399. */
  400. public function writeToFile( $file, $contents, $mode = false ) {
  401. wfSuppressWarnings();
  402. $fopen = fopen( $file, 'w' );
  403. wfRestoreWarnings();
  404. if ( !( $fp = $fopen ) ) {
  405. return false;
  406. }
  407. wfSuppressWarnings();
  408. fwrite( $fp, $contents );
  409. fclose( $fp );
  410. wfRestoreWarnings();
  411. $this->chmod( $file, $mode );
  412. return true;
  413. }
  414. }