1 : <?php
2 : /**
3 : * Smarty write file plugin
4 : *
5 : * @package Smarty
6 : * @subpackage PluginsInternal
7 : * @author Monte Ohrt
8 : */
9 :
10 : /**
11 : * Smarty Internal Write File Class
12 : *
13 : * @package Smarty
14 : * @subpackage PluginsInternal
15 : */
16 : class Smarty_Internal_Write_File {
17 :
18 : /**
19 : * Writes file in a safe way to disk
20 : *
21 : * @param string $_filepath complete filepath
22 : * @param string $_contents file content
23 : * @param Smarty $smarty smarty instance
24 : * @return boolean true
25 : */
26 : public static function writeFile($_filepath, $_contents, Smarty $smarty)
27 : {
28 0 : $_error_reporting = error_reporting();
29 0 : error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
30 0 : if ($smarty->_file_perms !== null) {
31 0 : $old_umask = umask(0);
32 0 : }
33 :
34 0 : $_dirpath = dirname($_filepath);
35 : // if subdirs, create dir structure
36 0 : if ($_dirpath !== '.' && !file_exists($_dirpath)) {
37 0 : mkdir($_dirpath, $smarty->_dir_perms === null ? 0777 : $smarty->_dir_perms, true);
38 0 : }
39 :
40 : // write to tmp file, then move to overt file lock race condition
41 0 : $_tmp_file = $_dirpath . DS . uniqid('wrt');
42 0 : if (!file_put_contents($_tmp_file, $_contents)) {
43 0 : error_reporting($_error_reporting);
44 0 : throw new SmartyException("unable to write file {$_tmp_file}");
45 : return false;
46 : }
47 :
48 : // remove original file
49 0 : unlink($_filepath);
50 :
51 : // rename tmp file
52 0 : $success = rename($_tmp_file, $_filepath);
53 0 : if (!$success) {
54 0 : error_reporting($_error_reporting);
55 0 : throw new SmartyException("unable to write file {$_filepath}");
56 : return false;
57 : }
58 :
59 0 : if ($smarty->_file_perms !== null) {
60 : // set file permissions
61 0 : chmod($_filepath, $smarty->_file_perms);
62 0 : umask($old_umask);
63 0 : }
64 0 : error_reporting($_error_reporting);
65 0 : return true;
66 : }
67 :
68 : }
69 :
|