PageRenderTime 98ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 1ms

/test/classes/file/File.php

https://github.com/enmand/Cerenkov
PHP | 616 lines | 508 code | 85 blank | 23 comment | 35 complexity | 0998d7c96569a41e1393648cb5a4628f MD5 | raw file
  1. <?php
  2. /**
  3. * File.php
  4. * File abstracts moving, copying, file output, etc.
  5. *
  6. * @author Cerenkov Group
  7. * @copyright Cerenkov 2007
  8. * @package classes/file
  9. */
  10. class File implements FileInterface
  11. {
  12. private $path;
  13. private $handle;
  14. public function __construct($path='')
  15. {
  16. // Should probably call convertPathToLocal()
  17. // before assigning...
  18. $this->path = $path;
  19. $this->handle = NULL;
  20. }
  21. public function __destruct()
  22. {
  23. if ( $this->handle != NULL && $this->isFile())
  24. $this->closeFile();
  25. else if($this->handle != NULL && $this->isDir())
  26. $this->closeDir();
  27. }
  28. public function setPath($path)
  29. {
  30. $this->path = $path;
  31. }
  32. public function getPath()
  33. {
  34. return $this->path;
  35. }
  36. public function getPathConverted()
  37. {
  38. return File::convertPathToLocal($this->path);
  39. }
  40. public static function convertPathToLocal($path)
  41. {
  42. global $Config;
  43. $type = strtolower($Config->get('file.path_type'));
  44. switch($type)
  45. {
  46. case 'unix':
  47. return File::convertPathToUNIX($path);
  48. break;
  49. case 'dos':
  50. case 'windows':
  51. return File::convertPathToWindows($path);
  52. break;
  53. default:
  54. throw new FileException('Local path type not understood. Check config.ini');
  55. break;
  56. }
  57. }
  58. public static function convertPathToUNIX($path, $root='/')
  59. {
  60. // Replace drive letter if present with $root
  61. $path = preg_replace('/^[A-Za-z]\:/', $root, $path, 1);
  62. // Convert \ to /
  63. $path = preg_replace('/\\//', '/', $path);
  64. return $path;
  65. }
  66. public static function convertPathToWindows($path, $drive='C')
  67. {
  68. // Replace / with C:\ where C is the drive letter from $drive
  69. $path = preg_replace('#^/#', $drive . ':\\', $path);
  70. // Convert / to \
  71. $path = preg_replace('#/#', '\\', $path);
  72. return $path;
  73. }
  74. private function check_exists()
  75. {
  76. if ( file_exists($this->path) )
  77. return true;
  78. else throw new FileException('File does not exist.');
  79. }
  80. public function isUploaded()
  81. {
  82. $this->check_exists();
  83. return is_uploaded_file($this->path);
  84. }
  85. public function moveUploadedFile($destination)
  86. {
  87. if ( $this->isUploaded() )
  88. {
  89. $this->move($destination);
  90. }
  91. else
  92. {
  93. throw new FileException('Source file is not uploaded.');
  94. }
  95. }
  96. public function move($destination)
  97. {
  98. // Move from $this->path to $destination
  99. if ( file_exists($destination) )
  100. {
  101. throw new FileException('Destination file exists, delete it first.');
  102. }
  103. else
  104. {
  105. if ( $this->isUploaded() )
  106. {
  107. move_uploaded_file($this->path, $destination);
  108. }
  109. else
  110. {
  111. rename($this->path, $destination);
  112. }
  113. $this->path = $destination;
  114. }
  115. }
  116. public function copy($destination)
  117. {
  118. if ( file_exists($destination) )
  119. {
  120. throw new FileException('Destination file exists, delete it first.');
  121. }
  122. else
  123. {
  124. copy($this->path, $destination);
  125. }
  126. }
  127. public function delete()
  128. {
  129. // Delete the file this object references
  130. $this->check_exists();
  131. unlink($this->path);
  132. $this->handle = NULL;
  133. }
  134. public function exists()
  135. {
  136. return file_exists($this->path);
  137. }
  138. public static function fileExists($path)
  139. {
  140. return file_exists($path);
  141. }
  142. public function getBaseName()
  143. {
  144. return basename($this->path);
  145. }
  146. public function getRealPath()
  147. {
  148. $this->check_exists();
  149. return realpath($this->path);
  150. }
  151. public function getSize()
  152. {
  153. $this->check_exists();
  154. return filesize($this->path);
  155. }
  156. public function changeGroup($group)
  157. {
  158. $this->check_exists();
  159. return chgrp($this->path, $group);
  160. }
  161. public function changeMode($mode)
  162. {
  163. $this->check_exists();
  164. return chmod($this->path, $mode);
  165. }
  166. public function changeOwner($owner)
  167. {
  168. $this->check_exists();
  169. return chmod($this->path, $owner);
  170. }
  171. public function changeOwnerGroup($owner, $group)
  172. {
  173. $this->check_exists();
  174. if ( $this->changeOwner($owner) and $this->changeGroup($group) )
  175. {
  176. return true;
  177. }
  178. else return false;
  179. }
  180. public static function clearStatCache()
  181. {
  182. clearstatcache();
  183. }
  184. public function getDirectory()
  185. {
  186. return dirname($this->path);
  187. }
  188. public function getFreeSpace()
  189. {
  190. return disk_free_space($this->path);
  191. }
  192. public function getTotalSpace()
  193. {
  194. return disk_total_space($this->path);
  195. }
  196. private function check_open()
  197. {
  198. $this->check_exists();
  199. if ( is_null($this->handle) )
  200. {
  201. throw new FileException('File is not open.');
  202. }
  203. }
  204. public function openFile($mode)
  205. {
  206. $this->check_exists();
  207. if ( is_null($this->handle) )
  208. {
  209. $this->handle = fopen($this->path, $mode);
  210. return $this->handle;
  211. }
  212. else throw new FileException('File is already open.');
  213. }
  214. public function closeFile()
  215. {
  216. fclose($this->handle);
  217. $this->handle = NULL;
  218. }
  219. public function openDir()
  220. {
  221. $this->isDir();
  222. if(is_null($this->handle))
  223. {
  224. $this->handle = opendir($this->path);
  225. return $this->handle;
  226. }
  227. }
  228. public function closeDir()
  229. {
  230. closedir($this->handle);
  231. $this->handle = NULL;
  232. }
  233. public function isEOF()
  234. {
  235. $this->check_open();
  236. return feof($this->handle);
  237. }
  238. public function flush()
  239. {
  240. $this->check_open();
  241. return fflush($this->handle);
  242. }
  243. public function getChar()
  244. {
  245. $this->check_open();
  246. return fgetc($this->handle);
  247. }
  248. public function getCSV($length=NULL, $delimiter=',', $enclosure='"')
  249. {
  250. $this->check_open();
  251. if ( is_null($length) )
  252. {
  253. return fgetcsv($this->handle);
  254. }
  255. else
  256. {
  257. return fgetcsv($this->handle, $length, $delimiter, $enclosure);
  258. }
  259. }
  260. public function putCSV(array $fields, $delimiter=',', $enclosure='"')
  261. {
  262. $this->check_open();
  263. return fputcsv($this->handle, $fields, $delimiter, $enclosure);
  264. }
  265. public function getLine($length=NULL)
  266. {
  267. $this->check_open();
  268. if ( is_null($length) )
  269. return fgets($this->handle);
  270. else return fgets($this->handle, $length);
  271. }
  272. public function readData($length)
  273. {
  274. $this->check_open();
  275. return fread($this->handle, $length);
  276. }
  277. public function writeData($data, $length=NULL)
  278. {
  279. $this->check_open();
  280. if ( is_null($length) )
  281. return fwrite($this->handle, $data);
  282. else
  283. return fwrite($this->handle, $data, $length);
  284. }
  285. public function seek($offset, $whence=SEEK_SET)
  286. {
  287. $this->check_open();
  288. return fseek($this->handle, $offset, $whence);
  289. }
  290. public function tell()
  291. {
  292. $this->check_open();
  293. return ftell($this->handle);
  294. }
  295. public function rewind()
  296. {
  297. $this->check_open();
  298. return rewind($this->handle);
  299. }
  300. public function truncateFile($length=0)
  301. {
  302. $this->check_open();
  303. ftruncate($this->handle, $length);
  304. }
  305. public function statFile()
  306. {
  307. $this->check_open();
  308. return fstat($this->handle);
  309. }
  310. public function statPath()
  311. {
  312. $this->check_exists();
  313. return stat($this->path);
  314. }
  315. public function getLineStripped($length=NULL, $allowable_tags=NULL)
  316. {
  317. $this->check_open();
  318. if ( is_null($length) )
  319. {
  320. return fgetss($this->handle);
  321. }
  322. elseif ( is_null($allowable_tags) )
  323. {
  324. return fgetss($this->handle, $length);
  325. }
  326. else return fgetss($this->handle, $length, $allowable_tags);
  327. }
  328. public function getContents($check = FALSE)
  329. {
  330. if($check) $this->check_exists();
  331. return file_get_contents($this->path);
  332. }
  333. public function putContents($data)
  334. {
  335. $this->check_exists();
  336. return file_put_contents($this->path, $data);
  337. }
  338. public function getContentsArray()
  339. {
  340. $this->check_exists();
  341. return file($this->path);
  342. }
  343. public function getAccessTime()
  344. {
  345. $this->check_exists();
  346. return fileatime($this->path);
  347. }
  348. public function getChangeTime()
  349. {
  350. $this->check_exists();
  351. return filectime($this->path);
  352. }
  353. public function getModifiedTime()
  354. {
  355. $this->check_exists();
  356. return filemtime($this->path);
  357. }
  358. public function getGroup()
  359. {
  360. $this->check_exists();
  361. return filegroup($this->path);
  362. }
  363. public function getInode()
  364. {
  365. $this->check_exists();
  366. return fileinode($this->path);
  367. }
  368. public function getOwner()
  369. {
  370. $this->check_exists();
  371. return fileowner($this->path);
  372. }
  373. public function getPermissions()
  374. {
  375. $this->check_exists();
  376. return fileperms($this->path);
  377. }
  378. public function getType()
  379. {
  380. $this->check_exists();
  381. return filetype($this->path);
  382. }
  383. public function lockFile($operation)
  384. {
  385. $this->check_open();
  386. $would_block = 0;
  387. if ( flock($this->handle, $operation, $would_block) == false )
  388. {
  389. if ( $would_block )
  390. throw new FileException('Locking would block.');
  391. else throw new FileException('Could not lock file.');
  392. }
  393. return true;
  394. }
  395. public function lockShared()
  396. {
  397. return $this->lockFile( LOCK_SH | LOCK_NB );
  398. }
  399. public function lockExclusive()
  400. {
  401. return $this->lockFile( LOCK_EX | LOCK_NB );
  402. }
  403. public function unlockFile()
  404. {
  405. return $this->lockFile( LOCK_UN | LOCK_NB );
  406. }
  407. public function passThrough()
  408. {
  409. $this->check_exists();
  410. if ( is_null($this->handle) )
  411. {
  412. // Use readfile()
  413. return readfile($this->path);
  414. }
  415. else
  416. {
  417. // use fpassthru()
  418. return fpassthru($this->handle);
  419. }
  420. }
  421. public static function glob($pattern, $flags=NULL)
  422. {
  423. if ( is_null($flags) )
  424. return glob($pattern);
  425. else return glob($pattern, $flags);
  426. }
  427. public function isDir()
  428. {
  429. //$this->check_exists();
  430. return is_dir($this->path);
  431. }
  432. public function isExecutable()
  433. {
  434. $this->check_exists();
  435. return is_executable($this->path);
  436. }
  437. public function isFile()
  438. {
  439. return is_file($this->path);
  440. }
  441. public function isLink()
  442. {
  443. $this->check_exists();
  444. return is_link($this->path);
  445. }
  446. public function isReadable()
  447. {
  448. $this->check_exists();
  449. return is_readable($this->path);
  450. }
  451. public function isWritable()
  452. {
  453. $this->check_exists();
  454. return is_writable($this->path);
  455. }
  456. public function isWriteable()
  457. {
  458. // Compatibility function
  459. return $this->isWritable();
  460. }
  461. public function hardLink($link)
  462. {
  463. $this->check_exists();
  464. return link($this->path, $link);
  465. }
  466. public function symLink($link)
  467. {
  468. $this->check_exists();
  469. return symlink($this->path, $link);
  470. }
  471. public function softLink($link)
  472. {
  473. // Compatibility function
  474. $this->symLink($link);
  475. }
  476. public function makeDirectory($recursive=true)
  477. {
  478. global $Config;
  479. $mode = $Config->get('file.create_mode');
  480. return mkdir($this->path, $mode, $recursive);
  481. }
  482. public function removeDirectory()
  483. {
  484. $this->check_exists();
  485. if ( is_dir($this->path) )
  486. return rmdir($this->path);
  487. else throw new FileException('File is not a directory.');
  488. }
  489. public function getPathInfo()
  490. {
  491. return pathinfo($this->path);
  492. }
  493. public function touchFile()
  494. {
  495. return touch($this->path);
  496. }
  497. public static function setUmask($mask)
  498. {
  499. return umask($mask);
  500. }
  501. public static function getUmask()
  502. {
  503. return umask();
  504. }
  505. public function MIMEType()
  506. {
  507. $this->check_exists();
  508. return mime_content_type($this->path);
  509. }
  510. public function passThroughWithHeaders()
  511. {
  512. // Output file contents into http stream with suitable headers
  513. header('Content-Type: ' . $this->MIMEType() );
  514. header('Content-Length: ' . $this->getSize() );
  515. header('Content-Disposition: attachment; filename="'. basename($this->path) .'"');
  516. header("Content-Transfer-Encoding: binary\n");
  517. readfile($this->path);
  518. }
  519. public function passThroughForceDownload()
  520. {
  521. // Output file as application/octet-stream to try to force browsers to download
  522. // rather than display
  523. header('Content-Type: application/octet-stream');
  524. header('Content-Length: ' . $this->getSize() );
  525. header('Content-Disposition: attachment; filename="'. basename($this->path) .'"');
  526. header("Content-Transfer-Encoding: binary\n");
  527. readfile($this->path);
  528. }
  529. }
  530. ?>