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 (!($fd = @fopen($_tmp_file, 'wb'))) {
34 $_tmp_file = $_dirpath . DS . uniqid('wrt');
35 if (!($fd = @fopen($_tmp_file, 'wb'))) {
36 throw new SmartyException("unable to write file {$_tmp_file}");
37 return false;
38 }
39 }
41 fwrite($fd, $_contents);
42 fclose($fd);
44 // remove original file
45 if (file_exists($_filepath))
46 @unlink($_filepath);
47 // rename tmp file
48 rename($_tmp_file, $_filepath);
49 // set file permissions
50 chmod($_filepath, $smarty->_file_perms);
51 umask($old_umask);
52 return true;
53 }
54 }
56 ?>