<?php
/**
* bmz_image_handler.php
* general filesystem access handling
*
* @author Tim Kroeger <tim@breakmyzencart.com>
* @author Andreas Gohr <andi@splitbrain.org>
* @copyright Copyright 2005-2006 breakmyzencart.com
* @copyright Portions Copyright 2005 splitbrain.org
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0
* @version $Id: functions_bmz_io.php,v 1.2 2006/04/11 22:00:55 tim Exp $
*/
/**
* Tries to lock a file
*
* Locking uses directories inside $bmzConf['lockdir']
*
* It waits maximal 3 seconds for the lock, after this time
* the lock is assumed to be stale and the function goes on
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Tim Kroeger <tim@breakmyzencart.com>
*/
function io_lock($file){
// no locking if safemode hack
if ($bmzConf['safemodehack']) return;
$lockDir = $bmzConf['lockdir'] . '/' . md5($file);
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 <andi@splitbrain.org>
* @author Tim Kroeger <tim@breakmyzencart.com>
*/
function io_unlock($file){
// no locking if safemode hack
if($bmzConf['safemodehack']) return;
$lockDir = $bmzConf['lockdir'] . '/' . md5($file);
}
/**
* Returns the name of a cachefile from given data
*
* The needed directory is created by this function!
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Tim Kroeger <tim@breakmyzencart.com>
*
* @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='') {
$file = $bmzConf['cachedir'] . '/' . $md5{0} . '/' . $md5.$ext;
io_makeFileDir($file);
return $file;
}
/**
* Create the directory needed for the given file
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Tim Kroeger <tim@breakmyzencart.com>
*/
function io_makeFileDir($file){
$dmask = $bmzConf['dmask'];
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 <saint@corenova.com>
* @author Andreas Gohr <andi@splitbrain.org>
* @author Tim Kroeger <tim@breakmyzencart.com>
*/
function io_mkdir_p($target){
if (is_dir($target) || empty($target)) return 1; // best case check first
//recursion
if($bmzConf['safemodehack']){
return io_mkdir_ftp($dir);
}else{
return @mkdir($target, 0777); // crawl back up & create dir tree
}
}
return 0;
}
/**
* Creates a directory using FTP
*
* This is used when the safemode workaround is enabled
*
* @author <andi@splitbrain.org>
*/
function io_mkdir_ftp($dir){
$messageStack->add("FTP support not found - safemode workaround not usable", "error");
return false;
}
$conn = @ftp_connect($bmzConf['ftp']['host'], $bmzConf['ftp']['port'], 10);
if(!$conn){
$messageStack->add("FTP connection failed", "error");
return false;
}
if(!@ftp_login($conn, $bmzConf['ftp']['user'], $bmzConf['ftp']['pass'])){
$messageStack->add("FTP login failed", "error");
return false;
}
//create directory
//set permissions (using the directory umask)
@ftp_site($conn, sprintf("CHMOD %04o %s", (0777 - $bmzConf['dmask']), $dir));
return $ok;
}