選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

smarty_internal_runtime_writefile.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 !== '.') {
  40. $i = 0;
  41. // loop if concurrency problem occurs
  42. // see https://bugs.php.net/bug.php?id=35326
  43. while (!is_dir($_dirpath)) {
  44. if (@mkdir($_dirpath, $_dir_perms, true)) {
  45. break;
  46. }
  47. clearstatcache();
  48. if (++$i === 3) {
  49. error_reporting($_error_reporting);
  50. throw new SmartyException("unable to create directory {$_dirpath}");
  51. }
  52. sleep(1);
  53. }
  54. }
  55. // write to tmp file, then move to overt file lock race condition
  56. $_tmp_file = $_dirpath . DIRECTORY_SEPARATOR . str_replace(array('.', ','), '_', uniqid('wrt', true));
  57. if (!file_put_contents($_tmp_file, $_contents)) {
  58. error_reporting($_error_reporting);
  59. throw new SmartyException("unable to write file {$_tmp_file}");
  60. }
  61. /*
  62. * Windows' rename() fails if the destination exists,
  63. * Linux' rename() properly handles the overwrite.
  64. * Simply unlink()ing a file might cause other processes
  65. * currently reading that file to fail, but linux' rename()
  66. * seems to be smart enough to handle that for us.
  67. */
  68. if (Smarty::$_IS_WINDOWS) {
  69. // remove original file
  70. if (is_file($_filepath)) {
  71. @unlink($_filepath);
  72. }
  73. // rename tmp file
  74. $success = @rename($_tmp_file, $_filepath);
  75. } else {
  76. // rename tmp file
  77. $success = @rename($_tmp_file, $_filepath);
  78. if (!$success) {
  79. // remove original file
  80. if (is_file($_filepath)) {
  81. @unlink($_filepath);
  82. }
  83. // rename tmp file
  84. $success = @rename($_tmp_file, $_filepath);
  85. }
  86. }
  87. if (!$success) {
  88. error_reporting($_error_reporting);
  89. throw new SmartyException("unable to write file {$_filepath}");
  90. }
  91. if ($_file_perms !== null) {
  92. // set file permissions
  93. chmod($_filepath, $_file_perms);
  94. umask($old_umask);
  95. }
  96. error_reporting($_error_reporting);
  97. return true;
  98. }
  99. }