1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: 11: 12: 13: 14: 15:
16: class Smarty_Internal_Write_File
17: {
18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: public static function writeFile($_filepath, $_contents, Smarty $smarty)
29: {
30: $_error_reporting = error_reporting();
31: error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
32: if ($smarty->_file_perms !== null) {
33: $old_umask = umask(0);
34: }
35:
36: $_dirpath = dirname($_filepath);
37:
38: if ($_dirpath !== '.' && !file_exists($_dirpath)) {
39: mkdir($_dirpath, $smarty->_dir_perms === null ? 0777 : $smarty->_dir_perms, true);
40: }
41:
42:
43: $_tmp_file = $_dirpath . DS . str_replace(array('.', ','), '_', uniqid('wrt', true));
44: if (!file_put_contents($_tmp_file, $_contents)) {
45: error_reporting($_error_reporting);
46: throw new SmartyException("unable to write file {$_tmp_file}");
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: if (Smarty::$_IS_WINDOWS) {
57:
58: @unlink($_filepath);
59:
60: $success = @rename($_tmp_file, $_filepath);
61: } else {
62:
63: $success = @rename($_tmp_file, $_filepath);
64: if (!$success) {
65:
66: @unlink($_filepath);
67:
68: $success = @rename($_tmp_file, $_filepath);
69: }
70: }
71:
72: if (!$success) {
73: error_reporting($_error_reporting);
74: throw new SmartyException("unable to write file {$_filepath}");
75: }
76:
77: if ($smarty->_file_perms !== null) {
78:
79: chmod($_filepath, $smarty->_file_perms);
80: umask($old_umask);
81: }
82: error_reporting($_error_reporting);
83:
84: return true;
85: }
86: }
87: