smarty_internal_runtime_writefile.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Smarty write file plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Monte Ohrt
  8. */
  9. /**
  10. * Smarty Internal Write File Class
  11. *
  12. * @package Smarty
  13. * @subpackage PluginsInternal
  14. */
  15. class Smarty_Internal_Runtime_WriteFile
  16. {
  17. /**
  18. * Writes file in a safe way to disk
  19. *
  20. * @param string $_filepath complete filepath
  21. * @param string $_contents file content
  22. * @param Smarty $smarty smarty instance
  23. *
  24. * @throws SmartyException
  25. * @return boolean true
  26. */
  27. public function writeFile($_filepath, $_contents, Smarty $smarty)
  28. {
  29. $_error_reporting = error_reporting();
  30. error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
  31. $_file_perms = property_exists($smarty, '_file_perms') ? $smarty->_file_perms : 0644;
  32. $_dir_perms =
  33. property_exists($smarty, '_dir_perms') ? (isset($smarty->_dir_perms) ? $smarty->_dir_perms : 0777) : 0771;
  34. if ($_file_perms !== null) {
  35. $old_umask = umask(0);
  36. }
  37. $_dirpath = dirname($_filepath);
  38. // if subdirs, create dir structure
  39. if ($_dirpath !== '.' && !file_exists($_dirpath)) {
  40. mkdir($_dirpath, $_dir_perms, true);
  41. }
  42. // write to tmp file, then move to overt file lock race condition
  43. $_tmp_file = $_dirpath . $smarty->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. * Windows' rename() fails if the destination exists,
  50. * Linux' rename() properly handles the overwrite.
  51. * Simply unlink()ing a file might cause other processes
  52. * currently reading that file to fail, but linux' rename()
  53. * seems to be smart enough to handle that for us.
  54. */
  55. if (Smarty::$_IS_WINDOWS) {
  56. // remove original file
  57. if (is_file($_filepath)) {
  58. @unlink($_filepath);
  59. }
  60. // rename tmp file
  61. $success = @rename($_tmp_file, $_filepath);
  62. } else {
  63. // rename tmp file
  64. $success = @rename($_tmp_file, $_filepath);
  65. if (!$success) {
  66. // remove original file
  67. if (is_file($_filepath)) {
  68. @unlink($_filepath);
  69. }
  70. // rename tmp file
  71. $success = @rename($_tmp_file, $_filepath);
  72. }
  73. }
  74. if (!$success) {
  75. error_reporting($_error_reporting);
  76. throw new SmartyException("unable to write file {$_filepath}");
  77. }
  78. if ($_file_perms !== null) {
  79. // set file permissions
  80. chmod($_filepath, $_file_perms);
  81. umask($old_umask);
  82. }
  83. error_reporting($_error_reporting);
  84. return true;
  85. }
  86. }