Provision: Database class integration.

This commit is contained in:
Nate
2019-08-12 06:03:27 -06:00
parent 6fdfe0cf75
commit 96c1ff5edf
2 changed files with 122 additions and 138 deletions
+98 -107
View File
@@ -39,16 +39,16 @@
$device_template = ''; $device_template = '';
//define PHP variables from the HTTP values //define PHP variables from the HTTP values
$mac = check_str($_REQUEST['mac']); $mac = $_REQUEST['mac'];
$file = check_str($_REQUEST['file']); $file = $_REQUEST['file'];
$ext = check_str($_REQUEST['ext']); $ext = $_REQUEST['ext'];
//if (strlen(check_str($_REQUEST['template'])) > 0) { //if (strlen($_REQUEST['template']) > 0) {
// $device_template = check_str($_REQUEST['template']); // $device_template = $_REQUEST['template'];
//} //}
//get the mac address for Cisco 79xx in the URL as &name=SEP000000000000 //get the mac address for Cisco 79xx in the URL as &name=SEP000000000000
if (empty($mac)){ if (empty($mac)) {
$name = check_str($_REQUEST['name']); $name = $_REQUEST['name'];
if (substr($name, 0, 3) == "SEP") { if (substr($name, 0, 3) == "SEP") {
$mac = strtolower(substr($name, 3, 12)); $mac = strtolower(substr($name, 3, 12));
unset($name); unset($name);
@@ -58,11 +58,11 @@
// Escence make request based on UserID for Memory keys // Escence make request based on UserID for Memory keys
// The file name is fixed to `Account1_Extern.xml`. // The file name is fixed to `Account1_Extern.xml`.
// (Account1 is the first account you register) // (Account1 is the first account you register)
if(empty($mac) && !empty($ext)){ if (empty($mac) && !empty($ext)) {
$domain_array = explode(":", $_SERVER["HTTP_HOST"]); $domain_array = explode(":", $_SERVER["HTTP_HOST"]);
$domain_name = $domain_array[0]; $domain_name = $domain_array[0];
$device = device_by_ext($db, $ext, $domain_name); $device = device_by_ext($db, $ext, $domain_name);
if(($device !== false)&&(($device['device_vendor']=='escene')||($device['device_vendor']=='grandstream'))){ if ($device !== false && ($device['device_vendor'] == 'escene' || $device['device_vendor'] == 'grandstream')) {
$mac = $device['device_mac_address']; $mac = $device['device_mac_address'];
} }
} }
@@ -79,11 +79,11 @@
echo "</body>\n"; echo "</body>\n";
echo "</html>\n"; echo "</html>\n";
} }
exit(); exit;
} }
//check alternate MAC source //check alternate MAC source
if (empty($mac)){ if (empty($mac)) {
//set the http user agent //set the http user agent
//$_SERVER['HTTP_USER_AGENT'] = "Yealink SIP-T38G 38.70.0.125 00:15:65:00:00:00"; //$_SERVER['HTTP_USER_AGENT'] = "Yealink SIP-T38G 38.70.0.125 00:15:65:00:00:00";
//$_SERVER['HTTP_USER_AGENT'] = "Yealink SIP-T56A 58.80.0.25 001565f429a4"; //$_SERVER['HTTP_USER_AGENT'] = "Yealink SIP-T56A 58.80.0.25 001565f429a4";
@@ -139,17 +139,12 @@
//get the domain_name and domain_uuid //get the domain_name and domain_uuid
if ($_SESSION['provision']['http_domain_filter']['boolean'] == "false") { if ($_SESSION['provision']['http_domain_filter']['boolean'] == "false") {
//get the domain_uuid //get the domain_uuid
$sql = "SELECT domain_uuid FROM v_devices "; $sql = "select domain_uuid from v_devices ";
$sql .= "WHERE device_mac_address = :mac "; $sql .= "where device_mac_address = :mac ";
//$sql .= "WHERE device_mac_address = '".$mac."' "; $parameters['mac'] = $mac;
$prep_statement = $db->prepare($sql); $database = new database;
$prep_statement->bindParam(':mac', $mac); $domain_uuid = $database->select($sql, $parameters, 'column');
$prep_statement->execute(); unset($sql, $parameters);
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($result as $row) {
$domain_uuid = $row["domain_uuid"];
}
unset($result, $prep_statement);
$_SESSION['domain_uuid'] = $domain_uuid; $_SESSION['domain_uuid'] = $domain_uuid;
//get the domain name //get the domain name
@@ -161,85 +156,81 @@
//get the default settings //get the default settings
$sql = "select * from v_default_settings "; $sql = "select * from v_default_settings ";
$sql .= "where default_setting_enabled = 'true' "; $sql .= "where default_setting_enabled = 'true' ";
try { $sql .= "order by default_setting_order asc ";
$prep_statement = $db->prepare($sql . " order by default_setting_order asc "); $database = new database;
$prep_statement->execute(); $result = $database->select($sql, null, 'all');
}
catch(PDOException $e) {
$prep_statement = $db->prepare($sql);
$prep_statement->execute();
}
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
//unset the previous settings //unset the previous settings
foreach ($result as $row) { if (is_array($result) && @sizeof($result) != 0) {
unset($_SESSION[$row['default_setting_category']]);
}
//set the settings as a session
foreach ($result as $row) {
$name = $row['default_setting_name'];
$category = $row['default_setting_category'];
$subcategory = $row['default_setting_subcategory'];
if (strlen($subcategory) == 0) {
if ($name == "array") {
$_SESSION[$category][] = $row['default_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['default_setting_value'];
}
} else {
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['default_setting_value'];
}
else {
$_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
$_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
}
}
}
//get the domains settings
if (strlen($domain_uuid) > 0 && is_uuid($domain_uuid)) {
$sql = "select * from v_domain_settings ";
$sql .= "where domain_uuid = '" . $domain_uuid . "' ";
$sql .= "and domain_setting_enabled = 'true' ";
try {
$prep_statement = $db->prepare($sql . " order by domain_setting_order asc ");
$prep_statement->execute();
}
catch(PDOException $e) {
$prep_statement = $db->prepare($sql);
$prep_statement->execute();
}
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
//unset the arrays that domains are overriding
foreach ($result as $row) { foreach ($result as $row) {
$name = $row['domain_setting_name']; unset($_SESSION[$row['default_setting_category']]);
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($name == "array") {
unset($_SESSION[$category][$subcategory]);
}
} }
//set the settings as a session //set the settings as a session
foreach ($result as $row) { foreach ($result as $row) {
$name = $row['domain_setting_name']; $name = $row['default_setting_name'];
$category = $row['domain_setting_category']; $category = $row['default_setting_category'];
$subcategory = $row['domain_setting_subcategory']; $subcategory = $row['default_setting_subcategory'];
if (strlen($subcategory) == 0) { if (strlen($subcategory) == 0) {
//$$category[$name] = $row['domain_setting_value'];
if ($name == "array") { if ($name == "array") {
$_SESSION[$category][] = $row['domain_setting_value']; $_SESSION[$category][] = $row['default_setting_value'];
} }
else { else {
$_SESSION[$category][$name] = $row['domain_setting_value']; $_SESSION[$category][$name] = $row['default_setting_value'];
} }
} else { }
//$$category[$subcategory][$name] = $row['domain_setting_value']; else {
if ($name == "array") { if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['domain_setting_value']; $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
} }
else { else {
$_SESSION[$category][$subcategory][$name] = $row['domain_setting_value']; $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
$_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
}
}
}
}
unset($sql, $result, $row);
//get the domains settings
if (is_uuid($domain_uuid)) {
$sql = "select * from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_enabled = 'true' ";
$sql .= "order by domain_setting_order asc ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$result = $database->select($sql, $parameters, 'all');
//unset the arrays that domains are overriding
if (is_array($result) && @sizeof($result) != 0) {
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($name == "array") {
unset($_SESSION[$category][$subcategory]);
}
}
//set the settings as a session
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if (strlen($subcategory) == 0) {
//$$category[$name] = $row['domain_setting_value'];
if ($name == "array") {
$_SESSION[$category][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['domain_setting_value'];
}
}
else {
//$$category[$subcategory][$name] = $row['domain_setting_value'];
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
}
} }
} }
} }
@@ -251,17 +242,12 @@
$domain_name = $domain_array[0]; $domain_name = $domain_array[0];
//get the domain_uuid //get the domain_uuid
$sql = "SELECT * FROM v_domains "; $sql = "select domain_uuid from v_domains ";
$sql .= "WHERE domain_name = :domain_name "; $sql .= "where domain_name = :domain_name ";
//$sql .= "WHERE domain_name = '".$domain_name."' "; $parameters['domain_uuid'] = $domain_uuid;
$prep_statement = $db->prepare($sql); $database = new database;
$prep_statement->bindParam(':domain_name', $domain_name); $domain_uuid = $database->select($sql, $parameters, 'column');
$prep_statement->execute(); unset($sql, $parameters);
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($result as $row) {
$domain_uuid = $row["domain_uuid"];
}
unset($result, $prep_statement);
} }
//build the provision array //build the provision array
@@ -321,7 +307,7 @@
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1); $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array(); $data = array();
$keys = implode('|', array_keys($needed_parts)); $keys = implode('|', array_keys($needed_parts));
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER); preg_match_all('@('.$keys.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
foreach ($matches as $m) { foreach ($matches as $m) {
$data[$m[1]] = $m[3] ? $m[3] : $m[4]; $data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]); unset($needed_parts[$m[1]]);
@@ -364,7 +350,7 @@
$authorized = false; $authorized = false;
if (!$authorized && is_array($_SESSION['provision']["http_auth_password"])) { if (!$authorized && is_array($_SESSION['provision']["http_auth_password"])) {
foreach ($_SESSION['provision']["http_auth_password"] as $password) { foreach ($_SESSION['provision']["http_auth_password"] as $password) {
$A1 = md5($provision["http_auth_username"] . ':' . $realm . ':' . $password); $A1 = md5($provision["http_auth_username"].':'.$realm.':'.$password);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']); $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2); $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if ($data['response'] == $valid_response) { if ($data['response'] == $valid_response) {
@@ -394,7 +380,8 @@
header("Content-Length: ".strval(strlen($content))); header("Content-Length: ".strval(strlen($content)));
echo $content; echo $content;
exit; exit;
} else { }
else {
$authorized = false; $authorized = false;
if (is_array($_SESSION['provision']["http_auth_password"])) { if (is_array($_SESSION['provision']["http_auth_password"])) {
foreach ($_SESSION['provision']["http_auth_password"] as $password) { foreach ($_SESSION['provision']["http_auth_password"] as $password) {
@@ -457,26 +444,30 @@
header('Expires: 0'); header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public'); header('Pragma: public');
header('Content-Length: ' . strlen($file_contents)); header('Content-Length: '.strlen($file_contents));
} }
else { else {
$cfg_ext = ".cfg"; $cfg_ext = ".cfg";
if ($device_vendor === "aastra" && strrpos($file, $cfg_ext, 0) === strlen($file) - strlen($cfg_ext)) { if ($device_vendor === "aastra" && strrpos($file, $cfg_ext, 0) === strlen($file) - strlen($cfg_ext)) {
header("Content-Type: text/plain"); header("Content-Type: text/plain");
header("Content-Length: ".strlen($file_contents)); header("Content-Length: ".strlen($file_contents));
} else if ($device_vendor === "yealink") { }
else if ($device_vendor === "yealink") {
header("Content-Type: text/plain"); header("Content-Type: text/plain");
header("Content-Length: ".strval(strlen($file_contents))); header("Content-Length: ".strval(strlen($file_contents)));
} else if ($device_vendor === "snom" && $device_template === "snom/m3") { }
else if ($device_vendor === "snom" && $device_template === "snom/m3") {
$file_contents = utf8_decode($file_contents); $file_contents = utf8_decode($file_contents);
header("Content-Type: text/plain; charset=iso-8859-1"); header("Content-Type: text/plain; charset=iso-8859-1");
header("Content-Length: ".strlen($file_contents)); header("Content-Length: ".strlen($file_contents));
} else { }
else {
$result = simplexml_load_string ($file_contents, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE); $result = simplexml_load_string ($file_contents, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if (false == $result){ if (false == $result){
header("Content-Type: text/plain"); header("Content-Type: text/plain");
header("Content-Length: ".strval(strlen($file_contents))); header("Content-Length: ".strval(strlen($file_contents)));
} else { }
else {
header("Content-Type: text/xml; charset=utf-8"); header("Content-Type: text/xml; charset=utf-8");
header("Content-Length: ".strlen($file_contents)); header("Content-Length: ".strlen($file_contents));
} }
+24 -31
View File
@@ -1,38 +1,31 @@
<?php <?php
function device_by_mac($db, $mac) { function device_by_mac($db, $mac) {
$sql = 'SELECT * FROM v_devices '; $sql = "select * from v_devices ";
$sql .= 'WHERE device_mac_address=:mac'; $sql .= "where device_mac_address = :mac ";
$sql .= 'AND device_enabled = \'true\' '; $sql .= "and device_enabled = 'true' ";
$parameters['mac'] = $mac;
$prep = $db->prepare(check_sql($sql)); $database = new database;
if ($prep) { $row = $database->select($sql, $parameters, 'row');
$prep->bindParam(':mac', $mac); return is_array($row) && @sizeof($row) != 0 ? $row : false;
$prep->execute(); unset($sql, $parameters, $row);
$row = $prep->fetch();
unset($prep);
return $row;
}
return false;
} }
function device_by_ext($db, $ext, $domain) { function device_by_ext($db, $ext, $domain) {
$sql = 'select t1.* '; $sql = "select t1.* ";
$sql .= 'from v_devices t1 inner join v_device_lines t2 on t1.device_uuid=t2.device_uuid '; $sql .= "from v_devices t1 ";
$sql .= 'inner join v_domains t3 on t2.domain_uuid=t3.domain_uuid '; $sql .- "inner join v_device_lines t2 on t1.device_uuid = t2.device_uuid ";
$sql .= 'where t2.user_id=:ext '; $sql .= "inner join v_domains t3 on t2.domain_uuid = t3.domain_uuid ";
$sql .= 'and t3.domain_name=:domain '; $sql .= "where t2.user_id = :ext ";
$sql .= 'and t3.domain_enabled = \'true\' '; $sql .= "and t3.domain_name = :domain ";
$sql .= 'and t1.device_enabled = \'true\' '; $sql .= "and t3.domain_enabled = 'true' ";
$sql .= "and t1.device_enabled = 'true' ";
$prep = $db->prepare(check_sql($sql)); $parameters['ext'] = $ext;
if ($prep) { $parameters['domain'] = $domain;
$prep->bindParam(':ext', $ext); $database = new database;
$prep->bindParam(':domain', $domain); $row = $database->select($sql, $parameters, 'row');
$prep->execute(); return is_array($row) && @sizeof($row) != 0 ? $row : false;
$row = $prep->fetch(); unset($sql, $parameters, $row);
unset($prep);
return $row;
}
return false;
} }
?>