* @author Tim Kroeger */ function io_lock($file){ global $bmzConf; // no locking if safemode hack //if ($bmzConf['safemodehack']) return; $lockDir = $bmzConf['lockdir'] . '/' . md5($file); @ignore_user_abort(1); $timeStart = time(); do { //waited longer than 3 seconds? -> stale lock if ((time() - $timeStart) > 3) break; $locked = @mkdir($lockDir); } while ($locked === false); } /** * Unlocks a file * * @author Andreas Gohr * @author Tim Kroeger */ function io_unlock($file){ global $bmzConf; // no locking if safemode hack //if($bmzConf['safemodehack']) return; $lockDir = $bmzConf['lockdir'] . '/' . md5($file); @rmdir($lockDir); @ignore_user_abort(0); } /** * Returns the name of a cachefile from given data * * The needed directory is created by this function! * * @author Andreas Gohr * @author Tim Kroeger * * @param string $data This data is used to create a unique md5 name * @param string $ext This is appended to the filename if given * @return string The filename of the cachefile */ function getCacheName($data, $ext='') { global $bmzConf; $md5 = md5($data); @preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/', $data, $match); if(!empty($match[1])) { $newfilename_arr = explode('.', $match[1]); $newfilename = $newfilename_arr[0]; } else { $newfilename = $md5; } $file = $bmzConf['cachedir'] . '/' . $md5{0} . '/' . $newfilename . $ext; io_makeFileDir($file); return $file; } /** * Create the directory needed for the given file * * @author Andreas Gohr * @author Tim Kroeger */ function io_makeFileDir($file){ global $messageStack; global $bmzConf; $dir = dirname($file); $dmask = $bmzConf['dmask']; umask($dmask); if(!is_dir($dir)){ io_mkdir_p($dir) || $messageStack->add("Creating directory $dir failed", "error"); } umask($bmzConf['umask']); } /** * Creates a directory hierachy. * * @link http://www.php.net/manual/en/function.mkdir.php * @author * @author Andreas Gohr * @author Tim Kroeger */ function io_mkdir_p($target){ global $bmzConf; if (is_dir($target) || empty($target)) return 1; // best case check first if (@file_exists($target) && !is_dir($target)) return 0; //recursion if (io_mkdir_p(substr($target, 0, strrpos($target, '/')))){ return @mkdir($target, 0755); // crawl back up & create dir tree } return 0; }