• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Smarty/libs/plugins/function.math.php

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

Generated on Thu Apr 19 2012 17:01:16 for openbiz by  doxygen 1.7.2