PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Files.class.php

https://gitlab.com/karl3/gs_libs
PHP | 1019 lines | 503 code | 104 blank | 412 comment | 179 complexity | d089d2f886be1967b8673606c7518fb9 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This function library contains custom functions related to
  5. * dealing with files and the file system including files themselves,
  6. * directories, and file uploads.
  7. *
  8. * @todo
  9. * NEEDED FUNCTIONALITY
  10. *
  11. * IMPROVEMENTS TO EXISTING FUNCTIONALITY:
  12. * - readfile - Outputs a file
  13. * - realpath - Returns canonicalized absolute pathname
  14. * - rename - Renames a file or directory
  15. * - rmdir - Removes directory
  16. * - stat - Gives information about a file
  17. * - file - Reads entire file into an array
  18. * - fstat - Gets information about a file using an open file pointer
  19. * - fwrite - Binary-safe file write
  20. * - fread - Binary-safe file read
  21. * - is_readable - Tells whether the filename is readable
  22. * - is_writable - Tells whether the filename is writable
  23. * - mkdir - Makes directory
  24. * - is_dir - Tells whether the filename is a directory
  25. * needs clearstatcache() after execution
  26. * - is_executable - Tells whether the filename is executable
  27. * needs clearstatcache() after execution
  28. * - is_file � Tells whether the filename is a regular file
  29. * needs clearstatcache() after execution
  30. *
  31. * NEW FUNCTIONALITY
  32. * - Handle file uploads
  33. * - Read contents of a directory into an array
  34. * - Images: Thumbnail Creation
  35. * - Bulk thumbnail creation
  36. * - Extract Zip/GZip
  37. * - Auto Image Gallery
  38. * - Watermark image with GD
  39. * - Clean up file names of bad characters
  40. * - Parse CSV/ Tab-Separated/ Pipe Separated Files
  41. */
  42. class Files
  43. {
  44. /**
  45. *
  46. */
  47. public function __construct()
  48. {
  49. // Valid modes for fopen(), used to verify that the mode(s) chosen throughout these functions will work for the file
  50. $this->valid_fopen_modes = array("r", "r+", "w", "w+", "a", "a+", "x", "x+");
  51. }
  52. /////////////////////////////////////////////////////////////////////////////////////////
  53. // The first section of this file contains improvements upon existing PHP functionality
  54. // such as adding debugging, or adding a layer of safety to the existing functionality
  55. /////////////////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * changes file (or directory) permissions
  58. *
  59. * @param string $file the full path of the file or directory we are working with
  60. * @param int $perms the new permissions. MUST BE OCTAL!!!
  61. * @param bool $strict whether to perform an explicit check for the file's existence first.
  62. *
  63. * @return bool true, on success
  64. */
  65. function change_perms($file, $perms, $strict = true)
  66. {
  67. // first, ensure that $perms is an octal
  68. if (filter_var($perms, FILTER_VALIDATE_INT, array("flags" => FILTER_FLAG_ALLOW_OCTAL)) === false) {
  69. return false;
  70. // $perms was not an octal
  71. } else {
  72. if (($strict == true) && (!file_exists($file))) {
  73. return false;
  74. // file doesn't exist
  75. } else {
  76. chmod($file, $perms);
  77. return true;
  78. // all's well that ends well
  79. }
  80. }
  81. }
  82. /**
  83. * copies file from one location to another
  84. *
  85. * @param string $infile the full path & name of the file being copied
  86. * @param string $outfile the full path & name of the new destination
  87. * @param bool $delete_infile
  88. * @param bool $allow_overwrite whether an existing file of the
  89. * same name as $outfile should be overwritten
  90. *
  91. * @internal param $copyfile
  92. * @return bool true on success, false otherwise
  93. */
  94. function copyfile($infile, $outfile, $delete_infile = false, $allow_overwrite = false)
  95. {
  96. if (($allow_overwrite == false) && (file_exists($outfile))) {
  97. return false;
  98. // the destination file already exists and we don't want to overwrite it
  99. }
  100. if (!file_exists($infile)) {
  101. return false;
  102. // original file doesn't exist
  103. }
  104. copy($infile, $outfile);
  105. // if we want to delete the original, go right ahead
  106. if ($delete_infile == true) {
  107. unlink($infile);
  108. }
  109. return true;
  110. }
  111. /**
  112. * provides improved functionality to PHP's native file_get_contents function
  113. *
  114. * @param string $file the full path of the file we are working with
  115. * @param string $method do we return an array, or a string? Options are, "array" or "string"
  116. * @param bool $strict whether to perform an explicit check for the file's existence first.
  117. *
  118. * @return string or array, depending upon the $method param
  119. * the $output variable is populated with the file's contents
  120. */
  121. function filegetcontents($file, $method = "string", $strict = true)
  122. {
  123. if (($strict == true) && (!file_exists($file))) {
  124. return false;
  125. } else {
  126. if ($method == "array") {
  127. $output = file($file);
  128. } else {
  129. $output = file_get_contents(urlencode($file));
  130. }
  131. return $output;
  132. }
  133. }
  134. /**
  135. * provides improved functionality to PHP's native file_put_contents function
  136. *
  137. * @param string $file the full path of the file we are working with
  138. * @param string $data
  139. * @param bool $strict whether to perform an explicit check for the file's existence first.
  140. *
  141. * @return mixed false on failure
  142. */
  143. function fileputcontents($file, $data, $strict = true)
  144. {
  145. if (($strict == true) && (!file_exists($file))) {
  146. return false;
  147. } else {
  148. return file_put_contents($file, $data);
  149. }
  150. }
  151. /**
  152. * returns the last time file was accessed
  153. *
  154. * @param string $file the full path of the file we are working with
  155. * @param string $format the format for the return value
  156. * must be a valid format for date()
  157. * @param bool $strict whether to perform an explicit check for the file's existence first.
  158. *
  159. * @return mixed, false on failure, else string with the last accessed time, formatted as desired
  160. */
  161. function file_atime($file, $format = "F d Y H:i:s", $strict = true)
  162. {
  163. if (($strict == true) && (!file_exists($file))) {
  164. return false;
  165. // file doesn't exist
  166. } else {
  167. $output = date("$format", fileatime($file));
  168. clearstatcache();
  169. }
  170. return $output;
  171. }
  172. /**
  173. * returns the last time file was modified
  174. *
  175. * @param string $file the full path of the file we are working with
  176. * @param string $format
  177. * @param bool $strict whether to perform an explicit check for the file's existence first.
  178. *
  179. * @return string the last modified time, formatted as desired
  180. */
  181. function file_mtime($file, $format = "F d Y H:i:s", $strict = true)
  182. {
  183. if (($strict == true) && (!file_exists($file))) {
  184. return false;
  185. // file doesn't exist
  186. } else {
  187. $output = date($format, fileatime($file));
  188. clearstatcache();
  189. return $output;
  190. }
  191. }
  192. /**
  193. * returns the octal value for permissins on the specified file
  194. *
  195. * @param string $file the full path of the file or directory we are working with
  196. * @param bool $strict whether to perform an explicit check for the file's existence first.
  197. *
  198. * @return string the permissions are returned
  199. */
  200. function check_perms($file, $strict = true)
  201. {
  202. if (($strict == true) && (!file_exists($file))) {
  203. return false;
  204. // file doesn't exist
  205. } else {
  206. $output = substr(sprintf('%o', fileperms($file)), -4);
  207. clearstatcache();
  208. return $output;
  209. }
  210. }
  211. /**
  212. * gets the size, in bytes, of a file
  213. *
  214. * @param string $file the full path of the file or directory we are working with
  215. * @param bool $strict whether to perform an explicit check for the file's existence first.
  216. *
  217. * @return string the size of the file is returned, in bytes
  218. */
  219. function get_filesize($file, $strict = true)
  220. {
  221. if (($strict == true) && (!file_exists($file))) {
  222. return false;
  223. // file doesn't exist
  224. } else {
  225. $output = filesize($file);
  226. clearstatcache();
  227. return $output;
  228. }
  229. }
  230. /**
  231. * @param string $file the full path of the file or directory we are working with
  232. * @param string $mode the mode to use when locking
  233. * To acquire a shared lock (reader), set operation to LOCK_SH
  234. * To acquire an exclusive lock (writer), set operation to LOCK_EX
  235. * To release a lock (shared or exclusive), set operation to LOCK_UN
  236. * If you don't want flock() to block while locking, add LOCK_NB
  237. * @param bool $strict whether to perform an explicit check for the file's existence first.
  238. *
  239. * @internal param $lock_file
  240. * @desc locks a file
  241. * @return bool
  242. */
  243. function lock_file($file, $mode = "LOCK_EX", $strict = true)
  244. {
  245. if (($strict == true) && (!file_exists($file))) {
  246. return false;
  247. // file doesn't exist
  248. } else {
  249. flock($file, $mode);
  250. return true;
  251. }
  252. }
  253. /**
  254. * @param string $file the full path of the file or directory we are working with
  255. * @param $mode
  256. * @param bool $strict whether to perform an explicit check for the file's existence first.
  257. *
  258. * @return bool
  259. */
  260. function file_open($file, $mode, $strict = true)
  261. {
  262. if (($strict == true) && (!file_exists($file))) {
  263. return false;
  264. // file doesn't exist
  265. } else {
  266. fopen($file, $mode);
  267. return true;
  268. }
  269. }
  270. /**
  271. * @param string $file the full path of the file or directory we are working with
  272. * @param bool $strict whether to perform an explicit check for the file's existence first.
  273. *
  274. * @desc get an improved pathinfo array with the following keys
  275. * dirname => /var/www/html
  276. * basename => example.html
  277. * extension => html
  278. * basenameWE => example (as in, basename without extension)
  279. * @return bool|mixed
  280. */
  281. function get_pathinfo($file, $strict = true)
  282. {
  283. if (($strict == true) && (!file_exists($file))) {
  284. return false;
  285. // file doesn't exist
  286. } else {
  287. //get a basic pathinfo
  288. $output = pathinfo($file);
  289. $output["basenameWE"] = substr($output["basename"], 0, strlen($output["basename"]) - (strlen($output["extension"]) + 1));
  290. return $output;
  291. }
  292. }
  293. /**
  294. * "touches" a file, updating its access time and (optionally) its last modified time
  295. *
  296. * @param string $file the full path of the file or directory we are working with
  297. * @param bool $keep_mtime whether to maintain the lastmodified time of the file
  298. * @param bool $strict whether to perform an explicit check for the file's existence first.
  299. *
  300. * @return bool
  301. */
  302. function touchfile($file, $keep_mtime = false, $strict = true)
  303. {
  304. if ($keep_mtime == true) {
  305. $mtime = date("U", filemtime($file));
  306. } else {
  307. $mtime = date("U");
  308. }
  309. if (($strict == true) && (!file_exists($file))) {
  310. return false;
  311. // file doesn't exist
  312. } else {
  313. touch($file, $mtime);
  314. return true;
  315. }
  316. }
  317. /**
  318. * if a file exists, delete it
  319. *
  320. * @param string $file the full path of the file or directory we are working with
  321. * @param bool $strict whether to perform an explicit check for the file's existence first.
  322. *
  323. * @return bool
  324. */
  325. function file_delete($file, $strict = true)
  326. {
  327. if (($strict == true) && (!file_exists($file))) {
  328. return false;
  329. // file doesn't exist
  330. } else {
  331. unlink($file);
  332. return true;
  333. }
  334. }
  335. /**
  336. * @param string $path the full path of the directory we are working with
  337. * @param string $mode the mode to create the directory in
  338. * @param bool $recursive whether to make this creation recursive
  339. * (meaning whether to create the directory and all other
  340. * directories needed in order to make this one)
  341. * @param string $context any special "context" wrapper
  342. *
  343. * @return bool
  344. */
  345. function makedirectory($path, $mode = "0777", $recursive = false, $context = null)
  346. {
  347. $path = trim($path);
  348. // Sanity check to make sure the directory doesn't already exist
  349. if (is_dir($path)) {
  350. return false;
  351. // directory could not be created because it exists already
  352. }
  353. if (is_null($context)) {
  354. if (!mkdir($path, $mode, $recursive)) {
  355. return false;
  356. // directory could not be created
  357. } else {
  358. return true;
  359. }
  360. } // $context parameter should be passed to mkdir
  361. else {
  362. if (!mkdir($path, $mode, $recursive, $context)) {
  363. return false;
  364. // directory could not be created
  365. } else {
  366. return true;
  367. }
  368. }
  369. }
  370. /**
  371. * deletes all contents of a directory
  372. *
  373. * @param string $directory the full path of the directory we are working with
  374. * @param bool $strict whether to perform an explicit check for the directory's existence first.
  375. *
  376. * @return bool
  377. */
  378. function delete_dircontents($directory, $strict = true)
  379. {
  380. $dir_files_array = Files::get_dir_files_array($directory);
  381. if (!is_array($dir_files_array)) {
  382. return false;
  383. }
  384. foreach ($dir_files_array AS $file) {
  385. if (is_file($directory . "/" . $file)) {
  386. Files::file_delete($directory . "/" . $file, $strict);
  387. }
  388. }
  389. return true;
  390. }
  391. ////////////////////////////////////////////////////////////////////////////////////////
  392. // The second section of this file contains completely new, specialized functionality
  393. // such as methods for writing, reading, and manipulating files and directories in more
  394. // specialized ways than that which exists in current functionality
  395. /////////////////////////////////////////////////////////////////////////////////////////
  396. /**
  397. * creates a gzipped file
  398. *
  399. * @param string $infile
  400. * @param string $outfile
  401. * @param bool $binary
  402. * @param bool $strict
  403. *
  404. * @return bool TRUE on success
  405. */
  406. function create_gzip($infile, $outfile, $binary = false, $strict = true)
  407. {
  408. if (($strict == true) && (!file_exists($infile))) {
  409. return false;
  410. // file doesn't exist
  411. } else {
  412. // append binary flag to mode, if desired
  413. if ($binary == true) {
  414. $mode = "wb";
  415. } else {
  416. $mode = "w";
  417. }
  418. $data = implode("", file($infile));
  419. $gzdata = gzencode($data, 9);
  420. $fp = fopen($outfile, $mode);
  421. fwrite($fp, $gzdata);
  422. fclose($fp);
  423. return true;
  424. }
  425. }
  426. /**
  427. * creates a new file
  428. *
  429. * @param string $file the full path of the file or directory we are working with
  430. * @param string $data the content of the new file
  431. * @param bool $binary
  432. * @param bool $strict whether to perform an explicit check for the file's existence first.
  433. *
  434. * @return bool TRUE on success
  435. */
  436. function write_new_file($file, $data, $binary = false, $strict = true)
  437. {
  438. if (($strict == true) && (file_exists($file))) {
  439. return false;
  440. // file already exists
  441. } else {
  442. // append binary flag to mode, if desired
  443. if ($binary == true) {
  444. $mode = "wb";
  445. } else {
  446. $mode = "w";
  447. }
  448. $handle = fopen($file, $mode);
  449. if (!$handle) {
  450. return false;
  451. // could not open the file
  452. } else {
  453. if (flock($handle, LOCK_EX)) {
  454. fwrite($handle, $data);
  455. flock($handle, LOCK_UN);
  456. // release the lock
  457. fclose($handle);
  458. return true;
  459. } else {
  460. return false;
  461. // could not lock the file for writing
  462. }
  463. }
  464. }
  465. }
  466. /**
  467. * appends data to a file that already exists
  468. *
  469. * @param string $file the full path of the file or directory we are working with
  470. * @param string $data the content of the new file
  471. * @param string $mode how the file will be entered.
  472. * $mode reference:
  473. * "r" - read only
  474. * "r+" - read and write if the file exists already
  475. * "w" - write only. creates file of doesn't exist - OVERWRITES EXISTING FILE CONTENTS
  476. * "w+" - read and write. creates file if it doesn't exist- OVERWRITES EXISTING FILE CONTENTS
  477. * "a" - write only to the end of the file. creates file if it doesn't exist
  478. * "a+" - read and write. creates file if it doesn't exist
  479. * "x" - writing only. creates file if it doesn't exist. RETURNS FALSE if it does exist already
  480. * "x+" - read and write. creates file if it doesn't exist. RETURNS FALSE if it does exist already
  481. * @param bool $binary
  482. *
  483. * @return bool TRUE on success
  484. */
  485. function append_file($file, $data, $mode = "a", $binary = false)
  486. {
  487. // if the $mode chosen is not a valid mode for fopen(), something is wrong
  488. if (!in_array($mode, $this->valid_fopen_modes)) {
  489. return false;
  490. // file open mode is invalid
  491. }
  492. // if the file doesn't exist, and mode requires it, return an error
  493. if ((!file_exists($file)) && (($mode !== "a") && ($mode !== "a+") && ($mode !== "r") && ($mode !== "r+"))) {
  494. return false;
  495. // file doesn't exist
  496. } // if the file exists, and the mode will cause fopen to choke, return an error
  497. elseif ((file_exists($file)) && (($mode !== "w") || ($mode !== "w+") || ($mode !== "x") || ($mode !== "x+"))) {
  498. return false;
  499. // file exists but invalid open mode defined
  500. } // otherwise, everything is cool and we're ready to rock.
  501. else {
  502. // append binary flag to mode, if desired
  503. if ($binary == true) {
  504. $mode .= "b";
  505. }
  506. $handle = fopen($file, $mode);
  507. // one last check
  508. if (!$handle) {
  509. return false;
  510. // file opening failure
  511. } else {
  512. if (flock($handle, LOCK_EX)) {
  513. fwrite($handle, $data);
  514. flock($handle, LOCK_UN);
  515. // release the lock
  516. fclose($handle);
  517. return true;
  518. } else {
  519. return false;
  520. // cannot lock the file
  521. }
  522. }
  523. }
  524. }
  525. /**
  526. * returns a string of the file contents
  527. * there is an important difference between this function and other file
  528. * reading functions in this library: this one is intended to output
  529. * text files (HTML, CSS, etc) with special characters converted, ready for
  530. * output to the screen, kind of like a quick & dirty source viewer
  531. *
  532. * @param string $file the full path of the file we are working with
  533. * @param bool $strict whether to perform an explicit check for the file's existence first.
  534. *
  535. * @return string the $output variable is populated with the file's data
  536. */
  537. function output_file_contents($file, $strict = true)
  538. {
  539. if (($strict == true) && (!file_exists($file))) {
  540. return false;
  541. // file does not exist
  542. } else {
  543. $fp = fopen($file, "r");
  544. //open in read-only mode
  545. if (!$fp) {
  546. return false;
  547. // file could not be opened
  548. } else {
  549. while (!feof($fp)) {
  550. $data = fgets($fp, 900);
  551. $data = htmlspecialchars($data);
  552. }
  553. $output .= $data;
  554. return $output;
  555. }
  556. }
  557. }
  558. /**
  559. * @param $file
  560. * @param bool $strict
  561. *
  562. * @return bool|string
  563. */
  564. function find_extension($file, $strict = true)
  565. {
  566. if (($strict == true) && (!file_exists($file))) {
  567. return false;
  568. // file doesn't exist
  569. } else {
  570. $revfile = strrev($file);
  571. $type = strtolower(strrev(substr($revfile, 0, strpos($revfile, "."))));
  572. return $type;
  573. }
  574. }
  575. /**
  576. * @param $dir
  577. *
  578. * @return array
  579. */
  580. function get_dir_files_array($dir)
  581. {
  582. if ($handle = opendir($dir)) {
  583. while (false !== ($file = readdir($handle))) {
  584. $files_array[] = $file;
  585. }
  586. closedir($handle);
  587. }
  588. return $files_array;
  589. }
  590. /**
  591. * @param $size
  592. *
  593. * @return string
  594. */
  595. function process_file_size($size)
  596. {
  597. $i = 0;
  598. $iec = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  599. while (($size / 1024) > 1) {
  600. $size = $size / 1024;
  601. $i++;
  602. }
  603. return substr($size, 0, strpos($size, '.') + 4) . " $iec[$i]";
  604. }
  605. /**
  606. * @param $file
  607. * @param bool $strict
  608. *
  609. * @return array|bool
  610. */
  611. function get_file_stats($file, $strict = true)
  612. {
  613. if (($strict == true) && (!file_exists($file))) {
  614. return false;
  615. // file doesn't exist
  616. } else {
  617. $output = array();
  618. $output['fileatime'] = fileatime($file);
  619. $output['filegroup'] = filegroup($file);
  620. $output['filemtime'] = filemtime($file);
  621. $output['fileowner'] = fileowner($file);
  622. $output['filesize'] = filesize($file);
  623. $output['is_dir'] = var_export(is_dir($file), true);
  624. $output['is_executable'] = var_export(is_executable($file), true);
  625. $output['is_file'] = var_export(is_file($file), true);
  626. $output['is_link'] = var_export(is_link($file), true);
  627. $output['is_readable'] = var_export(is_readable($file), true);
  628. $output['is_uploaded_file'] = var_export(is_uploaded_file($file), true);
  629. $output['is_writable'] = var_export(is_writable($file), true);
  630. return $output;
  631. }
  632. }
  633. /**
  634. * @param $path
  635. *
  636. * @return array with the following keys:
  637. * size - the data size of the directory and its contents
  638. * count - the total number of files in the directory
  639. * dircount - the number of sub directories
  640. */
  641. function get_directory_size($path)
  642. {
  643. $totalsize = 0;
  644. $totalcount = 0;
  645. $dircount = 0;
  646. if ($handle = opendir($path)) {
  647. while (false !== ($file = readdir($handle))) {
  648. $nextpath = $path . '/' . $file;
  649. if ($file != '.' && $file != '..' && !is_link($nextpath)) {
  650. if (is_dir($nextpath)) {
  651. $dircount++;
  652. $result = Files::get_directory_size($nextpath);
  653. $totalsize += $result['size'];
  654. $totalcount += $result['count'];
  655. $dircount += $result['dircount'];
  656. } elseif (is_file($nextpath)) {
  657. $totalsize += filesize($nextpath);
  658. $totalcount++;
  659. }
  660. }
  661. }
  662. }
  663. closedir($handle);
  664. $total['size'] = $totalsize;
  665. // data size of directory
  666. $total['count'] = $totalcount;
  667. // total number of files
  668. $total['dircount'] = $dircount;
  669. // number of sub directories
  670. return $total;
  671. }
  672. /**
  673. * takes a file and erases all the data in it
  674. *
  675. * @param string $file the full path of the file or directory we are working with
  676. * @param bool $strict whether to perform an explicit check for the file's existence first.
  677. *
  678. * @return bool TRUE on success
  679. */
  680. function truncate_file($file, $strict = true)
  681. {
  682. if (($strict == true) && (!file_exists($file))) {
  683. return false;
  684. // file already exists
  685. } else {
  686. $handle = fopen($file, "w");
  687. if (!$handle) {
  688. return false;
  689. // could not open the file
  690. } else {
  691. fclose($handle);
  692. return true;
  693. }
  694. }
  695. }
  696. /**
  697. * counts the total number of lines in a file
  698. *
  699. * @param string $filepath the full path to the file
  700. *
  701. * @return int
  702. */
  703. function count_lines($filepath)
  704. {
  705. // open the file for reading
  706. $handle = fopen($filepath, "r");
  707. // set a counter
  708. $count = 0;
  709. // loop over the file
  710. while (fgets($handle)) {
  711. // increment the counter
  712. $count++;
  713. }
  714. // close the file
  715. fclose($handle);
  716. // show the total
  717. return $count;
  718. }
  719. /**
  720. * copies a remote file to the server
  721. * USAGE EXAMPLE:
  722. * copyFile("http://test-server.com/file/movie.mpg", "myfolder/");
  723. *
  724. * @param string $url URL to the file
  725. * @param string $dirname the directory to put this into. Must be CHMOD 777
  726. * @param null $return_method
  727. * @param bool $strict whether to perform an explicit check for the file's existence first.
  728. *
  729. * @return bool
  730. */
  731. function copy_remote_file($url, $dirname, $return_method = null, $strict = true)
  732. {
  733. // attempt to open the
  734. @$file = fopen($url, "rb");
  735. if (($strict == true) && (!$file)) {
  736. return false;
  737. // file can't be opened
  738. } else {
  739. $filename = basename($url);
  740. $fc = fopen($dirname . "$filename", "wb");
  741. while (!feof($file)) {
  742. $line = fread($file, 1028);
  743. fwrite($fc, $line);
  744. }
  745. fclose($fc);
  746. if ($return_method == "name") {
  747. return $filename;
  748. } elseif ($return_method == "fullpath") {
  749. return $dirname . $filename;
  750. } else {
  751. return true;
  752. }
  753. }
  754. }
  755. /**
  756. * lists directory contents arranged by date
  757. *
  758. * @param string $path file path for the directory we're working with
  759. *
  760. * @return array
  761. */
  762. function listDirectoryByDate($path)
  763. {
  764. $dir = opendir($path);
  765. $list = array();
  766. while ($file = readdir($dir)) {
  767. if ($file != '.' && $file != '..' && !is_dir($file)) {
  768. $ctime = filectime($path . $file) . rand(1000, 9999);
  769. $list[$ctime] = $file;
  770. }
  771. }
  772. closedir($dir);
  773. krsort($list);
  774. return $list;
  775. }
  776. /**
  777. * @param $path
  778. *
  779. * @return bool
  780. */
  781. function rrmdir($path)
  782. {
  783. return is_file($path) ? @unlink($path) : array_map('rrmdir', glob($path . '/*')) == @rmdir($path);
  784. }
  785. /**
  786. * lists the contents of a zip file
  787. *
  788. * @param $path_to_zip
  789. * @param $destination
  790. * @param bool $fullpath
  791. * @param bool $dumpzip
  792. *
  793. * @return bool
  794. */
  795. function get_zip_files_array($path_to_zip, $destination, $fullpath = true, $dumpzip = false)
  796. {
  797. $zip = new ZipArchive;
  798. if ($zip->open($path_to_zip) === true) {
  799. $zip->extractTo($destination);
  800. // delete the original zip file
  801. if ($dumpzip === true) {
  802. Files::file_delete($path_to_zip);
  803. }
  804. $zip->close();
  805. // return the array
  806. $dir_files_array = Files::directoryToArray($destination, true);
  807. return $dir_files_array;
  808. } else {
  809. return false;
  810. }
  811. }
  812. /**
  813. * @param $directory
  814. * @param $recursive
  815. *
  816. * @return array
  817. */
  818. function directoryToArray($directory, $recursive = true)
  819. {
  820. $array_items = array();
  821. if ($handle = opendir($directory)) {
  822. while (false !== ($file = readdir($handle))) {
  823. if ($file != "." && $file != "..") {
  824. if (is_dir($directory . "/" . $file)) {
  825. if (false != $recursive) {
  826. $array_items = array_merge($array_items, Files::directoryToArray($directory . "/" . $file, $recursive));
  827. }
  828. $file = $directory . "/" . $file;
  829. $array_items[] = preg_replace("/\/\//si", "/", $file);
  830. } else {
  831. $file = $directory . "/" . $file;
  832. $array_items[] = preg_replace("/\/\//si", "/", $file);
  833. }
  834. }
  835. }
  836. closedir($handle);
  837. }
  838. return $array_items;
  839. }
  840. /**
  841. * opens a file, reads it, returns the data
  842. *
  843. * @param string $file the file to be read
  844. *
  845. * @return string
  846. */
  847. function getFileData($file)
  848. {
  849. $fp = fopen($file, "r");
  850. $data .= "<pre>";
  851. while (!feof($fp)) {
  852. $data = fgets($fp, 900);
  853. $data .= htmlspecialchars($data);
  854. }
  855. $data .= "</pre>";
  856. return $data;
  857. }
  858. /**
  859. *
  860. * makes an HTML table out of a CSV file
  861. *
  862. * @param string $file
  863. * @param string $tExtras
  864. *
  865. * @return string
  866. */
  867. function CSV_to_table($file, $tExtras)
  868. {
  869. $output = '';
  870. $row = 0;
  871. if (($handle = fopen($file, "r")) !== false) {
  872. $output .= "<table $tExtras>\n";
  873. while (($data = fgetcsv($handle, 1000, ",")) !== false) {
  874. $num = count($data);
  875. $output .= "<tr>\n";
  876. $row++;
  877. for ($c = 0; $c < $num; $c++) {
  878. if ($row == 1) {
  879. $output .= "<th scope=\"col\">" . $data[$c] . "</th>\n";
  880. } else {
  881. if ($c == 0) {
  882. $output .= "<th scope=\"row\">" . $data[$c] . "</th>\n";
  883. } else {
  884. $output .= "<td>" . $data[$c] . "</td>\n";
  885. }
  886. }
  887. }
  888. $output .= "</tr>\n";
  889. }
  890. $output .= "</table>\n";
  891. fclose($handle);
  892. return $output;
  893. } else {
  894. return false;
  895. }
  896. }
  897. /**
  898. *
  899. * Connects to a remote file and retrieves the MIME type of that file
  900. *
  901. * @param string $url full URL to the remote file
  902. *
  903. * @return string returns the MIME of the file
  904. */
  905. public function getRemoteFileMIMEType($url)
  906. {
  907. $ch = curl_init($url);
  908. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  909. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  910. curl_setopt($ch, CURLOPT_HEADER, 1);
  911. curl_setopt($ch, CURLOPT_NOBODY, 1);
  912. curl_exec($ch);
  913. return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  914. }
  915. }