[Freeswitch-svn] [commit] r12039 - in freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl: . libs
FreeSWITCH SVN
mphill at freeswitch.org
Sun Feb 15 19:40:38 PST 2009
Author: mphill
Date: Sun Feb 15 21:40:38 2009
New Revision: 12039
Log:
New dialplan importer uses new schema. Now uses PDO instead of MDB2.
fs_dialpan.php now uses the new schema.
Modified:
freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importer.php
freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialplan.php
freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.php
freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/libs/fs_pdo.php
Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importer.php
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importer.php (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importer.php Sun Feb 15 21:40:38 2009
@@ -8,9 +8,12 @@
*/
/**
- * include MDB2 PEAR Extension
+ * Switched to simple xml, pdo and new schemea. Added several new supporint functions
+ * @author Michael Phillips
*/
-require_once('MDB2.php');
+
+require_once('libs/fs_pdo.php');
+
/**
* require global definitions for FS_CURL
*/
@@ -23,10 +26,11 @@
*/
function upload_form() {
echo '<html>';
- echo '<h1>Select A File To Import</h1>';
+ 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>';
}
@@ -41,7 +45,7 @@
function run_query($db, $query) {
syslog(LOG_INFO, $query);
$affected = $db -> exec($query);
- if (MDB2::isError($affected)) {
+ if (FS_PDO::isError($affected)) {
if (!defined('UNSUCCESSFUL_QUERY')) {
define('UNSUCCESSFUL_QUERY', true);
}
@@ -95,52 +99,109 @@
// 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);
+ //move_uploaded_file($tmp_file, $xml_file);
+ //is_uploaded_file
+ //echo filesize($xml_file);
//echo $xml_file . "\n<br>";
- $xml_str = sprintf('%s', file_get_contents($xml_file));
- //echo $xml_str;
+ $xml_str = file_get_contents($xml_file);
}
-$db = MDB2::connect(DEFAULT_DSN);
-if (MDB2::isError($db)) {
- echo $db -> getMessage() . "\n";
-}
-
-$xml_obj = new SimpleXMLElement($xml_str);
-foreach ($xml_obj -> context as $context) {
- $cn = $context['name'];
- $global_weight = 100;
- foreach ($context -> extension as $extension) {
- $en = $extension['name'];
- //printf("<pre>%s</pre>", print_r($extension, true));
- $ec = $extension['continue'];
- $global_weight+=100;
- foreach ($extension -> condition as $condition) {
- //print_r($condition);
- $cf = $condition['field'];
- $ce = addslashes($condition['expression']);
- //echo "<pre>Condidtion Expression for $en:\n before: " . $condition['expression'] . "\n after: $ce</pre>";
- $cb = $condition['break'];
- $weight = 0;
- foreach ($condition as $type => $action) {
- //echo "-------------------$type-----------------------------\n";
- $app_name = $action['application'];
- $app_data = addslashes($action['data']);
- $weight++;
- //echo "$cn\t$en\t$cf\t$ce\t$cc\t$app_name\t$app_data\t$ec\t$global_weight\t$weight\n";
- $query = sprintf('%s %s %s %s %s %s;',
- "INSERT INTO dialplan SET",
- "context='$cn', extension='$en', condition_field='$cf',",
- "condition_expression='$ce', application_name='$app_name',",
- "application_data='$app_data', weight='$weight', type='$type',",
- "ext_continue='$ec', global_weight='$global_weight',",
- "cond_break='$cb'"
- );
- run_query($db, $query);
- }
- }
- }
+
+try {
+ $db = new FS_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->queryAll($sql);
+ 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 {
@@ -149,4 +210,4 @@
upload_form();
//printf("<pre>%s</pre>", print_r($xml_obj, true);
-
+?>
\ No newline at end of file
Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialplan.php
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialplan.php (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialplan.php Sun Feb 15 21:40:38 2009
@@ -83,10 +83,23 @@
*/
private function get_dialplan($context) {
$dp_array = array();
- $dpQuery = sprintf("%s %s;"
- , "SELECT * FROM dialplan WHERE context='$context' ORDER BY"
- , "global_weight, context, extension, weight"
- );
+ $dpQuery = sprintf("SELECT
+ `context`,
+ `name` as extension,
+ `application` as application_name,
+ `data` as application_data,
+ `field` as condition_field,
+ `expression` as condition_expression,
+ `continue` as ext_continue,
+ `type`
+ FROM dialplan
+ INNER JOIN dialplan_context USING(dialplan_id)
+ INNER JOIN dialplan_extension USING(context_id)
+ INNER JOIN dialplan_condition USING(extension_id)
+ INNER JOIN dialplan_actions USING(condition_id)
+ WHERE context = '%s'
+ ORDER BY dialplan_context.weight, dialplan_extension.weight, dialplan_condition.weight, dialplan_actions.weight", $context);
+
$res = $this -> db -> query($dpQuery);
if (FS_PDO::isError($res)) {
$this -> comment($this -> db -> getMessage());
@@ -99,11 +112,11 @@
$ec = $row['ext_continue'];
$app = $row['application_name'];
$data = $row['application_data'];
- $app_cdata = $row['app_cdata'];
+ //$app_cdata = $row['app_cdata'];
$type = $row['type'];
$cf = $row['condition_field'];
$ce = $row['condition_expression'];
- $rcd = $row['re_cdata'];
+ //$rcd = $row['re_cdata'];
$cc = empty($row['cond_break']) ? '0' : $row['cond_break'];
$dp_array[$ct]["$et;$ec"]["$cf;$ce;$cc;$rcd"][] = array(
'type'=>$type,
Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.php
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.php (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.php Sun Feb 15 21:40:38 2009
@@ -17,11 +17,11 @@
/**
* Defines the default dsn login for the PDO class
*/
-define('DEFAULT_DSN_LOGIN', 'freeswitch');
+define('DEFAULT_DSN_LOGIN', 'root');
/**
* Defines the default dsn password for the PDOclass
*/
-define('DEFAULT_DSN_PASSWORD', 'fr33sw1tch');
+define('DEFAULT_DSN_PASSWORD', 'aasml2009');
/**
* Generic return success
*/
@@ -74,4 +74,4 @@
//define('', '');
-
+?>
\ No newline at end of file
Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/libs/fs_pdo.php
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/libs/fs_pdo.php (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/libs/fs_pdo.php Sun Feb 15 21:40:38 2009
@@ -105,5 +105,5 @@
return $this->rowCount();
}
-}
-?>
+}
+?>
\ No newline at end of file
More information about the Freeswitch-svn
mailing list