-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfs_curl.php
309 lines (279 loc) · 9.01 KB
/
fs_curl.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* @package FS_CURL
* @license BSD
* @author Raymond Chandler (intralanman) <[email protected]>
* @version 0.1
* FreeSWITCH CURL base class
* Base class for all curl XML output, contains methods for XML output and
* connecting to a database
* @return void
*/
class fs_curl
{
/**
* @var PDO
*/
public $db;
/**
* Array of _REQUEST parameters passed
* @var array
*/
public $request;
/**
* @var XMLWriter
*/
public $xmlw;
/**
* Array of comments to be output in the XML
* @see fs_curl::comment
* @var array
*/
private $comments = [];
public function __construct()
{
set_error_handler([$this, 'error_handler']);
set_exception_handler([$this, 'exception_handler']);
$this->generate_request_array();
openlog('fs_curl', LOG_NDELAY | LOG_PID, LOG_USER);
header('Content-Type: text/xml');
$this->xmlw = $this->open_xml();
$this->connect_db(DEFAULT_DSN, DEFAULT_DSN_LOGIN, DEFAULT_DSN_PASSWORD);
}
/**
* Connect to a database via FS_PDO
*
* @param mixed $dsn data source for database connection (array or string)
* @param $login
* @param $password
*/
public function connect_db($dsn, $login, $password)
{
try {
$this->db = new PDO($dsn, $login, $password);
} catch (PDOException $e) {
$this->debug($e->getMessage());
$this->file_not_found(); //program terminates in function file_not_found()
}
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$driver = $this->db->getAttribute(PDO::ATTR_DRIVER_NAME);
$this->debug("our driver is $driver");
switch ($driver) {
case 'mysql':
$quoter = '`';
break;
case 'pgsql':
if (DEFAULT_DSN_SCHEMA) {
$this->db->exec('SET SEARCH_PATH=' . DEFAULT_DSN_SCHEMA);
}
$quoter = '"';
break;
default:
$quoter = '';
break;
}
define('DB_FIELD_QUOTE', $quoter);
}
/**
* Method to add comments to XML
* Adds a comment to be displayed in the final XML
*
* @param string $comment comment string to be output in XML
*
* @return void
*/
public function comment($comment)
{
$this->comments[] = $comment;
}
/**
* Generate a globally accesible array of the _REQUEST parameters passed
* Generates an array from the _REQUEST parameters that were passed, keeping
* all key => value combinations intact
* @return void
*/
private function generate_request_array()
{
while (list($req_key, $req_val) = each($_REQUEST)) {
if (!defined('FS_CURL_DEBUG') && $req_key == 'fs_curl_debug') {
define('FS_CURL_DEBUG', $req_val);
}
//$this -> comment("$req_key => $req_val");
$this->request[$req_key] = $req_val;
}
}
/**
* Actual Instantiation of XMLWriter Object
* This method creates an XMLWriter Object and sets some needed options
*
* @return XMLWriter
*/
private function open_xml()
{
$xmlw = new XMLWriter();
$xmlw->openMemory();
if (isset($this->request['fs_curl_debug']) && $this->request['fs_curl_debug'] > 0) {
$indent = true;
} else {
$indent = false;
}
$xmlw->setIndent($indent);
$xmlw->setIndentString(' ');
$xmlw->startDocument('1.0', 'UTF-8', 'no');
$xmlw->startElement('document');
$xmlw->writeAttribute('type', 'freeswitch/xml');
return $xmlw;
}
/**
* Method to call on any error that can not be revovered from
* This method was written to return a valid XML response to FreeSWITCH
* in the event that we are unable to generate a valid configuration file
* from the passed information
* @return void
*/
public function file_not_found()
{
$not_found = $this->open_xml();
$not_found->startElement('section');
$not_found->writeAttribute('name', 'result');
$not_found->startElement('result');
$not_found->writeAttribute('status', 'not found');
$not_found->endElement();
$not_found->endElement();
/* we put the comments inside the root element so we don't
* get complaints about markup outside of it */
$this->comments2xml($not_found, $this->comments);
$not_found->endElement();
echo $not_found->outputMemory();
exit;
}
/**
* Generate XML comments from comments array
* This [recursive] method will iterate over the passed array, writing XML
* comments and calling itself in the event that the "comment" is an array
*
* @param object $xml_obj Already instantiated XMLWriter object
* @param array $comments [Multi-dementional] Array of comments to be added
* @param integer $space_pad Number of spaces to indent the comments
*
* @return void
*/
private function comments2xml($xml_obj, $comments, $space_pad = 0)
{
$comment_count = count($comments);
for ($i = 0; $i < $comment_count; $i++) {
if (array_key_exists($i, $comments)) {
if (!is_array($comments[$i])) {
$xml_obj->writeComment(" " . $comments[$i] . " ");
} else {
$this->comments2xml($xml_obj, $comments[$i], $space_pad + 2);
}
}
}
}
/**
* End open XML elments in XMLWriter object
* @return void
*/
private function close_xml()
{
$this->xmlw->endElement();
$this->xmlw->endElement();
$this->xmlw->endElement();
}
/**
* Close and Output XML and stop script execution
* @return void
*/
public function output_xml()
{
$this->comment(sprintf("Estimated Execution Time Is: %s"
, (preg_replace(
'/^0\.(\d+) (\d+)$/', '\2.\1', microtime()) - START_TIME)
));
$this->comments2xml($this->xmlw, $this->comments);
$this->close_xml();
$xml_out = $this->xmlw->outputMemory();
$this->debug('---- Start XML Output ----');
$this->debug(explode("\n", $xml_out));
$this->debug('---- End XML Output ----');
echo $xml_out;
exit();
}
/**
* Recursive method to add an array of comments
*
* @param $array
* @param int $spacepad
*/
public function comment_array($array, $spacepad = 0)
{
$spaces = str_repeat(' ', $spacepad);
foreach ($array as $key => $val) {
if (is_array($val)) {
$this->comment("$spaces$key => Array");
$this->comment_array($val, $spacepad + 2);
} else {
$this->comment("$spaces$key => $val");
}
}
}
/**
* @param $errno
* @param $errstr
* @param $errfile
* @param $errline
*
* @throws ErrorException
*/
public function error_handler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
return;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
/**
* @param Exception $e
*/
public function exception_handler(Exception $e)
{
$message = $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine();
$this->debug($message);
$this->comment($message);
$this->file_not_found();
}
/**
* Function to print out debugging info
* This method will recieve arbitrary data and send it using your method of
* choice.... enable/disable by defining FS_CURL_DEBUG to and arbitrary integer
*
* @param mixed $input what to debug, arrays and strings tested, objects MAY work
* @param integer $debug_level debug if $debug_level <= FS_CURL_DEBUG
* @param integer $spaces
*/
public function debug($input, $debug_level = -1, $spaces = 0)
{
if (defined('FS_CURL_DEBUG') && $debug_level <= FS_CURL_DEBUG) {
if (is_array($input)) {
$input = print_r($input, true);
}
$debug_str = sprintf("%s%s", str_repeat(' ', $spaces), $input);
switch (FS_DEBUG_TYPE) {
case 0:
syslog(LOG_NOTICE, $debug_str);
break;
case 1:
$debug_str = preg_replace('/--/', '- - ', $debug_str);
$this->comment($debug_str);
break;
case 2:
file_put_contents(FS_DEBUG_FILE, "$debug_str\n", FILE_APPEND);
break;
default:
return;
}
}
}
}