1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inok\phpagi; |
4
|
|
|
|
5
|
|
|
class AGI_Others |
6
|
|
|
{ |
7
|
|
|
const AST_CONFIG_DIR = '/etc/asterisk/'; |
8
|
|
|
const AST_SPOOL_DIR = '/var/spool/asterisk/'; |
9
|
|
|
const AST_TMP_DIR = self::AST_SPOOL_DIR . '/tmp/'; |
10
|
|
|
const DEFAULT_PHPAGI_CONFIG = self::AST_CONFIG_DIR . '/phpagi.conf'; |
11
|
|
|
|
12
|
|
|
const AST_DIGIT_ANY = '0123456789#*'; |
13
|
|
|
|
14
|
|
|
const AGIRES_OK = 200; |
15
|
|
|
|
16
|
|
|
const AST_STATE_DOWN = 0; |
17
|
|
|
const AST_STATE_RESERVED = 1; |
18
|
|
|
const AST_STATE_OFFHOOK = 2; |
19
|
|
|
const AST_STATE_DIALING = 3; |
20
|
|
|
const AST_STATE_RING = 4; |
21
|
|
|
const AST_STATE_RINGING = 5; |
22
|
|
|
const AST_STATE_UP = 6; |
23
|
|
|
const AST_STATE_BUSY = 7; |
24
|
|
|
const AST_STATE_DIALING_OFFHOOK = 8; |
25
|
|
|
const AST_STATE_PRERING = 9; |
26
|
|
|
|
27
|
|
|
const AUDIO_FILENO = 3; // STDERR_FILENO + 1 |
28
|
|
|
|
29
|
|
|
public static $phpagi_error_handler_email = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* error handler for phpagi. |
33
|
|
|
* |
34
|
|
|
* @param integer $level PHP error level |
35
|
|
|
* @param string $message error message |
36
|
|
|
* @param string $file path to file |
37
|
|
|
* @param integer $line line number of error |
38
|
|
|
* @param array $context variables in the current scope |
39
|
|
|
*/ |
40
|
|
|
public static function phpagi_error_handler(int $level, string $message, string $file, int $line, array $context) { |
41
|
|
|
if (ini_get('error_reporting') == 0) { |
42
|
|
|
return; // this happens with an @ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
@syslog(LOG_WARNING, $file . '[' . $line . ']: ' . $message); |
|
|
|
|
46
|
|
|
|
47
|
|
|
if (function_exists('mail') && !is_null(self::$phpagi_error_handler_email)) { // generate email debugging information |
48
|
|
|
// decode error level |
49
|
|
|
switch ($level) { |
50
|
|
|
case E_WARNING: |
51
|
|
|
case E_USER_WARNING: |
52
|
|
|
$level = "Warning"; |
53
|
|
|
break; |
54
|
|
|
case E_NOTICE: |
55
|
|
|
case E_USER_NOTICE: |
56
|
|
|
$level = "Notice"; |
57
|
|
|
break; |
58
|
|
|
case E_USER_ERROR: |
59
|
|
|
$level = "Error"; |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// build message |
64
|
|
|
$basefile = basename($file); |
65
|
|
|
$subject = "$basefile/$line/$level: $message"; |
66
|
|
|
$message = "$level: $message in $file on line $line\n\n"; |
67
|
|
|
|
68
|
|
|
// figure out who we are |
69
|
|
|
if (function_exists('socket_create')) { |
70
|
|
|
$addr = null; |
71
|
|
|
$port = 80; |
72
|
|
|
$socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
73
|
|
|
@socket_connect($socket, '64.0.0.0', $port); |
|
|
|
|
74
|
|
|
@socket_getsockname($socket, $addr, $port); |
|
|
|
|
75
|
|
|
@socket_close($socket); |
|
|
|
|
76
|
|
|
$message .= "\n\nIP Address: $addr\n"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// include variables |
80
|
|
|
$message .= "\n\nContext:\n" . print_r($context, true); |
|
|
|
|
81
|
|
|
$message .= "\n\nGLOBALS:\n" . print_r($GLOBALS, true); |
|
|
|
|
82
|
|
|
$message .= "\n\nBacktrace:\n" . print_r(debug_backtrace(), true); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// include code fragment |
85
|
|
|
if (file_exists($file)) { |
86
|
|
|
$message .= "\n\n$file:\n"; |
87
|
|
|
$code = @file($file); |
88
|
|
|
for ($i = max(0, $line - 10); $i < min($line + 10, count($code)); $i++) { |
|
|
|
|
89
|
|
|
$message .= ($i + 1) . "\t$code[$i]"; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// make sure message is fully readable (convert unprintable chars to hex representation) |
94
|
|
|
$ret = ''; |
95
|
|
|
for ($i = 0; $i < strlen($message); $i++) { |
96
|
|
|
$c = ord($message[$i]); |
97
|
|
|
if ($c == 10 || $c == 13 || $c == 9) { |
98
|
|
|
$ret .= $message[$i]; |
99
|
|
|
} elseif ($c < 16) { |
100
|
|
|
$ret .= '\x0' . dechex($c); |
101
|
|
|
} elseif ($c < 32 || $c > 127) { |
102
|
|
|
$ret .= '\x' . dechex($c); |
103
|
|
|
} else { |
104
|
|
|
$ret .= $message[$i]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$message = $ret; |
108
|
|
|
|
109
|
|
|
// send the mail if less than 5 errors |
110
|
|
|
static $mailcount = 0; |
111
|
|
|
if ($mailcount < 5) { |
112
|
|
|
@mail(self::$phpagi_error_handler_email, $subject, $message); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
$mailcount++; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: