Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * write out a file to disk
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * @param string $filename
Chris PeBenito 696b41
 * @param string $contents
Chris PeBenito 696b41
 * @param boolean $create_dirs
Chris PeBenito 696b41
 * @return boolean
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_core_write_file($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    $_dirname = dirname($params['filename']);
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($params['create_dirs']) {
Chris PeBenito 696b41
        $_params = array('dir' => $_dirname);
Chris PeBenito 696b41
        require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
Chris PeBenito 696b41
        smarty_core_create_dir_structure($_params, $smarty);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    // write to tmp file, then rename it to avoid
Chris PeBenito 696b41
    // file locking race condition
Chris PeBenito 696b41
    $_tmp_file = tempnam($_dirname, 'wrt');
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!($fd = @fopen($_tmp_file, 'wb'))) {
Chris PeBenito 696b41
        $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
Chris PeBenito 696b41
        if (!($fd = @fopen($_tmp_file, 'wb'))) {
Chris PeBenito 696b41
            $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
Chris PeBenito 696b41
            return false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    fwrite($fd, $params['contents']);
Chris PeBenito 696b41
    fclose($fd);
Chris PeBenito 696b41
Chris PeBenito 696b41
    // Delete the file if it allready exists (this is needed on Win,
Chris PeBenito 696b41
    // because it cannot overwrite files with rename()
Chris PeBenito 696b41
    if (file_exists($params['filename'])) {
Chris PeBenito 696b41
        @unlink($params['filename']);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    @rename($_tmp_file, $params['filename']);
Chris PeBenito 696b41
    @chmod($params['filename'], $smarty->_file_perms);
Chris PeBenito 696b41
Chris PeBenito 696b41
    return true;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>