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