forked from jmcguirl/dynroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.agi
executable file
·99 lines (77 loc) · 2.25 KB
/
test.agi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/php -q
<?php
// Copyright (c) 2015-2017 John Fawcett
// This is a dervied work licenced under GPL V3 or later
GLOBAL $stdin, $stdout, $stdlog, $parm_debug_on;
ob_implicit_flush(false);
set_time_limit(30);
error_reporting(0);
$parm_error_log = '/tmp/error.log';
// Set to 1 to turn on the log file
$parm_debug_on = 1;
if ($parm_debug_on)
{
$stdlog = fopen( $parm_error_log, 'w' );
fputs( $stdlog, "---Start---\n" );
}
$stdin = fopen( 'php://stdin', 'r' );
$stdout = fopen( 'php://stdout', 'w' );
while ( !feof($stdin) )
{
$temp = fgets( $stdin );
// 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;
}
}
$result=$argv[1]+$argv[2];
execute_agi('SET VARIABLE dynroute '.$result);
//execute_agi('SET VARIABLE xxxx '.$result);
// where xxxx is the AGI result variable defined in dynroute
exit;
//--------------------------------------------------
// 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, $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];
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;
}
}