smarty_internal_write_file.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. class Smarty_Internal_Write_File {
  13. /**
  14. * Writes file in a save way to disk
  15. *
  16. * @param string $_filepath complete filepath
  17. * @param string $_contents file content
  18. * @return boolean true
  19. */
  20. public static function writeFile($_filepath, $_contents, $smarty)
  21. {
  22. $old_umask = umask(0);
  23. $_dirpath = dirname($_filepath);
  24. // if subdirs, create dir structure
  25. if ($_dirpath !== '.' && !file_exists($_dirpath)) {
  26. mkdir($_dirpath, $smarty->_dir_perms, true);
  27. }
  28. // write to tmp file, then move to overt file lock race condition
  29. $_tmp_file = tempnam($_dirpath, 'wrt');
  30. if (!file_put_contents($_tmp_file, $_contents)) {
  31. umask($old_umask);
  32. throw new Exception("unable to write file {$_tmp_file}");
  33. return false;
  34. }
  35. // remove original file
  36. if (file_exists($_filepath))
  37. @unlink($_filepath);
  38. // rename tmp file
  39. rename($_tmp_file, $_filepath);
  40. // set file permissions
  41. chmod($_filepath, $smarty->_file_perms);
  42. umask($old_umask);
  43. return true;
  44. }
  45. }
  46. ?>