123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- // remove all subdirectories of a cache folder which are older than a given number of minutes
- // the age is determined by the folder name which has the format XXXXXXX_YYYYMMDDHHMMSS
- // where XXXXXXX is an arbitrary string (e.g. itemKey) which must not contain underscores (_)
- function purge_cache($path, $cacheage=0) {
- $dir = opendir($path);
- $controldate = intval(date("YmdHis", mktime(date("H"), intval(date("i")) - $cacheage, date("s"), date("m"), date("d"), date("Y"))));
- while ($dir_item = readdir($dir)) {
- if ((is_dir($path . "/" . $dir_item)) && ($dir_item != ".") && ($dir_item != "..")) {
- $dirdate = intval(str_replace("_", "", substr($dir_item, strpos($dir_item, "_") + 1)));
- if ($dirdate < $controldate) {
- rmdirr($path . "/" . $dir_item);
- }
- }
- }
- }
- // unzips a zipfile to a path and decodes filenames encoded in base64 (if they have a %ZB64 suffix)
- // can be set to not execute writing of unzipped files ($writefiles=false)
- // returns filename of the unzipped file (if only one file in zip file) otherwise the directory path that holds unzipped files
- function unzip($zipfile,$outpath,$depreciated_option=0,$writefiles=true) {
- $zip=zip_open($zipfile);
- if(!$zip) {return("Unable to proccess zipfile \"" . $zipfile . "\"");}
- $e='';
- $i=0;
- while($zip_entry=zip_read($zip)) {
- $zdir=dirname(zip_entry_name($zip_entry));
- $zname=zip_entry_name($zip_entry);
- if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file \"".$zname."\"";continue;}
- if(!is_dir($outpath."/".$zdir)) mkdirr($outpath."/".$zdir,0777);
- #print "{$zdir} | {$zname} \n";
- $zip_fs=zip_entry_filesize($zip_entry);
- if(empty($zip_fs)) continue;
- $zz=zip_entry_read($zip_entry,$zip_fs);
-
- if (substr($zname,-5)=="%ZB64") {
- $zname=substr($zname,0,strpos($zname,"%ZB64"));
- $zname=base64_decode($zname);
- }
-
- if($writefiles) {
- $z=fopen($outpath . "/" . $zname,"w");
- fwrite($z,$zz);
- fclose($z);
- }
- zip_entry_close($zip_entry);
-
- $i = $i +1;
- }
- zip_close($zip);
- if (strlen($e)>0) {
- echo($e);
- return($e);
- } else {
- if($i>1) {
- return($outpath);
- } else {
- return($outpath . "/" . $zname);
- }
- }
- }
- // recursively remove directory (ie. directory and any file and subdirectory contents)
- function rmdirr($dir) {
- if (is_dir($dir)) {
- $objects = scandir($dir);
- foreach ($objects as $object) {
- if ($object != "." && $object != "..") {
- if (filetype($dir."/".$object) == "dir") rmdirr($dir."/".$object); else unlink($dir."/".$object);
- }
- }
- reset($objects);
- rmdir($dir);
- }
- }
- // recursively create a directory (i.e. creates any parent tree if not existing)
- function mkdirr($pn,$mode=null) {
- if(is_dir($pn)||empty($pn)) return true;
- $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn);
- if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;}
- $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
- if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
- return false;
- }
- function findwebfile($path) {
- $filename = "";
- if (is_dir($path)) {
- $objects = scandir($path);
- foreach ($objects as $object) {
- if(substr($object,-5)==".html") {
- if ($filename!="") {
- if(substr($filename,-7,1)=="_") $filename = $object;
- } else {
- $filename = $object;
- }
- }
- }
- reset($objects);
- }
- return $filename;
- }
- ?>
|