| 1 |
<?php |
|---|
| 2 |
/** |
|---|
| 3 |
* @copyright Niklas Larsson, Infracom AB |
|---|
| 4 |
* @license GPL2 |
|---|
| 5 |
* |
|---|
| 6 |
* chan_sccp stuff |
|---|
| 7 |
* |
|---|
| 8 |
* @todo add speeddails |
|---|
| 9 |
* @todo hooks into extensions |
|---|
| 10 |
* |
|---|
| 11 |
*/ |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
function chan_sccp_destinations() { |
|---|
| 15 |
// return an associative array with destination and description |
|---|
| 16 |
|
|---|
| 17 |
return; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
function chan_sccp_getdest($exten) { |
|---|
| 21 |
|
|---|
| 22 |
return; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
function chan_sccp_getdestinfo($dest) { |
|---|
| 26 |
|
|---|
| 27 |
return; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
function chan_sccp_get_config($engine) { |
|---|
| 31 |
global $db; |
|---|
| 32 |
|
|---|
| 33 |
$sql = "SELECT d.id, d.description, m.mac, m.type, m.speeds |
|---|
| 34 |
FROM devices d |
|---|
| 35 |
LEFT JOIN sccp_mac m ON m.ext = d.id |
|---|
| 36 |
WHERE LEFT(d.dial, 4) = 'SCCP'"; |
|---|
| 37 |
|
|---|
| 38 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 39 |
if(DB::IsError($results)) { |
|---|
| 40 |
die_freepbx($results->getMessage()."<br><br>Error selecting from sccp_mac"); |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
foreach ($results as $row){ |
|---|
| 44 |
chan_sccp_create_line($row['id'], $row['description']); |
|---|
| 45 |
chan_sccp_create_device($row['mac'], $row['id'], $row['type'], $row['description'], $row['speeds']); |
|---|
| 46 |
chan_sccp_create_tftp_SEP($row['mac']); |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
/** Get a list of all phones |
|---|
| 51 |
*/ |
|---|
| 52 |
function chan_sccp_list() { |
|---|
| 53 |
global $db; |
|---|
| 54 |
|
|---|
| 55 |
$sql = "SELECT id, mac, ext, type |
|---|
| 56 |
FROM sccp_mac |
|---|
| 57 |
ORDER BY ext"; |
|---|
| 58 |
|
|---|
| 59 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 60 |
if(DB::IsError($results)) { |
|---|
| 61 |
die_freepbx($results->getMessage()."<br><br>Error selecting from sccp_mac"); |
|---|
| 62 |
} |
|---|
| 63 |
return $results; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
function chan_sccp_get($chan_sccp_id) { |
|---|
| 67 |
global $db; |
|---|
| 68 |
|
|---|
| 69 |
$sql = "SELECT id, mac, ext, type, speeds |
|---|
| 70 |
FROM sccp_mac |
|---|
| 71 |
WHERE id = " . (int) $chan_sccp_id; |
|---|
| 72 |
|
|---|
| 73 |
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 74 |
if(DB::IsError($row)) { |
|---|
| 75 |
die_freepbx($row->getMessage()."<br><br>Error selecting row from sccp_mac"); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
return $row; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
function chan_sccp_add($mac, $phone_type, $ext, $speeds) { |
|---|
| 82 |
global $db; |
|---|
| 83 |
|
|---|
| 84 |
$ext = (int) $ext; |
|---|
| 85 |
$phone_type = $db->escapeSimple($phone_type); |
|---|
| 86 |
$mac = $db->escapeSimple(strtoupper($mac)); |
|---|
| 87 |
$speeds = $db->escapeSimple(strtoupper($speeds)); |
|---|
| 88 |
|
|---|
| 89 |
$sql = "INSERT INTO sccp_mac |
|---|
| 90 |
(mac, type, ext, speeds) |
|---|
| 91 |
VALUES ('$mac', '$phone_type', $ext, '$speeds')"; |
|---|
| 92 |
|
|---|
| 93 |
$result = $db->query($sql); |
|---|
| 94 |
if(DB::IsError($result)) { |
|---|
| 95 |
die_freepbx($result->getMessage().$sql); |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
function chan_sccp_delete($chan_sccp_id) { |
|---|
| 100 |
global $db; |
|---|
| 101 |
|
|---|
| 102 |
$chan_sccp_id = (int) $chan_sccp_id; |
|---|
| 103 |
|
|---|
| 104 |
$sql = "SELECT mac, ext |
|---|
| 105 |
FROM sccp_mac |
|---|
| 106 |
WHERE id = $chan_sccp_id"; |
|---|
| 107 |
|
|---|
| 108 |
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 109 |
if(DB::IsError($row)) { |
|---|
| 110 |
die_freepbx($row->getMessage()."<br><br>Error selecting row from sccp_mac"); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
$sql = "DELETE FROM sccpline |
|---|
| 114 |
WHERE id = " . (int) $row['ext']; |
|---|
| 115 |
|
|---|
| 116 |
$result = $db->query($sql); |
|---|
| 117 |
if(DB::IsError($result)) { |
|---|
| 118 |
die_freepbx($result->getMessage().$sql); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
$sql = "DELETE FROM sccpdevice |
|---|
| 122 |
WHERE name = 'SEP{$row['mac']}'"; |
|---|
| 123 |
|
|---|
| 124 |
$result = $db->query($sql); |
|---|
| 125 |
if(DB::IsError($result)) { |
|---|
| 126 |
die_freepbx($result->getMessage().$sql); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
$sql = "DELETE FROM sccp_mac |
|---|
| 130 |
WHERE id = $chan_sccp_id"; |
|---|
| 131 |
|
|---|
| 132 |
$result = $db->query($sql); |
|---|
| 133 |
if(DB::IsError($result)) { |
|---|
| 134 |
die_freepbx($result->getMessage().$sql); |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
function chan_sccp_edit($chan_sccp_id, $mac, $phone_type, $ext, $speeds) { |
|---|
| 139 |
global $db; |
|---|
| 140 |
|
|---|
| 141 |
$chan_sccp_id = (int) $chan_sccp_id; |
|---|
| 142 |
$ext = (int) $ext; |
|---|
| 143 |
$phone_type = $db->escapeSimple($phone_type); |
|---|
| 144 |
$mac = $db->escapeSimple(strtoupper($mac)); |
|---|
| 145 |
$speeds = $db->escapeSimple($speeds); |
|---|
| 146 |
|
|---|
| 147 |
$sql = "UPDATE sccp_mac |
|---|
| 148 |
SET mac = '$mac', type = '$phone_type', ext = $ext, speeds = '$speeds' |
|---|
| 149 |
WHERE id = $chan_sccp_id"; |
|---|
| 150 |
|
|---|
| 151 |
$result = $db->query($sql); |
|---|
| 152 |
if(DB::IsError($result)) { |
|---|
| 153 |
die_freepbx($result->getMessage().$sql); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
/* |
|---|
| 157 |
function chan_sccp_check_destinations($dest=true) { |
|---|
| 158 |
global $active_modules; |
|---|
| 159 |
|
|---|
| 160 |
$destlist = array(); |
|---|
| 161 |
if (is_array($dest) && empty($dest)) { |
|---|
| 162 |
return $destlist; |
|---|
| 163 |
} |
|---|
| 164 |
$sql = "SELECT chan_sccp_id, dest, description FROM jabber "; |
|---|
| 165 |
if ($dest !== true) { |
|---|
| 166 |
$sql .= "WHERE dest in ('".implode("','",$dest)."')"; |
|---|
| 167 |
} |
|---|
| 168 |
$results = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| 169 |
|
|---|
| 170 |
$type = isset($active_modules['jabber']['type'])?$active_modules['jabber']['type']:'setup'; |
|---|
| 171 |
|
|---|
| 172 |
foreach ($results as $result) { |
|---|
| 173 |
$thisdest = $result['dest']; |
|---|
| 174 |
$thisid = $result['chan_sccp_id']; |
|---|
| 175 |
$destlist[] = array( |
|---|
| 176 |
'dest' => $thisdest, |
|---|
| 177 |
'description' => 'jabber Change: '.$result['description'], |
|---|
| 178 |
'edit_url' => 'config.php?display=jabber&type='.$type.'&extdisplay='.urlencode($thisid), |
|---|
| 179 |
); |
|---|
| 180 |
} |
|---|
| 181 |
return $destlist; |
|---|
| 182 |
} |
|---|
| 183 |
*/ |
|---|
| 184 |
function chan_sccp_create_line($ext, $name){ |
|---|
| 185 |
global $db; |
|---|
| 186 |
|
|---|
| 187 |
$ext = (int) $ext; |
|---|
| 188 |
$name = $db->escapeSimple($name); |
|---|
| 189 |
|
|---|
| 190 |
$sql = "SELECT COUNT(*) |
|---|
| 191 |
FROM sccpline |
|---|
| 192 |
WHERE name = '$ext'"; |
|---|
| 193 |
|
|---|
| 194 |
$res = $db->getone($sql); |
|---|
| 195 |
if(DB::IsError($res)) { |
|---|
| 196 |
die_freepbx($res->getMessage().$sql); |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
if ($res > 0){ |
|---|
| 200 |
$sql = "UPDATE sccpline |
|---|
| 201 |
SET id = $ext, label = $ext, description = '$name', mailbox = $ext, cid_name = '$name', cid_num = '$ext' |
|---|
| 202 |
WHERE name = '$ext'"; |
|---|
| 203 |
}else{ |
|---|
| 204 |
$sql = "INSERT INTO sccpline |
|---|
| 205 |
(id, label, description, mailbox, cid_name, cid_num, name) |
|---|
| 206 |
VALUES ($ext, $ext, '$name', $ext, '$name', $ext, $ext)"; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
$res = $db->query($sql); |
|---|
| 210 |
if(DB::IsError($res)) { |
|---|
| 211 |
die_freepbx($res->getMessage().$sql); |
|---|
| 212 |
} |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
function make_speeds($speeds, $type = '7960'){ |
|---|
| 216 |
global $db; |
|---|
| 217 |
$str = ''; |
|---|
| 218 |
|
|---|
| 219 |
$speeds_arr = explode(';', $speeds); |
|---|
| 220 |
foreach ($speeds_arr as $speed){ |
|---|
| 221 |
$speed_arr = explode(',', $speed); |
|---|
| 222 |
$speed = $speed_arr[0]; |
|---|
| 223 |
$desc = $speed_arr[1]; |
|---|
| 224 |
|
|---|
| 225 |
$speed = $db->escapeSimple($speed); |
|---|
| 226 |
|
|---|
| 227 |
if (!$desc){ |
|---|
| 228 |
$sql = "SELECT d.`description` |
|---|
| 229 |
FROM devices d |
|---|
| 230 |
WHERE d.`id` = '$speed'"; |
|---|
| 231 |
|
|---|
| 232 |
$res = $db->getone($sql); |
|---|
| 233 |
if(DB::IsError($res)) { |
|---|
| 234 |
die_freepbx($res->getMessage().$sql); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
$desc = $res; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
if ($desc != ''){ |
|---|
| 241 |
$desc = str_replace(array(',', ';'), '', $desc); |
|---|
| 242 |
$str .= "$speed,$desc"; |
|---|
| 243 |
if ($type != '7905' && $type != '7912') |
|---|
| 244 |
$str .= ",$speed@from-internal"; |
|---|
| 245 |
}else{ |
|---|
| 246 |
$str .= "$speed,$speed"; |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
$str .= ';'; |
|---|
| 251 |
$desc = ''; |
|---|
| 252 |
} |
|---|
| 253 |
return $str; |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
function chan_sccp_create_device($mac, $ext, $type, $name, $speeds){ |
|---|
| 257 |
global $db; |
|---|
| 258 |
|
|---|
| 259 |
$sep = 'SEP' . $db->escapeSimple(strtoupper($mac)); |
|---|
| 260 |
|
|---|
| 261 |
$ext = (int) $ext; |
|---|
| 262 |
$type = $db->escapeSimple($type); |
|---|
| 263 |
$name = $db->escapeSimple($name); |
|---|
| 264 |
$speeds = make_speeds($speeds, $type); |
|---|
| 265 |
|
|---|
| 266 |
$sql = "SELECT COUNT(*) |
|---|
| 267 |
FROM sccpdevice |
|---|
| 268 |
WHERE name = '$sep'"; |
|---|
| 269 |
|
|---|
| 270 |
$res = $db->getone($sql); |
|---|
| 271 |
if(DB::IsError($res)) { |
|---|
| 272 |
die_freepbx($res->getMessage().$sql); |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
if ($res > 0){ |
|---|
| 276 |
$sql = "UPDATE sccpdevice |
|---|
| 277 |
SET type = '$type', autologin = $ext, description = '$name', speeddial = '$speeds' |
|---|
| 278 |
WHERE name = '$sep'"; |
|---|
| 279 |
}else{ |
|---|
| 280 |
$sql = "INSERT INTO sccpdevice |
|---|
| 281 |
(type, autologin, description, speeddial, name) |
|---|
| 282 |
VALUES ('$type', $ext, '$name', '$speeds', '$sep')"; |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
$res = $db->query($sql); |
|---|
| 286 |
if(DB::IsError($res)) { |
|---|
| 287 |
die_freepbx($res->getMessage().$sql); |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
return $sep; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
function chan_sccp_create_tftp_SEP($mac, $cm_ip = ''){ |
|---|
| 294 |
if ($cm_ip == ''){ |
|---|
| 295 |
$cm_ip = exec('hostname -i'); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
$template = file_get_contents($_SERVER['PWD'] . '/modules/chan_sccp/SEPXML.txt'); |
|---|
| 299 |
|
|---|
| 300 |
$template = str_replace('{$cm_ip}', $cm_ip, $template); |
|---|
| 301 |
|
|---|
| 302 |
$filename = '/tftpboot/SEP' . $mac . '.cnf.xml'; |
|---|
| 303 |
file_put_contents($filename, $template); |
|---|
| 304 |
|
|---|
| 305 |
return true; |
|---|
| 306 |
} |
|---|
| 307 |
?> |
|---|