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

/firstrend/src/install/Lib/ORG/Dir.class.php

http://ownerpress.googlecode.com/
PHP | 524 lines | 232 code | 33 blank | 259 comment | 36 complexity | e4968b2320df58ca76fbcd701d4c6452 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * DirectoryIterator??? PHP5?????DirectoryIterator?
  15. +------------------------------------------------------------------------------
  16. * @category ORG
  17. * @package ORG
  18. * @subpackage Io
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Dir extends Think implements IteratorAggregate
  24. {//?????
  25. private $_values = array();
  26. /**
  27. +----------------------------------------------------------
  28. * ????
  29. +----------------------------------------------------------
  30. * @access public
  31. +----------------------------------------------------------
  32. * @param string $path ????
  33. +----------------------------------------------------------
  34. */
  35. function __construct($path,$pattern='*')
  36. {
  37. if(substr($path, -1) != "/") $path .= "/";
  38. $this->listFile($path,$pattern);
  39. }
  40. /**
  41. +----------------------------------------------------------
  42. * ???????????
  43. +----------------------------------------------------------
  44. * @access public
  45. +----------------------------------------------------------
  46. * @param mixed $pathname ??
  47. +----------------------------------------------------------
  48. */
  49. function listFile($pathname,$pattern='*')
  50. {
  51. static $_listDirs = array();
  52. $guid = md5($pathname.$pattern);
  53. if(!isset($_listDirs[$guid])){
  54. $dir = array();
  55. $list = glob($pathname.$pattern);
  56. foreach ($list as $i=>$file){
  57. $dir[$i]['filename'] = basename($file);
  58. $dir[$i]['pathname'] = realpath($file);
  59. $dir[$i]['owner'] = fileowner($file);
  60. $dir[$i]['perms'] = fileperms($file);
  61. $dir[$i]['inode'] = fileinode($file);
  62. $dir[$i]['group'] = filegroup($file);
  63. $dir[$i]['path'] = dirname($file);
  64. $dir[$i]['atime'] = fileatime($file);
  65. $dir[$i]['ctime'] = filectime($file);
  66. $dir[$i]['size'] = filesize($file);
  67. $dir[$i]['type'] = filetype($file);
  68. $dir[$i]['ext'] = is_file($file)?strtolower(substr(strrchr(basename($file), '.'),1)):'';
  69. $dir[$i]['mtime'] = filemtime($file);
  70. $dir[$i]['isDir'] = is_dir($file);
  71. $dir[$i]['isFile'] = is_file($file);
  72. $dir[$i]['isLink'] = is_link($file);
  73. //$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
  74. $dir[$i]['isReadable'] = is_readable($file);
  75. $dir[$i]['isWritable'] = is_writable($file);
  76. }
  77. $cmp_func = create_function('$a,$b','
  78. $k = "isDir";
  79. if($a[$k] == $b[$k]) return 0;
  80. return $a[$k]>$b[$k]?-1:1;
  81. ');
  82. // ????? ???????
  83. usort($dir,$cmp_func);
  84. $this->_values = $dir;
  85. $_listDirs[$guid] = $dir;
  86. }else{
  87. $this->_values = $_listDirs[$guid];
  88. }
  89. }
  90. /**
  91. +----------------------------------------------------------
  92. * ????????
  93. +----------------------------------------------------------
  94. * @access public
  95. +----------------------------------------------------------
  96. * @return integer
  97. +----------------------------------------------------------
  98. */
  99. function getATime()
  100. {
  101. $current = $this->current($this->_values);
  102. return $current['atime'];
  103. }
  104. /**
  105. +----------------------------------------------------------
  106. * ????? inode ????
  107. +----------------------------------------------------------
  108. * @access public
  109. +----------------------------------------------------------
  110. * @return integer
  111. +----------------------------------------------------------
  112. */
  113. function getCTime()
  114. {
  115. $current = $this->current($this->_values);
  116. return $current['ctime'];
  117. }
  118. /**
  119. +----------------------------------------------------------
  120. * ?????????
  121. +----------------------------------------------------------
  122. * @access public
  123. +----------------------------------------------------------
  124. * @return DirectoryIterator
  125. +----------------------------------------------------------
  126. */
  127. function getChildren()
  128. {
  129. $current = $this->current($this->_values);
  130. if($current['isDir']){
  131. return new Dir($current['pathname']);
  132. }
  133. return false;
  134. }
  135. /**
  136. +----------------------------------------------------------
  137. * ?????
  138. +----------------------------------------------------------
  139. * @access public
  140. +----------------------------------------------------------
  141. * @return string
  142. +----------------------------------------------------------
  143. */
  144. function getFilename()
  145. {
  146. $current = $this->current($this->_values);
  147. return $current['filename'];
  148. }
  149. /**
  150. +----------------------------------------------------------
  151. * ??????
  152. +----------------------------------------------------------
  153. * @access public
  154. +----------------------------------------------------------
  155. * @return integer
  156. +----------------------------------------------------------
  157. */
  158. function getGroup()
  159. {
  160. $current = $this->current($this->_values);
  161. return $current['group'];
  162. }
  163. /**
  164. +----------------------------------------------------------
  165. * ????? inode
  166. +----------------------------------------------------------
  167. * @access public
  168. +----------------------------------------------------------
  169. * @return integer
  170. +----------------------------------------------------------
  171. */
  172. function getInode()
  173. {
  174. $current = $this->current($this->_values);
  175. return $current['inode'];
  176. }
  177. /**
  178. +----------------------------------------------------------
  179. * ???????????
  180. +----------------------------------------------------------
  181. * @access public
  182. +----------------------------------------------------------
  183. * @return integer
  184. +----------------------------------------------------------
  185. */
  186. function getMTime()
  187. {
  188. $current = $this->current($this->_values);
  189. return $current['mtime'];
  190. }
  191. /**
  192. +----------------------------------------------------------
  193. * ????????
  194. +----------------------------------------------------------
  195. * @access public
  196. +----------------------------------------------------------
  197. * @return string
  198. +----------------------------------------------------------
  199. */
  200. function getOwner()
  201. {
  202. $current = $this->current($this->_values);
  203. return $current['owner'];
  204. }
  205. /**
  206. +----------------------------------------------------------
  207. * ?????????????
  208. +----------------------------------------------------------
  209. * @access public
  210. +----------------------------------------------------------
  211. * @return string
  212. +----------------------------------------------------------
  213. */
  214. function getPath()
  215. {
  216. $current = $this->current($this->_values);
  217. return $current['path'];
  218. }
  219. /**
  220. +----------------------------------------------------------
  221. * ???????????????
  222. +----------------------------------------------------------
  223. * @access public
  224. +----------------------------------------------------------
  225. * @return string
  226. +----------------------------------------------------------
  227. */
  228. function getPathname()
  229. {
  230. $current = $this->current($this->_values);
  231. return $current['pathname'];
  232. }
  233. /**
  234. +----------------------------------------------------------
  235. * ???????
  236. +----------------------------------------------------------
  237. * @access public
  238. +----------------------------------------------------------
  239. * @return integer
  240. +----------------------------------------------------------
  241. */
  242. function getPerms()
  243. {
  244. $current = $this->current($this->_values);
  245. return $current['perms'];
  246. }
  247. /**
  248. +----------------------------------------------------------
  249. * ???????
  250. +----------------------------------------------------------
  251. * @access public
  252. +----------------------------------------------------------
  253. * @return integer
  254. +----------------------------------------------------------
  255. */
  256. function getSize()
  257. {
  258. $current = $this->current($this->_values);
  259. return $current['size'];
  260. }
  261. /**
  262. +----------------------------------------------------------
  263. * ??????
  264. +----------------------------------------------------------
  265. * @access public
  266. +----------------------------------------------------------
  267. * @return string
  268. +----------------------------------------------------------
  269. */
  270. function getType()
  271. {
  272. $current = $this->current($this->_values);
  273. return $current['type'];
  274. }
  275. /**
  276. +----------------------------------------------------------
  277. * ?????
  278. +----------------------------------------------------------
  279. * @access public
  280. +----------------------------------------------------------
  281. * @return boolen
  282. +----------------------------------------------------------
  283. */
  284. function isDir()
  285. {
  286. $current = $this->current($this->_values);
  287. return $current['isDir'];
  288. }
  289. /**
  290. +----------------------------------------------------------
  291. * ?????
  292. +----------------------------------------------------------
  293. * @access public
  294. +----------------------------------------------------------
  295. * @return boolen
  296. +----------------------------------------------------------
  297. */
  298. function isFile()
  299. {
  300. $current = $this->current($this->_values);
  301. return $current['isFile'];
  302. }
  303. /**
  304. +----------------------------------------------------------
  305. * ???????????
  306. +----------------------------------------------------------
  307. * @access public
  308. +----------------------------------------------------------
  309. * @return boolen
  310. +----------------------------------------------------------
  311. */
  312. function isLink()
  313. {
  314. $current = $this->current($this->_values);
  315. return $current['isLink'];
  316. }
  317. /**
  318. +----------------------------------------------------------
  319. * ????????
  320. +----------------------------------------------------------
  321. * @access public
  322. +----------------------------------------------------------
  323. * @return boolen
  324. +----------------------------------------------------------
  325. */
  326. function isExecutable()
  327. {
  328. $current = $this->current($this->_values);
  329. return $current['isExecutable'];
  330. }
  331. /**
  332. +----------------------------------------------------------
  333. * ??????
  334. +----------------------------------------------------------
  335. * @access public
  336. +----------------------------------------------------------
  337. * @return boolen
  338. +----------------------------------------------------------
  339. */
  340. function isReadable()
  341. {
  342. $current = $this->current($this->_values);
  343. return $current['isReadable'];
  344. }
  345. /**
  346. +----------------------------------------------------------
  347. * ??foreach?????
  348. +----------------------------------------------------------
  349. * @access public
  350. +----------------------------------------------------------
  351. * @return string
  352. +----------------------------------------------------------
  353. */
  354. function getIterator()
  355. {
  356. return new ArrayObject($this->_values);
  357. }
  358. // ?????????
  359. function toArray() {
  360. return $this->_values;
  361. }
  362. // ????
  363. /**
  364. +----------------------------------------------------------
  365. * ????????
  366. +----------------------------------------------------------
  367. * @access static
  368. +----------------------------------------------------------
  369. * @return void
  370. +----------------------------------------------------------
  371. */
  372. function isEmpty($directory)
  373. {
  374. $handle = opendir($directory);
  375. while (($file = readdir($handle)) !== false)
  376. {
  377. if ($file != "." && $file != "..")
  378. {
  379. closedir($handle);
  380. return false;
  381. }
  382. }
  383. closedir($handle);
  384. return true;
  385. }
  386. /**
  387. +----------------------------------------------------------
  388. * ??????????
  389. +----------------------------------------------------------
  390. * @access static
  391. +----------------------------------------------------------
  392. * @return void
  393. +----------------------------------------------------------
  394. */
  395. function getList($directory)
  396. {
  397. return scandir($directory);
  398. }
  399. /**
  400. +----------------------------------------------------------
  401. * ?????????????
  402. +----------------------------------------------------------
  403. * @access static
  404. +----------------------------------------------------------
  405. * @return void
  406. +----------------------------------------------------------
  407. */
  408. function delDir($directory,$subdir=true)
  409. {
  410. if (is_dir($directory) == false)
  411. {
  412. return false;
  413. }
  414. $handle = @opendir($directory);
  415. while (($file = readdir($handle)) !== false)
  416. {
  417. if ($file != "." && $file != "..")
  418. {
  419. is_dir("$directory/$file")?
  420. Dir::delDir("$directory/$file"):
  421. @unlink("$directory/$file");
  422. }
  423. }
  424. if (readdir($handle) == false)
  425. {
  426. @closedir($handle);
  427. @rmdir($directory);
  428. }
  429. return true;
  430. }
  431. /**
  432. +----------------------------------------------------------
  433. * ??????????????????
  434. +----------------------------------------------------------
  435. * @access static
  436. +----------------------------------------------------------
  437. * @return void
  438. +----------------------------------------------------------
  439. */
  440. function del($directory)
  441. {
  442. if (is_dir($directory) == false)
  443. {
  444. exit("The Directory Is Not Exist!");
  445. }
  446. $handle = opendir($directory);
  447. while (($file = readdir($handle)) !== false)
  448. {
  449. if ($file != "." && $file != ".." && is_file("$directory/$file"))
  450. {
  451. unlink("$directory/$file");
  452. }
  453. }
  454. closedir($handle);
  455. }
  456. /**
  457. +----------------------------------------------------------
  458. * ????
  459. +----------------------------------------------------------
  460. * @access static
  461. +----------------------------------------------------------
  462. * @return void
  463. +----------------------------------------------------------
  464. */
  465. function copyDir($source, $destination)
  466. {
  467. if (is_dir($source) == false)
  468. {
  469. exit("The Source Directory Is Not Exist!");
  470. }
  471. if (is_dir($destination) == false)
  472. {
  473. mkdir($destination, 0700);
  474. }
  475. $handle=opendir($source);
  476. while (false !== ($file = readdir($handle)))
  477. {
  478. if ($file != "." && $file != "..")
  479. {
  480. is_dir("$source/$file")?
  481. Dir::copyDir("$source/$file", "$destination/$file"):
  482. copy("$source/$file", "$destination/$file");
  483. }
  484. }
  485. closedir($handle);
  486. }
  487. }//?????
  488. if(!class_exists('DirectoryIterator')) {
  489. class DirectoryIterator extends Dir {}
  490. }
  491. ?>