#!/usr/bin/php -q '; $parm_extension = 18; // ---------------------------------------------------- // Which application to run when the call is connected. $parm_application = 'Playback'; $parm_data = 'bunny_troubles'; GLOBAL $stdin, $stdout, $stdlog, $result, $parm_debug_on, $parm_test_mode; // These setting are on the WIKI pages http://www.voip-info.org ob_implicit_flush(false); set_time_limit(30); error_reporting(0); $stdin = fopen( 'php://stdin', 'r' ); $stdout = fopen( 'php://stdout', 'w' ); // You will see a whole bunch of this its for development or if you change anything and // want to write to a log file in the TMP directory if ($parm_debug_on) { $stdlog = fopen( $parm_error_log, 'w' ); fputs( $stdlog, "---Start---\n" ); } // ASTERISK * Sends in a bunch of variables, This accepts them and puts them in an array // http://www.voip-info.org/tiki-index.php?page=Asterisk%20AGI%20php while ( !feof($stdin) ) { $temp = fgets( $stdin ); if ($parm_debug_on) fputs( $stdlog, $temp ); // Strip off any new-line characters $temp = str_replace( "\n", "", $temp ); $s = explode( ":", $temp ); $agivar[$s[0]] = trim( $s[1] ); if ( ( $temp == "") || ($temp == "\n") ) { break; } } $rc = execute_agi( "ANSWER "); sleep(1); // Wait for the channel to get created and RTP packets to be sent // On my system the welcome you would only hear 'elcome' So I paused for 1 second if ( !$rc['result'] ) $rc = execute_agi( "STREAM FILE welcome \"\" "); $rc = execute_agi( "STREAM FILE goodbye \"\" "); //if ( !$rc['result'] ) // $rc = execute_agi( "HANGUP"); switch ($agivar['agi_extension']) { case 63: $parm_wakeupcallerid = '"OVF ALARM 1" <63>'; $parm_data = 'alarm1'; break; case 66: $parm_wakeupcallerid = '"OVF ALARM 2" <66>'; $parm_data = 'alarm2'; break; case 69: $parm_wakeupcallerid = '"OVF ALARM 3" <69>'; $parm_data = 'alarm3'; break; case 93: $parm_wakeupcallerid = '"OVF ALARM 4" <93>'; break; } $time_now = time(); $wtime = date('Hi', $time_now); fputs($stdlog, $wtime."\n"); $wakefile = "$parm_temp_dir/$wtime.ext.$parm_extension.call"; $callfile = "$parm_call_dir/$wtime.ext.$parm_extension.call"; fputs($stdlog, $wakefile."\n"); $ftmp = fopen($wakefile, 'w'); if ($ftmp) { fputs ($ftmp, "channel: Local/$parm_extension@$agivar[agi_context]\n"); fputs ($ftmp, "maxretries: $parm_maxretries\n"); fputs ($ftmp, "retrytime: $parm_retrytime\n"); fputs ($ftmp, "waittime: $parm_waittime\n"); fputs ($ftmp, "callerid: $parm_wakeupcallerid\n"); fputs ($ftmp, "application: $parm_application\n"); fputs ($ftmp, "data: $parm_data\n"); fclose($ftmp); rename($wakefile, $callfile); //touch($callfile, time()+60, time()+60); touch($callfile); } fputs( $stdlog, "Done\n" ); } //-------------------------------------------------- // This function will send out the command and get // the response back //-------------------------------------------------- function execute_agi( $command ) { GLOBAL $stdin, $stdout, $stdlog, $parm_debug_on; fputs( $stdout, $command . "\n" ); fflush( $stdout ); if ($parm_debug_on) fputs( $stdlog, $command . "\n" ); $resp = fgets( $stdin, 4096 ); if ($parm_debug_on) fputs( $stdlog, " READ=$resp" ); if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) ) { if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match)) { $arr['code'] = $matches[1]; $arr['result'] = $match[1]; if (isset($match[3]) && $match[3]) $arr['data'] = $match[3]; if ( trim( $match[2] ) == '(timeout)' ) { //$arr['result'] = -1; $arr['timeout'] = TRUE; } if ($parm_debug_on) fputs( $stdlog, "1=$match[1], 2=$match[2], 3=$match[3], 4=$match[4]\n" ); return $arr; } else { if ($parm_debug_on) fputs( $stdlog, "Couldn't figure out returned string, Returning code=$matches[1] result=0\n" ); $arr['code'] = $matches[1]; $arr['result'] = 0; return $arr; } } else { if ($parm_debug_on) fputs( $stdlog, "Could not process string, Returning -1\n" ); $arr['code'] = -1; $arr['result'] = -1; return $arr; } } ?>