| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
// Setup arrays for device typesi |
|---|
| 4 |
function autoprovision_init_devices() { |
|---|
| 5 |
global $currentcomponent; |
|---|
| 6 |
|
|---|
| 7 |
$sql = "SELECT id FROM devices"; |
|---|
| 8 |
$results = sql($sql,"getAll"); |
|---|
| 9 |
foreach($results as $result){ |
|---|
| 10 |
if (checkRange($result[0])){ |
|---|
| 11 |
$currentcomponent->addoptlistitem('devicelist', $result[0], $result[0]); |
|---|
| 12 |
} |
|---|
| 13 |
} |
|---|
| 14 |
} |
|---|
| 15 |
|
|---|
| 16 |
//get the existing devices |
|---|
| 17 |
function autoprovision_devices_list() { |
|---|
| 18 |
$results = sql("SELECT id, mac FROM autoprovision ORDER BY id","getAll"); |
|---|
| 19 |
|
|---|
| 20 |
//only allow extensions that are within administrator's allowed range |
|---|
| 21 |
foreach($results as $result){ |
|---|
| 22 |
if (checkRange($result[0])){ |
|---|
| 23 |
$extens[] = array($result[0],$result[1]); |
|---|
| 24 |
} |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
if (isset($extens)) { |
|---|
| 28 |
sort($extens); |
|---|
| 29 |
return $extens; |
|---|
| 30 |
} else { |
|---|
| 31 |
return null; |
|---|
| 32 |
} |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
function autoprovision_devices_get($device){ |
|---|
| 36 |
global $db; |
|---|
| 37 |
$sql="SELECT id, mac FROM autoprovision where id='$device'"; |
|---|
| 38 |
$results = $db->getAssoc($sql); |
|---|
| 39 |
if(DB::IsError($results)) { |
|---|
| 40 |
$results = null; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
return $results; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
function autoprovision_macs_get($mac){ |
|---|
| 47 |
global $db; |
|---|
| 48 |
$sql="SELECT mac, id FROM autoprovision where mac='$mac'"; |
|---|
| 49 |
$results = $db->getAssoc($sql); |
|---|
| 50 |
if(DB::IsError($results)) { |
|---|
| 51 |
$results = null; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
return $results; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
function autoprovision_device_add($device, $mac){ |
|---|
| 58 |
|
|---|
| 59 |
$sql="INSERT INTO autoprovision (id,mac) VALUES ('$device','$mac')"; |
|---|
| 60 |
sql($sql); |
|---|
| 61 |
|
|---|
| 62 |
autoprovision_update_config($device,$mac); |
|---|
| 63 |
|
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
function autoprovision_device_edit($device, $mac){ |
|---|
| 67 |
|
|---|
| 68 |
$sql="UPDATE autoprovision SET mac='$mac' WHERE id='$device'"; |
|---|
| 69 |
sql($sql); |
|---|
| 70 |
|
|---|
| 71 |
autoprovision_update_config($device,$mac); |
|---|
| 72 |
|
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
function autoprovision_device_del($device,$mac) { |
|---|
| 76 |
|
|---|
| 77 |
$sql="DELETE FROM autoprovision WHERE id='$device'"; |
|---|
| 78 |
sql($sql); |
|---|
| 79 |
|
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
function autoprovision_mac_del($device,$mac) { |
|---|
| 83 |
|
|---|
| 84 |
$sql="DELETE FROM autoprovision WHERE mac='$mac'"; |
|---|
| 85 |
sql($sql); |
|---|
| 86 |
|
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
function autoprovision_update_config($device, $mac) { |
|---|
| 92 |
|
|---|
| 93 |
$values = core_devices_getsip($device); |
|---|
| 94 |
$values['mac'] = $mac; |
|---|
| 95 |
|
|---|
| 96 |
switch (substr($mac,0,6)) { |
|---|
| 97 |
|
|---|
| 98 |
case "0004f2": |
|---|
| 99 |
autoprovision_create_polycom($mac,$values); |
|---|
| 100 |
break; |
|---|
| 101 |
case "000b82": |
|---|
| 102 |
autoprovision_create_grandstream($mac,$values); |
|---|
| 103 |
break; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
function autoprovision_create_polycom($mac,$values) { |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
$templates = array("MAC.cfg","MAC-phone1.cfg"); |
|---|
| 112 |
|
|---|
| 113 |
foreach($templates as $template) { |
|---|
| 114 |
|
|---|
| 115 |
$file_contents = file_get_contents("modules/autoprovision/templates/$template"); |
|---|
| 116 |
$file_contents = autoprovision_replace_values($values,$file_contents); |
|---|
| 117 |
|
|---|
| 118 |
$template = str_replace("MAC",$mac,$template); |
|---|
| 119 |
|
|---|
| 120 |
$template_temp = "/tmp/$template"; |
|---|
| 121 |
|
|---|
| 122 |
file_put_contents($template_temp,$file_contents); |
|---|
| 123 |
|
|---|
| 124 |
autoprovision_upload($template_temp,$template); |
|---|
| 125 |
|
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
function autoprovision_create_grandstream($mac,$values) { |
|---|
| 131 |
|
|---|
| 132 |
$template ="cfgMAC"; |
|---|
| 133 |
|
|---|
| 134 |
$file_contents = file_get_contents("modules/autoprovision/templates/$template"); |
|---|
| 135 |
$file_contents = autoprovision_replace_values($values,$file_contents); |
|---|
| 136 |
|
|---|
| 137 |
$template = str_replace("MAC",$mac,$template); |
|---|
| 138 |
$template_temp = "/tmp/$template"; |
|---|
| 139 |
$template_temp_cfg = "$template_temp.temp"; |
|---|
| 140 |
|
|---|
| 141 |
file_put_contents($template_temp_cfg,$file_contents); |
|---|
| 142 |
|
|---|
| 143 |
exec("modules/autoprovision/helpers/config.php $template_temp_cfg $mac > $template_temp"); |
|---|
| 144 |
|
|---|
| 145 |
#echo $template; |
|---|
| 146 |
|
|---|
| 147 |
autoprovision_upload($template_temp,$template); |
|---|
| 148 |
|
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
function autoprovision_replace_values($values,$str) { |
|---|
| 152 |
|
|---|
| 153 |
foreach ($values as $keyword => $data) { |
|---|
| 154 |
$str = str_replace("%" . strtoupper($keyword) . "%", $data, $str); |
|---|
| 155 |
} |
|---|
| 156 |
return $str; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
function autoprovision_display_edit($extdisplay) { |
|---|
| 160 |
global $currentcomponent; |
|---|
| 161 |
|
|---|
| 162 |
$section = "_top"; |
|---|
| 163 |
$currentcomponent->addguielem('_top', new gui_hidden('extdisplay', $extdisplay)); |
|---|
| 164 |
$currentcomponent->addguielem('_top', new gui_hidden('action', 'edit')); |
|---|
| 165 |
$currentcomponent->addguielem($section, new gui_pageheading('title', _("Edit Device").": $extdisplay")); |
|---|
| 166 |
$delURL = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&action=del"; |
|---|
| 167 |
$currentcomponent->addguielem($section, new gui_link('del', _("Delete Device")." $extdisplay", $delURL, true, false)); |
|---|
| 168 |
|
|---|
| 169 |
autoprovision_display_mac_section($extdisplay); |
|---|
| 170 |
|
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
function autoprovision_display_mac_section($extdisplay) { |
|---|
| 174 |
global $currentcomponent; |
|---|
| 175 |
|
|---|
| 176 |
$deviceInfo=autoprovision_devices_get($extdisplay); |
|---|
| 177 |
$mac=$deviceInfo[$extdisplay]; |
|---|
| 178 |
|
|---|
| 179 |
// Setup two option lists we need |
|---|
| 180 |
// Enable / Disable list |
|---|
| 181 |
$currentcomponent->addoptlistitem('autoprovisionena', 'enabled', 'Enabled'); |
|---|
| 182 |
$currentcomponent->addoptlistitem('autoprovisionena', 'disabled', 'Disabled'); |
|---|
| 183 |
$currentcomponent->setoptlistopts('autoprovisionena', 'sort', false); |
|---|
| 184 |
|
|---|
| 185 |
$apena = $mac==null?"disabled":"enabled"; |
|---|
| 186 |
|
|---|
| 187 |
$section = "Auto Provision"; |
|---|
| 188 |
$currentcomponent->addguielem( |
|---|
| 189 |
$section, |
|---|
| 190 |
new gui_selectbox( |
|---|
| 191 |
'autoprovision', |
|---|
| 192 |
$currentcomponent->getoptlist( |
|---|
| 193 |
'autoprovisionena' |
|---|
| 194 |
), |
|---|
| 195 |
$apena, |
|---|
| 196 |
'Status', |
|---|
| 197 |
'', |
|---|
| 198 |
false |
|---|
| 199 |
) |
|---|
| 200 |
); |
|---|
| 201 |
$currentcomponent->addguielem($section, new gui_textbox('mac', $mac, 'MAC Address:', 'The MAC address of the phone')); |
|---|
| 202 |
|
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
function autoprovision_display_add($extdisplay) { |
|---|
| 206 |
global $currentcomponent; |
|---|
| 207 |
|
|---|
| 208 |
$section = "_top"; |
|---|
| 209 |
$currentcomponent->addguielem('_top', new gui_hidden('extdisplay', $extdisplay)); |
|---|
| 210 |
$currentcomponent->addguielem('_top', new gui_hidden('action', 'add')); |
|---|
| 211 |
$currentcomponent->addguielem($section, new gui_pageheading('title', _("Add Device"))); |
|---|
| 212 |
|
|---|
| 213 |
autoprovision_init_devices(); |
|---|
| 214 |
|
|---|
| 215 |
$section= _("Device"); |
|---|
| 216 |
$currentcomponent->addguielem( |
|---|
| 217 |
$section, |
|---|
| 218 |
new gui_selectbox( |
|---|
| 219 |
'device', |
|---|
| 220 |
$currentcomponent->getoptlist( |
|---|
| 221 |
'devicelist' |
|---|
| 222 |
), |
|---|
| 223 |
'', |
|---|
| 224 |
_("Device"), |
|---|
| 225 |
"The Device", |
|---|
| 226 |
false, |
|---|
| 227 |
'' |
|---|
| 228 |
) |
|---|
| 229 |
); |
|---|
| 230 |
|
|---|
| 231 |
autoprovision_display_mac_section($extdisplay); |
|---|
| 232 |
|
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
function autoprovision_display_tools() { |
|---|
| 236 |
global $currentcomponent; |
|---|
| 237 |
|
|---|
| 238 |
$section = "_top"; |
|---|
| 239 |
$currentcomponent->addguielem('_top', new gui_hidden('extdisplay', $settings)); |
|---|
| 240 |
$currentcomponent->addguielem('_top', new gui_hidden('action', 'update_settings')); |
|---|
| 241 |
$currentcomponent->addguielem($section, new gui_pageheading('title', 'Settings')); |
|---|
| 242 |
$detectURL = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&action=detect"; |
|---|
| 243 |
$currentcomponent->addguielem($section, new gui_link('del', _("Detect and match Phones")." $extdisplay", $detectURL, true, false)); |
|---|
| 244 |
$detectURL = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&action=updateall"; |
|---|
| 245 |
$currentcomponent->addguielem($section, new gui_link('del', _("Update All Config Files")." $extdisplay", $detectURL, true, false)); |
|---|
| 246 |
|
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
function autoprovision_display_detect() { |
|---|
| 251 |
global $currentcomponent; |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
$section = "_top"; |
|---|
| 255 |
$currentcomponent->addguielem($section, new gui_pageheading('title', 'Detect MACs')); |
|---|
| 256 |
$currentcomponent->addguielem('_top', new gui_hidden('action', 'scan')); |
|---|
| 257 |
|
|---|
| 258 |
$section = "Range to scan"; |
|---|
| 259 |
$currentcomponent->addguielem($section, new gui_textbox('range', '', 'Seach Range:', 'The IP Addresses to Ping')); |
|---|
| 260 |
|
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
function autoprovision_display_scanned() { |
|---|
| 264 |
global $currentcomponent; |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
$section = "_top"; |
|---|
| 268 |
$currentcomponent->addguielem($section, new gui_pageheading('title', 'Detect MACs')); |
|---|
| 269 |
$currentcomponent->addguielem('_top', new gui_hidden('action', 'addmacs')); |
|---|
| 270 |
|
|---|
| 271 |
$range = $_REQUEST['range']; |
|---|
| 272 |
exec("nmap -sP $range"); |
|---|
| 273 |
exec('arp -n', $output); |
|---|
| 274 |
|
|---|
| 275 |
foreach ($output as $str) { |
|---|
| 276 |
if (preg_match("/(00:04:F2(:[0-9A-Fa-f]{2}){3})/", $str, $matches)) |
|---|
| 277 |
$macs[] = strtolower( str_replace(":","",$matches[0])); |
|---|
| 278 |
|
|---|
| 279 |
if (preg_match("/(00:0B:82(:[0-9A-Fa-f]{2}){3})/", $str, $matches)) |
|---|
| 280 |
$macs[]= strtolower( str_replace(":","",$matches[0])); |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
autoprovision_init_devices(); |
|---|
| 284 |
|
|---|
| 285 |
$section = "Assign MACs to Devices"; |
|---|
| 286 |
$list = $currentcomponent->getoptlist('devicelist'); |
|---|
| 287 |
foreach($macs as $mac) { |
|---|
| 288 |
|
|---|
| 289 |
$deviceInfo=autoprovision_macs_get($mac); |
|---|
| 290 |
$device=$deviceInfo[$mac]; |
|---|
| 291 |
|
|---|
| 292 |
$currentcomponent->addguielem( |
|---|
| 293 |
$section, |
|---|
| 294 |
new gui_selectbox( |
|---|
| 295 |
'mac['.$mac.']', |
|---|
| 296 |
$list, |
|---|
| 297 |
$device, |
|---|
| 298 |
$mac.": ", |
|---|
| 299 |
"Assign a MAC address to a Device" |
|---|
| 300 |
) |
|---|
| 301 |
); |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
function autoprovision_detect_addmacs () { |
|---|
| 307 |
global $currentcomponent; |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
$section = "_top"; |
|---|
| 311 |
$currentcomponent->addguielem($section, new gui_pageheading('title', 'Detect MACs')); |
|---|
| 312 |
|
|---|
| 313 |
$macs = $_REQUEST['mac']; |
|---|
| 314 |
|
|---|
| 315 |
foreach($macs as $mac => $device){ |
|---|
| 316 |
|
|---|
| 317 |
autoprovision_mac_del($device, $mac); |
|---|
| 318 |
autoprovision_device_del($device, $mac); |
|---|
| 319 |
|
|---|
| 320 |
if($device!='') |
|---|
| 321 |
autoprovision_device_add($device, $mac); |
|---|
| 322 |
|
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
redirect_standard(); |
|---|
| 327 |
|
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
function autoprovision_updateall ($action) { |
|---|
| 331 |
|
|---|
| 332 |
$items = autoprovision_devices_list(); |
|---|
| 333 |
|
|---|
| 334 |
foreach($items as $item) |
|---|
| 335 |
autoprovision_update_config($item[0], $item[1]); |
|---|
| 336 |
|
|---|
| 337 |
redirect_standard(); |
|---|
| 338 |
|
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
function autoprovision_upload($src,$dst) { |
|---|
| 342 |
|
|---|
| 343 |
$autoprovision_tftp_put ="tftp localhost -c put"; |
|---|
| 344 |
|
|---|
| 345 |
exec("$autoprovision_tftp_put $src $dst"); |
|---|
| 346 |
#echo("$autoprovision_tftp_put $src $dst<br/>\n"); |
|---|
| 347 |
|
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
function autoprovision_configpageinit($pagename) { |
|---|
| 352 |
global $currentcomponent; |
|---|
| 353 |
|
|---|
| 354 |
$action = isset($_REQUEST['action'])?$_REQUEST['action']:null; |
|---|
| 355 |
$extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null; |
|---|
| 356 |
$extension = isset($_REQUEST['extension'])?$_REQUEST['extension']:null; |
|---|
| 357 |
$tech_hardware = isset($_REQUEST['tech_hardware'])?$_REQUEST['tech_hardware']:null; |
|---|
| 358 |
|
|---|
| 359 |
if ($pagename != 'devices' && $pagename != 'extensions') |
|---|
| 360 |
return true; |
|---|
| 361 |
|
|---|
| 362 |
if ($tech_hardware != null ) { |
|---|
| 363 |
$currentcomponent->addguifunc('autoprovision_configpageload'); |
|---|
| 364 |
} elseif ($action!=null) { |
|---|
| 365 |
$currentcomponent->addprocessfunc('autoprovision_configprocess', 1); |
|---|
| 366 |
} elseif ($extdisplay != '' || $pagename == 'devices') { |
|---|
| 367 |
$currentcomponent->addguifunc('autoprovision_configpageload'); |
|---|
| 368 |
} |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
function autoprovision_configpageload() { |
|---|
| 372 |
global $currentcomponent; |
|---|
| 373 |
|
|---|
| 374 |
$extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:null; |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
autoprovision_display_mac_section($extdisplay); |
|---|
| 379 |
|
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
function autoprovision_configprocess() { |
|---|
| 383 |
|
|---|
| 384 |
$action = isset($_REQUEST['action'])?$_REQUEST['action']:null; |
|---|
| 385 |
$apena = isset($_REQUEST['autoprovision'])?$_REQUEST['autoprovision']:null; |
|---|
| 386 |
$device = isset($_REQUEST['deviceid'])?$_REQUEST['deviceid']:null; |
|---|
| 387 |
$device = isset($_GET['extdisplay'])?$_GET['extdisplay']:$device; |
|---|
| 388 |
$device = isset($_REQUEST['extension'])?$_REQUEST['extension']:$device; |
|---|
| 389 |
$mac = isset($_REQUEST['mac'])?$_REQUEST['mac']:null; |
|---|
| 390 |
$mac = str_replace(":","",strtolower($mac)); |
|---|
| 391 |
$now = date(DATE_ATOM,time()); |
|---|
| 392 |
$checkdevicemac = autoprovision_check_device_mac($device, $mac); |
|---|
| 393 |
|
|---|
| 394 |
//$deviceInfo=autoprovision_devices_get($device); |
|---|
| 395 |
//$mac_old=$deviceInfo[$extdisplay]; |
|---|
| 396 |
//if($mac_old==null) $action="add"; |
|---|
| 397 |
|
|---|
| 398 |
if($action=="edit" && !$checkdevicemac) $action="add"; |
|---|
| 399 |
|
|---|
| 400 |
if($mac==null || $mac=='') $action="del"; |
|---|
| 401 |
|
|---|
| 402 |
if($apena=="disabled") $action="del"; |
|---|
| 403 |
|
|---|
| 404 |
switch ($action) { |
|---|
| 405 |
case "add": |
|---|
| 406 |
file_put_contents("/tmp/t.txt","A:$action AP:$apena D:$device M:$mac T:$now\n",FILE_APPEND); |
|---|
| 407 |
autoprovision_device_add($device, $mac); |
|---|
| 408 |
break; |
|---|
| 409 |
case "edit": |
|---|
| 410 |
file_put_contents("/tmp/t.txt","A:$action AP:$apena D:$device M:$mac T:$now\n",FILE_APPEND); |
|---|
| 411 |
autoprovision_device_edit($device, $mac); |
|---|
| 412 |
break; |
|---|
| 413 |
case "del": |
|---|
| 414 |
file_put_contents("/tmp/t.txt","A:$action AP:$apena D:$device M:$mac T:$now\n",FILE_APPEND); |
|---|
| 415 |
autoprovision_device_del($device, $mac); |
|---|
| 416 |
break; |
|---|
| 417 |
|
|---|
| 418 |
} |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
function autoprovision_check_device_mac($device, $mac){ |
|---|
| 423 |
if( autoprovision_macs_get($mac) || autoprovision_devices_get($device)) { |
|---|
| 424 |
return true; |
|---|
| 425 |
} |
|---|
| 426 |
return false; |
|---|
| 427 |
} |
|---|