awsswa писал(а):whoim писал(а):
И, кстати, разработанный мною скрипт работы с ами
Не поделитись ?
куском
Код: Выделить всё
#защита от запуска в пределах вебсервера
error_reporting(E_ERROR | E_PARSE);
if(isset($_SERVER['REMOTE_ADDR'])){
header('HTTP/1.0 404 Not Found');
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
die();
}
#подключение файла конфигурации
require("config.inc.php");
date_default_timezone_set('UTC');
#connect to mysql
set_time_limit(0);
ini_set('mysql.connect_timeout','0');
ini_set('max_execution_time', '0');
mysql_connect($hostname,$username,$password) or die ("Not connect to MySQL:".mysql_error());
mysql_select_db($dbName) or die("Error:".mysql_error());
mysql_query("set names 'utf8'");
print "------------------------\n";
print date("d.m.Y H:i:s")." Script started\n";
$ping_timer_restore = 10;
$ping_timer = $ping_timer_restore;
$pong_timer = $ping_timer_restore + 2;
#главный цикл
while(1) {
$socket = fsockopen($asterisk_host, $asterisk_port, $errno, $errstr);
stream_set_timeout($socket, 1);
if(!$socket) die("Error: ".$errstr."\r\n");
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $asterisk_user\r\n");
fputs($socket, "Secret: $asterisk_password\r\n\r\n");
$buffer_rx = "";
$buffer_tx = "";
$timersec = time();
while ($socket) {
$buffer_rx .= fread($socket, 1);
if(substr($buffer_rx, -4) == "\r\n\r\n") exec_from_buffer();
if(!empty($buffer_tx) && substr($buffer_tx, -1) == "\n") send_command();
//ping/pong timer
if(time() >= $timersec + 1) {
$timersec = time();
$ping_timer--;
if($ping_timer == 0) {
$ping_timer = $ping_timer_restore;
$buffer_tx = "Action: Ping\r\n\r\n";
}
$pong_timer--;
if($pong_timer == 0) {
print "Reconnect, no ping reply\n";
print "------------------------\n";
fclose($socket);
break;
}
}
}
fclose($socket);
}
#разбор пришедших пакетов
function exec_from_buffer(){
global $buffer_rx;
global $max_internal_phones_length;
global $asterisk_monitor_dir;
global $outgoing_context;
global $ping_timer_restore;
global $pong_timer;
$packet = array();
$cmd = explode("\r", $buffer_rx);
$buffer_rx = "";
foreach($cmd as $key => $val) {
if(strlen($cmd[$key]) == 1) unset($cmd[$key]);
else {
if($cmd[$key][0] == "\n") $cmd[$key] = substr($cmd[$key], 1);
$cmd[$key] = explode(":", $cmd[$key]);
$packet[$cmd[$key][0]] = trim($cmd[$key][1]);
}
}
#debug
//print_r($packet);
}
...
#send buffer to AMI
function send_command(){
global $buffer_tx;
global $socket;
//print date("d.m.Y H:i:s")." send packet: $buffer_tx\n";
fputs($socket, $buffer_tx);
$buffer_tx = '';
}
тут много лишнего для задачи, повыкидывайте