Base for a static organization website

File.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * Convenience class for reading, writing and appending to files.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Utility
  15. * @since CakePHP(tm) v 0.2.9
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Folder', 'Utility');
  19. /**
  20. * Convenience class for reading, writing and appending to files.
  21. *
  22. * @package Cake.Utility
  23. */
  24. class File {
  25. /**
  26. * Folder object of the file
  27. *
  28. * @var Folder
  29. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$Folder
  30. */
  31. public $Folder = null;
  32. /**
  33. * File name
  34. *
  35. * @var string
  36. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$name
  37. */
  38. public $name = null;
  39. /**
  40. * File info
  41. *
  42. * @var array
  43. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$info
  44. */
  45. public $info = array();
  46. /**
  47. * Holds the file handler resource if the file is opened
  48. *
  49. * @var resource
  50. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$handle
  51. */
  52. public $handle = null;
  53. /**
  54. * Enable locking for file reading and writing
  55. *
  56. * @var bool
  57. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$lock
  58. */
  59. public $lock = null;
  60. /**
  61. * Path property
  62. *
  63. * Current file's absolute path
  64. *
  65. * @var mixed
  66. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$path
  67. */
  68. public $path = null;
  69. /**
  70. * Constructor
  71. *
  72. * @param string $path Path to file
  73. * @param bool $create Create file if it does not exist (if true)
  74. * @param int $mode Mode to apply to the folder holding the file
  75. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File
  76. */
  77. public function __construct($path, $create = false, $mode = 0755) {
  78. $this->Folder = new Folder(dirname($path), $create, $mode);
  79. if (!is_dir($path)) {
  80. $this->name = basename($path);
  81. }
  82. $this->pwd();
  83. $create && !$this->exists() && $this->safe($path) && $this->create();
  84. }
  85. /**
  86. * Closes the current file if it is opened
  87. */
  88. public function __destruct() {
  89. $this->close();
  90. }
  91. /**
  92. * Creates the file.
  93. *
  94. * @return bool Success
  95. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::create
  96. */
  97. public function create() {
  98. $dir = $this->Folder->pwd();
  99. if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
  100. if (touch($this->path)) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Opens the current file with a given $mode
  108. *
  109. * @param string $mode A valid 'fopen' mode string (r|w|a ...)
  110. * @param bool $force If true then the file will be re-opened even if its already opened, otherwise it won't
  111. * @return bool True on success, false on failure
  112. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::open
  113. */
  114. public function open($mode = 'r', $force = false) {
  115. if (!$force && is_resource($this->handle)) {
  116. return true;
  117. }
  118. if ($this->exists() === false) {
  119. if ($this->create() === false) {
  120. return false;
  121. }
  122. }
  123. $this->handle = fopen($this->path, $mode);
  124. if (is_resource($this->handle)) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /**
  130. * Return the contents of this file as a string.
  131. *
  132. * @param string $bytes where to start
  133. * @param string $mode A `fread` compatible mode.
  134. * @param bool $force If true then the file will be re-opened even if its already opened, otherwise it won't
  135. * @return mixed string on success, false on failure
  136. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::read
  137. */
  138. public function read($bytes = false, $mode = 'rb', $force = false) {
  139. if ($bytes === false && $this->lock === null) {
  140. return file_get_contents($this->path);
  141. }
  142. if ($this->open($mode, $force) === false) {
  143. return false;
  144. }
  145. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  146. return false;
  147. }
  148. if (is_int($bytes)) {
  149. return fread($this->handle, $bytes);
  150. }
  151. $data = '';
  152. while (!feof($this->handle)) {
  153. $data .= fgets($this->handle, 4096);
  154. }
  155. if ($this->lock !== null) {
  156. flock($this->handle, LOCK_UN);
  157. }
  158. if ($bytes === false) {
  159. $this->close();
  160. }
  161. return trim($data);
  162. }
  163. /**
  164. * Sets or gets the offset for the currently opened file.
  165. *
  166. * @param int|bool $offset The $offset in bytes to seek. If set to false then the current offset is returned.
  167. * @param int $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
  168. * @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
  169. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::offset
  170. */
  171. public function offset($offset = false, $seek = SEEK_SET) {
  172. if ($offset === false) {
  173. if (is_resource($this->handle)) {
  174. return ftell($this->handle);
  175. }
  176. } elseif ($this->open() === true) {
  177. return fseek($this->handle, $offset, $seek) === 0;
  178. }
  179. return false;
  180. }
  181. /**
  182. * Prepares an ASCII string for writing. Converts line endings to the
  183. * correct terminator for the current platform. If Windows, "\r\n" will be used,
  184. * all other platforms will use "\n"
  185. *
  186. * @param string $data Data to prepare for writing.
  187. * @param bool $forceWindows If true forces usage Windows newline string.
  188. * @return string The with converted line endings.
  189. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::prepare
  190. */
  191. public static function prepare($data, $forceWindows = false) {
  192. $lineBreak = "\n";
  193. if (DIRECTORY_SEPARATOR === '\\' || $forceWindows === true) {
  194. $lineBreak = "\r\n";
  195. }
  196. return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
  197. }
  198. /**
  199. * Write given data to this file.
  200. *
  201. * @param string $data Data to write to this File.
  202. * @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
  203. * @param string $force Force the file to open
  204. * @return bool Success
  205. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::write
  206. */
  207. public function write($data, $mode = 'w', $force = false) {
  208. $success = false;
  209. if ($this->open($mode, $force) === true) {
  210. if ($this->lock !== null) {
  211. if (flock($this->handle, LOCK_EX) === false) {
  212. return false;
  213. }
  214. }
  215. if (fwrite($this->handle, $data) !== false) {
  216. $success = true;
  217. }
  218. if ($this->lock !== null) {
  219. flock($this->handle, LOCK_UN);
  220. }
  221. }
  222. return $success;
  223. }
  224. /**
  225. * Append given data string to this file.
  226. *
  227. * @param string $data Data to write
  228. * @param string $force Force the file to open
  229. * @return bool Success
  230. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::append
  231. */
  232. public function append($data, $force = false) {
  233. return $this->write($data, 'a', $force);
  234. }
  235. /**
  236. * Closes the current file if it is opened.
  237. *
  238. * @return bool True if closing was successful or file was already closed, otherwise false
  239. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::close
  240. */
  241. public function close() {
  242. if (!is_resource($this->handle)) {
  243. return true;
  244. }
  245. return fclose($this->handle);
  246. }
  247. /**
  248. * Deletes the file.
  249. *
  250. * @return bool Success
  251. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::delete
  252. */
  253. public function delete() {
  254. if (is_resource($this->handle)) {
  255. fclose($this->handle);
  256. $this->handle = null;
  257. }
  258. if ($this->exists()) {
  259. return unlink($this->path);
  260. }
  261. return false;
  262. }
  263. /**
  264. * Returns the file info as an array with the following keys:
  265. *
  266. * - dirname
  267. * - basename
  268. * - extension
  269. * - filename
  270. * - filesize
  271. * - mime
  272. *
  273. * @return array File information.
  274. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::info
  275. */
  276. public function info() {
  277. if (!$this->info) {
  278. $this->info = pathinfo($this->path);
  279. }
  280. if (!isset($this->info['filename'])) {
  281. $this->info['filename'] = $this->name();
  282. }
  283. if (!isset($this->info['filesize'])) {
  284. $this->info['filesize'] = $this->size();
  285. }
  286. if (!isset($this->info['mime'])) {
  287. $this->info['mime'] = $this->mime();
  288. }
  289. return $this->info;
  290. }
  291. /**
  292. * Returns the file extension.
  293. *
  294. * @return string The file extension
  295. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::ext
  296. */
  297. public function ext() {
  298. if (!$this->info) {
  299. $this->info();
  300. }
  301. if (isset($this->info['extension'])) {
  302. return $this->info['extension'];
  303. }
  304. return false;
  305. }
  306. /**
  307. * Returns the file name without extension.
  308. *
  309. * @return string The file name without extension.
  310. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::name
  311. */
  312. public function name() {
  313. if (!$this->info) {
  314. $this->info();
  315. }
  316. if (isset($this->info['extension'])) {
  317. return basename($this->name, '.' . $this->info['extension']);
  318. } elseif ($this->name) {
  319. return $this->name;
  320. }
  321. return false;
  322. }
  323. /**
  324. * Makes file name safe for saving
  325. *
  326. * @param string $name The name of the file to make safe if different from $this->name
  327. * @param string $ext The name of the extension to make safe if different from $this->ext
  328. * @return string ext The extension of the file
  329. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::safe
  330. */
  331. public function safe($name = null, $ext = null) {
  332. if (!$name) {
  333. $name = $this->name;
  334. }
  335. if (!$ext) {
  336. $ext = $this->ext();
  337. }
  338. return preg_replace("/(?:[^\w\.-]+)/", "_", basename($name, $ext));
  339. }
  340. /**
  341. * Get md5 Checksum of file with previous check of Filesize
  342. *
  343. * @param int|bool $maxsize in MB or true to force
  344. * @return string|false md5 Checksum {@link http://php.net/md5_file See md5_file()}, or false in case of an error
  345. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::md5
  346. */
  347. public function md5($maxsize = 5) {
  348. if ($maxsize === true) {
  349. return md5_file($this->path);
  350. }
  351. $size = $this->size();
  352. if ($size && $size < ($maxsize * 1024) * 1024) {
  353. return md5_file($this->path);
  354. }
  355. return false;
  356. }
  357. /**
  358. * Returns the full path of the file.
  359. *
  360. * @return string Full path to the file
  361. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::pwd
  362. */
  363. public function pwd() {
  364. if ($this->path === null) {
  365. $dir = $this->Folder->pwd();
  366. if (is_dir($dir)) {
  367. $this->path = $this->Folder->slashTerm($dir) . $this->name;
  368. }
  369. }
  370. return $this->path;
  371. }
  372. /**
  373. * Returns true if the file exists.
  374. *
  375. * @return bool True if it exists, false otherwise
  376. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::exists
  377. */
  378. public function exists() {
  379. $this->clearStatCache();
  380. return (file_exists($this->path) && is_file($this->path));
  381. }
  382. /**
  383. * Returns the "chmod" (permissions) of the file.
  384. *
  385. * @return string|false Permissions for the file, or false in case of an error
  386. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::perms
  387. */
  388. public function perms() {
  389. if ($this->exists()) {
  390. return substr(sprintf('%o', fileperms($this->path)), -4);
  391. }
  392. return false;
  393. }
  394. /**
  395. * Returns the file size
  396. *
  397. * @return int|false Size of the file in bytes, or false in case of an error
  398. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::size
  399. */
  400. public function size() {
  401. if ($this->exists()) {
  402. return filesize($this->path);
  403. }
  404. return false;
  405. }
  406. /**
  407. * Returns true if the file is writable.
  408. *
  409. * @return bool True if it's writable, false otherwise
  410. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::writable
  411. */
  412. public function writable() {
  413. return is_writable($this->path);
  414. }
  415. /**
  416. * Returns true if the File is executable.
  417. *
  418. * @return bool True if it's executable, false otherwise
  419. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::executable
  420. */
  421. public function executable() {
  422. return is_executable($this->path);
  423. }
  424. /**
  425. * Returns true if the file is readable.
  426. *
  427. * @return bool True if file is readable, false otherwise
  428. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::readable
  429. */
  430. public function readable() {
  431. return is_readable($this->path);
  432. }
  433. /**
  434. * Returns the file's owner.
  435. *
  436. * @return int|false The file owner, or false in case of an error
  437. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::owner
  438. */
  439. public function owner() {
  440. if ($this->exists()) {
  441. return fileowner($this->path);
  442. }
  443. return false;
  444. }
  445. /**
  446. * Returns the file's group.
  447. *
  448. * @return int|false The file group, or false in case of an error
  449. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::group
  450. */
  451. public function group() {
  452. if ($this->exists()) {
  453. return filegroup($this->path);
  454. }
  455. return false;
  456. }
  457. /**
  458. * Returns last access time.
  459. *
  460. * @return int|false Timestamp of last access time, or false in case of an error
  461. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastAccess
  462. */
  463. public function lastAccess() {
  464. if ($this->exists()) {
  465. return fileatime($this->path);
  466. }
  467. return false;
  468. }
  469. /**
  470. * Returns last modified time.
  471. *
  472. * @return int|false Timestamp of last modification, or false in case of an error
  473. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastChange
  474. */
  475. public function lastChange() {
  476. if ($this->exists()) {
  477. return filemtime($this->path);
  478. }
  479. return false;
  480. }
  481. /**
  482. * Returns the current folder.
  483. *
  484. * @return Folder Current folder
  485. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::Folder
  486. */
  487. public function folder() {
  488. return $this->Folder;
  489. }
  490. /**
  491. * Copy the File to $dest
  492. *
  493. * @param string $dest Destination for the copy
  494. * @param bool $overwrite Overwrite $dest if exists
  495. * @return bool Success
  496. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::copy
  497. */
  498. public function copy($dest, $overwrite = true) {
  499. if (!$this->exists() || is_file($dest) && !$overwrite) {
  500. return false;
  501. }
  502. return copy($this->path, $dest);
  503. }
  504. /**
  505. * Get the mime type of the file. Uses the finfo extension if
  506. * its available, otherwise falls back to mime_content_type
  507. *
  508. * @return false|string The mimetype of the file, or false if reading fails.
  509. */
  510. public function mime() {
  511. if (!$this->exists()) {
  512. return false;
  513. }
  514. if (function_exists('finfo_open')) {
  515. $finfo = finfo_open(FILEINFO_MIME);
  516. $finfo = finfo_file($finfo, $this->pwd());
  517. if (!$finfo) {
  518. return false;
  519. }
  520. list($type) = explode(';', $finfo);
  521. return $type;
  522. }
  523. if (function_exists('mime_content_type')) {
  524. return mime_content_type($this->pwd());
  525. }
  526. return false;
  527. }
  528. /**
  529. * Clear PHP's internal stat cache
  530. *
  531. * For 5.3 onwards it's possible to clear cache for just a single file. Passing true
  532. * will clear all the stat cache.
  533. *
  534. * @param bool $all Clear all cache or not
  535. * @return void
  536. */
  537. public function clearStatCache($all = false) {
  538. if ($all === false && version_compare(PHP_VERSION, '5.3.0') >= 0) {
  539. return clearstatcache(true, $this->path);
  540. }
  541. return clearstatcache();
  542. }
  543. /**
  544. * Searches for a given text and replaces the text if found.
  545. *
  546. * @param string|array $search Text(s) to search for.
  547. * @param string|array $replace Text(s) to replace with.
  548. * @return bool Success
  549. */
  550. public function replaceText($search, $replace) {
  551. if (!$this->open('r+')) {
  552. return false;
  553. }
  554. if ($this->lock !== null) {
  555. if (flock($this->handle, LOCK_EX) === false) {
  556. return false;
  557. }
  558. }
  559. $replaced = $this->write(str_replace($search, $replace, $this->read()), 'w', true);
  560. if ($this->lock !== null) {
  561. flock($this->handle, LOCK_UN);
  562. }
  563. $this->close();
  564. return $replaced;
  565. }
  566. }