You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_writefile.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 = property_exists($smarty, '_dir_perms') ? (isset($smarty->_dir_perms) ? $smarty->_dir_perms : 0777) : 0771;
  33. if ($_file_perms !== null) {
  34. $old_umask = umask(0);
  35. }
  36. $_dirpath = dirname($_filepath);
  37. // if subdirs, create dir structure
  38. if ($_dirpath !== '.' && !file_exists($_dirpath)) {
  39. mkdir($_dirpath, $_dir_perms, true);
  40. }
  41. // write to tmp file, then move to overt file lock race condition
  42. $_tmp_file = $_dirpath . DS . str_replace(array('.', ','), '_', uniqid('wrt', true));
  43. if (!file_put_contents($_tmp_file, $_contents)) {
  44. error_reporting($_error_reporting);
  45. throw new SmartyException("unable to write file {$_tmp_file}");
  46. }
  47. /*
  48. * Windows' rename() fails if the destination exists,
  49. * Linux' rename() properly handles the overwrite.
  50. * Simply unlink()ing a file might cause other processes
  51. * currently reading that file to fail, but linux' rename()
  52. * seems to be smart enough to handle that for us.
  53. */
  54. if (Smarty::$_IS_WINDOWS) {
  55. // remove original file
  56. if (is_file($_filepath)) {
  57. @unlink($_filepath);
  58. }
  59. // rename tmp file
  60. $success = @rename($_tmp_file, $_filepath);
  61. } else {
  62. // rename tmp file
  63. $success = @rename($_tmp_file, $_filepath);
  64. if (!$success) {
  65. // remove original file
  66. if (is_file($_filepath)) {
  67. @unlink($_filepath);
  68. }
  69. // rename tmp file
  70. $success = @rename($_tmp_file, $_filepath);
  71. }
  72. }
  73. if (!$success) {
  74. error_reporting($_error_reporting);
  75. throw new SmartyException("unable to write file {$_filepath}");
  76. }
  77. if ($_file_perms !== null) {
  78. // set file permissions
  79. chmod($_filepath, $_file_perms);
  80. umask($old_umask);
  81. }
  82. error_reporting($_error_reporting);
  83. return true;
  84. }
  85. }