root/contributed_modules/modules/dynroute/page.dynroute.php

Revision 14068, 14.1 kB (checked in by john@erba.tv, 3 weeks ago)

security: added block on calling directly to all .php files in module,
changed uninstall from sql script to php to be aligned with current
standards, removed closing tags on .php

Line 
1 <?php 
2 // Dynamic routing modules
3 // Copied from ivr and calleridlookup modules
4 // John Fawcett Sept 2009
5
6 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
7
8
9 $action = isset($_REQUEST['action'])?$_REQUEST['action']:'';
10 $id = isset($_REQUEST['id'])?$_REQUEST['id']:'';
11 $nbroptions = isset($_REQUEST['nbroptions'])?$_REQUEST['nbroptions']:'3';
12 $tabindex = 0;
13
14 switch ($action) {
15   case "add":
16     $id = dynroute_get_dynroute_id('Unnamed');
17     // Set the defaults
18     dynroute_sidebar($id);
19     dynroute_show_edit($id, 3,  $def);
20     $def['timeout']=5;
21     break;
22   case "edit":
23     dynroute_sidebar($id);
24     dynroute_show_edit($id, $nbroptions, $_POST);
25     break;
26   case "edited":
27     if (isset($_REQUEST['delete'])) {
28       sql("DELETE from dynroute where dynroute_id='$id'");
29       sql("DELETE FROM dynroute_dests where dynroute_id='$id'");
30       needreload();
31     } else {
32       dynroute_do_edit($id, $_POST);
33       dynroute_sidebar($id);
34       if (isset($_REQUEST['increase']))
35         $nbroptions++;
36       if (isset($_REQUEST['decrease'])) {
37         $nbroptions--;
38       }
39       if ($nbroptions < 1)
40         $nbroptions = 1;
41       $url = 'config.php?type=setup&display=dynroute&action=edit&id='.$id.'&nbroptions='.$nbroptions;
42       needreload();
43       redirect($url);
44       break;
45     }
46   default:
47     dynroute_sidebar($id);
48 ?>
49 <div class="content">
50 <h2><?php echo _("Routing"); ?></h2>
51 <h3><?php 
52 echo _("Instructions")."</h3>";
53 echo _("You use the Dynamic Routing module to route calls based on sql lookup.")."\n";
54 echo _("It is also possible to request user input (dtmf) and then use that in the query too.")."\n";
55 echo _("Optionally an announcement can be played before reading dtmf.")."\n";
56 echo _("You need to specify hostname, database name, username and password for mysql server.")."\n";
57 echo _("In the query you can use the special string [NUMBER] to be substituted by the incoming callerid number")."\n";
58 echo _("or the special string [INPUT] to be substituted by the captured dtmf.")."\n";
59 echo _("The selected field returned from the query is matched against the options text to decide which destination to use.")."\n";
60 echo _("Optionally you may define variable names in order to capture the dtmf input and / or query result. Those variables may")."\n";
61 echo _("be used later on a further pass through a dynroute inside the query string be enclosing in [] or in custom destinations.")."\n";
62 echo _("When refering to the variables in custom destinations (typically to pass to an agi script) DYNROUTE_ is prefixed to the variable name.")."\n";
63 echo _("You should define an option named default which will be used if no match is found. If you do not the call will be hang up on no match.")."\n";
64 echo _("If you have defined a default option you may also omit the mysql hostname and other parameters in order to bypass a query and")."\n";
65 echo _("procede with the default action. This is useful if you only want to capture dtmf into a variable without a mysql lookup.")."\n"; ?>
66 </div>
67
68 <?php
69 }
70
71
72 function dynroute_sidebar($id)  {
73 ?>
74         <div class="rnav"><ul>
75         <li><a id="<?php echo empty($id)?'current':'nul' ?>" href="config.php?display=dynroute&amp;action=add"><?php echo _("Add Route")?></a></li>
76 <?php
77
78         $dynroute_results = dynroute_list();
79         if (isset($dynroute_results)){
80                 foreach ($dynroute_results as $tresult) {
81                         echo "<li><a id=\"".($id==$tresult['dynroute_id'] ? 'current':'nul')."\" href=\"config.php?display=dynroute";
82                         echo "&amp;action=edit&amp;id={$tresult['dynroute_id']}\">{$tresult['displayname']}</a></li>\n";
83                 }
84         }
85         echo "</ul></div>\n";
86 }
87
88 function dynroute_show_edit($id, $nbroptions, $post) {
89   global $db;
90   global $tabindex;
91
92   $dynroute_details = dynroute_get_details($id);
93   $dynroute_dests = dynroute_get_dests($id);
94 ?>
95   <div class="content">
96   <h2><?php echo _("Dynamic Routes"); ?></h2>
97   <h3><?php echo _("Edit Menu")." ".$dynroute_details['displayname']; ?></h3>
98 <?php 
99 ?>
100   <form name="prompt" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return prompt_onsubmit();">
101   <input type="hidden" name="action" value="edited" />
102   <input type="hidden" name="display" value="dynroute" />
103   <input type="hidden" name="id" value="<?php echo $id ?>" />
104   <input name="Submit" type="submit" style="display:none;" value="save" />
105   <input name="delete" type="submit" value="<?php echo _("Delete")." "._("Route")." {$dynroute_details['displayname']}"; ?>" />
106 <?php
107   if ($id) {
108     $usage_list = framework_display_destination_usage(dynroute_getdest($id));
109     if (!empty($usage_list)) {
110     ?>
111       <br /><a href="#" class="info"><?php echo $usage_list['text']?>:<span><?php echo $usage_list['tooltip']?></span></a>
112     <?php
113     }
114   }
115   ?>
116   <table>
117     <tr><td colspan=2><hr /></td></tr>
118     <tr>
119       <td><a href="#" class="info"><?php echo _("Change Name"); ?><span><?php echo _("This changes the short name, visible on the right, of this Route");?></span></a></td>
120       <td><input type="text" name="displayname" value="<?php echo $dynroute_details['displayname'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
121     </tr>
122                 <tr>
123                         <td><a href="#" class="info"><?php echo _("Get DTMF input");?><span><?php echo _("If checked reads in DTMF digis, the value is available in the sql query with special name of [INPUT].");?></span></a></td>
124                         <td><input type="checkbox" name="enable_dtmf_input" <?php echo $dynroute_details['enable_dtmf_input'] ?> tabindex="<?php echo ++$tabindex;?>"></td>
125                 </tr>
126                 <tr>
127                         <td><a href="#" class="info"><?php echo _("Timeout");?><span><?php echo _("The amount of time (in seconds) to wait for input");?></span></a></td>
128                         <td><input type="text" name="timeout" value="<?php echo $dynroute_details['timeout'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
129                 </tr>
130                 <tr>
131                         <td><a href="#" class="info"><?php echo _("Input Variable");?><span><?php echo _("Optional variable name if you want the dmtf input to be available later in the call (e.g. futher dynamic route query or to pass to agi script)");?></span></a></td>
132                         <td><input type="text" name="chan_var_name" value="<?php echo $dynroute_details['chan_var_name'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
133                 </tr>
134  
135 <?php
136         $annmsg_id = isset($dynroute_details['announcement_id'])?$dynroute_details['announcement_id']:'';
137         if(function_exists('recordings_list')) { //only include if recordings is enabled ?>
138                 <tr>
139                         <td><a href="#" class="info"><?php echo _("Announcement")?><span><?php echo _("Message to be played to the caller. To add additional recordings please use the \"System Recordings\" MENU to the left")?></span></a></td>
140                         <td>
141                                 <select name="annmsg_id" tabindex="<?php echo ++$tabindex;?>">
142                                 <?php
143                                         $tresults = recordings_list();
144                                         echo '<option value="">'._("None")."</option>";
145                                         if (isset($tresults[0])) {
146                                                 foreach ($tresults as $tresult) {
147                                                         echo '<option value="'.$tresult['id'].'"'.($tresult['id'] == $annmsg_id ? ' SELECTED' : '').'>'.$tresult['displayname']."</option>\n";
148                                                 }
149                                         }
150                                 ?>
151                                 </select>
152                         </td>
153                 </tr>
154
155 <?php
156         } else {
157 ?>
158                 <tr>
159                         <td><a href="#" class="info"><?php echo _("Announcement")?><span><?php echo _("Message to be played to the caller.<br><br>You must install and enable the \"Systems Recordings\" Module to edit this option")?></span></a></td>
160                         <td>
161                         <?php
162                                 $default = (isset($annmsg_id) ? $annmsg_id : '');
163                         ?>
164                                 <input type="hidden" name="annmsg_id" value="<?php echo $default; ?>"><?php echo ($default != '' ? $default : 'None'); ?>
165                         </td>
166                 </tr>
167 <?php
168         }
169 ?>
170
171     <tr>
172       <td><a href="#" class="info"><?php echo _("Host");?><span><?php echo _("Host where database to check is located");?></span></a></td>
173       <td><input type="text" iname="mysql_host" value="<?php echo $dynroute_details['mysql_host'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
174     </tr>
175     <tr>
176       <td><a href="#" class="info"><?php echo _("Database");?><span><?php echo _("Database name");?></span></a></td>
177       <td><input type="text" name="mysql_dbname" value="<?php echo $dynroute_details['mysql_dbname'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
178     </tr>
179     <tr>
180       <td><a href="#" class="info"><?php echo _("Username");?><span><?php echo _("Username for mysql database");?></span></a></td>
181       <td><input type="text" name="mysql_username" value="<?php echo $dynroute_details['mysql_username'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
182     </tr>
183     <tr>
184       <td><a href="#" class="info"><?php echo _("Password");?><span><?php echo _("Password for username above");?></span></a></td>
185       <td><input type="text" name="mysql_password" value="<?php echo $dynroute_details['mysql_password'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
186     </tr>
187     <tr>
188       <td><a href="#" class="info"><?php echo _("Query");?><span><?php echo _("Query to execute");?></span></a></td>
189       <td><input type="text" name="mysql_query" size="100" value="<?php echo $dynroute_details['mysql_query'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
190     </tr>
191                 <tr>
192                         <td><a href="#" class="info"><?php echo _("Result Variable");?><span><?php echo _("Optional variable name if you want the query result to be available later in the call (e.g. futher dynamic route query or to pass to agi script)");?></span></a></td>
193                         <td><input type="text" name="chan_var_name_res" value="<?php echo $dynroute_details['chan_var_name_res'] ?>" tabindex="<?php echo ++$tabindex;?>"></td>
194                 </tr>
195     <tr><td colspan=2><hr /></td></tr>
196     <tr><td colspan=2>
197
198       <input name="increase" type="submit" value="<?php echo _("Increase Options")?>">
199       &nbsp;
200       <input name="Submit" type="submit" value="<?php echo _("Save")?>" tabindex="<?php echo ++$tabindex;?>">
201       &nbsp;
202       <?php if ($nbroptions > 1) { ?>
203       <input name="decrease" type="submit" value="<?php echo _("Decrease Options")?>">
204       <?php } ?>
205     </td>
206   </tr>
207   <tr><td colspan=2><hr /></td></tr>
208 <?php
209   // Draw the destinations
210   $dests = dynroute_get_dests($id);
211   $count = 0;
212   if (!empty($dests)) {
213     foreach ($dests as $dest) {
214       drawdestinations($count, $dest['selection'], $dest['dest']);
215       $count++;
216     }
217   }
218   while ($count < $nbroptions) {
219     drawdestinations($count, null, null, 0);
220     $count++;
221   }
222 ?>
223  
224 </table>
225 <?php
226   if ($nbroptions < $count) {
227     echo "<input type='hidden' name='nbroptions' value=$count />\n";
228   } else {
229     echo "<input type='hidden' name='nbroptions' value=$nbroptions />\n";
230   }
231
232   global $module_hook;
233   echo $module_hook->hookHtml;
234 ?>
235   <input name="increase" type="submit" value="<?php echo _("Increase Options")?>">
236   &nbsp;
237   <input name="Submit" type="submit" value="<?php echo _("Save")?>">
238   &nbsp;
239   <?php if ($nbroptions > 1) { ?>
240   <input name="decrease" type="submit" value="<?php echo _("Decrease Options")?>">
241   <?php } ?>
242  
243   <script language="javascript">
244   <!--
245
246 var theForm = document.prompt;
247 theForm.displayname.focus();
248
249   function prompt_onsubmit() {
250     var msgInvalidOption = "<?php echo _("Invalid option"); ?>";
251     
252     defaultEmptyOK = true;
253
254     // go thru the form looking for options
255     // where the option isn't blank (as that will be removed) do the validation
256       var allelems = theForm.elements;
257         if (allelems != null)
258         {
259           var i, elem;
260             for (i = 0; elem = allelems[i]; i++)
261             {
262               if (elem.type == 'text' && elem.name.indexOf('option') == 0)
263                 {
264                   if (elem.value != '') {
265                       if (!isRouteOption(elem.value))
266                           return warnInvalid(elem, msgInvalidOption);
267                         
268                         var gotoNum = elem.name.charAt(6);
269                         var isok = validateSingleDestination(theForm,gotoNum,true);
270                         if (!isok)
271                           return false;
272                     }
273                  }
274             }
275         }
276                                 
277     return true;
278   }
279  
280   //-->
281   </script>
282         </form>
283         </div>
284
285
286 <?php
287
288 echo "</div>\n";
289 }
290
291 function drawdestinations($count, $sel,  $dest) {
292   global $tabindex
293 ?>
294   <tr> <td style="text-align:right;">
295
296     <input size="10" type="text" name="option<?php echo $count ?>" value="<?php echo $sel ?>" tabindex="<?php echo ++$tabindex;?>"><br />
297 <?php if (strlen($sel)) {  ?>
298     <i style='font-size: x-small'><?php echo _("Leave blank to remove");?></i>
299 <?php }  ?>
300   </td>
301     <td> <table> <?php echo drawselects($dest,$count); ?> </table> </td>
302   </tr>
303   <tr><td colspan=2><hr /></td></tr>
304 <?php
305 }
306
307 // this can be removed in 2.2 and put back to just runModuleSQL which is in admin/functions.inc.php
308 // I didn't want to do it in 2.1 as there's a significant user base out there, and it will break
309 // them if we do it here.
310
311 function localrunModuleSQL($moddir,$type){
312         global $db;
313         $data='';
314         if (is_file("modules/{$moddir}/{$type}.sql")) {
315                 // run sql script
316                 $fd = fopen("modules/{$moddir}/{$type}.sql","r");
317                 while (!feof($fd)) {
318                         $data .= fread($fd, 1024);
319                 }
320                 fclose($fd);
321
322                 preg_match_all("/((SELECT|INSERT|UPDATE|DELETE|CREATE|DROP).*);\s*\n/Us", $data, $matches);
323
324                 foreach ($matches[1] as $sql) {
325                                 $result = $db->query($sql);
326                                 if(DB::IsError($result)) {
327                                         return false;
328                                 }
329                 }
330                 return true;
331         }
332                 return true;
333 }
Note: See TracBrowser for help on using the browser.