| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty write file plugin | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsInternal | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |  * @author Monte Ohrt | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty Internal Write File Class | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsInternal | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-08-08 17:28:23 +00:00
										 |  |  | class Smarty_Internal_Write_File { | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |      * Writes file in a safe way to disk | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * @param string $_filepath complete filepath | 
					
						
							|  |  |  |      * @param string $_contents file content | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |      * @param Smarty $smarty    smarty instance | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * @return boolean true | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |     public static function writeFile($_filepath, $_contents, Smarty $smarty) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |         $_error_reporting = error_reporting(); | 
					
						
							|  |  |  |         error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING); | 
					
						
							|  |  |  |         if ($smarty->_file_perms !== null) { | 
					
						
							|  |  |  |             $old_umask = umask(0); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $_dirpath = dirname($_filepath); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // if subdirs, create dir structure
 | 
					
						
							|  |  |  |         if ($_dirpath !== '.' && !file_exists($_dirpath)) { | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |             mkdir($_dirpath, $smarty->_dir_perms === null ? 0777 : $smarty->_dir_perms, true); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-09-21 19:39:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |         // write to tmp file, then move to overt file lock race condition
 | 
					
						
							|  |  |  |         $_tmp_file = $_dirpath . DS . uniqid('wrt'); | 
					
						
							|  |  |  |         if (!file_put_contents($_tmp_file, $_contents)) { | 
					
						
							|  |  |  |             error_reporting($_error_reporting); | 
					
						
							| 
									
										
										
										
											2010-08-13 10:39:51 +00:00
										 |  |  |             throw new SmartyException("unable to write file {$_tmp_file}"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-21 19:39:30 +00:00
										 |  |  |         // remove original file
 | 
					
						
							| 
									
										
										
										
											2011-10-20 10:55:11 +00:00
										 |  |  |         @unlink($_filepath); | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // rename tmp file
 | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |         $success = rename($_tmp_file, $_filepath); | 
					
						
							|  |  |  |         if (!$success) { | 
					
						
							|  |  |  |             error_reporting($_error_reporting); | 
					
						
							|  |  |  |             throw new SmartyException("unable to write file {$_filepath}"); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($smarty->_file_perms !== null) { | 
					
						
							|  |  |  |             // set file permissions
 | 
					
						
							|  |  |  |             chmod($_filepath, $smarty->_file_perms); | 
					
						
							|  |  |  |             umask($old_umask); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         error_reporting($_error_reporting); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2011-09-21 22:02:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-13 10:39:51 +00:00
										 |  |  | ?>
 |