]> Untitled Git - dev/commitdiff
Косметические исправления
authorsleepy <sleepy@vvsu.ru>
Tue, 28 Apr 2020 14:50:07 +0000 (00:50 +1000)
committersleepy <sleepy@vvsu.ru>
Tue, 28 Apr 2020 14:50:07 +0000 (00:50 +1000)
check_code.php
dvblastmaster/lib/mplib.php [deleted file]
dvblastmaster/lib/sts.php [deleted file]
dvblastmaster/lib/timer.php [deleted file]
lib/mplib.php [new file with mode: 0644]
lib/sts.php [new file with mode: 0644]
lib/timer.php [new file with mode: 0644]

index 8ec6b1b924a617fedb13e41bd877db7300765f91..be7c519115ba21d06402c06aac695fe61665086c 100644 (file)
@@ -1,50 +1,75 @@
 <?php
 declare (ticks=1);
 require_once 'astapilib/ami.php';
+require_once 'lib/mplib.php';
 
 //SETTINGS
 $PINLEN = 4;
 $REPEATNO = 5;
-$CALLFROM = '"suka" <3505>';
+$CALLWAITFORANSWER = 30;
+$CALLFROM = '"verify code sytem" <3505>';
 $RETRYCALL = 3;
 $INTERVAL = 5;
+$CALLCONEXT = 'c-2';
+$AMIHOST ="127.0.0.1";
+$AMIUSER = "monast";
+$AMIPASS = "blabla";
 //
 //test data
-echo "<pre>\n";
-$_GET['phone'] = '3400';
-var_dump($_GET);
+//$_GET['phone'] = '3400';
+//var_dump($_GET);
 
-if (!isset($_GET['phone']))
+if (php_sapi_name() != 'cli')
 {
+    $ADDR=$_SERVER["REMOTE_ADDR"];
+//    var_dump($ADDR);    
+//if ($ADDR=="192.168.10.225" || $ADDR=="10.20.2.120" || $ADDR=="217.74.116.10")
+//exit;
+    if (!isset($_GET['phone']))
+    {
+       exit();
+    }
+    if(preg_match('/^7\d{10}$/', $_GET['phone']) != 1)
+    {
+       echo 'BADPHONE'.PHP_EOL;
+        exit();
+    }
+    $CALLEDPHONENUM = '8'.substr($_GET['phone'], 1);
+    //generate random pin
+    $PIN = [];
+    for ($cnt = 0; $cnt < $PINLEN;$cnt++)
+    {
+       mt_srand(str_replace('.', '', (string)array_product(sys_getloadavg())) * crc32(microtime()));
+       $cnte = mt_rand(0,30);
+       $cntr = 0;
+       do
+       {
+           $rnd = mt_rand(0,9);
+           $cntr++;
+       }
+       while($cntr < $cnte);
+       $PIN[] = $rnd;
+       usleep(mt_rand(1,250000));
+    }
+
+    echo implode('', $PIN),PHP_EOL;
+    system('php -f '.basename(__FILE__).' '.$CALLEDPHONENUM.' '.implode('-', $PIN));
     exit();
 }
-if(preg_match('/^7\d{10}$/', $_GET['phone']) != 1)
-{
-    //exit();
-}
-//generate random pin
-$PIN = [];
-for ($cnt = 0; $cnt < $PINLEN;$cnt++)
+if (!isset($argv[1]) || !isset($argv[2]))
 {
-    mt_srand(str_replace('.', '', (string)array_product(sys_getloadavg())) * crc32(microtime()));
-    $cnte = mt_rand(0,30);
-    $cntr = 0;
-    do
-    {
-       $rnd = mt_rand(0,9);
-       $cntr++;
-    }
-    while($cntr < $cnte);
-    $PIN[] = $rnd;
-    usleep(mt_rand(1,250000));
+    exit();
 }
 
-echo implode('', $PIN),PHP_EOL;
+$CALLEDPHONENUM = $argv[1];
+$PIN = explode('-', $argv[2]);
 
 //fork point
+$D = new Daemon();
+$D->daemonize();
 
 $AMI = new AMI(array('autorefresh' => TRUE, 'logverbose' => 3));
-$is_connected = $AMI->connect("127.0.0.1", "monast", "blabla");
+$is_connected = $AMI->connect($AMIHOST, $AMIUSER, $AMIPASS);
 
 if (!$is_connected)
 {
@@ -53,7 +78,7 @@ if (!$is_connected)
 
 for ($tryno = 0; $tryno < $RETRYCALL;$tryno++)
 {
-    $OriginateResponce = $AMI->Originate("Local/{$_GET['phone']}@c-2", NULL, NULL, NULL, 'AGI', 'agi:async', 30, $CALLFROM, ['CALLERID(ANI-num)' => '3505'], NULL, NULL, NULL);
+    $OriginateResponce = $AMI->Originate("Local/{$CALLEDPHONENUM}@{$CALLCONEXT}", NULL, NULL, NULL, 'AGI', 'agi:async', $CALLWAITFORANSWER, $CALLFROM);
     $OriginateResponce->WaitUntilReady();
     if ($OriginateResponce->Response == 'Success')
     {
@@ -67,7 +92,7 @@ if ($OriginateResponce->Response != 'Success')
 }
 $channel = $OriginateResponce->Channel;
 $AGI = $AMI->GetAsyncAGIInstance($channel);
-
+sleep(1);
 for ($tryno = 0; $tryno < $REPEATNO;$tryno++)
 {
     $AGI->StreamFile('your');
diff --git a/dvblastmaster/lib/mplib.php b/dvblastmaster/lib/mplib.php
deleted file mode 100644 (file)
index 871965c..0000000
+++ /dev/null
@@ -1,271 +0,0 @@
-<?php\r
-\r
-/*\r
- * VERSION 2.1b\r
- */\r
-define('MPRole_PARENT', 0);\r
-define('MPRole_CHILD', 1);\r
-define('MPRole_ERROR', -1);\r
-\r
-class Daemon\r
-{\r
-    private static $unique_flag = FALSE;\r
-    private static $pidfilename;\r
-    \r
-    public static function shutdown()\r
-    {\r
-       if (self::$unique_flag)\r
-       {\r
-           @unlink(static::$pidfilename);\r
-       }\r
-       \r
-    }\r
-\r
-    public static function daemonize()\r
-    {\r
-       $pid = pcntl_fork();\r
-       if ($pid == -1)\r
-        {\r
-                return FALSE;\r
-        }\r
-        elseif ($pid > 0)\r
-        {\r
-            exit;\r
-        }\r
-       self::$origin_pid = posix_getpid();\r
-        umask(0);\r
-        chdir('/');\r
-        if (posix_setsid() == -1)\r
-        {\r
-           return FALSE;\r
-        }\r
-       \r
-       fclose(STDIN);\r
-       fclose(STDOUT);\r
-       fclose(STDERR);\r
-       $GLOBALS['STDIN'] = fopen('/dev/null', 'r');\r
-       $GLOBALS['STDOUT']= fopen('/dev/null', 'w');\r
-       $GLOBALS['STDERR'] = fopen('/dev/null', 'w');\r
-       \r
-       return TRUE;\r
-    }\r
-    \r
-    public static function is_unique($pidfilename)\r
-    {\r
-       $mypid = posix_getpid();\r
-       if (is_readable($pidfilename))\r
-       {\r
-           $pid = (int)  rtrim(file_get_contents($pidfilename));\r
-           if ($pid == $mypid)\r
-           {\r
-               return TRUE;\r
-           }\r
-           if ($pid > 0 && posix_kill($pid, 0))\r
-           {\r
-               return FALSE;\r
-           }\r
-       }\r
-       return TRUE;\r
-\r
-    }\r
-    \r
-    public static function no_unique($pidfilename)\r
-    {\r
-       if (!@unlink($pidfilename))\r
-       {\r
-           return FALSE;\r
-       }\r
-       else\r
-       {\r
-           return TRUE;\r
-       }\r
-    }\r
-\r
-    public static function do_unique($pidfilename)\r
-    {\r
-       if (!self::is_unique($pidfilename))\r
-       {\r
-           return FALSE;\r
-       }\r
-\r
-       $mypid = posix_getpid();\r
-       if (!@file_put_contents($pidfilename, $mypid.PHP_EOL))\r
-       {\r
-           return FALSE;\r
-       }\r
-       register_shutdown_function(['self','shutdown']);\r
-       return TRUE;\r
-    }\r
-}    \r
-\r
-abstract class Children \r
-{\r
-    protected $PIPE;\r
-    protected $ROLE;\r
-    protected static $CHILDREN;\r
-    protected $CHILD;\r
-    protected $PARENT;\r
-    protected $IPCRXBUFFER = [];\r
-\r
-\r
-    public function __construct($data = NULL)\r
-    {\r
-       self::set_sig_handlers();\r
-       $this->create_child();\r
-       switch ($this->ROLE)\r
-       {\r
-           case MPRole_ERROR:\r
-               return FALSE;\r
-           case MPRole_PARENT:\r
-               return TRUE;\r
-           case MPRole_CHILD:\r
-               $this->ChildBody($data);\r
-               exit();\r
-       }\r
-       \r
-    }\r
-    \r
-    public function __destruct()\r
-    {\r
-       if ($this->ROLE == MPRole_PARENT)\r
-       {\r
-           posix_kill($this->CHILD, SIGTERM);\r
-       }\r
-    }\r
-\r
-    abstract protected function ChildBody($data);\r
-\r
-    static function set_sig_handlers()\r
-    {\r
-       pcntl_signal(SIGCHLD, [__CLASS__, 'handler_sigchld']);\r
-    }\r
-    \r
-    public function SendUnixSignal($sig)\r
-    {\r
-       return posix_kill($this->CHILD, $sig);\r
-    }\r
-      \r
-    public function IsAlive()\r
-    {\r
-       if ($this->ROLE == MPRole_CHILD)\r
-       {\r
-           return TRUE;\r
-       }\r
-       if (isset(self::$CHILDREN[$this->CHILD]))\r
-       {\r
-           return TRUE;\r
-       }\r
-       else\r
-       {\r
-           return FALSE;\r
-       }\r
-    }\r
-\r
-    public static function handler_sigchld()\r
-    {  \r
-       $pid = pcntl_waitpid(0, $status, WNOHANG);\r
-       if($pid > 0 && isset(self::$CHILDREN[$pid]))\r
-       {\r
-           unset(self::$CHILDREN[$pid]);\r
-       }\r
-    }\r
-\r
-    protected function create_child()\r
-    {\r
-       $sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);\r
-       stream_set_blocking ($sockets[0] , 0);\r
-       stream_set_blocking ($sockets[1] , 0);\r
-       $pid = pcntl_fork();\r
-       if ($pid == -1)\r
-       {\r
-           $this->ROLE = MPRole_ERROR;\r
-           return FALSE;\r
-       }\r
-       elseif ($pid > 0)\r
-       {\r
-           // код для родителя\r
-           $this->CHILD = $pid;\r
-           self::$CHILDREN[$pid] = $pid;\r
-           $this->PARENT = FALSE;\r
-           $this->ROLE = MPRole_PARENT;\r
-           $this->PIPE = &$sockets[0];\r
-           unset($sockets);\r
-           usleep(5000);\r
-       }\r
-       elseif ($pid == 0)\r
-       {\r
-           // код для ребёнка\r
-           $this->CHILD = FALSE;\r
-           $this->PARENT = posix_getppid();\r
-           $this->ROLE = MPRole_CHILD;\r
-           $this->PIPE = &$sockets[1];\r
-           unset($sockets);\r
-       }\r
-       return TRUE;\r
-    }\r
-    \r
-    public function SendEvent($eventname, $data)\r
-    {\r
-       if (!is_resource($this->PIPE))\r
-       {\r
-           return FALSE;\r
-       }\r
-       $eventpack = base64_encode($eventname).chr(255).chr(0).chr(255).base64_encode(serialize($data)).chr(0).chr(15).chr(240).chr(255);\r
-       \r
-       $res = @stream_socket_sendto($this->PIPE, $eventpack);  \r
-       if ($res == -1)\r
-       {\r
-           return TRUE;\r
-       }\r
-       else\r
-       {\r
-           return FALSE;\r
-       }\r
-    }\r
-    \r
-    public function WaitEvent($wait = TRUE)\r
-    {\r
-       if (count($this->IPCRXBUFFER) == 0)\r
-       {\r
-           if (!is_resource($this->PIPE))\r
-           {\r
-               return FALSE;\r
-           }\r
-\r
-           $r = [$this->PIPE];\r
-           if ($wait)\r
-           {\r
-               while (stream_select($r, $w, $x, 0) == 0)\r
-               {\r
-                   usleep(10000);\r
-                   $r = [$this->PIPE];\r
-               }\r
-           }\r
-           elseif (stream_select($r, $w, $x, 0) == 0)\r
-           {\r
-               return NULL;\r
-           }\r
-           $ipc_msg = '';\r
-           while (substr($ipc_msg , -4) != chr(0).chr(15).chr(240).chr(255))\r
-           {\r
-               $rcv_buf = stream_socket_recvfrom($this->PIPE, 1500);\r
-               if ($rcv_buf == '')\r
-               {\r
-                   return NULL;\r
-               }\r
-               $ipc_msg .= $rcv_buf;\r
-           }\r
-           $ipc_msg = explode(chr(0).chr(15).chr(240).chr(255), $ipc_msg);\r
-           array_pop($ipc_msg);\r
-           $this->IPCRXBUFFER = array_merge($this->IPCRXBUFFER, $ipc_msg);\r
-       }\r
-       $ipc_msg = array_shift($this->IPCRXBUFFER);\r
-       $ipc_msg = explode(chr(255).chr(0).chr(255), $ipc_msg);\r
-       \r
-       $retval['eventname'] = base64_decode($ipc_msg[0]);\r
-       $retval['data'] = unserialize(base64_decode($ipc_msg[1]));\r
-       \r
-       return $retval;\r
-       \r
-    }\r
-}\r
diff --git a/dvblastmaster/lib/sts.php b/dvblastmaster/lib/sts.php
deleted file mode 100644 (file)
index 9c7b8a5..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-<?php
-
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/**
- * Description of inigen
- *
- * @author sleepy
- */
-class STS
-{
-    private static $SigArray = FALSE;
-    private static $IsPrefixed = FALSE;
-    private static $IsLastEol;
-    private static $StdPrefix = '';
-
-    public static function generate_ini($ini_array)
-    {
-       if (!is_array($ini_array))
-       {
-           return FALSE;
-       }
-       if (count($ini_array) == 0)
-       {
-           return NULL;
-       }
-       $_RETVAL= '';
-       if (is_array($ini_array[key ($ini_array)]))
-       {
-           foreach ($ini_array as $sectionname => $sectiondata)
-           {
-               if(!is_array($sectiondata))
-               {
-                   return FALSE;
-               }
-               $_RETVAL .= '['.$sectionname.']'.PHP_EOL;
-               foreach ($sectiondata as $param => $data)
-               {
-                   if (is_array($data))
-                   {
-                       $data = '';
-                   }
-                   $_RETVAL .= $param.' = '.$data.PHP_EOL;
-               }
-               $_RETVAL .= PHP_EOL;
-           }
-       }
-       else
-       {
-           foreach ($ini_array as $param => $data)
-           {
-               $_RETVAL .= $param.' = '.$data.PHP_EOL;
-           }
-       }
-       return $_RETVAL;
-    }
-    private static function GetSignalDescription()
-    {
-       if (self::$SigArray === FALSE)
-       {
-           self::$SigArray = explode("\n", shell_exec('kill -l'));
-       }       
-    }
-    
-    public static function GetSignalMnemonicByNumber($signo)
-    {
-       self::GetSignalDescription();
-       if (isset(self::$SigArray[$signo]))
-       {
-           return self::$SigArray[$signo];
-       }
-       else
-       {
-           return FALSE;
-       }
-    }
-
-    public static function GetSignalNumberByMnemonic($sigmnemo)
-    {
-       self::GetSignalDescription();
-       return array_search($sigmnemo, self::$SigArray);
-    }
-
-    public static function stdout_prefix($prefix)
-    {
-       if (!self::$IsPrefixed)
-       {
-           self::$StdPrefix = $prefix;
-           ob_start(['self','ob_callback'],1);
-           self::$IsPrefixed = TRUE;
-           return TRUE;
-       }
-       else
-       {
-           return FALSE;
-       }
-
-    }
-
-    public static function stdout_unprefix()
-    {
-       if (self::$IsPrefixed)
-       {
-           ob_end_clean();
-           self::$IsPrefixed = FALSE;
-           return TRUE;
-       }
-       else
-       {
-           return FALSE;
-       }
-
-    }
-    
-    private static function ob_callback($buffer)
-    {
-       if (self::$IsLastEol)
-       {
-           $prefix = self::$StdPrefix;
-       }
-       else
-       {
-           $prefix = '';
-       }
-       self::$IsLastEol = substr($buffer, -1) == PHP_EOL;
-       return $prefix.$buffer;
-
-    }
-    
-}
-
diff --git a/dvblastmaster/lib/timer.php b/dvblastmaster/lib/timer.php
deleted file mode 100644 (file)
index dc577cd..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-<?php
-//VERSION 2.4
-register_tick_function(array('Timer','poll'));
-
-class Timer
-{
-    private $id; //метка
-    private $time; //таймер
-    private $IsStop = TRUE; //флаг остановленности true/false
-    private $UserCall; //задача
-    private $UserCallArg; //параметр к задаче в виде массива для передачи нескольких значений или единственного.
-    private $StartTime; //момент запуска таймера
-    private $ltime; //оставшееся время
-    private $type; //тип true регенерируемый, false не регенерируемый, null самоуничтожаемый не регенерируемый, применяется по умолчанию без присвоения, не может быть перезапущен.
-    private static $timers = []; 
-
-    public function __construct($time, $callable, $args = NULL,  $type = NULL) 
-    {
-       if ($args === NULL)
-       {
-           $args = [];
-       }
-       if (!is_array($args))
-       {
-           throw new Exception("parameter 3 must be an array, \$args=".gettype($args));
-       }
-       $this->id = uniqid();
-        $this->time = $time;
-        $this->UserCall = $callable;
-       $this->UserCallArg = $args;
-        $this->type = $type;
-       if (!is_callable($this->UserCall, TRUE, $callablename))
-       {
-           throw new Exception("parameter 2 must be callable, \$callable=". $callablename);
-       }
-       else
-       {    
-           $this->start();
-           self::$timers[$this->id] = &$this;
-       }
-    }
-
-    public function __destruct() 
-    {
-       unset(self::$timers[$this->id]);
-    }
-
-    private function getId()
-    {
-       return $this->id;
-    }
-
-    public function getTimeLeft()
-    {
-           return $this->ltime;
-    }
-
-    public function setTimeLeft($time)
-    {
-           $this->time = $time;
-    }
-
-
-    public function reset()
-    {
-       $this->StartTime = microtime(true);
-       $this->IsStop = FALSE;
-    }
-
-    public function start() 
-    {
-       $this->reset();
-       $this->IsStop = FALSE;
-    }
-
-    public function stop() 
-    {
-       $this->IsStop = TRUE;
-    }
-
-    public static function poll()
-    {
-       foreach (self::$timers as $timer)
-       {
-           if ($timer->IsStop == FALSE)
-           {
-               $timer->update();
-           }
-       }
-    }
-
-    private function update()
-    {
-       $this->ltime = $this->StartTime - microtime(true) + $this->time;
-       if ($this->ltime <= 0)
-       {
-           $this->ltime = 0;
-           $this->task();
-           if ($this->type === FALSE)
-           {
-               $this->stop();
-           }
-           elseif ($this->type === TRUE)
-           {
-               $this->reset();
-           }
-           elseif ($this->type === NULL)
-           {
-               $this->stop();
-               $this->__destruct();
-           }
-       }
-    }
-
-    private function task() 
-    {
-       call_user_func_array ($this->UserCall, $this->UserCallArg);
-    }
-}
diff --git a/lib/mplib.php b/lib/mplib.php
new file mode 100644 (file)
index 0000000..a756d6a
--- /dev/null
@@ -0,0 +1,270 @@
+<?php\r
+\r
+/*\r
+ * VERSION 2.1.1b\r
+ */\r
+define('MPRole_PARENT', 0);\r
+define('MPRole_CHILD', 1);\r
+define('MPRole_ERROR', -1);\r
+\r
+class Daemon\r
+{\r
+    private static $unique_flag = FALSE;\r
+    private static $pidfilename;\r
+    \r
+    public static function shutdown()\r
+    {\r
+       if (self::$unique_flag)\r
+       {\r
+           @unlink(static::$pidfilename);\r
+       }\r
+       \r
+    }\r
+\r
+    public static function daemonize()\r
+    {\r
+       $pid = pcntl_fork();\r
+       if ($pid == -1)\r
+        {\r
+                return FALSE;\r
+        }\r
+        elseif ($pid > 0)\r
+        {\r
+            exit;\r
+        }\r
+       umask(0);\r
+        chdir('/');\r
+        if (posix_setsid() == -1)\r
+        {\r
+           return FALSE;\r
+        }\r
+       \r
+       fclose(STDIN);\r
+       fclose(STDOUT);\r
+       fclose(STDERR);\r
+       $GLOBALS['STDIN'] = fopen('/dev/null', 'r');\r
+       $GLOBALS['STDOUT']= fopen('/dev/null', 'w');\r
+       $GLOBALS['STDERR'] = fopen('/dev/null', 'w');\r
+       \r
+       return TRUE;\r
+    }\r
+    \r
+    public static function is_unique($pidfilename)\r
+    {\r
+       $mypid = posix_getpid();\r
+       if (is_readable($pidfilename))\r
+       {\r
+           $pid = (int)  rtrim(file_get_contents($pidfilename));\r
+           if ($pid == $mypid)\r
+           {\r
+               return TRUE;\r
+           }\r
+           if ($pid > 0 && posix_kill($pid, 0))\r
+           {\r
+               return FALSE;\r
+           }\r
+       }\r
+       return TRUE;\r
+\r
+    }\r
+    \r
+    public static function no_unique($pidfilename)\r
+    {\r
+       if (!@unlink($pidfilename))\r
+       {\r
+           return FALSE;\r
+       }\r
+       else\r
+       {\r
+           return TRUE;\r
+       }\r
+    }\r
+\r
+    public static function do_unique($pidfilename)\r
+    {\r
+       if (!self::is_unique($pidfilename))\r
+       {\r
+           return FALSE;\r
+       }\r
+\r
+       $mypid = posix_getpid();\r
+       if (!@file_put_contents($pidfilename, $mypid.PHP_EOL))\r
+       {\r
+           return FALSE;\r
+       }\r
+       register_shutdown_function(['self','shutdown']);\r
+       return TRUE;\r
+    }\r
+}    \r
+\r
+abstract class Children \r
+{\r
+    protected $PIPE;\r
+    protected $ROLE;\r
+    protected static $CHILDREN;\r
+    protected $CHILD;\r
+    protected $PARENT;\r
+    protected $IPCRXBUFFER = [];\r
+\r
+\r
+    public function __construct($data = NULL)\r
+    {\r
+       self::set_sig_handlers();\r
+       $this->create_child();\r
+       switch ($this->ROLE)\r
+       {\r
+           case MPRole_ERROR:\r
+               return FALSE;\r
+           case MPRole_PARENT:\r
+               return TRUE;\r
+           case MPRole_CHILD:\r
+               $this->ChildBody($data);\r
+               exit();\r
+       }\r
+       \r
+    }\r
+    \r
+    public function __destruct()\r
+    {\r
+       if ($this->ROLE == MPRole_PARENT)\r
+       {\r
+           posix_kill($this->CHILD, SIGTERM);\r
+       }\r
+    }\r
+\r
+    abstract protected function ChildBody($data);\r
+\r
+    static function set_sig_handlers()\r
+    {\r
+       pcntl_signal(SIGCHLD, [__CLASS__, 'handler_sigchld']);\r
+    }\r
+    \r
+    public function SendUnixSignal($sig)\r
+    {\r
+       return posix_kill($this->CHILD, $sig);\r
+    }\r
+      \r
+    public function IsAlive()\r
+    {\r
+       if ($this->ROLE == MPRole_CHILD)\r
+       {\r
+           return TRUE;\r
+       }\r
+       if (isset(self::$CHILDREN[$this->CHILD]))\r
+       {\r
+           return TRUE;\r
+       }\r
+       else\r
+       {\r
+           return FALSE;\r
+       }\r
+    }\r
+\r
+    public static function handler_sigchld()\r
+    {  \r
+       $pid = pcntl_waitpid(0, $status, WNOHANG);\r
+       if($pid > 0 && isset(self::$CHILDREN[$pid]))\r
+       {\r
+           unset(self::$CHILDREN[$pid]);\r
+       }\r
+    }\r
+\r
+    protected function create_child()\r
+    {\r
+       $sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);\r
+       stream_set_blocking ($sockets[0] , 0);\r
+       stream_set_blocking ($sockets[1] , 0);\r
+       $pid = pcntl_fork();\r
+       if ($pid == -1)\r
+       {\r
+           $this->ROLE = MPRole_ERROR;\r
+           return FALSE;\r
+       }\r
+       elseif ($pid > 0)\r
+       {\r
+           // код для родителя\r
+           $this->CHILD = $pid;\r
+           self::$CHILDREN[$pid] = $pid;\r
+           $this->PARENT = FALSE;\r
+           $this->ROLE = MPRole_PARENT;\r
+           $this->PIPE = &$sockets[0];\r
+           unset($sockets);\r
+           usleep(5000);\r
+       }\r
+       elseif ($pid == 0)\r
+       {\r
+           // код для ребёнка\r
+           $this->CHILD = FALSE;\r
+           $this->PARENT = posix_getppid();\r
+           $this->ROLE = MPRole_CHILD;\r
+           $this->PIPE = &$sockets[1];\r
+           unset($sockets);\r
+       }\r
+       return TRUE;\r
+    }\r
+    \r
+    public function SendEvent($eventname, $data)\r
+    {\r
+       if (!is_resource($this->PIPE))\r
+       {\r
+           return FALSE;\r
+       }\r
+       $eventpack = base64_encode($eventname).chr(255).chr(0).chr(255).base64_encode(serialize($data)).chr(0).chr(15).chr(240).chr(255);\r
+       \r
+       $res = @stream_socket_sendto($this->PIPE, $eventpack);  \r
+       if ($res == -1)\r
+       {\r
+           return TRUE;\r
+       }\r
+       else\r
+       {\r
+           return FALSE;\r
+       }\r
+    }\r
+    \r
+    public function WaitEvent($wait = TRUE)\r
+    {\r
+       if (count($this->IPCRXBUFFER) == 0)\r
+       {\r
+           if (!is_resource($this->PIPE))\r
+           {\r
+               return FALSE;\r
+           }\r
+\r
+           $r = [$this->PIPE];\r
+           if ($wait)\r
+           {\r
+               while (stream_select($r, $w, $x, 0) == 0)\r
+               {\r
+                   usleep(10000);\r
+                   $r = [$this->PIPE];\r
+               }\r
+           }\r
+           elseif (stream_select($r, $w, $x, 0) == 0)\r
+           {\r
+               return NULL;\r
+           }\r
+           $ipc_msg = '';\r
+           while (substr($ipc_msg , -4) != chr(0).chr(15).chr(240).chr(255))\r
+           {\r
+               $rcv_buf = stream_socket_recvfrom($this->PIPE, 1500);\r
+               if ($rcv_buf == '')\r
+               {\r
+                   return NULL;\r
+               }\r
+               $ipc_msg .= $rcv_buf;\r
+           }\r
+           $ipc_msg = explode(chr(0).chr(15).chr(240).chr(255), $ipc_msg);\r
+           array_pop($ipc_msg);\r
+           $this->IPCRXBUFFER = array_merge($this->IPCRXBUFFER, $ipc_msg);\r
+       }\r
+       $ipc_msg = array_shift($this->IPCRXBUFFER);\r
+       $ipc_msg = explode(chr(255).chr(0).chr(255), $ipc_msg);\r
+       \r
+       $retval['eventname'] = base64_decode($ipc_msg[0]);\r
+       $retval['data'] = unserialize(base64_decode($ipc_msg[1]));\r
+       \r
+       return $retval;\r
+       \r
+    }\r
+}\r
diff --git a/lib/sts.php b/lib/sts.php
new file mode 100644 (file)
index 0000000..9c7b8a5
--- /dev/null
@@ -0,0 +1,135 @@
+<?php
+
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/**
+ * Description of inigen
+ *
+ * @author sleepy
+ */
+class STS
+{
+    private static $SigArray = FALSE;
+    private static $IsPrefixed = FALSE;
+    private static $IsLastEol;
+    private static $StdPrefix = '';
+
+    public static function generate_ini($ini_array)
+    {
+       if (!is_array($ini_array))
+       {
+           return FALSE;
+       }
+       if (count($ini_array) == 0)
+       {
+           return NULL;
+       }
+       $_RETVAL= '';
+       if (is_array($ini_array[key ($ini_array)]))
+       {
+           foreach ($ini_array as $sectionname => $sectiondata)
+           {
+               if(!is_array($sectiondata))
+               {
+                   return FALSE;
+               }
+               $_RETVAL .= '['.$sectionname.']'.PHP_EOL;
+               foreach ($sectiondata as $param => $data)
+               {
+                   if (is_array($data))
+                   {
+                       $data = '';
+                   }
+                   $_RETVAL .= $param.' = '.$data.PHP_EOL;
+               }
+               $_RETVAL .= PHP_EOL;
+           }
+       }
+       else
+       {
+           foreach ($ini_array as $param => $data)
+           {
+               $_RETVAL .= $param.' = '.$data.PHP_EOL;
+           }
+       }
+       return $_RETVAL;
+    }
+    private static function GetSignalDescription()
+    {
+       if (self::$SigArray === FALSE)
+       {
+           self::$SigArray = explode("\n", shell_exec('kill -l'));
+       }       
+    }
+    
+    public static function GetSignalMnemonicByNumber($signo)
+    {
+       self::GetSignalDescription();
+       if (isset(self::$SigArray[$signo]))
+       {
+           return self::$SigArray[$signo];
+       }
+       else
+       {
+           return FALSE;
+       }
+    }
+
+    public static function GetSignalNumberByMnemonic($sigmnemo)
+    {
+       self::GetSignalDescription();
+       return array_search($sigmnemo, self::$SigArray);
+    }
+
+    public static function stdout_prefix($prefix)
+    {
+       if (!self::$IsPrefixed)
+       {
+           self::$StdPrefix = $prefix;
+           ob_start(['self','ob_callback'],1);
+           self::$IsPrefixed = TRUE;
+           return TRUE;
+       }
+       else
+       {
+           return FALSE;
+       }
+
+    }
+
+    public static function stdout_unprefix()
+    {
+       if (self::$IsPrefixed)
+       {
+           ob_end_clean();
+           self::$IsPrefixed = FALSE;
+           return TRUE;
+       }
+       else
+       {
+           return FALSE;
+       }
+
+    }
+    
+    private static function ob_callback($buffer)
+    {
+       if (self::$IsLastEol)
+       {
+           $prefix = self::$StdPrefix;
+       }
+       else
+       {
+           $prefix = '';
+       }
+       self::$IsLastEol = substr($buffer, -1) == PHP_EOL;
+       return $prefix.$buffer;
+
+    }
+    
+}
+
diff --git a/lib/timer.php b/lib/timer.php
new file mode 100644 (file)
index 0000000..dc577cd
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+//VERSION 2.4
+register_tick_function(array('Timer','poll'));
+
+class Timer
+{
+    private $id; //метка
+    private $time; //таймер
+    private $IsStop = TRUE; //флаг остановленности true/false
+    private $UserCall; //задача
+    private $UserCallArg; //параметр к задаче в виде массива для передачи нескольких значений или единственного.
+    private $StartTime; //момент запуска таймера
+    private $ltime; //оставшееся время
+    private $type; //тип true регенерируемый, false не регенерируемый, null самоуничтожаемый не регенерируемый, применяется по умолчанию без присвоения, не может быть перезапущен.
+    private static $timers = []; 
+
+    public function __construct($time, $callable, $args = NULL,  $type = NULL) 
+    {
+       if ($args === NULL)
+       {
+           $args = [];
+       }
+       if (!is_array($args))
+       {
+           throw new Exception("parameter 3 must be an array, \$args=".gettype($args));
+       }
+       $this->id = uniqid();
+        $this->time = $time;
+        $this->UserCall = $callable;
+       $this->UserCallArg = $args;
+        $this->type = $type;
+       if (!is_callable($this->UserCall, TRUE, $callablename))
+       {
+           throw new Exception("parameter 2 must be callable, \$callable=". $callablename);
+       }
+       else
+       {    
+           $this->start();
+           self::$timers[$this->id] = &$this;
+       }
+    }
+
+    public function __destruct() 
+    {
+       unset(self::$timers[$this->id]);
+    }
+
+    private function getId()
+    {
+       return $this->id;
+    }
+
+    public function getTimeLeft()
+    {
+           return $this->ltime;
+    }
+
+    public function setTimeLeft($time)
+    {
+           $this->time = $time;
+    }
+
+
+    public function reset()
+    {
+       $this->StartTime = microtime(true);
+       $this->IsStop = FALSE;
+    }
+
+    public function start() 
+    {
+       $this->reset();
+       $this->IsStop = FALSE;
+    }
+
+    public function stop() 
+    {
+       $this->IsStop = TRUE;
+    }
+
+    public static function poll()
+    {
+       foreach (self::$timers as $timer)
+       {
+           if ($timer->IsStop == FALSE)
+           {
+               $timer->update();
+           }
+       }
+    }
+
+    private function update()
+    {
+       $this->ltime = $this->StartTime - microtime(true) + $this->time;
+       if ($this->ltime <= 0)
+       {
+           $this->ltime = 0;
+           $this->task();
+           if ($this->type === FALSE)
+           {
+               $this->stop();
+           }
+           elseif ($this->type === TRUE)
+           {
+               $this->reset();
+           }
+           elseif ($this->type === NULL)
+           {
+               $this->stop();
+               $this->__destruct();
+           }
+       }
+    }
+
+    private function task() 
+    {
+       call_user_func_array ($this->UserCall, $this->UserCallArg);
+    }
+}