64 lines
1.1 KiB
PHP
64 lines
1.1 KiB
PHP
<?php
|
|
|
|
class Agi
|
|
{
|
|
private static $initialized = FALSE;
|
|
private static $agivars = array ();
|
|
|
|
static function init ()
|
|
{
|
|
if (self::$initialized)
|
|
return;
|
|
|
|
self::$initialized = TRUE;
|
|
pcntl_signal (SIGHUP, SIG_IGN);
|
|
pcntl_signal (SIGTERM, SIG_IGN);
|
|
|
|
while (!feof (STDIN))
|
|
{
|
|
$agivar = trim (fgets (STDIN, 4096));
|
|
|
|
if ($agivar === '')
|
|
break;
|
|
|
|
$agivar = explode (':', $agivar);
|
|
|
|
if (count($agivar) == 2)
|
|
self::$agivars[$agivar[0]] = trim ($agivar[1]);
|
|
}
|
|
}
|
|
|
|
static function get ($agivar)
|
|
{
|
|
if (self::$initialized && isset (self::$agivars[$agivar]))
|
|
return self::$agivars[$agivar];
|
|
else
|
|
return NULL;
|
|
}
|
|
|
|
static function exec ($cmd, &$result = NULL)
|
|
{
|
|
if (!self::$initialized)
|
|
return -1;
|
|
|
|
fwrite (STDOUT, "$cmd\n");
|
|
fflush (STDOUT);
|
|
|
|
$res = trim (fgets (STDIN, 4096));
|
|
|
|
if (preg_match ("/^([0-9]{1,3}) (.*)/", $res, $matches))
|
|
{
|
|
if (preg_match ('/^result=([0-9\-]*)( ?\((.*)\))?$/', $matches[2], $match))
|
|
{
|
|
$ret = (int) $match[1];
|
|
if ($num > 0)
|
|
$result = $match[3];
|
|
return $ret;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
|