| 1 |
<?php |
|---|
| 2 |
// Dynamic routing modules |
|---|
| 3 |
// Copied from ivr and calleridlookup modules |
|---|
| 4 |
// John Fawcett Sept 2009 |
|---|
| 5 |
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); } |
|---|
| 6 |
|
|---|
| 7 |
function dynroute_init() { |
|---|
| 8 |
global $db; |
|---|
| 9 |
global $amp_conf; |
|---|
| 10 |
|
|---|
| 11 |
// Check to make sure that install.sql has been run |
|---|
| 12 |
$sql = "SELECT displayname from dynroute where displayname='__install_done' LIMIT 1"; |
|---|
| 13 |
|
|---|
| 14 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 15 |
|
|---|
| 16 |
if (DB::IsError($results)) { |
|---|
| 17 |
// It couldn't locate the table. This is bad. Lets try to re-create it, just |
|---|
| 18 |
// in case the user has had the brilliant idea to delete it. |
|---|
| 19 |
// runModuleSQL taken from page.module.php. It's inclusion here is probably |
|---|
| 20 |
// A bad thing. It should be, I think, globally available. |
|---|
| 21 |
localrunModuleSQL('dynroute', 'uninstall'); |
|---|
| 22 |
if (localrunModuleSQL('dynroute', 'install')==false) { |
|---|
| 23 |
echo _("There is a problem with install.sql, cannot re-create databases. Contact support\n"); |
|---|
| 24 |
die; |
|---|
| 25 |
} else { |
|---|
| 26 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 27 |
} |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
if (!isset($results[0])) { |
|---|
| 31 |
// Note: There's an invalid entry created, __invalid, after this is run, |
|---|
| 32 |
// so as long as this has been run _once_, there will always be a result. |
|---|
| 33 |
|
|---|
| 34 |
$result = sql("INSERT INTO dynroute (displayname) VALUES ('__install_done')"); |
|---|
| 35 |
needreload(); |
|---|
| 36 |
} |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
// The destinations this module provides |
|---|
| 40 |
// returns a associative arrays with keys 'destination' and 'description' |
|---|
| 41 |
function dynroute_destinations() { |
|---|
| 42 |
//get the list of routes |
|---|
| 43 |
$results = dynroute_list(); |
|---|
| 44 |
|
|---|
| 45 |
// return an associative array with destination and description |
|---|
| 46 |
if (isset($results)) { |
|---|
| 47 |
foreach($results as $result){ |
|---|
| 48 |
$extens[] = array('destination' => 'dynroute-'.$result['dynroute_id'].',s,1', 'description' => $result['displayname']); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
if (isset($extens)) |
|---|
| 52 |
return $extens; |
|---|
| 53 |
else |
|---|
| 54 |
return null; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
function dynroute_getdest($exten) { |
|---|
| 58 |
return array('dynroute-'.$exten.',s,1'); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
function dynroute_getdestinfo($dest) { |
|---|
| 62 |
global $active_modules; |
|---|
| 63 |
|
|---|
| 64 |
if (substr(trim($dest),0,9) == 'dynroute-') { |
|---|
| 65 |
$exten = explode(',',$dest); |
|---|
| 66 |
$exten = substr($exten[0],9); |
|---|
| 67 |
|
|---|
| 68 |
$thisexten = dynroute_get_details($exten); |
|---|
| 69 |
if (empty($thisexten)) { |
|---|
| 70 |
return array(); |
|---|
| 71 |
} else { |
|---|
| 72 |
return array('description' => sprintf(_("Route: %s"),$thisexten['displayname']), |
|---|
| 73 |
'edit_url' => 'config.php?display=dynroute&action=edit&id='.urlencode($exten), |
|---|
| 74 |
); |
|---|
| 75 |
} |
|---|
| 76 |
} else { |
|---|
| 77 |
return false; |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
function dynroute_recordings_usage($recording_id) { |
|---|
| 81 |
global $active_modules; |
|---|
| 82 |
|
|---|
| 83 |
$results = sql("SELECT `dynroute_id`, `displayname` FROM `dynroute` WHERE `announcement_id` = '$recording_id'","getAll",DB_FETCHMODE_ASSOC); |
|---|
| 84 |
if (empty($results)) { |
|---|
| 85 |
return array(); |
|---|
| 86 |
} else { |
|---|
| 87 |
//$type = isset($active_modules['dynroute']['type'])?$active_modules['dynroute']['type']:'setup'; |
|---|
| 88 |
foreach ($results as $result) { |
|---|
| 89 |
$usage_arr[] = array( |
|---|
| 90 |
'url_query' => 'config.php?display=dynroute&action=edit&id='.urlencode($result['dynroute_id']), |
|---|
| 91 |
'description' => sprintf(_("Dynamic route: %s"),$result['displayname']), |
|---|
| 92 |
); |
|---|
| 93 |
} |
|---|
| 94 |
return $usage_arr; |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
function dynroute_get_config($engine) { |
|---|
| 99 |
global $ext; |
|---|
| 100 |
global $conferences_conf; |
|---|
| 101 |
global $version; |
|---|
| 102 |
|
|---|
| 103 |
switch($engine) { |
|---|
| 104 |
case "asterisk": |
|---|
| 105 |
$dynroutelist = dynroute_list(); |
|---|
| 106 |
if(is_array($dynroutelist)) { |
|---|
| 107 |
foreach($dynroutelist as $item) { |
|---|
| 108 |
$id = "dynroute-".$item['dynroute_id']; |
|---|
| 109 |
$details = dynroute_get_details($item['dynroute_id']); |
|---|
| 110 |
if (version_compare($version, "1.6", "lt")) { |
|---|
| 111 |
//Escaping MySQL query - thanks to http://www.asteriskgui.com/index.php?get=utilities-mysqlscape |
|---|
| 112 |
$replacements = array ( |
|---|
| 113 |
'\\' => '\\\\', |
|---|
| 114 |
'"' => '\\"', |
|---|
| 115 |
'\'' => '\\\'', |
|---|
| 116 |
' ' => '\\ ', |
|---|
| 117 |
',' => '\\,', |
|---|
| 118 |
'(' => '\\(', |
|---|
| 119 |
')' => '\\)', |
|---|
| 120 |
'.' => '\\.', |
|---|
| 121 |
'|' => '\\|' |
|---|
| 122 |
); |
|---|
| 123 |
$query = str_replace(array_keys($replacements), array_values($replacements), $item['mysql_query']); |
|---|
| 124 |
} else { |
|---|
| 125 |
$query = $item['mysql_query']; |
|---|
| 126 |
} |
|---|
| 127 |
$query = str_replace('[NUMBER]', '${CALLERID(num)}', $query); |
|---|
| 128 |
$query = str_replace('[INPUT]', '${dtmfinput}', $query); |
|---|
| 129 |
$query = str_replace('[DID]', '${FROM_DID}', $query); |
|---|
| 130 |
$query = preg_replace('/\[([^\]]*)\]/','${DYNROUTE_$1}',$query); |
|---|
| 131 |
$announcement_id = (isset($details['announcement_id']) ? $details['announcement_id'] : ''); |
|---|
| 132 |
if ($item['enable_dtmf_input']=='CHECKED') |
|---|
| 133 |
{ |
|---|
| 134 |
if ($announcement_id) { |
|---|
| 135 |
$announcement_msg = recordings_get_file($announcement_id); |
|---|
| 136 |
} else { |
|---|
| 137 |
$announcement_msg = ''; |
|---|
| 138 |
} |
|---|
| 139 |
$ext->add($id, 's', '', new ext_read('dtmfinput',$announcement_msg,'','','',$item['timeout'])); |
|---|
| 140 |
if ($item['chan_var_name'] != '') |
|---|
| 141 |
$ext->add($id, 's', '', new ext_setvar('__DYNROUTE_'.$item['chan_var_name'], '${dtmfinput}')); |
|---|
| 142 |
} |
|---|
| 143 |
if ($item['mysql_host']!='') |
|---|
| 144 |
{ |
|---|
| 145 |
$ext->add($id, 's', '', new ext_setvar('connid', '""')); |
|---|
| 146 |
$ext->add($id, 's', '', new ext_mysql_connect('connid', $item['mysql_host'], $item['mysql_username'], $item['mysql_password'], $item['mysql_dbname'])); |
|---|
| 147 |
$ext->add($id, 's', '', new ext_gotoif('$["${connid}" = ""]',$id.',1,1')); |
|---|
| 148 |
$ext->add($id, 's', '', new ext_mysql_query('resultid', 'connid', $query)); |
|---|
| 149 |
$ext->add($id, 's', '', new ext_mysql_fetch('fetchid', 'resultid', 'dynroute')); |
|---|
| 150 |
$ext->add($id, 's', '', new ext_mysql_clear('resultid')); |
|---|
| 151 |
$ext->add($id, 's', '', new ext_mysql_disconnect('connid')); |
|---|
| 152 |
if ($item['chan_var_name_res'] != '') |
|---|
| 153 |
$ext->add($id, 's', '', new ext_setvar('__DYNROUTE_'.$item['chan_var_name_res'], '${dynroute}')); |
|---|
| 154 |
$ext->add($id, 's', '', new ext_gotoif('$[${fetchid} = 0]',$id.',1,1')); |
|---|
| 155 |
} |
|---|
| 156 |
$dests = dynroute_get_dests($item['dynroute_id']); |
|---|
| 157 |
if (!empty($dests)) { |
|---|
| 158 |
$default_dest=''; |
|---|
| 159 |
foreach($dests as $dest) { |
|---|
| 160 |
if ($dest['selection'] == 'default') { |
|---|
| 161 |
$default_dest=$dest['dest']; |
|---|
| 162 |
} else { |
|---|
| 163 |
$ext->add($id, 's', '', new ext_gotoif('$["${dynroute}" = "'.$dest['selection'].'"]',$dest['dest'])); |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
$ext->add($id, 's', '', new ext_goto($id.',1,1')); |
|---|
| 168 |
if ($default_dest != '') $ext->add($id, '1', '', new ext_goto($default_dest)); |
|---|
| 169 |
$ext->add($id, '1', '', new ext_hangup('')); |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
break; |
|---|
| 173 |
} |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
function dynroute_get_dynroute_id($name) { |
|---|
| 179 |
global $db; |
|---|
| 180 |
$res = $db->getRow("SELECT dynroute_id from dynroute where displayname='$name'"); |
|---|
| 181 |
if (count($res) == 0) { |
|---|
| 182 |
// It's not there. Create it and return the ID |
|---|
| 183 |
sql("INSERT INTO dynroute (displayname ) values('$name')"); |
|---|
| 184 |
$res = $db->getRow("SELECT dynroute_id from dynroute where displayname='$name'"); |
|---|
| 185 |
} |
|---|
| 186 |
return ($res[0]); |
|---|
| 187 |
|
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
function dynroute_add_command($id, $cmd, $dest) { |
|---|
| 191 |
global $db; |
|---|
| 192 |
// Does it already exist? |
|---|
| 193 |
$res = $db->getRow("SELECT * from dynroute_dests where dynroute_id='$id' and selection='$cmd'"); |
|---|
| 194 |
if (count($res) == 0) { |
|---|
| 195 |
// Just add it. |
|---|
| 196 |
sql("INSERT INTO dynroute_dests VALUES('$id', '$cmd', '$dest')"); |
|---|
| 197 |
} else { |
|---|
| 198 |
// Update it. |
|---|
| 199 |
sql("UPDATE dynroute_dests SET dest='$dest' where dynroute_id='$id' and selection='$cmd'"); |
|---|
| 200 |
} |
|---|
| 201 |
} |
|---|
| 202 |
function dynroute_do_edit($id, $post) { |
|---|
| 203 |
global $db; |
|---|
| 204 |
$displayname = $db->escapeSimple($post['displayname']); |
|---|
| 205 |
$mysql_host = $db->escapeSimple($post['mysql_host']); |
|---|
| 206 |
$mysql_dbname = $db->escapeSimple($post['mysql_dbname']); |
|---|
| 207 |
$mysql_query = $db->escapeSimple($post['mysql_query']); |
|---|
| 208 |
$mysql_username = $db->escapeSimple($post['mysql_username']); |
|---|
| 209 |
$mysql_password = $db->escapeSimple($post['mysql_password']); |
|---|
| 210 |
$annmsg_id = isset($post['annmsg_id'])?$post['annmsg_id']:''; |
|---|
| 211 |
$enable_dtmf_input = isset($post['enable_dtmf_input'])?$post['enable_dtmf_input']:''; |
|---|
| 212 |
|
|---|
| 213 |
if (!empty($enable_dtmf_input)) { |
|---|
| 214 |
$enable_dtmf_input='CHECKED'; |
|---|
| 215 |
} |
|---|
| 216 |
$timeout = isset($post['timeout'])?$post['timeout']:''; |
|---|
| 217 |
$chan_var_name = isset($post['chan_var_name'])?$post['chan_var_name']:''; |
|---|
| 218 |
$chan_var_name_res = isset($post['chan_var_name_res'])?$post['chan_var_name_res']:''; |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
$sql = " |
|---|
| 223 |
UPDATE dynroute |
|---|
| 224 |
SET |
|---|
| 225 |
displayname='$displayname', |
|---|
| 226 |
sourcetype='mysql', |
|---|
| 227 |
mysql_host='$mysql_host', |
|---|
| 228 |
mysql_dbname='$mysql_dbname', |
|---|
| 229 |
mysql_username='$mysql_username', |
|---|
| 230 |
mysql_password='$mysql_password', |
|---|
| 231 |
mysql_query='$mysql_query', |
|---|
| 232 |
announcement_id='$annmsg_id', |
|---|
| 233 |
enable_dtmf_input='$enable_dtmf_input', |
|---|
| 234 |
timeout='$timeout', |
|---|
| 235 |
chan_var_name='$chan_var_name', |
|---|
| 236 |
chan_var_name_res='$chan_var_name_res' |
|---|
| 237 |
WHERE dynroute_id='$id' |
|---|
| 238 |
"; |
|---|
| 239 |
sql($sql); |
|---|
| 240 |
|
|---|
| 241 |
// Delete all the old dests |
|---|
| 242 |
sql("DELETE FROM dynroute_dests where dynroute_id='$id'"); |
|---|
| 243 |
// Now, lets find all the goto's in the post. Destinations return gotoN => foo and get fooN for the dest. |
|---|
| 244 |
// Is that right, or am I missing something? |
|---|
| 245 |
foreach(array_keys($post) as $var) { |
|---|
| 246 |
if (preg_match('/goto(\d+)/', $var, $match)) { |
|---|
| 247 |
// This is a really horrible line of code. take N, and get value of fooN. See above. Note we |
|---|
| 248 |
// get match[1] from the preg_match above |
|---|
| 249 |
$dest = $post[$post[$var].$match[1]]; |
|---|
| 250 |
$cmd = $post['option'.$match[1]]; |
|---|
| 251 |
// Debugging if it all goes pear shaped. |
|---|
| 252 |
// print "I think pushing $cmd does $dest<br>\n"; |
|---|
| 253 |
if (strlen($cmd)) |
|---|
| 254 |
dynroute_add_command($id, $cmd, $dest); |
|---|
| 255 |
} |
|---|
| 256 |
} |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
function dynroute_list() { |
|---|
| 261 |
global $db; |
|---|
| 262 |
|
|---|
| 263 |
$sql = "SELECT * FROM dynroute where displayname <> '__install_done' ORDER BY displayname"; |
|---|
| 264 |
$res = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 265 |
if(DB::IsError($res)) { |
|---|
| 266 |
return null; |
|---|
| 267 |
} |
|---|
| 268 |
return $res; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
function dynroute_get_details($id) { |
|---|
| 272 |
global $db; |
|---|
| 273 |
|
|---|
| 274 |
$sql = "SELECT * FROM dynroute where dynroute_id='$id'"; |
|---|
| 275 |
$res = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 276 |
if(DB::IsError($res)) { |
|---|
| 277 |
return null; |
|---|
| 278 |
} |
|---|
| 279 |
return $res[0]; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
function dynroute_get_dests($id) { |
|---|
| 283 |
global $db; |
|---|
| 284 |
|
|---|
| 285 |
$sql = "SELECT selection, dest FROM dynroute_dests where dynroute_id='$id' ORDER BY selection"; |
|---|
| 286 |
$res = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 287 |
if(DB::IsError($res)) { |
|---|
| 288 |
return null; |
|---|
| 289 |
} |
|---|
| 290 |
return $res; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
function dynroute_get_name($id) { |
|---|
| 294 |
$res = dynroute_get_details($id); |
|---|
| 295 |
if (isset($res['displayname'])) { |
|---|
| 296 |
return $res['displayname']; |
|---|
| 297 |
} else { |
|---|
| 298 |
return null; |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
function dynroute_check_destinations($dest=true) { |
|---|
| 303 |
global $active_modules; |
|---|
| 304 |
|
|---|
| 305 |
$destlist = array(); |
|---|
| 306 |
if (is_array($dest) && empty($dest)) { |
|---|
| 307 |
return $destlist; |
|---|
| 308 |
} |
|---|
| 309 |
$sql = "SELECT dest, displayname, selection, a.dynroute_id dynroute_id FROM dynroute a INNER JOIN dynroute_dests d ON a.dynroute_id = d.dynroute_id "; |
|---|
| 310 |
if ($dest !== true) { |
|---|
| 311 |
$sql .= "WHERE dest in ('".implode("','",$dest)."')"; |
|---|
| 312 |
} |
|---|
| 313 |
$sql .= "ORDER BY displayname"; |
|---|
| 314 |
$results = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| 315 |
|
|---|
| 316 |
foreach ($results as $result) { |
|---|
| 317 |
$thisdest = $result['dest']; |
|---|
| 318 |
$thisid = $result['dynroute_id']; |
|---|
| 319 |
$destlist[] = array( |
|---|
| 320 |
'dest' => $thisdest, |
|---|
| 321 |
'description' => sprintf(_("Route: %s / Option: %s"),$result['displayname'],$result['selection']), |
|---|
| 322 |
'edit_url' => 'config.php?display=dynroute&action=edit&id='.urlencode($thisid), |
|---|
| 323 |
); |
|---|
| 324 |
} |
|---|
| 325 |
return $destlist; |
|---|
| 326 |
} |
|---|