| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty plugin | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage Security | 
					
						
							|  |  |  |  * @author Uwe Tews  | 
					
						
							|  |  |  |  */  | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * This class does contain the security settings | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | class Smarty_Security { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This determines how Smarty handles "<?php ... ?>" tags in templates. | 
					
						
							|  |  |  |      * possible values: | 
					
						
							|  |  |  |      * <ul> | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      *   <li>Smarty::PHP_PASSTHRU -> echo PHP tags as they are</li> | 
					
						
							|  |  |  |      *   <li>Smarty::PHP_QUOTE    -> escape tags as entities</li> | 
					
						
							|  |  |  |      *   <li>Smarty::PHP_REMOVE   -> remove php tags</li> | 
					
						
							|  |  |  |      *   <li>Smarty::PHP_ALLOW    -> execute php tags</li> | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * </ul> | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @var integer  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     public $php_handling = Smarty::PHP_PASSTHRU; | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This is the list of template directories that are considered secure. | 
					
						
							|  |  |  |      * $template_dir is in this list implicitly. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $secure_dir = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This is an array of directories where trusted php scripts reside. | 
					
						
							|  |  |  |      * {@link $security} is disabled during their inclusion/execution. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $trusted_dir = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 18:01:03 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This is an array of trusted static classes. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If empty access to all static classes is allowed. | 
					
						
							|  |  |  |      * If set to 'none' none is allowed. | 
					
						
							|  |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-02-24 18:01:03 +00:00
										 |  |  |     public $static_classes = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This is an array of trusted PHP functions. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If empty all functions are allowed. | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * To disable all PHP functions set $php_functions = null. | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $php_functions = array('isset', 'empty', | 
					
						
							|  |  |  |             'count', 'sizeof','in_array', 'is_array','time','nl2br'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * This is an array of trusted PHP modifers. | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * If empty all modifiers are allowed. | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * To disable all modifier set $modifiers = null. | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |     public $php_modifiers = array('escape','count'); | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * This is an array of trusted streams. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If empty all streams are allowed. | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * To disable all streams set $streams = null. | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * @var array  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $streams = array('file'); | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * + flag if constants can be accessed from template | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $allow_constants = true; | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      * + flag if super globals can be accessed from template | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-12-04 15:44:47 +00:00
										 |  |  |     public $allow_super_globals = true; | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  |      * + flag if the {php} and {include_php} tag can be executed | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  |     public $allow_php_tag = false; | 
					
						
							| 
									
										
										
										
											2010-11-11 21:34:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct($smarty) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->smarty = $smarty;  | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if PHP function is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $function_name  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if function is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedPhpFunction($function_name, $compiler) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $compiler->trigger_template_error ("PHP function '{$function_name}' not allowed by security setting"); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if static class is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $class_name  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if class is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedStaticClass($class_name, $compiler) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $compiler->trigger_template_error ("access to static class '{$class_name}' not allowed by security setting"); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if modifier is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $modifier_name  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if modifier is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedModifier($modifier_name, $compiler) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $compiler->trigger_template_error ("modifier '{$modifier_name}' not allowed by security setting"); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if stream is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $stream_name  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if stream is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedStream($stream_name) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new SmartyException ("stream '{$stream_name}' not allowed by security setting"); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if directory of file resource is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $filepath  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if directory is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedResourceDir($filepath) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $_rp = realpath($filepath); | 
					
						
							|  |  |  |         if (isset($this->smarty->template_dir)) { | 
					
						
							|  |  |  |             foreach ((array)$this->smarty->template_dir as $curr_dir) { | 
					
						
							|  |  |  |                 if (($_cd = realpath($curr_dir)) !== false && | 
					
						
							|  |  |  |                         strncmp($_rp, $_cd, strlen($_cd)) == 0 && | 
					
						
							|  |  |  |                         (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |             }  | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |         if (!empty($this->smarty->security_policy->secure_dir)) { | 
					
						
							|  |  |  |             foreach ((array)$this->smarty->security_policy->secure_dir as $curr_dir) { | 
					
						
							|  |  |  |                 if (($_cd = realpath($curr_dir)) !== false) { | 
					
						
							|  |  |  |                     if ($_cd == $_rp) { | 
					
						
							|  |  |  |                         return true; | 
					
						
							|  |  |  |                     } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 && | 
					
						
							|  |  |  |                             (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) { | 
					
						
							|  |  |  |                         return true; | 
					
						
							|  |  |  |                     }  | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |             }  | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         throw new SmartyException ("directory '{$_rp}' not allowed by security setting"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if directory of file resource is trusted. | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * @param string $filepath  | 
					
						
							|  |  |  |      * @param object $compiler compiler object | 
					
						
							|  |  |  |      * @return boolean true if directory is trusted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function isTrustedPHPDir($filepath) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $_rp = realpath($filepath); | 
					
						
							|  |  |  |         if (!empty($this->trusted_dir)) { | 
					
						
							|  |  |  |             foreach ((array)$this->trusted_dir as $curr_dir) { | 
					
						
							|  |  |  |                 if (($_cd = realpath($curr_dir)) !== false) { | 
					
						
							|  |  |  |                     if ($_cd == $_rp) { | 
					
						
							|  |  |  |                         return true; | 
					
						
							|  |  |  |                     } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 && | 
					
						
							|  |  |  |                             substr($_rp, strlen($_cd), 1) == DS) { | 
					
						
							|  |  |  |                         return true; | 
					
						
							|  |  |  |                     }  | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |             }  | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         throw new SmartyException ("directory '{$_rp}' not allowed by security setting"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }  | 
					
						
							| 
									
										
										
										
											2009-11-06 14:35:00 +00:00
										 |  |  | }  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 18:01:03 +00:00
										 |  |  | ?>
 |