Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F881532
function.math.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
function.math.php
View Options
<?php
/**
* Smarty plugin
*
* This plugin is only for Smarty2 BC
* @package Smarty
* @subpackage PluginsFunction
*/
/**
* Smarty {math} function plugin
*
* Type: function<br>
* Name: math<br>
* Purpose: handle math computations in template<br>
* @link http://smarty.php.net/manual/en/language.function.math.php {math}
* (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com>
* @param array $params parameters
* @param object $smarty Smarty object
* @param object $template template object
* @return string|null
*/
function
smarty_function_math
(
$params
,
$smarty
,
$template
)
{
// be sure equation parameter is present
if
(
empty
(
$params
[
'equation'
]))
{
throw
new
Exception
(
"math: missing equation parameter"
);
return
;
}
$equation
=
$params
[
'equation'
];
// make sure parenthesis are balanced
if
(
substr_count
(
$equation
,
"("
)
!=
substr_count
(
$equation
,
")"
))
{
throw
new
Exception
(
"math: unbalanced parenthesis"
);
return
;
}
// match all vars in equation, make sure all are passed
preg_match_all
(
"!(?:0x[a-fA-F0-9]+)|([a-zA-Z][a-zA-Z0-9_]+)!"
,
$equation
,
$match
);
$allowed_funcs
=
array
(
'int'
,
'abs'
,
'ceil'
,
'cos'
,
'exp'
,
'floor'
,
'log'
,
'log10'
,
'max'
,
'min'
,
'pi'
,
'pow'
,
'rand'
,
'round'
,
'sin'
,
'sqrt'
,
'srand'
,
'tan'
);
foreach
(
$match
[
1
]
as
$curr_var
)
{
if
(
$curr_var
&&
!
in_array
(
$curr_var
,
array_keys
(
$params
))
&&
!
in_array
(
$curr_var
,
$allowed_funcs
))
{
throw
new
Exception
(
"math: function call $curr_var not allowed"
);
return
;
}
}
foreach
(
$params
as
$key
=>
$val
)
{
if
(
$key
!=
"equation"
&&
$key
!=
"format"
&&
$key
!=
"assign"
)
{
// make sure value is not empty
if
(
strlen
(
$val
)==
0
)
{
throw
new
Exception
(
"math: parameter $key is empty"
);
return
;
}
if
(!
is_numeric
(
$val
))
{
throw
new
Exception
(
"math: parameter $key: is not numeric"
);
return
;
}
$equation
=
preg_replace
(
"/
\b
$key
\b
/"
,
"
\$
params['$key'] "
,
$equation
);
}
}
eval
(
"
\$
smarty_math_result = "
.
$equation
.
";"
);
if
(
empty
(
$params
[
'format'
]))
{
if
(
empty
(
$params
[
'assign'
]))
{
return
$smarty_math_result
;
}
else
{
$template
->
assign
(
$params
[
'assign'
],
$smarty_math_result
);
}
}
else
{
if
(
empty
(
$params
[
'assign'
])){
printf
(
$params
[
'format'
],
$smarty_math_result
);
}
else
{
$template
->
assign
(
$params
[
'assign'
],
sprintf
(
$params
[
'format'
],
$smarty_math_result
));
}
}
}
?>
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Apr 2, 06:21 (4 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
25817
Default Alt Text
function.math.php (2 KB)
Attached To
rZED Zed
Event Timeline
Log In to Comment