root/contributed_modules/modules/cidroute/functions.inc.php

Revision 7192, 8.9 kB (checked in by xrobau, 4 years ago)

Minor fixes discovered while comissioning cidrouting

  • Property svn:executable set to *
Line 
1 <?php /* $Id */
2 //Copyright (C) Rob Thomas <xrobau@gmail.com> -
3 //    Why Pay More 4 Less Pty Ltd (Australia) 2008
4 //
5 //       THIS IS NOT OPEN SOURCE SOFTWARE.
6 //
7 //  Whilst the source is available for you to look at, you
8 //  do NOT have a licence to change or re-distribute this
9 //  software.
10
11 //  This specific document, all other associated files, any
12 //  databases, and the database designs that accompany it
13 //  are Copyright 2008, and  are licenced for the SOLE USE of
14 //  Astrogen LLC, otherwise known as FreePBX, to be
15 //  distributed SOLEY with the FreePBX software package.
16
17 //  If you wish to licence the redistribution of these
18 //  copyrighted documents, database, and database designs,
19 //  the ONLY company that is able to do so is:
20
21 //    Why Pay More 4 Less Pty Ltd,
22 //    1 Grayson st,
23 //    Gladstone, QLD, 4680, AUSTRALIA
24 //
25 //   PHYSICAL MAIL only (Electronic mail is not acceptable)
26
27 //   If you do not have written permission from this company to
28 //   redistribute this copyrighted package and do so, you are
29 //   violating international copyright laws, and will be
30 //   prosecuted to the FULL EXTENT of the law.
31
32 //   You may be asking why this licence is so strict?  At the time
33 //   this was written, the Author believed that Fonality was
34 //   involved in numerous GPL Violations with their Trixbox
35 //   product.  If and when that is ever resolved, this document
36 //   will be re-licenced under v2 of the GPL.
37
38
39 // Stick this in the DID page.
40 function cidroute_hook_core($viewing_itemid, $target_menuid) {
41   $html = '';
42   if ($target_menuid == 'did')  {
43     global $db;
44     // Figure out if CIDroute is enabled for this trunk here...
45     $results = sql("SELECT * FROM cidroute_config WHERE trunk = '".$db->escapeSimple($viewing_itemid)."'","getRow");
46     $cidroute = $results[1];
47
48     // Draw the HTML
49     $html = '<tr><td colspan="2"><h5>';
50     $html .= _("Caller Location Routing");
51     $html .= '<hr></h5></td></tr>';
52     $html .= '<tr><td>';
53     $html .= 'Note that this OVERRIDES the destination below if selected<br>';
54     $html .= "</td></tr>";
55     $html .= '<td><a href="#" class="info">';
56     $html .= _("Enabled").'<span>'._("If enabled, this will set the destination of the call to what is specified in the Caller Location Routing page, and will over-ride any further settings. If it does NOT match, it will proceed as per normal.").".</span></a>:</td>\n";
57     $html .= '<td><select name="cidroute"><option value="0" '.($cidroute == '0' ? 'SELECTED' : '').">"._("No")."</option>\n";
58     $html .= '<option value="1" '.($cidroute == '1' ? 'SELECTED' : '').'>'._("Yes")."</option>\n</select></td>";
59     $html .= '</td></tr>';
60   }
61
62   return $html;
63  
64 }
65
66 function cidroute_hookProcess_core($viewing_itemid, $request) {
67         if (!isset($request['action']))
68                 return;
69         global $db;
70         switch ($request['action'])     {
71                 case 'addIncoming':
72                         $results = sql(sprintf('INSERT INTO cidroute_config (trunk, enabled) VALUES ("%s", %d)', 
73         $db->escapeSimple($request['extdisplay']), $request['cidroute']));
74     break;
75     case 'delIncoming':
76       $results = sql(sprintf("DELETE FROM cidroute_config WHERE trunk = '%s'",
77         $db->escapeSimple($request['extdisplay']))); 
78     break;
79     case 'edtIncoming':     // deleting and adding as in core module
80       $results = sql(sprintf("DELETE FROM cidroute_config WHERE trunk = '%s'",
81         $db->escapeSimple($request['extdisplay']))); 
82       $results = sql(sprintf('INSERT INTO cidroute_config (trunk, enabled) VALUES ("%s", %d)', 
83         $db->escapeSimple($request['extdisplay']), $request['cidroute']));
84     break;
85   }
86 } 
87
88 function cidroute_hookGet_config($engine) {
89   global $ext; 
90   switch($engine) {
91     case "asterisk":
92       $trunks = cidroute_get_trunks();
93       // Returns ("12345/", "23456/", "/");
94       if(is_array($trunks)) {
95         foreach($trunks as $trunk) {
96           $arr = explode("/", $trunk[0]);
97           print_r($arr);
98           if (empty($arr[0])) {
99             $dest = "_.";
100           } else {
101             $dest = $arr[0];
102           }
103           if (empty($arr[1])) {
104             $cid = "";
105           } else {
106             $cid = "/".$arr[1];
107           }
108           $context = "ext-did-0002";
109                                         $exten = "$dest$cid";
110           $ext->splice($context, $exten, 1, new ext_agi('cidrouting.agi'));
111
112         }
113       }
114     break;
115   }
116 }
117
118 function cidroute_get_trunks() {
119   $res = sql("select trunk from cidroute_config where enabled='1'", "getAll");
120   if (is_array($res)) {
121     foreach ($res as $r) {
122       $arr[]=$r[0];
123     }
124     return $res;
125   }
126   return null;
127 }
128  
129  
130
131 function cidroute_list() {
132   $results = sql("SELECT * FROM cidroute_dests","getAll",DB_FETCHMODE_ASSOC);
133   if(is_array($results)){
134     foreach($results as $result){
135       $dests[$result['destid']] = $result;
136     }
137   }
138   return isset($dests)?$dests:null;
139 }
140
141 function cidroute_del($post){
142   global $db;
143   $itemid = $db->escapeSimple($post['itemid']);
144   // Delete any references to the map in matches...
145   $results = sql("DELETE FROM cidroute_matches WHERE dest = '$itemid'","query");
146   // Delete any references to the map in overrides...
147   $results = sql("DELETE FROM cidroute_override WHERE dest = '$itemid'","query");
148   // Delete the map..
149   $results = sql("DELETE FROM cidroute_dests WHERE destid = '$itemid'","query");
150   // All gone!
151 }
152
153 function cidroute_add($post){
154   global $db;
155   print_r($post);
156   $command = "INSERT INTO cidroute_dests (name, dest) VALUES ('".$db->escapeSimple($post['name'])."','";
157   $command .= $db->escapeSimple($post[$post['goto0'].'0'])."')";
158   $res = sql($command);
159 }
160
161 function cidroute_alter($post){
162   global $db;
163   if (isset($post['addquick'])) {
164     // Check for Range: Area: State:
165     $defn = explode(":", $post['quick']);
166     print_r($defn);
167     if (strcasecmp($defn[0], 'Range') === 0) {
168       $res = explode("-", $defn[1]);
169       print "<br>Adding range ".$defn[1]."\n";
170       $q = "select count(min_numb) from cidroute_cidlist where min_numb = '".$db->escapeSimple($res[0])."'";
171       print "Doing $q<br>\n";
172       $ret = sql($q,"getRow");
173       print_r($ret);
174       if ($ret[0] == 0) {
175         // This is a manual range entry.. So don't know the area code
176         $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) values ";
177         $q .= "('au', '99', '".$db->escapeSimple($res[0])."', '".$db->escapeSimple($res[1])."', ";
178         $q .= $db->escapeSimple($post['itemid']).", 'Manual Range')";
179       } else {
180         $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) select ";
181         $q .= "country, areacode, min_numb, max_numb, ".$db->escapeSimple($post['itemid']).", ";
182         $q .= "localarea as name from cidroute_cidlist where min_numb = '".$db->escapeSimple($res[0]);
183         $q .= "' and max_numb = '".$db->escapeSimple($res[1])."'";
184       }
185       print "Doing $q<br>\n";
186       sql($q);
187     } elseif (strcasecmp($defn[0], "Area") === 0) {
188       $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) select ";
189       $q .= "country, areacode, min_numb, max_numb, ".$db->escapeSimple($post['itemid']).", localarea ";
190       $q .= "as name from cidroute_cidlist where localarea = '".$db->escapeSimple($defn[1])."'";
191       sql($q);
192     } elseif (strcasecmp($defn[0], "State") === 0) {
193       print "<br>Adding State ".$defn[1]."\n";
194       $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) select ";
195       $q .= "country, areacode, min_numb, max_numb, ".$db->escapeSimple($post['itemid']).", localarea ";
196       $q .= "as name from cidroute_cidlist where state = '".$db->escapeSimple($defn[1])."'";
197       sql($q);
198     } elseif (strcasecmp($defn[0], "Region") === 0) {
199       print "<br>Adding region ".$defn[1]."\n";
200       $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) select ";
201       $q .= "country, areacode, min_numb, max_numb, ".$db->escapeSimple($post['itemid']).", localarea ";
202       $q .= "as name from cidroute_cidlist where region = '".$db->escapeSimple($defn[1])."'";
203       sql($q);
204     }
205   } elseif (isset($post['updatedest'])) {
206     $command = "update cidroute_dests set dest = '";
207     $command .= $db->escapeSimple($post[$post['goto0'].'0'])."' where destid='";
208     $command .= $db->escapeSimple($post['itemid'])."'";
209     sql($command);
210   } elseif (isset($post['area'])) {
211     $reg = explode("|", $post['area']);
212     $q = "insert into cidroute_matches (country, areacode, min_numb, max_numb, dest, name) select ";
213     $q .= "country, areacode, min_numb, max_numb, ".$db->escapeSimple($post['itemid']).", localarea ";
214     $q .= "as name from cidroute_cidlist where state = '".$db->escapeSimple($reg[0])."' and region = '";
215     $q .= $db->escapeSimple($reg[1])."' and localarea='".$db->escapeSimple($reg[2])."'";
216     print "Doing $q<br>\n";
217     sql($q);
218   }
219 }
220
221 function cidroute_delmaps($post){
222   global $db;
223   if (isset($post['myselect'])) {
224     // Javascript combo box didn't work..
225       $res = $post['myselect'];
226     } elseif (isset($post['myselect_right'])) {
227       $res = $post['myselect_right'];
228     } else {
229       return;
230     }
231   foreach ($res as $r) {
232     $arr = explode("|", $r);
233     $q = "delete from cidroute_matches where min_numb='".$arr[0]."' and max_numb='".$arr[1]."'";
234     sql($q);
235   }       
236 } 
237 ?>
Note: See TracBrowser for help on using the browser.