2016-12-12 08:30:01 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
class Agi {
|
|
|
|
private static $initialized = false;
|
|
|
|
private static $agivars = [];
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
/**
|
|
|
|
* Initializes the AGI class, must be called once when the
|
|
|
|
* application starts.
|
|
|
|
*/
|
|
|
|
static function init() {
|
2016-12-12 08:30:01 +00:00
|
|
|
if (self::$initialized)
|
|
|
|
return;
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
self::$initialized = true;
|
|
|
|
pcntl_signal(SIGHUP, SIG_IGN);
|
|
|
|
pcntl_signal(SIGTERM, SIG_IGN);
|
|
|
|
|
|
|
|
while (!feof(STDIN)) {
|
|
|
|
$agivar = trim(fgets(STDIN, 4096));
|
2016-12-12 13:03:28 +00:00
|
|
|
|
|
|
|
if ($agivar === '')
|
|
|
|
break;
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
$agivar = explode(':', $agivar);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
|
|
|
if (count($agivar) == 2)
|
2018-08-02 11:43:57 +00:00
|
|
|
self::$agivars[$agivar[0]] = trim($agivar[1]);
|
2016-12-12 08:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the value for an AGI variable.
|
|
|
|
*
|
|
|
|
* @param string $agivar The variable name
|
|
|
|
* @return string The variable value
|
|
|
|
*/
|
|
|
|
static function get($agivar) {
|
|
|
|
if (self::$initialized && isset(self::$agivars[$agivar]))
|
2016-12-12 08:30:01 +00:00
|
|
|
return self::$agivars[$agivar];
|
|
|
|
else
|
2018-08-02 11:43:57 +00:00
|
|
|
return null;
|
2016-12-12 08:30:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
/**
|
|
|
|
* Sends a command to the
|
|
|
|
*
|
|
|
|
* @param string $cmd The command
|
|
|
|
* @param string $result The result string
|
|
|
|
* @return int The command status
|
|
|
|
*/
|
|
|
|
static function exec($cmd, &$result = null) {
|
2016-12-12 08:30:01 +00:00
|
|
|
if (!self::$initialized)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
fwrite(STDOUT, "$cmd\n");
|
|
|
|
fflush(STDOUT);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
$res = trim(fgets(STDIN, 4096));
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
if (preg_match("/^([0-9]{1,3})(.*)/", $res, $matches)) {
|
|
|
|
if (preg_match('/^result=([0-9\-]*)( ?\((.*)\))?$/', $matches[2], $match)) {
|
2016-12-12 08:30:01 +00:00
|
|
|
$ret = (int) $match[1];
|
|
|
|
if ($num > 0)
|
|
|
|
$result = $match[3];
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|