-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialplan_importer.php
216 lines (188 loc) · 7.04 KB
/
dialplan_importer.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
<?php
/**
* @package FS_CURL
* @subpackage FS_CURL_Dialplan
* @license
* @author Raymond Chandler (intralanman) <intralanman@gmail.com>
* @version 0.1
*/
/**
* Switched to simple xml, pdo and new schemea. Added several new supporint functions
* @author Michael Phillips
*/
require_once('libs/fs_pdo.php');
/**
* require global definitions for FS_CURL
*/
require_once('global_defines.php');
/**
* Output the upload form
* echo out the HTML for the upload form.
* @return null
*/
function upload_form()
{
echo '<html>';
echo '<h2>Select A File To Import</h2>';
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data">';
echo '<input type="file" name="file">';
echo '<input type="submit" name="confirmed">';
echo '<p><input type="checkbox" name="clear_dialplan" value="true"> Clear all dialplan data before insert?</p>';
echo '</form>';
echo '</html>';
}
/**
* Perform Insert Query
* take MDB2 object and query as params and
* perform query, setting error flag in the event
* of a db error.
* @return null
*/
function run_query($db, $query)
{
syslog(LOG_INFO, $query);
$db->exec($query);
}
/**
* Check uploaded file for obvious problems
* This function checks the uploaded file's
* size, type, length, etc to make sure it's
* worth continuing with processing
* @return bool
*/
function check_uploaded_file($file_array)
{
if (!is_uploaded_file($file_array['tmp_name'])) {
echo "File NOT uploaded OK<br>";
die(upload_form());
} elseif ($file_array['size'] < 1) {
echo "File was empty";
die(upload_form());
} elseif ($file_array['error'] > 0) {
echo "Uploading file encountered error #" . $file_array['error'];
die(upload_form());
} elseif ($file_array['type'] != 'text/xml') {
echo "Expected file of type 'text/xml', but got " . $file_array['type'];
die(upload_form());
} else {
//echo "File seems uploaded OK<br>";
return true;
}
}
if (!array_key_exists('confirmed', $_REQUEST)) {
die(upload_form());
}
/*
foreach ($_REQUEST as $key => $val) {
echo "$key => $val <br>\n";
}
if (is_array($_FILES) && count($_FILES)>0) {
echo "<h2>FILES is an array</h2>";
print_r($_FILES);
}
*/
// no need to do anything till we check that the file's ok
if (check_uploaded_file($_FILES['file'])) {
$xml_file = $_FILES['file']['tmp_name'];
//move_uploaded_file($tmp_file, $xml_file);
//is_uploaded_file
//echo filesize($xml_file);
//echo $xml_file . "\n<br>";
$xml_str = file_get_contents($xml_file);
}
try {
$db = new PDO(DEFAULT_DSN, DEFAULT_DSN_LOGIN, DEFAULT_DSN_PASSWORD);
} catch (Exception $e) {
die($e->getMessage());
}
if ($_POST['clear_dialplan']) {
truncate_dialplan();
}
echo "<pre>";
$xml = simplexml_load_string($xml_str);
$num_of_conext = sizeof($xml->context);
$dialplan_id = insert_dialplan();
foreach ($xml->children() as $context => $context_children) {
//echo $context . " => " . $context_children->attributes()->name . "\n";
$context_id = insert_dialplan_context($dialplan_id, $context_children->attributes()->name);
foreach ($context_children as $extension => $extension_children) {
//echo "\t" . $extension . " => name: " . $extension_children->attributes()->name . " continue: " . $extension_children->attributes()->continue . "\n" ;
if ($extension == 'extension') { //verify again bad input
$extension_id = insert_dialplan_extension($context_id, $extension_children->attributes()->name, $extension_children->attributes()->continue);
}
foreach ($extension_children as $condition => $condition_children) {
//echo "\t\t" . $condition . " => " . $condition_children->attributes()->field . ", expression; " .$condition_children->attributes()->expression . "\n";
$condition_id = insert_dialplan_condition($extension_id, $condition_children->attributes()->field, $condition_children->attributes()->expression);
foreach ($condition_children as $action => $action_childress) {
//echo "\t\t\t" . $action . " => " . $action_childress->attributes()->application . ", expression; " .$action_childress->attributes()->data . "\n";
if ($action == ('action' || 'anti-action')) { //verify again bad input
insert_dialplan_actions($condition_id, $action_childress->attributes()->application, $action_childress->attributes()->data, $action);
} else {
echo "bad xml $action";
}// end if
} //end foreach
} //end foreach
}// end foreach
} // end foreach
echo "</pre>";
function insert_dialplan($domain = 'freeswitch', $ip_address = '127.0.0.1')
{
global $db;
$sql = sprintf("INSERT INTO dialplan (`domain`, `ip_address`) VALUES ('%s', '%s')", $domain, $ip_address);
$db->query($sql);
return $db->lastInsertId();
}
function insert_dialplan_context($dialplan_id, $context)
{
global $db;
$sql = sprintf("INSERT INTO dialplan_context (`dialplan_id`, `context`, `weight`) VALUES (%d, '%s', %d)", $dialplan_id, $context, get_next_weight('context'));
$db->query($sql);
return $db->lastInsertId();
}
function insert_dialplan_extension($context_id, $name, $continue)
{
global $db;
$sql = sprintf("INSERT INTO dialplan_extension (`context_id`, `name`, `continue`, `weight`) VALUES (%d, '%s', '%s', %d)", $context_id, $name, $continue, get_next_weight('extension'));
get_next_weight('extension');
$db->query($sql);
return $db->lastInsertId();
}
function insert_dialplan_condition($extension_id, $field, $expression)
{
global $db;
$sql = sprintf("INSERT INTO dialplan_condition (`extension_id`, `field`, `expression`, `weight`) VALUES (%d, '%s', '%s', %d)", $extension_id, addslashes($field), addslashes($expression), get_next_weight('condition'));
//echo $sql . "\n";
$db->query($sql);
return $db->lastInsertId();
}
function insert_dialplan_actions($condition_id, $application, $data, $type)
{
global $db;
$sql = sprintf("INSERT INTO dialplan_actions(`condition_id`, `application`, `data`, `type`, `weight`) VALUES (%d, '%s', '%s', '%s', %d)", $condition_id, addslashes($application), addslashes($data), $type, get_next_weight('actions'));
$db->query($sql);
return $db->lastInsertId();
}
function get_next_weight($table)
{ //used for weighting system
global $db;
$sql = sprintf("SELECT MAX(weight) AS max FROM dialplan_%s", $table);
$res = $db->query($sql);
$res = $res->fetchAll();
return ($res[0]['max'] + 10);
}
function truncate_dialplan()
{
global $db;
$db->query('TRUNCATE dialplan_extension');
$db->query('TRUNCATE dialplan');
$db->query('TRUNCATE dialplan_context');
$db->query('TRUNCATE dialplan_condition');
$db->query('TRUNCATE dialplan_actions');
}
if (defined(UNSUCCESSFUL_QUERY) && UNSUCCESSFUL_QUERY == true) {
echo "<h2>Some Queries Were Not Successful</h2>";
} else {
echo "<h2>File Successfully Imported</h2>";
}
upload_form();
//printf("<pre>%s</pre>", print_r($xml_obj, true);