[Freeswitch-svn] [commit] r8081 - in freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl: . dialplans

Freeswitch SVN intralanman at freeswitch.org
Thu Apr 10 03:53:56 EDT 2008


Author: intralanman
Date: Thu Apr 10 03:53:56 2008
New Revision: 8081

Added:
   freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplans/
   freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplans/lcr.php
Modified:
   freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_dialplan.php
   freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_directory.php

Log:
fixed a small bug in the directory (thanks to jdjurici)
added a bit of logic to allow implementors to use custom php scripts for certain contexts and added a quick n dirty lcr for a sample. will commit the schema changes later.... must sleep now.



Added: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplans/lcr.php
==============================================================================
--- (empty file)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/dialplans/lcr.php	Thu Apr 10 03:53:56 2008
@@ -0,0 +1,73 @@
+<?php
+
+class dialplan_lcr {
+
+    function main(&$obj) {
+        $obj -> xmlw -> startElement('section');
+        $obj -> xmlw -> writeAttribute('name', 'dialplan');
+        $obj -> xmlw -> writeAttribute('description', 'FreeSWITCH Dialplan');
+        $obj -> xmlw -> startElement('context');
+        $obj -> xmlw -> writeAttribute('name', 'lcr');
+        $obj -> xmlw -> startElement('extension');
+        $obj -> xmlw -> startElement('condition');
+        $applications = $this -> lcr_lookup($obj);
+        $app_count = count($applications);
+        
+        $sets = array(
+        'hangup_after_bridge'=>'true', 
+        'continue_on_failure'=>'true'
+        );
+        foreach ($sets as $var=>$val) {
+            $this -> xmlw -> startElement('action');
+            $this -> xmlw -> writeAttribute('application', 'set');
+            $this -> xmlw -> writeAttribute('data', "$var=$val");
+            $this -> xmlw -> endElement();
+        }
+        for ($i=0; $i<$app_count; $i++) {
+            $obj -> xmlw -> startElement($applications[$i]['type']);
+            $obj -> xmlw -> writeAttribute('application', 'bridge');
+            $obj -> xmlw -> writeAttribute('data', $applications[$i]['data']);
+            $obj -> xmlw -> endElement();
+        }
+        $obj -> xmlw -> endElement(); //</condition>
+        $obj -> xmlw -> endElement(); //</extension>
+        $obj -> xmlw -> endElement(); //</context>
+        $obj -> xmlw -> endElement(); //</section>
+
+    }
+
+    function lcr_lookup(&$obj) {
+        $obj -> comment_array($obj -> request);
+        $digitstr = $obj -> request['destination_number'];
+        $digitlen = strlen($digitstr);
+        $where_clause = '';
+        for ($i=0; $i<$digitlen; $i++) {
+            $where_clause .= sprintf("%s digits='%s' "
+            , ($i==0?"WHERE":"OR")
+            , substr($digitstr, 0, $i+1)
+            );
+        }
+        $query = sprintf("SELECT l.digits, c.Carrier_Name, l.rate, cg.id, cg.gateway, cg.id AS gwid, l.lead_strip, l.trail_strip, l.prefix, l.suffix FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id %s ORDER BY length(digits) DESC, rate;", $where_clause);
+        $res = $obj -> db -> query($query);
+        $obj -> comment($query);
+        if (MDB2::isError($res)) {
+            $obj -> comment($query);
+            $obj -> comment($res -> getMessage());
+            $obj -> file_not_found();
+        }
+        $carriers = array();
+        while ($row = $res -> fetchRow()) {
+            $carrier_id = $row['gwid'];
+            if (array_key_exists($carrier_id, $carriers)) {
+                continue;
+            }
+            $carriers[$carrier_id] = true;
+            $datastr = sprintf('sofia/gateway/%s/%s', $row['gateway'], $digitstr);
+            $results[] = array('type'=>'action', 'data'=>$datastr);
+        }
+        return $results;
+    }
+}
+
+
+?>
\ 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	Thu Apr 10 03:53:56 2008
@@ -18,18 +18,63 @@
  * Class for XML dialplan
 */
 class fs_dialplan extends fs_curl {
+    private $special_class_file;
+
     public function fs_dialplan() {
         $this -> fs_curl();
     }
 
+    /**
+     * This is the method that determines the XML output. Customized dialplans can
+     * be easily created by adding a record to the dialplan_special table with the 
+     * appropriate values. The php class MUST contain a "main()" method. The method
+     * should write directly to the xmlw obj that's pased or take care of writing
+     * out the xml itself and exiting as to not return.
+     *
+     */
     public function main() {
         $this -> comment($this -> request);
         $context = $this -> request['Caller-Context'];
-        $dp_array = $this -> get_dialplan($context);
-        $this -> writeDialplan($dp_array);
+        if ($this -> is_specialized_dialplan($context)) {
+            if (!include_once($this -> special_class_file)) {
+                $this -> file_not_found();
+            }
+            $class = sprintf('dialplan_%s', $context);
+            if (!class_exists($class)) {
+                $this -> comment("No Class of name $class");
+                $this -> file_not_found();
+            }
+            $obj = new $class;
+            /**
+             * recieving method should take incoming parameter as &$something
+             */
+            $obj -> main($this);
+        } else {
+            $dp_array = $this -> get_dialplan($context);
+            $this -> writeDialplan($dp_array);
+        }
         $this -> output_xml();
     }
 
+    public function is_specialized_dialplan($context) {
+        $query = sprintf(
+        "SELECT * FROM dialplan_special WHERE context='%s'", $context
+        );
+        $res = $this -> db -> query($query);
+        if (MDB2::isError($res)) {
+            $this -> comment($query);
+            $this -> comment($res -> getMessage());
+            $this -> file_not_found();
+        }
+        if ($res -> numRows() == 1) {
+            $row = $res -> fetchRow();
+            $this -> special_class_file = $row['class_file'];
+            return true;
+        } else {
+            return false;
+        }
+    }
+
     /**
      * This method will pull dialplan from database
      *

Modified: freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_directory.php
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_directory.php	(original)
+++ freeswitch/trunk/scripts/contrib/intralanman/PHP/fs_curl/fs_directory.php	Thu Apr 10 03:53:56 2008
@@ -196,7 +196,7 @@
         if (MDB2::isError($res)) {
             $this -> comment($query);
             $error_msg = sprintf("Error while selecting global params - %s"
-            , $this -> db -> getMessage()
+            , $res -> getMessage()
             );
             trigger_error($error_msg);
         }
@@ -225,7 +225,7 @@
         if (MDB2::isError($res)) {
             $this -> comment($query);
             $error_msg = sprintf("Error while selecting global vars - %s"
-            , $this -> db -> getMessage()
+            , $res -> getMessage()
             );
             trigger_error($error_msg);
         }



More information about the Freeswitch-svn mailing list