2009-03-22 16:09:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty write file plugin
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage PluginsInternal
|
|
|
|
* @author Monte Ohrt
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Smarty Internal Write File Class
|
|
|
|
*/
|
2009-08-08 17:28:23 +00:00
|
|
|
class Smarty_Internal_Write_File {
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
|
|
|
* Writes file in a save way to disk
|
|
|
|
*
|
|
|
|
* @param string $_filepath complete filepath
|
|
|
|
* @param string $_contents file content
|
|
|
|
* @return boolean true
|
|
|
|
*/
|
2009-11-17 17:46:03 +00:00
|
|
|
public static function writeFile($_filepath, $_contents, $smarty)
|
2009-03-22 16:09:05 +00:00
|
|
|
{
|
2009-11-18 17:25:18 +00:00
|
|
|
$old_umask = umask(0);
|
2009-03-22 16:09:05 +00:00
|
|
|
$_dirpath = dirname($_filepath);
|
|
|
|
// if subdirs, create dir structure
|
|
|
|
if ($_dirpath !== '.' && !file_exists($_dirpath)) {
|
2009-11-17 17:46:03 +00:00
|
|
|
mkdir($_dirpath, $smarty->_dir_perms, true);
|
2009-03-22 16:09:05 +00:00
|
|
|
}
|
|
|
|
// write to tmp file, then move to overt file lock race condition
|
|
|
|
$_tmp_file = tempnam($_dirpath, 'wrt');
|
|
|
|
|
|
|
|
if (!file_put_contents($_tmp_file, $_contents)) {
|
2009-11-18 17:25:18 +00:00
|
|
|
umask($old_umask);
|
2010-08-13 10:39:51 +00:00
|
|
|
throw new SmartyException("unable to write file {$_tmp_file}");
|
2009-03-22 16:09:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// remove original file
|
|
|
|
if (file_exists($_filepath))
|
2009-11-23 22:31:22 +00:00
|
|
|
@unlink($_filepath);
|
2009-03-22 16:09:05 +00:00
|
|
|
// rename tmp file
|
|
|
|
rename($_tmp_file, $_filepath);
|
|
|
|
// set file permissions
|
2009-11-17 17:46:03 +00:00
|
|
|
chmod($_filepath, $smarty->_file_perms);
|
2009-11-18 17:25:18 +00:00
|
|
|
umask($old_umask);
|
2009-03-22 16:09:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 10:39:51 +00:00
|
|
|
?>
|