00001 <?php 00022 function smarty_function_math($params, &$smarty) 00023 { 00024 // be sure equation parameter is present 00025 if (empty($params['equation'])) { 00026 $smarty->trigger_error("math: missing equation parameter"); 00027 return; 00028 } 00029 00030 // strip out backticks, not necessary for math 00031 $equation = str_replace('`','',$params['equation']); 00032 00033 // make sure parenthesis are balanced 00034 if (substr_count($equation,"(") != substr_count($equation,")")) { 00035 $smarty->trigger_error("math: unbalanced parenthesis"); 00036 return; 00037 } 00038 00039 // match all vars in equation, make sure all are passed 00040 preg_match_all("!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!",$equation, $match); 00041 $allowed_funcs = array('int','abs','ceil','cos','exp','floor','log','log10', 00042 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan'); 00043 00044 foreach($match[1] as $curr_var) { 00045 if ($curr_var && !in_array($curr_var, array_keys($params)) && !in_array($curr_var, $allowed_funcs)) { 00046 $smarty->trigger_error("math: function call $curr_var not allowed"); 00047 return; 00048 } 00049 } 00050 00051 foreach($params as $key => $val) { 00052 if ($key != "equation" && $key != "format" && $key != "assign") { 00053 // make sure value is not empty 00054 if (strlen($val)==0) { 00055 $smarty->trigger_error("math: parameter $key is empty"); 00056 return; 00057 } 00058 if (!is_numeric($val)) { 00059 $smarty->trigger_error("math: parameter $key: is not numeric"); 00060 return; 00061 } 00062 $equation = preg_replace("/\b$key\b/", " \$params['$key'] ", $equation); 00063 } 00064 } 00065 00066 eval("\$smarty_math_result = ".$equation.";"); 00067 00068 if (empty($params['format'])) { 00069 if (empty($params['assign'])) { 00070 return $smarty_math_result; 00071 } else { 00072 $smarty->assign($params['assign'],$smarty_math_result); 00073 } 00074 } else { 00075 if (empty($params['assign'])){ 00076 printf($params['format'],$smarty_math_result); 00077 } else { 00078 $smarty->assign($params['assign'],sprintf($params['format'],$smarty_math_result)); 00079 } 00080 } 00081 } 00082 00083 /* vim: set expandtab: */ 00084 00085 ?>
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
