From b9711091f449b59548eded33dbcfa9a457541343 Mon Sep 17 00:00:00 2001 From: Mark Crane Date: Thu, 5 Jul 2012 18:22:02 +0000 Subject: [PATCH] Fix bugs in provisioning. When an extension is disabled don't provide provision it. Normalize the MAC address when stored in the db as lower case and no formatting. When using the MAC address in the template change it to upper case the MAC address for SNOM with no dashes. --- app/extensions/v_extensions_edit.php | 12 +- .../v_hardware_phones_edit.php | 44 ++++++-- app/provision/app_defaults.php | 51 +++++++++ app/provision/index.php | 106 ++++++++++-------- app/provision/provision_write.php | 49 +++++++- 5 files changed, 201 insertions(+), 61 deletions(-) create mode 100644 app/provision/app_defaults.php diff --git a/app/extensions/v_extensions_edit.php b/app/extensions/v_extensions_edit.php index 603f54a867..17142d2a70 100644 --- a/app/extensions/v_extensions_edit.php +++ b/app/extensions/v_extensions_edit.php @@ -916,18 +916,14 @@ if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) { echo "\n"; foreach($result as $row) { + $phone_mac_address = $row['phone_mac_address']; + $phone_mac_address = substr($phone_mac_address, 0,2).'-'.substr($phone_mac_address, 2,2).'-'.substr($phone_mac_address, 4,2).'-'.substr($phone_mac_address, 6,2).'-'.substr($phone_mac_address, 8,2).'-'.substr($phone_mac_address, 10,2); if ($row['phone_mac_address'] == $select_mac_address) { - echo "\n"; + echo "\n"; } else { - echo "\n"; + echo "\n"; } - //$row[phone_mac_address] - //$row[phone_vendor] - //$row[phone_model] - //$row[phone_provision_enable] - //$row[phone_description] - //$row[hardware_phone_uuid] } //end foreach unset($sql, $result, $row_count); echo "\n"; diff --git a/app/hardware_phones/v_hardware_phones_edit.php b/app/hardware_phones/v_hardware_phones_edit.php index 7644e2be42..c93e7d6f13 100644 --- a/app/hardware_phones/v_hardware_phones_edit.php +++ b/app/hardware_phones/v_hardware_phones_edit.php @@ -48,6 +48,7 @@ require_once "includes/require.php"; if (count($_POST)>0) { $phone_mac_address = check_str($_POST["phone_mac_address"]); $phone_mac_address = strtolower($phone_mac_address); + $phone_mac_address = preg_replace('#[^a-fA-F0-9./]#', '', $phone_mac_address); $phone_label = check_str($_POST["phone_label"]); $phone_vendor = check_str($_POST["phone_vendor"]); $phone_model = check_str($_POST["phone_model"]); @@ -60,6 +61,40 @@ require_once "includes/require.php"; $phone_description = check_str($_POST["phone_description"]); } +//use the mac address to find the vendor + if (strlen($phone_vendor) == 0) { + switch (substr($phone_mac_address, 0, 6)) { + case "00085d": + $phone_vendor = "aastra"; + break; + case "000e08": + $phone_vendor = "linksys"; + break; + case "0004f2": + $phone_vendor = "polycom"; + break; + case "00907a": + $phone_vendor = "polycom"; + break; + case "001873": + $phone_vendor = "cisco"; + break; + case "00045a": + $phone_vendor = "linksys"; + break; + case "000625": + $phone_vendor = "linksys"; + break; + case "001565": + $phone_vendor = "yealink"; + break; + case "000413": + $phone_vendor = "snom"; + default: + $phone_vendor = ""; + } + } + //add or update the database if (count($_POST)>0 && strlen($_POST["persistformvar"]) == 0) { @@ -183,8 +218,7 @@ require_once "includes/require.php"; //pre-populate the form if (count($_GET)>0 && $_POST["persistformvar"] != "true") { $hardware_phone_uuid = $_GET["id"]; - $sql = ""; - $sql .= "select * from v_hardware_phones "; + $sql = "select * from v_hardware_phones "; $sql .= "where domain_uuid = '$domain_uuid' "; $sql .= "and hardware_phone_uuid = '$hardware_phone_uuid' "; $prep_statement = $db->prepare(check_sql($sql)); @@ -192,7 +226,7 @@ require_once "includes/require.php"; $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); foreach ($result as &$row) { $phone_mac_address = $row["phone_mac_address"]; - $phone_mac_address = strtolower($phone_mac_address); + $phone_mac_address = substr($phone_mac_address, 0,2).'-'.substr($phone_mac_address, 2,2).'-'.substr($phone_mac_address, 4,2).'-'.substr($phone_mac_address, 6,2).'-'.substr($phone_mac_address, 8,2).'-'.substr($phone_mac_address, 10,2); $phone_label = $row["phone_label"]; $phone_vendor = $row["phone_vendor"]; $phone_model = $row["phone_model"]; @@ -203,7 +237,6 @@ require_once "includes/require.php"; $phone_password = $row["phone_password"]; $phone_time_zone = $row["phone_time_zone"]; $phone_description = $row["phone_description"]; - break; //limit to 1 row } unset ($prep_statement); } @@ -213,14 +246,11 @@ require_once "includes/require.php"; echo "
"; echo "\n"; - echo "\n"; echo "
\n"; echo "
"; - echo "
\n"; - echo "
\n"; echo "\n"; diff --git a/app/provision/app_defaults.php b/app/provision/app_defaults.php new file mode 100644 index 0000000000..ddfc03342d --- /dev/null +++ b/app/provision/app_defaults.php @@ -0,0 +1,51 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//normalize the mac address + $sql = "select hardware_phone_uuid, phone_mac_address "; + $sql .= "from v_hardware_phones "; + $sql .= "where domain_uuid = '$domain_uuid' "; + $sql .= "and (phone_mac_address like '%-%' or phone_mac_address like '%:%') "; + $prep_statement = $db->prepare(check_sql($sql)); + if ($prep_statement) { + $prep_statement->execute(); + $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); + foreach($result as $row) { + $hardware_phone_uuid = $row["hardware_phone_uuid"]; + $phone_mac_address = $row["phone_mac_address"]; + $phone_mac_address = strtolower($phone_mac_address); + $phone_mac_address = preg_replace('#[^a-fA-F0-9./]#', '', $phone_mac_address); + + $sql = "update v_hardware_phones set "; + $sql .= "phone_mac_address = '".$phone_mac_address."' "; + $sql .= "where hardware_phone_uuid = '".$hardware_phone_uuid."' "; + $db->exec(check_sql($sql)); + unset($sql); + } + } + unset($prep_statement, $result); + +?> \ No newline at end of file diff --git a/app/provision/index.php b/app/provision/index.php index 1c93a279c0..ff5464d81f 100644 --- a/app/provision/index.php +++ b/app/provision/index.php @@ -33,7 +33,7 @@ require_once "includes/require.php"; $phone_template = ''; //get any system -> variables defined in the 'provision; - $sql .= "select * from v_vars "; + $sql = "select * from v_vars "; $sql .= "where var_enabled = 'true' "; $sql .= "and var_cat = 'Provision' "; $prep_statement = $db->prepare(check_sql($sql)); @@ -53,7 +53,7 @@ require_once "includes/require.php"; if (strlen($password) > 0) { //deny access if the password doesn't match if ($password != $_REQUEST['password']) { - //Log the failed auth attempt to the system, to be available for fail2ban. + //log the failed auth attempt to the system, to be available for fail2ban. openlog('FusionPBX', LOG_NDELAY, LOG_AUTH); syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] provision attempt bad password for ".$_REQUEST['mac']); closelog(); @@ -73,24 +73,55 @@ require_once "includes/require.php"; } } -//define variables from HTTP GET +//define PHP variables from the HTTP values $mac = $_REQUEST['mac']; + $file = $_REQUEST['file']; if (strlen($_REQUEST['template']) > 0) { $phone_template = $_REQUEST['template']; } - if(empty($mac)){//check alternate MAC source - if($_SERVER['HTTP_USER_AGENT'][strlen($_SERVER['HTTP_USER_AGENT'])-17-1]==" ") { - $mac= substr($_SERVER['HTTP_USER_AGENT'],-17); - }//Yealink: 17 digit mac appended to the user agent, so check for a space exactly 17 digits before the end. - }//check alternates +//check alternate MAC source + if (empty($mac)){ + if($_SERVER['HTTP_USER_AGENT'][strlen($_SERVER['HTTP_USER_AGENT'])-17-1] == " ") { + $mac = substr($_SERVER['HTTP_USER_AGENT'],-17); + } //Yealink: 17 digit mac appended to the user agent, so check for a space exactly 17 digits before the end. + }//check alternates +//prepare the mac address $mac = strtolower($mac); - $mac = str_replace(":", "-", $mac); - if (strlen($mac) == 12) { - $mac = substr($mac, 0,2).'-'.substr($mac, 2,2).'-'.substr($mac, 4,2).'-'.substr($mac, 6,2).'-'.substr($mac, 8,2).'-'.substr($mac, 10,2); + $mac = preg_replace('#[^a-fA-F0-9./]#', '', $mac); + +//use the mac address to find the vendor + switch (substr($mac, 0, 6)) { + case "00085d": + $phone_vendor = "aastra"; + break; + case "000e08": + $phone_vendor = "linksys"; + break; + case "0004f2": + $phone_vendor = "polycom"; + break; + case "00907a": + $phone_vendor = "polycom"; + break; + case "001873": + $phone_vendor = "cisco"; + break; + case "00045a": + $phone_vendor = "linksys"; + break; + case "000625": + $phone_vendor = "linksys"; + break; + case "001565": + $phone_vendor = "yealink"; + break; + case "000413": + $phone_vendor = "snom"; + default: + $phone_vendor = ""; } - $file = $_REQUEST['file']; //check to see if the mac_address exists in v_hardware_phones if (mac_exists_in_v_hardware_phones($db, $mac)) { @@ -106,7 +137,9 @@ require_once "includes/require.php"; $prep_statement_2->execute(); $row = $prep_statement_2->fetch(); $phone_label = $row["phone_label"]; - $phone_vendor = $row["phone_vendor"]; + if (strlen($row["phone_vendor"]) > 0) { + $phone_vendor = $row["phone_vendor"]; + } $phone_model = $row["phone_model"]; $phone_firmware_version = $row["phone_firmware_version"]; $phone_provision_enable = $row["phone_provision_enable"]; @@ -142,37 +175,6 @@ require_once "includes/require.php"; } } else { - //mac does not exist in v_hardware_phones add it to the table - //use the mac address to find the vendor - switch (substr($mac, 0, 8)) { - case "00-08-5d": - $phone_vendor = "aastra"; - break; - case "00-0e-08": - $phone_vendor = "linksys"; - break; - case "00-04-f2": - $phone_vendor = "polycom"; - break; - case "00-90-7a": - $phone_vendor = "polycom"; - break; - case "00-18-73": - $phone_vendor = "cisco"; - break; - case "00-04-5a": - $phone_vendor = "linksys"; - break; - case "00-06-25": - $phone_vendor = "linksys"; - break; - case "00-15-65": - $phone_vendor = "yealink"; - break; - default: - $phone_vendor = ""; - } - //use the user_agent to pre-assign a template for 1-hit provisioning. Enter the a unique string to match in the user agent, and the template it should match. $template_list=array( "Linksys/SPA-2102"=>"linksys/spa2102", @@ -187,7 +189,7 @@ require_once "includes/require.php"; } unset($template_list); - //the mac address does not exist in the table so add it + //mac address does not exist in the table so add it $hardware_phone_uuid = uuid(); $sql = "insert into v_hardware_phones "; $sql .= "("; @@ -297,6 +299,7 @@ require_once "includes/require.php"; $sql = "select * from v_extensions "; $sql .= "where provisioning_list like '%|".$mac.":%' "; $sql .= "and domain_uuid = '$domain_uuid' "; + $sql .= "and enabled = 'true' "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); @@ -334,6 +337,19 @@ require_once "includes/require.php"; } unset ($prep_statement); + //set the mac address in the correct format + switch ($phone_vendor) { + case "aastra": + $mac = strtoupper($mac); + break; + case "snom": + $mac = strtoupper($mac); + $mac = str_replace("-", "", $mac); + default: + $mac = strtolower($mac); + $mac = substr($mac, 0,2).'-'.substr($mac, 2,2).'-'.substr($mac, 4,2).'-'.substr($mac, 6,2).'-'.substr($mac, 8,2).'-'.substr($mac, 10,2); + } + //replace the variables in the template in the future loop through all the line numbers to do a replace for each possible line number $file_contents = str_replace("{v_mac}", $mac, $file_contents); $file_contents = str_replace("{v_label}", $phone_label, $file_contents); diff --git a/app/provision/provision_write.php b/app/provision/provision_write.php index 688a19d6ce..2a7b4beb69 100644 --- a/app/provision/provision_write.php +++ b/app/provision/provision_write.php @@ -62,7 +62,6 @@ else { $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); foreach ($result as &$row) { $phone_mac_address = $row["phone_mac_address"]; - $phone_mac_address = strtolower($phone_mac_address); $phone_label = $row["phone_label"]; $phone_vendor = $row["phone_vendor"]; $phone_model = $row["phone_model"]; @@ -74,6 +73,53 @@ else { $phone_time_zone = $row["phone_time_zone"]; $phone_description = $row["phone_description"]; + //use the mac address to find the vendor + if (strlen($phone_vendor) == 0) { + switch (substr($phone_mac_address, 0, 6)) { + case "00085d": + $phone_vendor = "aastra"; + break; + case "000e08": + $phone_vendor = "linksys"; + break; + case "0004f2": + $phone_vendor = "polycom"; + break; + case "00907a": + $phone_vendor = "polycom"; + break; + case "001873": + $phone_vendor = "cisco"; + break; + case "00045a": + $phone_vendor = "linksys"; + break; + case "000625": + $phone_vendor = "linksys"; + break; + case "001565": + $phone_vendor = "yealink"; + break; + case "000413": + $phone_vendor = "snom"; + default: + $phone_vendor = ""; + } + } + + //set the mac address in the correct format + switch ($phone_vendor) { + case "aastra": + $mac = strtoupper($mac); + break; + case "snom": + $mac = strtoupper($mac); + $mac = str_replace("-", "", $mac); + default: + $mac = strtolower($mac); + $mac = substr($mac, 0,2).'-'.substr($mac, 2,2).'-'.substr($mac, 4,2).'-'.substr($mac, 6,2).'-'.substr($mac, 8,2).'-'.substr($mac, 10,2); + } + //loop through the provision template directory $provision_template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/includes/templates/provision/".$phone_template; @@ -151,6 +197,7 @@ else { $sql2 = "select * from v_extensions "; $sql2 .= "where provisioning_list like '%$phone_mac_address%' "; $sql2 .= "and domain_uuid = '$domain_uuid' "; + $sql2 .= "and enabled = 'true' "; $prep_statement_2 = $db->prepare(check_sql($sql2)); $prep_statement_2->execute(); $result2 = $prep_statement_2->fetchAll(PDO::FETCH_NAMED);