1 <?php
3 /**
4 * Smarty write file plugin
5 *
6 * @package Smarty
7 * @subpackage PluginsInternal
8 * @author Monte Ohrt
9 */
11 /**
12 * Smarty Internal Write File Class
13 */
14 class Smarty_Internal_Write_File {
15 /**
16 * Writes file in a save way to disk
17 *
18 * @param string $_filepath complete filepath
19 * @param string $_contents file content
20 * @return boolean true
21 */
22 public static function writeFile($_filepath, $_contents, $smarty)
23 {
24 $old_umask = umask(0);
25 $_dirpath = dirname($_filepath);
26 // if subdirs, create dir structure
27 if ($_dirpath !== '.' && !file_exists($_dirpath)) {
28 mkdir($_dirpath, $smarty->_dir_perms, true);
29 }
30 // write to tmp file, then move to overt file lock race condition
31 $_tmp_file = tempnam($_dirpath, 'wrt');
33 if (!file_put_contents($_tmp_file, $_contents)) {
34 umask($old_umask);
35 throw new SmartyException("unable to write file {$_tmp_file}");
36 return false;
37 }
38 // remove original file
39 if (file_exists($_filepath))
40 @unlink($_filepath);
41 // rename tmp file
42 rename($_tmp_file, $_filepath);
43 // set file permissions
44 chmod($_filepath, $smarty->_file_perms);
45 umask($old_umask);
46 return true;
47 }
48 }
50 ?>