2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2007-04-03 14:10:23 +00:00
< ? php
2014-12-15 11:10:26 +00:00
/* Copyright ( C ) 2014 Daniel Preussker < f0o @ devilcode . org >
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />. */
2007-04-03 14:10:23 +00:00
2012-05-09 10:01:42 +00:00
/**
2014-12-15 11:10:26 +00:00
* Alerts Cronjob
* @ author f0o < f0o @ devilcode . org >
* @ copyright 2014 f0o , LibreNMS
* @ license GPL
* @ package LibreNMS
* @ subpackage Alerts
2012-05-09 10:01:42 +00:00
*/
2015-01-25 21:39:33 +00:00
include_once ( " includes/defaults.inc.php " );
include_once ( " config.php " );
2014-12-22 18:17:43 +00:00
$lock = false ;
if ( file_exists ( $config [ 'install_dir' ] . " /.alerts.lock " ) ) {
$pids = explode ( " \n " , trim ( `ps -e | grep php | awk '{print $1}'` ));
$lpid = trim ( file_get_contents ( $config [ 'install_dir' ] . " /.alerts.lock " ));
if ( in_array ( $lpid , $pids ) ) {
$lock = true ;
}
}
if ( $lock === true ) {
2015-01-25 21:39:33 +00:00
exit ( 1 );
2014-12-22 18:17:43 +00:00
} else {
file_put_contents ( $config [ 'install_dir' ] . " /.alerts.lock " , getmypid ());
}
2015-01-25 21:39:33 +00:00
include_once ( $config [ 'install_dir' ] . " /includes/definitions.inc.php " );
include_once ( $config [ 'install_dir' ] . " /includes/functions.php " );
include_once ( $config [ 'install_dir' ] . " /includes/alerts.inc.php " );
2007-04-03 14:10:23 +00:00
2015-01-25 21:39:33 +00:00
if ( ! defined ( " TEST " ) ) {
echo " Start: " . date ( 'r' ) . " \r \n " ;
echo " RunFollowUp(): \r \n " ;
RunFollowUp ();
echo " RunAlerts(): \r \n " ;
RunAlerts ();
2015-05-14 14:37:59 +00:00
echo " RunAcks(): \r \n " ;
RunAcks ();
2015-01-25 21:39:33 +00:00
echo " End : " . date ( 'r' ) . " \r \n " ;
}
2007-04-03 14:10:23 +00:00
2014-12-22 18:17:43 +00:00
unlink ( $config [ 'install_dir' ] . " /.alerts.lock " );
2015-04-04 17:13:22 +00:00
/**
* Re - Validate Rule - Mappings
* @ param int $device Device - ID
* @ param int $rule Rule - ID
* @ return boolean
*/
function IsRuleValid ( $device , $rule ) {
global $rulescache ;
if ( empty ( $rulescache [ $device ]) || ! isset ( $rulescache [ $device ]) ) {
foreach ( GetRules ( $device ) as $chk ) {
$rulescache [ $device ][ $chk [ 'id' ]] = true ;
}
}
if ( $rulescache [ $device ][ $rule ] === true ) {
return true ;
}
return false ;
}
2015-05-14 14:37:59 +00:00
/**
* Issue Alert - Object
* @ param array $alert
* @ return boolean
*/
function IssueAlert ( $alert ) {
global $config ;
2015-05-18 10:23:25 +00:00
if ( dbFetchCell ( 'SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?' , array ( $alert [ 'device_id' ])) == " 1 " ) {
return true ;
}
2015-05-14 14:37:59 +00:00
$default_tpl = " %title \r \n Severity: %severity \r \n { if %state == 0}Time elapsed: %elapsed \r \n { /if}Timestamp: %timestamp \r \n Unique-ID: %uid \r \n Rule: { if %name}%name { else}%rule { /if} \r \n { if %faults}Faults: \r \n { foreach %faults} #%key: %value.string \r \n { /foreach} { /if}Alert sent to: { foreach %contacts}%value <%key> { /foreach} " ; //FIXME: Put somewhere else?
2015-05-22 10:26:19 +00:00
if ( $config [ 'alert' ][ 'fixed-contacts' ] == false ) {
$alert [ 'details' ][ 'contacts' ] = GetContacts ( $alert [ 'details' ][ 'rule' ]);
}
2015-05-14 14:37:59 +00:00
$obj = DescribeAlert ( $alert );
if ( is_array ( $obj ) ) {
2015-06-15 10:13:19 +00:00
$tpl = dbFetchRow ( " SELECT `template` FROM `alert_templates` JOIN `alert_template_map` ON `alert_template_map`.`alert_templates_id`=`alert_templates`.`id` WHERE `alert_template_map`.`alert_rule_id`=? " , array ( $alert [ 'rule_id' ]));
2015-05-14 14:37:59 +00:00
if ( isset ( $tpl [ 'template' ]) ) {
$tpl = $tpl [ 'template' ];
} else {
$tpl = $default_tpl ;
}
echo " Issuing Alert-UID # " . $alert [ 'id' ] . " / " . $alert [ 'state' ] . " : " ;
$msg = FormatAlertTpl ( $tpl , $obj );
$obj [ 'msg' ] = $msg ;
if ( ! empty ( $config [ 'alert' ][ 'transports' ]) ) {
ExtTransports ( $obj );
}
echo " \r \n " ;
}
2015-05-18 10:23:25 +00:00
return true ;
2015-05-14 14:37:59 +00:00
}
/**
* Issue ACK notification
* @ return void
*/
function RunAcks () {
foreach ( dbFetchRows ( " SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state = 2 && alerts.open = 1 " ) as $alert ) {
$tmp = array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]);
$alert = dbFetchRow ( " SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1 " , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ]) ) {
// Alert-Rule does not exist anymore, let's remove the alert-state.
echo " Stale-Rule: # " . $tmp [ 0 ] . " / " . $tmp [ 1 ] . " \r \n " ;
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
$alert [ 'state' ] = 2 ;
IssueAlert ( $alert );
dbUpdate ( array ( 'open' => 0 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
}
2015-01-25 21:39:33 +00:00
/**
* Run Follow - Up alerts
* @ return void
*/
function RunFollowUp () {
global $config ;
2015-05-07 19:55:52 +00:00
foreach ( dbFetchRows ( " SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state != 2 && alerts.state > 0 && alerts.open = 0 " ) as $alert ) {
2015-02-17 20:52:50 +00:00
$tmp = array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]);
2015-04-04 17:13:22 +00:00
$alert = dbFetchRow ( " SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1 " , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ]) ) {
2015-02-17 20:52:50 +00:00
// Alert-Rule does not exist anymore, let's remove the alert-state.
2015-04-04 17:13:22 +00:00
echo " Stale-Rule: # " . $tmp [ 0 ] . " / " . $tmp [ 1 ] . " \r \n " ;
2015-02-17 20:52:50 +00:00
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
2015-01-25 21:39:33 +00:00
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
$rextra = json_decode ( $alert [ 'extra' ], true );
if ( $rextra [ 'invert' ] ) {
continue ;
}
$chk = dbFetchRows ( GenSQL ( $alert [ 'rule' ]), array ( $alert [ 'device_id' ]));
$o = sizeof ( $alert [ 'details' ][ 'rule' ]);
$n = sizeof ( $chk );
$ret = " Alert # " . $alert [ 'id' ];
$state = 0 ;
if ( $n > $o ) {
$ret .= " Worsens " ;
$state = 3 ;
} elseif ( $n < $o ) {
$ret .= " Betters " ;
$state = 4 ;
}
if ( $state > 0 ) {
$alert [ 'details' ][ 'rule' ] = $chk ;
if ( dbInsert ( array ( 'state' => $state , 'device_id' => $alert [ 'device_id' ], 'rule_id' => $alert [ 'rule_id' ], 'details' => gzcompress ( json_encode ( $alert [ 'details' ]), 9 )), 'alert_log' ) ) {
dbUpdate ( array ( 'state' => $state , 'open' => 1 , 'alerted' => 1 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
echo $ret . " ( " . $o . " / " . $n . " ) \r \n " ;
}
}
}
2014-12-15 11:10:26 +00:00
/**
* Run all alerts
* @ return void
*/
function RunAlerts () {
global $config ;
2015-05-07 19:55:52 +00:00
foreach ( dbFetchRows ( " SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state != 2 && alerts.open = 1 " ) as $alert ) {
2015-02-17 20:52:50 +00:00
$tmp = array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]);
2015-04-04 17:13:22 +00:00
$alert = dbFetchRow ( " SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? && alert_rules.disabled = 0 ORDER BY alert_log.id DESC LIMIT 1 " , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
if ( empty ( $alert [ 'rule_id' ]) || ! IsRuleValid ( $tmp [ 1 ], $tmp [ 0 ]) ) {
echo " Stale-Rule: # " . $tmp [ 0 ] . " / " . $tmp [ 1 ] . " \r \n " ;
2015-02-17 20:52:50 +00:00
// Alert-Rule does not exist anymore, let's remove the alert-state.
dbDelete ( 'alerts' , 'rule_id = ? && device_id = ?' , array ( $tmp [ 0 ], $tmp [ 1 ]));
continue ;
}
2014-12-15 11:10:26 +00:00
$alert [ 'details' ] = json_decode ( gzuncompress ( $alert [ 'details' ]), true );
$noiss = false ;
$noacc = false ;
$updet = false ;
$rextra = json_decode ( $alert [ 'extra' ], true );
2015-01-31 11:05:45 +00:00
$chk = dbFetchRow ( 'SELECT alerts.alerted,devices.ignore,devices.disabled FROM alerts,devices WHERE alerts.device_id = ? && devices.device_id = alerts.device_id && alerts.rule_id = ?' , array ( $alert [ 'device_id' ], $alert [ 'rule_id' ]));
2014-12-15 11:10:26 +00:00
if ( $chk [ 'alerted' ] == $alert [ 'state' ] ) {
$noiss = true ;
}
if ( ! empty ( $rextra [ 'delay' ]) ) {
2015-05-17 12:13:52 +00:00
if ( ( time () - strtotime ( $alert [ 'time_logged' ]) + $config [ 'alert' ][ 'tolerance-window' ]) < $rextra [ 'delay' ] || ( ! empty ( $alert [ 'details' ][ 'delay' ]) && ( time () - $alert [ 'details' ][ 'delay' ] + $config [ 'alert' ][ 'tolerance-window' ]) < $rextra [ 'delay' ]) ) {
2014-12-15 11:10:26 +00:00
continue ;
} else {
$alert [ 'details' ][ 'delay' ] = time ();
$updet = true ;
}
}
if ( $alert [ 'state' ] == 1 && ! empty ( $rextra [ 'count' ]) && ( $rextra [ 'count' ] == - 1 || $alert [ 'details' ][ 'count' ] ++ < $rextra [ 'count' ]) ) {
if ( $alert [ 'details' ][ 'count' ] < $rextra [ 'count' ] ) {
$noacc = true ;
}
$updet = true ;
$noiss = false ;
}
2015-01-31 11:05:45 +00:00
if ( $chk [ 'ignore' ] == 1 || $chk [ 'disabled' ] == 1 ) {
$noiss = true ;
$updet = false ;
$noacc = false ;
}
2015-04-20 18:18:22 +00:00
if ( IsMaintenance ( $alert [ 'device_id' ]) > 0 ) {
2015-04-20 16:39:55 +00:00
$noiss = true ;
2015-04-20 18:18:22 +00:00
$noacc = true ;
2015-04-20 16:39:55 +00:00
}
2014-12-15 11:10:26 +00:00
if ( $updet ) {
dbUpdate ( array ( 'details' => gzcompress ( json_encode ( $alert [ 'details' ]), 9 )), 'alert_log' , 'id = ?' , array ( $alert [ 'id' ]));
}
2015-01-31 10:57:43 +00:00
if ( ! empty ( $rextra [ 'mute' ]) ) {
2014-12-15 11:10:26 +00:00
echo " Muted Alert-UID # " . $alert [ 'id' ] . " \r \n " ;
$noiss = true ;
}
if ( ! $noiss ) {
2015-05-14 14:37:59 +00:00
IssueAlert ( $alert );
dbUpdate ( array ( 'alerted' => $alert [ 'state' ]), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
2014-12-15 11:10:26 +00:00
}
if ( ! $noacc ) {
dbUpdate ( array ( 'open' => 0 ), 'alerts' , 'rule_id = ? && device_id = ?' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
}
}
}
2011-05-13 11:42:26 +00:00
2014-12-15 11:10:26 +00:00
/**
* Run external transports
* @ param array $obj Alert - Array
* @ return void
*/
function ExtTransports ( $obj ) {
global $config ;
2014-12-15 14:33:31 +00:00
$tmp = false ; //To keep scrutinizer from naging because it doesnt understand eval
2014-12-15 11:10:26 +00:00
foreach ( $config [ 'alert' ][ 'transports' ] as $transport => $opts ) {
2015-05-25 20:51:27 +00:00
if ( ( $opts === true || ! empty ( $opts )) && $opts != false && file_exists ( $config [ 'install_dir' ] . " /includes/alerts/transport. " . $transport . " .php " ) ) {
2014-12-15 11:10:26 +00:00
echo $transport . " => " ;
eval ( '$tmp = function($obj,$opts) { global $config; ' . file_get_contents ( $config [ 'install_dir' ] . " /includes/alerts/transport. " . $transport . " .php " ) . ' };' );
$tmp = $tmp ( $obj , $opts );
2015-06-15 18:13:47 +00:00
if ( $tmp ) {
echo " OK " ;
log_event ( " Issued " . $obj [ 'severity' ] . " alert for rule ' " . $obj [ 'name' ] . " ' to transport ' " . $transport . " ' " , $obj [ 'device_id' ]);
} else {
echo " ERROR " ;
log_event ( " Could not issue " . $obj [ 'severity' ] . " alert for rule ' " . $obj [ 'name' ] . " ' to transport ' " . $transport . " ' " , $obj [ 'device_id' ]);
}
2014-12-15 11:10:26 +00:00
}
2015-06-15 18:13:47 +00:00
echo " ; " ;
2014-12-15 11:10:26 +00:00
}
2007-04-03 14:10:23 +00:00
}
2014-12-15 11:10:26 +00:00
/**
* Format Alert
* @ param string $tpl Template
* @ param array $obj Alert - Array
* @ return string
*/
function FormatAlertTpl ( $tpl , $obj ) {
2015-01-25 21:39:33 +00:00
$msg = '$ret .= "' . str_replace ( array ( " { else} " , " { /if} " , " { /foreach} " ), array ( '"; } else { $ret .= "' , '"; } $ret .= "' , '"; } $ret .= "' ), addslashes ( $tpl )) . '";' ;
$parsed = $msg ;
$s = strlen ( $msg );
$x = $pos = - 1 ;
$buff = " " ;
$if = $for = false ;
while ( ++ $x < $s ) {
if ( $msg [ $x ] == " { " && $buff == " " ) {
$buff .= $msg [ $x ];
} elseif ( $buff == " { " ) {
$buff = " " ;
} elseif ( $buff != " " ) {
$buff .= $msg [ $x ];
}
if ( $buff == " { if " ) {
$pos = $x ;
$if = true ;
} elseif ( $buff == " { foreach " ) {
$pos = $x ;
$for = true ;
}
if ( $pos != - 1 && $msg [ $x ] == " } " ) {
$orig = $buff ;
$buff = " " ;
$pos = - 1 ;
if ( $if ) {
$if = false ;
$o = 3 ;
$native = array ( '"; if( ' , ' ) { $ret .= "' );
} elseif ( $for ) {
$for = false ;
$o = 8 ;
$native = array ( '"; foreach( ' , ' as $key=>$value) { $ret .= "' );
} else {
continue ;
}
2015-01-26 08:11:28 +00:00
$cond = trim ( populate ( substr ( $orig , $o , - 1 ), false ));
2015-01-25 21:39:33 +00:00
$native = $native [ 0 ] . $cond . $native [ 1 ];
$parsed = str_replace ( $orig , $native , $parsed );
unset ( $cond , $o , $orig , $native );
}
2014-12-15 11:10:26 +00:00
}
2015-01-25 21:39:33 +00:00
$parsed = populate ( $parsed );
return RunJail ( $parsed , $obj );
2014-12-15 11:10:26 +00:00
}
/**
* Describe Alert
* @ param array $alert Alert - Result from DB
* @ return array
*/
function DescribeAlert ( $alert ) {
$obj = array ();
2014-12-15 14:33:31 +00:00
$i = 0 ;
2014-12-15 11:10:26 +00:00
$device = dbFetchRow ( " SELECT hostname FROM devices WHERE device_id = ? " , array ( $alert [ 'device_id' ]));
$obj [ 'hostname' ] = $device [ 'hostname' ];
2015-05-18 10:23:25 +00:00
$obj [ 'device_id' ] = $alert [ 'device_id' ];
2014-12-15 11:10:26 +00:00
$extra = $alert [ 'details' ];
2015-01-25 21:39:33 +00:00
if ( $alert [ 'state' ] >= 1 ) {
2014-12-15 11:10:26 +00:00
$obj [ 'title' ] = 'Alert for device ' . $device [ 'hostname' ] . ' Alert-ID #' . $alert [ 'id' ];
2015-05-14 14:37:59 +00:00
if ( $alert [ 'state' ] == 2 ) {
$obj [ 'title' ] .= " got acknowledged " ;
} elseif ( $alert [ 'state' ] == 3 ) {
2015-01-25 21:39:33 +00:00
$obj [ 'title' ] .= " got worse " ;
} elseif ( $alert [ 'state' ] == 4 ) {
$obj [ 'title' ] .= " got better " ;
}
2014-12-15 11:10:26 +00:00
foreach ( $extra [ 'rule' ] as $incident ) {
$i ++ ;
2015-01-25 21:39:33 +00:00
$obj [ 'faults' ][ $i ] = $incident ;
2014-12-15 11:10:26 +00:00
foreach ( $incident as $k => $v ) {
2015-04-24 19:05:55 +00:00
if ( ! empty ( $v ) && $k != 'device_id' && ( stristr ( $k , 'id' ) || stristr ( $k , 'desc' ) || stristr ( $k , 'msg' )) && substr_count ( $k , '_' ) <= 1 ) {
2015-01-25 21:39:33 +00:00
$obj [ 'faults' ][ $i ][ 'string' ] .= $k . ' => ' . $v . " ; " ;
2014-12-15 11:10:26 +00:00
}
}
}
} elseif ( $alert [ 'state' ] == 0 ) {
2015-01-25 21:39:33 +00:00
$id = dbFetchRow ( " SELECT alert_log.id,alert_log.time_logged,alert_log.details FROM alert_log WHERE alert_log.state != 2 && alert_log.state != 0 && alert_log.rule_id = ? && alert_log.device_id = ? && alert_log.id < ? ORDER BY id DESC LIMIT 1 " , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ], $alert [ 'id' ]));
2014-12-15 11:10:26 +00:00
if ( empty ( $id [ 'id' ]) ) {
return false ;
}
$extra = json_decode ( gzuncompress ( $id [ 'details' ]), true );
$obj [ 'title' ] = 'Device ' . $device [ 'hostname' ] . ' recovered from Alert-ID #' . $id [ 'id' ];
$obj [ 'elapsed' ] = TimeFormat ( strtotime ( $alert [ 'time_logged' ]) - strtotime ( $id [ 'time_logged' ]));
$obj [ 'id' ] = $id [ 'id' ];
$obj [ 'faults' ] = false ;
} else {
return " Unknown State " ;
}
$obj [ 'uid' ] = $alert [ 'id' ];
$obj [ 'severity' ] = $alert [ 'severity' ];
$obj [ 'rule' ] = $alert [ 'rule' ];
2015-01-25 21:39:33 +00:00
$obj [ 'name' ] = $alert [ 'name' ];
2014-12-15 11:10:26 +00:00
$obj [ 'timestamp' ] = $alert [ 'time_logged' ];
$obj [ 'contacts' ] = $extra [ 'contacts' ];
$obj [ 'state' ] = $alert [ 'state' ];
return $obj ;
2007-04-03 14:10:23 +00:00
}
2014-12-15 11:10:26 +00:00
/**
* Format Elapsed Time
* @ param int $secs Seconds elapsed
* @ return string
*/
function TimeFormat ( $secs ){
$bit = array (
'y' => $secs / 31556926 % 12 ,
'w' => $secs / 604800 % 52 ,
'd' => $secs / 86400 % 7 ,
'h' => $secs / 3600 % 24 ,
'm' => $secs / 60 % 60 ,
's' => $secs % 60
);
2014-12-15 14:33:31 +00:00
$ret = array ();
2014-12-15 11:10:26 +00:00
foreach ( $bit as $k => $v ){
if ( $v > 0 ) {
$ret [] = $v . $k ;
}
}
if ( empty ( $ret ) ) {
return " none " ;
}
return join ( ' ' , $ret );
}
2015-01-25 21:39:33 +00:00
/**
* " Safely " run eval
* @ param string $code Code to run
* @ param array $obj Object with variables
* @ return string | mixed
*/
function RunJail ( $code , $obj ) {
$ret = " " ;
eval ( $code );
return $ret ;
}
/**
* Populate variables
2015-01-26 08:11:28 +00:00
* @ param string $txt Text with variables
* @ param bool $wrap Wrap variable for text - usage ( default : true )
2015-01-25 21:39:33 +00:00
* @ return string
*/
2015-01-26 08:11:28 +00:00
function populate ( $txt , $wrap = true ) {
2015-01-25 21:39:33 +00:00
preg_match_all ( '/%([\w\.]+)/' , $txt , $m );
foreach ( $m [ 1 ] as $tmp ) {
$orig = $tmp ;
$rep = false ;
if ( $tmp == " key " || $tmp == " value " ) {
$rep = '$' . $tmp ;
} else {
if ( strstr ( $tmp , '.' ) ) {
$tmp = explode ( '.' , $tmp , 2 );
$pre = '$' . $tmp [ 0 ];
$tmp = $tmp [ 1 ];
} else {
$pre = '$obj' ;
}
2015-01-26 08:11:28 +00:00
$rep = $pre . " [' " . str_replace ( '.' , " '][' " , $tmp ) . " '] " ;
if ( $wrap ) {
$rep = " { " . $rep . " } " ;
}
2015-01-25 21:39:33 +00:00
}
$txt = str_replace ( " % " . $orig , $rep , $txt );
}
return $txt ;
}
2011-05-13 11:42:26 +00:00
?>