Пример #1
0
 /**
  * Set runtime information.
  */
 static function init($port = 8089)
 {
     // Allow override of default port
     VCLIManager::$port = $port;
     // Provide session support
     if (session_id() === "") {
         session_id('virtualcli');
         session_start();
     }
     // Provide unique security key
     if (!isset($_SESSION['vcli_security_key'])) {
         $_SESSION['vcli_security_key'] = uniqid() . dechex(rand(0, 32000));
     }
     VCLIManager::$security_key = $_SESSION['vcli_security_key'];
     // Determine platform
     $uname = strtolower(php_uname());
     if (strpos($uname, "darwin") !== false) {
         VCLIManager::$platform = 'darwin';
         // OS X
     } else {
         if (strpos($uname, "win") !== false) {
             VCLIManager::$platform = 'win32';
             // Windows
         } else {
             if (strpos($uname, "linux") !== false) {
                 VCLIManager::$platform = 'linux';
                 // Linux
             } else {
                 VCLIManager::$platform = 'unsupported';
                 // Unsupported
             }
         }
     }
     // Ensure vcli native binary is in memory
     $process_id = false;
     $cmd = '"' . __DIR__ . "/Builds - vcli.xojo_xml_project/";
     if (VCLIManager::$platform === 'win32') {
         exec("tasklist.exe", $ps);
         foreach ($ps as $p) {
             if (false !== strpos($p, "vcli.exe")) {
                 $p = new GString($p);
                 $process_id = intval($p->delLeftMost("vcli.exe")->trim()->getLeftMost(" ")->__toString());
                 break;
             }
         }
         $cmd .= 'Windows\\vcli\\vcli.exe" --port ' . VCLIManager::$port . ' --security_key ' . VCLIManager::$security_key;
         $cmd = str_replace('/', '\\', $cmd);
         $cmd = 'start /b "vcli" ' . $cmd;
     } else {
         $process_id = exec("ps -ax | awk '/[v]cli\\/vcli/{print \$1}'") | false;
         if (!$process_id) {
             $process_id = exec("ps -ax | awk '/[v]cli.debug\\/vcli.debug/{print \$1}'") | false;
         }
         if (VCLIManager::$platform === 'darwin') {
             $cmd .= 'Mac OS X (Intel)/vcli/vcli" --port ' . VCLIManager::$port . ' --security_key ';
             $cmd .= VCLIManager::$security_key . ' > /dev/null 2>&1 &';
         } else {
             $cmd .= 'Linux/vcli/vcli" --port ' . VCLIManager::$port . ' --security_key ' . VCLIManager::$security_key;
             $cmd .= ' > /dev/null 2>&1 &';
         }
     }
     // Launch vcli instance
     if (!$process_id) {
         if (VCLIManager::$platform === 'win32') {
             pclose(popen($cmd, "r"));
         } else {
             exec($cmd);
         }
     }
     // Wait up to 15 seconds to see if socket is online
     $connected = @fsockopen("127.0.0.1", VCLIManager::$port);
     $timeup = 15;
     while (false === $connected && $timeup > 0) {
         $connected = @fsockopen("127.0.0.1", VCLIManager::$port);
         $timeup = $timeup - 1;
         sleep(1);
     }
     @fclose($connected);
 }