-
Notifications
You must be signed in to change notification settings - Fork 18
/
fs_curl.php
379 lines (357 loc) · 10.7 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* @package FS_CURL
* @subpackage FS_CURL_Configuration
* fs_curl.php
*/
if ( basename( $_SERVER['PHP_SELF'] ) == basename( __FILE__ ) ) {
header( 'Location: index.php' );
}
/**
* @package FS_CURL
* @license BSD
* @author Raymond Chandler (intralanman) <intralanman@gmail.com>
* @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 {
/**
* FS_PDO Object
* @link http://www.php.net/pdo
* @var $db FS_PDO
*/
public $db;
/**
* Array of _REQUEST parameters passed
*
* @var array
*/
public $request;
/**
* XMLWriter object
* @link http://php.net/XMLWriter
* @var object
*/
public $xmlw;
/**
* Array of comments to be output in the XML
* @see fs_curl::comment
* @var array
*/
private $comments;
/**
* Instantiation of XMLWriter and FS_PDO
* This method will instantiate the FS_PDO and XMLWriter classes for use
* in child classes
* @return void
*/
public function fs_curl()
{
self::__construct();
}
public function __construct() {
//public function fs_curl() {
openlog( 'fs_curl', LOG_NDELAY | LOG_PID, LOG_USER );
header( 'Content-Type: text/xml' );
$this->generate_request_array();
$this->open_xml();
$inc
= array( 'required' => 'libs/fs_pdo.php' ); // include an external file. i.e. 'required'=>'important_file.php'
$this->include_files( $inc );
$this->connect_db( DEFAULT_DSN, DEFAULT_DSN_LOGIN, DEFAULT_DSN_PASSWORD );
set_error_handler( array( $this, 'error_handler' ) );
//trigger_error('blah', E_USER_ERROR);
}
/**
* Connect to a database via FS_PDO
*
* @param mixed $dsn data source for database connection (array or string)
*
* @return void
*/
public function connect_db( $dsn, $login, $password ) {
try {
$options = array(
);
$this->db = new FS_PDO( $dsn, $login, $password, $options );
}
catch ( Exception $e ) {
$this->comment( $e->getMessage() );
$this->file_not_found(); //program terminates in function file_not_found()
}
$driver = $this->db->getAttribute( constant( "PDO::ATTR_DRIVER_NAME" ) );
$this->debug( "our driver is $driver" );
switch ( $driver ) {
case 'mysql':
$quoter = '`';
break;
case 'pgsql':
$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 void
*/
private function open_xml() {
$this->xmlw = new XMLWriter();
$this->xmlw->openMemory();
if ( array_key_exists( 'fs_curl_debug', $this->request )
&& $this->request['fs_curl_debug'] > 0
) {
$this->xmlw->setIndent( TRUE );
$this->xmlw->setIndentString( ' ' );
} else {
$this->xmlw->setIndent( FALSE );
$this->xmlw->setIndentString( ' ' );
}
$this->xmlw->startDocument( '1.0', 'UTF-8', 'no' );
//set the freeswitch document type
$this->xmlw->startElement( 'document' );
$this->xmlw->writeAttribute( 'type', 'freeswitch/xml' );
}
/**
* 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() {
$this->comment( 'Include Path = ' . ini_get( 'include_path' ) );
$not_found = new XMLWriter();
$not_found->openMemory();
$not_found->setIndent( TRUE );
$not_found->setIndentString( ' ' );
$not_found->startDocument( '1.0', 'UTF-8', 'no' );
//set the freeswitch document type
$not_found->startElement( 'document' );
$not_found->writeAttribute( 'type', 'freeswitch/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( 'Total # of Queries Run: %d', $this->db->counter )
);
$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
* @return void
*/
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" );
}
}
}
/**
* Include files for child classes
* This method will include the files needed by child classes.
* Expects an associative array of type => path
* where type = [required|any other string]
*
* @param array $file_array associative array of files to include
*
* @return void
* @todo add other types for different levels of errors
*/
public function include_files( $file_array ) {
$return = FS_CURL_SUCCESS;
while ( list( $type, $file ) = each( $file_array ) ) {
$inc = @include_once( $file );
if ( ! $inc ) {
$comment = sprintf(
'Unable To Include %s File %s', $type, $file
);
$this->comment( $comment );
if ( $type == 'required' ) {
$return = FS_CURL_CRITICAL;
} else {
if ( $return != FS_CURL_CRITICAL ) {
$return = FS_CURL_WARNING;
}
}
}
}
if ( $return == FS_CURL_CRITICAL ) {
$this->file_not_found();
}
return $return;
}
/**
* Class-wide error handler
* This method should be called whenever there is an error in any child
* classes, script execution and returning is pariatlly determined by
* defines
* @see RETURN_ON_WARN
* @return void
* @todo add other defines that control what, if any, comments gets output
*/
public function error_handler( $no, $str, $file, $line ) {
if ( $no == E_STRICT ) {
return TRUE;
}
$file = preg_replace( '/\.(inc|php)$/', '', $file );
$this->comment( basename( $file ) . ":$line - $no:$str" );
switch ( $no ) {
case E_USER_NOTICE:
case E_NOTICE:
break;
case E_USER_WARNING:
case E_WARNING:
if ( defined( 'RETURN_ON_WARN' ) && RETURN_ON_WARN == TRUE ) {
break;
}
case E_ERROR:
case E_USER_ERROR:
default:
$this->file_not_found();
}
return TRUE;
}
/**
* 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 ) ) {
$this->debug( 'Array (', $debug_level, $spaces );
foreach ( $input as $key => $val ) {
if ( is_array( $val ) || is_object( $val ) ) {
$this->debug( "[$key] => $val", $debug_level, $spaces + 4 );
$this->debug( '(', $debug_level, $spaces + 8 );
$this->debug( $val, $debug_level, $spaces + 8 );
} else {
$this->debug( "[$key] => '$val'", $debug_level, $spaces + 4 );
}
}
$this->debug( ")", $debug_level, $spaces );
} else {
$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:
$ptr = fopen( FS_DEBUG_FILE, 'a' );
fputs( $ptr, "$debug_str\r\n" );
fclose( $ptr );
break;
default:
return;
}
}
}
}
}