| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty plugin | 
					
						
							|  |  |  |  * This plugin is only for Smarty2 BC | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @subpackage PluginsFunction | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty {math} function plugin | 
					
						
							|  |  |  |  * Type:     function<br> | 
					
						
							|  |  |  |  * Name:     math<br> | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * Purpose:  handle math computations in template | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @link     http://www.smarty.net/manual/en/language.function.math.php {math} | 
					
						
							|  |  |  |  *           (Smarty online manual) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @author   Monte Ohrt <monte at ohrt dot com> | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @param array                    $params   parameters | 
					
						
							|  |  |  |  * @param Smarty_Internal_Template $template template object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @return string|null | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-12 23:42:32 +00:00
										 |  |  | function smarty_function_math($params, $template) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-01 18:10:48 +00:00
										 |  |  |     static $_allowed_funcs = array( | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         'int'  => true, 'abs' => true, 'ceil' => true, 'cos' => true, 'exp' => true, 'floor' => true, | 
					
						
							|  |  |  |         'log'  => true, 'log10' => true, 'max' => true, 'min' => true, 'pi' => true, 'pow' => true, | 
					
						
							|  |  |  |         'rand' => true, 'round' => true, 'sin' => true, 'sqrt' => true, 'srand' => true, 'tan' => true | 
					
						
							| 
									
										
										
										
											2011-10-01 18:10:48 +00:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     // be sure equation parameter is present
 | 
					
						
							|  |  |  |     if (empty($params['equation'])) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         trigger_error("math: missing equation parameter", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $equation = $params['equation']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // make sure parenthesis are balanced
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     if (substr_count($equation, "(") != substr_count($equation, ")")) { | 
					
						
							|  |  |  |         trigger_error("math: unbalanced parenthesis", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // match all vars in equation, make sure all are passed
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]*)!", $equation, $match); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     foreach ($match[1] as $curr_var) { | 
					
						
							| 
									
										
										
										
											2011-10-01 19:35:54 +00:00
										 |  |  |         if ($curr_var && !isset($params[$curr_var]) && !isset($_allowed_funcs[$curr_var])) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             trigger_error("math: function call $curr_var not allowed", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     foreach ($params as $key => $val) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         if ($key != "equation" && $key != "format" && $key != "assign") { | 
					
						
							|  |  |  |             // make sure value is not empty
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             if (strlen($val) == 0) { | 
					
						
							|  |  |  |                 trigger_error("math: parameter $key is empty", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!is_numeric($val)) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 trigger_error("math: parameter $key: is not numeric", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-23 14:40:10 +00:00
										 |  |  |     $smarty_math_result = null; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     eval("\$smarty_math_result = " . $equation . ";"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (empty($params['format'])) { | 
					
						
							|  |  |  |         if (empty($params['assign'])) { | 
					
						
							|  |  |  |             return $smarty_math_result; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             $template->assign($params['assign'], $smarty_math_result); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |         if (empty($params['assign'])) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             printf($params['format'], $smarty_math_result); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             $template->assign($params['assign'], sprintf($params['format'], $smarty_math_result)); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |