function _sock_read($sock, $timeout, $stop_regex) { if (!is_resource($sock)) { return false; } @stream_set_blocking($sock, false); //stream_set_timeout( $sock, 5 ); // not really used here $tStart = time(); $data = ''; while (!@fEof($sock) && time() < $tStart + $timeout) { $data .= @fRead($sock, 8192); if (@preg_match($stop_regex, $data)) { break; } uSleep(1000); # sleep 0.001 secs } if ($data === '') { # assume timeout return false; } return $data; }
if (subStr($info['l'], 0, 3) === '*7*') { echo ",l:'" . 'privat' . "'"; } else { if (preg_match('/[^0-9*#+]/', $info['l'])) { // e.g. "offer" when offered call completion echo ",l:'" . "*" . "'"; } else { echo ",l:'" . $info['l'] . "'"; } } } echo "}"; ++$i; if ($i < $c) { echo ","; } echo "\n"; } echo "});\n"; echo $msie_pad; echo $msg_close; @ob_flush(); @flush(); } uSleep(20000); # sleep 0.02 secs } echo $html_end; @ob_flush(); @flush(); exit(0);
function gs_agi_do($cmd) { global $agi_basename; $fail = array('code' => 500, 'result' => -1, 'data' => ''); $response = array('code' => null, 'result' => null, 'data' => null); $cmd = trim($cmd); if ($cmd === '') { trigger_error("Empty AGI command.", E_USER_NOTICE); return $fail; } if (@fWrite(STDOUT, $cmd . "\n", strLen($cmd) + 1) === false) { trigger_error("AGI command \"{$cmd}\" failed! Could not write to StdOut.", E_USER_WARNING); return $fail; } gs_agi_log(GS_LOG_DEBUG, 'Tx << ' . $cmd); /* if (! in_array(php_sapi_name(), array('cgi'), true)) { # the correct way */ if (!@fFlush(STDOUT)) { gs_agi_log(GS_LOG_WARNING, 'Failed to flush StdOut!'); gs_log(GS_LOG_WARNING, "Failed to flush StdOut in AGI script {$agi_basename}!"); @ob_flush(); @flush(); uSleep(1000); } /* } else { # STDOUT is not defined in the "cgi" version of PHP. # However, the RedHat/Centos way of running shell scripts in # php-cgi instead of php-cli is simply wrong. uSleep(10); } */ stream_set_blocking(STDIN, true); $count = 0; $str = ''; do { uSleep(1000); stream_set_timeout(STDIN, 1); $str .= trim(fGetS(STDIN, 4096)); } while ($str == '' && $count++ < 5); gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $str); if ($count >= 5) { trigger_error("AGI command \"{$cmd}\" failed! Could not read response.", E_USER_WARNING); return $fail; } $response['code'] = subStr($str, 0, 3); $str = trim(subStr($str, 3)); if (subStr($str, 0, 1) === '-') { # multi-line response $count = 0; $str = subStr($str, 1) . "\n"; $line = fGetS(STDIN, 4096); gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $line); while (subStr($line, 0, 3) !== $response['code'] && $count < 5) { $str .= $line; $line = fGetS(STDIN, 4096); gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $line); $count = trim($line) == '' ? $count + 1 : 0; } if ($count >= 5) { trigger_error("AGI command \"{$cmd}\" failed! Could not read multi-line response.", E_USER_WARNING); return $fail; } } $response['result'] = null; $response['data'] = ''; if ($response['code'] !== '200') { $response['data'] = $str; } else { $parse = explode(' ', trim($str)); $in_token = false; foreach ($parse as $token) { if ($in_token) { $response['data'] .= ' ' . trim($token, '() '); if (subStr($token, strLen($token) - 1, 1) === ')') { $in_token = false; } } elseif (subStr($token, 0, 1) === '(') { if (subStr($token, strLen($token) - 1, 1) !== ')') { $in_token = true; } $response['data'] .= ' ' . trim($token, '() '); } elseif (strPos($token, '=') !== false) { $token = explode('=', $token); $response[$token[0]] = $token[1]; } elseif ($token != '') { $response['data'] .= ' ' . $token; } } $response['data'] = trim($response['data']); } return $response; }