From mphill@freeswitch.org Sun Feb 15 19:40:39 2009 From: FreeSWITCH SVN To: freeswitch-svn@lists.freeswitch.org Subject: [Freeswitch-svn] [commit] r12039 - in freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl: . libs Date: Sun, 15 Feb 2009 21:40:38 -0600 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1789576677136682835==" --===============1789576677136682835== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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.=20 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_i= mporter.php =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importe= r.php (original) +++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplan_importe= r.php Sun Feb 15 21:40:38 2009 @@ -8,9 +8,12 @@ */ =20 /** - * 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 ''; - echo '

Select A File To Import

'; + echo '

Select A File To Import

'; echo '
'; echo ''; echo ''; + echo '

C= lear all dialplan data before insert?

'; echo '
'; echo ''; } @@ -41,7 +45,7 @@ function run_query($db, $query) { syslog(LOG_INFO, $query); $affected =3D $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 =3D $_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
"; - $xml_str =3D sprintf('%s', file_get_contents($xml_file)); - //echo $xml_str; + $xml_str =3D file_get_contents($xml_file); } =20 -$db =3D MDB2::connect(DEFAULT_DSN); -if (MDB2::isError($db)) { - echo $db -> getMessage() . "\n"; -} - -$xml_obj =3D new SimpleXMLElement($xml_str); -foreach ($xml_obj -> context as $context) { - $cn =3D $context['name']; - $global_weight =3D 100; - foreach ($context -> extension as $extension) { - $en =3D $extension['name']; - //printf("
%s
", print_r($extension, true)); - $ec =3D $extension['continue']; - $global_weight+=3D100; - foreach ($extension -> condition as $condition) { - //print_r($condition); - $cf =3D $condition['field']; - $ce =3D addslashes($condition['expression']); - //echo "
Condidtion Expression for $en:\n    before: " . $co=
ndition['expression'] . "\n    after: $ce
"; - $cb =3D $condition['break']; - $weight =3D 0; - foreach ($condition as $type =3D> $action) { - //echo "-------------------$type----------------------------= -\n"; - $app_name =3D $action['application']; - $app_data =3D 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 =3D sprintf('%s %s %s %s %s %s;', - "INSERT INTO dialplan SET", - "context=3D'$cn', extension=3D'$en', condition_field=3D'$cf'= ,", - "condition_expression=3D'$ce', application_name=3D'$app_name= ',", - "application_data=3D'$app_data', weight=3D'$weight', type=3D= '$type',", - "ext_continue=3D'$ec', global_weight=3D'$global_weight',", - "cond_break=3D'$cb'" - ); - run_query($db, $query); - } - } - } + +try { + $db =3D new FS_PDO(DEFAULT_DSN,DEFAULT_DSN_LOGIN, DEFAULT_DSN_PASSWORD); +} catch(Exception $e) { + die($e->getMessage()); +} + +if($_POST['clear_dialplan']) { + truncate_dialplan(); +} + +echo "
";
+$xml =3D simplexml_load_string($xml_str);
+$num_of_conext =3D sizeof($xml->context);
+
+
+$dialplan_id =3D insert_dialplan();
+foreach($xml->children() as $context =3D> $context_children) {
+	//echo $context . " =3D> " . $context_children->attributes()->name . "\n";
+	$context_id =3D insert_dialplan_context($dialplan_id, $context_children->at=
tributes()->name);
+
+	foreach($context_children as $extension =3D> $extension_children) {
+		//echo "\t" . $extension . " =3D> name: " . $extension_children->attribute=
s()->name . "  continue: " . $extension_children->attributes()->continue . "\=
n" ;
+		if($extension =3D=3D 'extension') { //verify again bad input
+			$extension_id =3D insert_dialplan_extension($context_id, $extension_child=
ren->attributes()->name, $extension_children->attributes()->continue);
+		}
+		foreach($extension_children as $condition =3D> $condition_children) {
+			//echo "\t\t" . $condition . " =3D> " . $condition_children->attributes()=
->field . ", expression; " .$condition_children->attributes()->expression . "=
\n";
+			$condition_id =3D insert_dialplan_condition($extension_id, $condition_chi=
ldren->attributes()->field, $condition_children->attributes()->expression);
+			foreach($condition_children as $action =3D> $action_childress) {
+				//echo "\t\t\t" . $action . " =3D> " . $action_childress->attributes()->=
application . ", expression; " .$action_childress->attributes()->data . "\n";
+				if($action =3D=3D ('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 "
"; + +function insert_dialplan($domain =3D 'freeswitch' , $ip_address =3D '127.0.0= .1') { + global $db; + $sql =3D 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 =3D sprintf("INSERT INTO dialplan_context (`dialplan_id`, `context`, `= weight`) VALUES (%d, '%s', %d)", $dialplan_id, $context, get_next_weight('con= text')); + $db->query($sql); + return $db->lastInsertId() ; +} + +function insert_dialplan_extension($context_id, $name, $continue) { + global $db; + $sql =3D sprintf("INSERT INTO dialplan_extension (`context_id`, `name`, `co= ntinue`, `weight`) VALUES (%d, '%s', '%s', %d)", $context_id, $name, $continu= e, 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 =3D sprintf("INSERT INTO dialplan_condition (`extension_id`, `field`, = `expression`, `weight`) VALUES (%d, '%s', '%s', %d)", $extension_id, addslash= es($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 =3D sprintf("INSERT INTO dialplan_actions(`condition_id`, `application= `, `data`, `type`, `weight`) VALUES (%d, '%s', '%s', '%s', %d)", $condition_i= d, addslashes($application), addslashes($data), $type, get_next_weight('actio= ns')); + $db->query($sql); + return $db->lastInsertId() ; +} + +function get_next_weight($table) { //used for weighting system + global $db; + $sql =3D sprintf("SELECT MAX(weight) as max FROM dialplan_%s", $table); + $res =3D $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 =3D=3D true) { echo "

Some Queries Were Not Successful

"; } else { @@ -149,4 +210,4 @@ upload_form(); =20 //printf("
%s
", print_r($xml_obj, true); - +?> \ No newline at end of file Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialpla= n.php =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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 =3D array(); - $dpQuery =3D sprintf("%s %s;" - , "SELECT * FROM dialplan WHERE context=3D'$context' ORDER BY" - , "global_weight, context, extension, weight" - ); + $dpQuery =3D 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 =3D '%s' + ORDER BY dialplan_context.weight, dialplan_extension.weight, dialplan_cond= ition.weight, dialplan_actions.weight", $context); + $res =3D $this -> db -> query($dpQuery); if (FS_PDO::isError($res)) { $this -> comment($this -> db -> getMessage()); @@ -99,11 +112,11 @@ $ec =3D $row['ext_continue']; $app =3D $row['application_name']; $data =3D $row['application_data']; - $app_cdata =3D $row['app_cdata']; + //$app_cdata =3D $row['app_cdata']; $type =3D $row['type']; $cf =3D $row['condition_field']; $ce =3D $row['condition_expression']; - $rcd =3D $row['re_cdata']; + //$rcd =3D $row['re_cdata']; $cc =3D empty($row['cond_break']) ? '0' : $row['cond_break']; $dp_array[$ct]["$et;$ec"]["$cf;$ce;$cc;$rcd"][] =3D array( 'type'=3D>$type, Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_def= ines.php =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.p= hp (original) +++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/global_defines.p= hp 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 @@ =20 =20 //define('', ''); - +?> \ No newline at end of file Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/libs/fs_pd= o.php =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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 @@ =09 return $this->rowCount(); } -}=09 -?> +} +?> \ No newline at end of file --===============1789576677136682835==--