include.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // remove all subdirectories of a cache folder which are older than a given number of minutes
  3. // the age is determined by the folder name which has the format XXXXXXX_YYYYMMDDHHMMSS
  4. // where XXXXXXX is an arbitrary string (e.g. itemKey) which must not contain underscores (_)
  5. function purge_cache($path, $cacheage=0) {
  6. $dir = opendir($path);
  7. $controldate = intval(date("YmdHis", mktime(date("H"), intval(date("i")) - $cacheage, date("s"), date("m"), date("d"), date("Y"))));
  8. while ($dir_item = readdir($dir)) {
  9. if ((is_dir($path . "/" . $dir_item)) && ($dir_item != ".") && ($dir_item != "..")) {
  10. $dirdate = intval(str_replace("_", "", substr($dir_item, strpos($dir_item, "_") + 1)));
  11. if ($dirdate < $controldate) {
  12. rmdirr($path . "/" . $dir_item);
  13. }
  14. }
  15. }
  16. }
  17. // unzips a zipfile to a path and decodes filenames encoded in base64 (if they have a %ZB64 suffix)
  18. // can be set to not execute writing of unzipped files ($writefiles=false)
  19. // returns filename of the unzipped file (if only one file in zip file) otherwise the directory path that holds unzipped files
  20. function unzip($zipfile,$outpath,$depreciated_option=0,$writefiles=true) {
  21. $zip=zip_open($zipfile);
  22. if(!$zip) {return("Unable to proccess zipfile \"" . $zipfile . "\"");}
  23. $e='';
  24. $i=0;
  25. while($zip_entry=zip_read($zip)) {
  26. $zdir=dirname(zip_entry_name($zip_entry));
  27. $zname=zip_entry_name($zip_entry);
  28. if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file \"".$zname."\"";continue;}
  29. if(!is_dir($outpath."/".$zdir)) mkdirr($outpath."/".$zdir,0777);
  30. #print "{$zdir} | {$zname} \n";
  31. $zip_fs=zip_entry_filesize($zip_entry);
  32. if(empty($zip_fs)) continue;
  33. $zz=zip_entry_read($zip_entry,$zip_fs);
  34. if (substr($zname,-5)=="%ZB64") {
  35. $zname=substr($zname,0,strpos($zname,"%ZB64"));
  36. $zname=base64_decode($zname);
  37. }
  38. if($writefiles) {
  39. $z=fopen($outpath . "/" . $zname,"w");
  40. fwrite($z,$zz);
  41. fclose($z);
  42. }
  43. zip_entry_close($zip_entry);
  44. $i = $i +1;
  45. }
  46. zip_close($zip);
  47. if (strlen($e)>0) {
  48. echo($e);
  49. return($e);
  50. } else {
  51. if($i>1) {
  52. return($outpath);
  53. } else {
  54. return($outpath . "/" . $zname);
  55. }
  56. }
  57. }
  58. // recursively remove directory (ie. directory and any file and subdirectory contents)
  59. function rmdirr($dir) {
  60. if (is_dir($dir)) {
  61. $objects = scandir($dir);
  62. foreach ($objects as $object) {
  63. if ($object != "." && $object != "..") {
  64. if (filetype($dir."/".$object) == "dir") rmdirr($dir."/".$object); else unlink($dir."/".$object);
  65. }
  66. }
  67. reset($objects);
  68. rmdir($dir);
  69. }
  70. }
  71. // recursively create a directory (i.e. creates any parent tree if not existing)
  72. function mkdirr($pn,$mode=null) {
  73. if(is_dir($pn)||empty($pn)) return true;
  74. $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn);
  75. if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;}
  76. $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
  77. if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
  78. return false;
  79. }
  80. function findwebfile($path) {
  81. $filename = "";
  82. if (is_dir($path)) {
  83. $objects = scandir($path);
  84. foreach ($objects as $object) {
  85. if(substr($object,-5)==".html") {
  86. if ($filename!="") {
  87. if(substr($filename,-7,1)=="_") $filename = $object;
  88. } else {
  89. $filename = $object;
  90. }
  91. }
  92. }
  93. reset($objects);
  94. }
  95. return $filename;
  96. }
  97. ?>