Fix. Disable device provision. (#1700)

Close #1389
Rewrite code to write config file to file.
This commit is contained in:
Alexey Melnichuk
2016-06-23 12:47:06 -06:00
committed by FusionPBX
parent 3f47e26616
commit 032872f5b3
2 changed files with 99 additions and 77 deletions
+95 -77
View File
@@ -147,6 +147,9 @@ include "root.php";
case "snom": case "snom":
$mac = strtolower($mac); $mac = strtolower($mac);
break; break;
case "escene":
$mac = strtolower($mac);
break;
default: default:
$mac = strtolower($mac); $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); $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);
@@ -307,7 +310,6 @@ include "root.php";
if($provision['http_domain_filter'] == "true") { if($provision['http_domain_filter'] == "true") {
$sql .= "AND domain_uuid=:domain_uuid "; $sql .= "AND domain_uuid=:domain_uuid ";
} }
//$sql .= "WHERE device_mac_address= '$mac' ";
$prep_statement_2 = $this->db->prepare(check_sql($sql)); $prep_statement_2 = $this->db->prepare(check_sql($sql));
if ($prep_statement_2) { if ($prep_statement_2) {
//use the prepared statement //use the prepared statement
@@ -317,6 +319,15 @@ include "root.php";
} }
$prep_statement_2->execute(); $prep_statement_2->execute();
$row = $prep_statement_2->fetch(); $row = $prep_statement_2->fetch();
//checks either device enabled
if($row['device_enabled'] != 'true'){
if ($_SESSION['provision']['debug']['boolean'] == 'true'){
echo "<br/>device disabled<br/>";
}
echo "file not found";
exit;
}
//set the variables from values in the database //set the variables from values in the database
$device_uuid = $row["device_uuid"]; $device_uuid = $row["device_uuid"];
$device_label = $row["device_label"]; $device_label = $row["device_label"];
@@ -337,6 +348,7 @@ include "root.php";
if (strlen($device_template) == 0) { if (strlen($device_template) == 0) {
$sql = "SELECT * FROM v_devices "; $sql = "SELECT * FROM v_devices ";
$sql .= "WHERE domain_uuid=:domain_uuid "; $sql .= "WHERE domain_uuid=:domain_uuid ";
$sql .= "AND device_enabled='true' ";
$sql .= "limit 1 "; $sql .= "limit 1 ";
$prep_statement_3 = $this->db->prepare(check_sql($sql)); $prep_statement_3 = $this->db->prepare(check_sql($sql));
if ($prep_statement_3) { if ($prep_statement_3) {
@@ -431,6 +443,7 @@ include "root.php";
if (strlen($device_uuid) > 0) { if (strlen($device_uuid) > 0) {
$sql = "SELECT * FROM v_devices "; $sql = "SELECT * FROM v_devices ";
$sql .= "WHERE device_uuid = '".$device_uuid."' "; $sql .= "WHERE device_uuid = '".$device_uuid."' ";
$sql .= "AND device_enabled = 'true' ";
if($provision['http_domain_filter'] == "true") { if($provision['http_domain_filter'] == "true") {
$sql .= "AND domain_uuid=:domain_uuid "; $sql .= "AND domain_uuid=:domain_uuid ";
} }
@@ -458,13 +471,15 @@ include "root.php";
} }
$prep_statement_4->execute(); $prep_statement_4->execute();
$row = $prep_statement_4->fetch(); $row = $prep_statement_4->fetch();
$device_label = $row["device_label"]; if($row["device_enabled"] == "true") {
$device_firmware_version = $row["device_firmware_version"]; $device_label = $row["device_label"];
$device_user_uuid = $row["device_user_uuid"]; $device_firmware_version = $row["device_firmware_version"];
$device_enabled = $row["device_enabled"]; $device_user_uuid = $row["device_user_uuid"];
//keep the original template $device_enabled = $row["device_enabled"];
$device_profile_uuid = $row["device_profile_uuid"]; //keep the original template
$device_description = $row["device_description"]; $device_profile_uuid = $row["device_profile_uuid"];
$device_description = $row["device_description"];
}
} }
unset($prep_statement_4); unset($prep_statement_4);
} }
@@ -945,27 +960,29 @@ include "root.php";
} //end render function } //end render function
function write() { function write() {
//set default variables
$dir_count = 0;
$file_count = 0;
$row_count = 0;
$tmp_array = '';
$i = 0;
//build the provision array //build the provision array
$provision = Array();
foreach($_SESSION['provision'] as $key=>$val) { foreach($_SESSION['provision'] as $key=>$val) {
if (strlen($val['var']) > 0) { $value = $val['var']; } if (strlen($val['var']) > 0) { $value = $val['var']; }
if (strlen($val['text']) > 0) { $value = $val['text']; } if (strlen($val['text']) > 0) { $value = $val['text']; }
$provision[$key] = $value; if (strlen($value) > 0) { $provision[$key] = $value; }
unset($value);
} }
//get the devices //check either we have destination path to write files
if(strlen($provision["path"]) == 0){
return;
}
//get the devices from database
$sql = "select * from v_devices "; $sql = "select * from v_devices ";
//$sql .= "where domain_uuid = '".$this->domain_uuid."' "; //$sql .= "where domain_uuid = '".$this->domain_uuid."' ";
$prep_statement = $this->db->prepare(check_sql($sql)); $prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute(); $prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
unset ($prep_statement);
//process each device
foreach ($result as &$row) { foreach ($result as &$row) {
//get the values from the database and set as variables //get the values from the database and set as variables
$domain_uuid = $row["domain_uuid"]; $domain_uuid = $row["domain_uuid"];
@@ -981,81 +998,82 @@ include "root.php";
$device_password = $row["device_password"]; $device_password = $row["device_password"];
$device_description = $row["device_description"]; $device_description = $row["device_description"];
//loop through the provision template directory
clearstatcache(); clearstatcache();
$dir_list = '';
$file_list = ''; //loop through the provision template directory
$dir_array = array();
if (strlen($device_template) > 0) { if (strlen($device_template) > 0) {
$dir_list = opendir($this->template_dir."/".$device_template); $template_path = path_join($this->template_dir, $device_template);
$dir_array = array(); $dir_list = opendir($template_path);
while (false !== ($file = readdir($dir_list))) { if ($dir_list) {
if ($file != "." AND $file != ".."){ $x = 0;
$new_path = $dir.'/'.$file; while (false !== ($file = readdir($dir_list))) {
$level = explode('/',$new_path); $ignore = $file == "." || $file == ".." || substr($file, -3) == ".db" ||
if (substr($new_path, -4) == ".svn" || substr($file, -4) == ".svn" || substr($file, -4) == ".git";
substr($new_path, -4) == ".git") { if (!$ignore) {
//ignore .svn and .git dir and subdir $dir_array[] = path_join($template_path, $file);
if ($x > 1000) { break; };
$x++;
} }
elseif (substr($new_path, -3) == ".db") {
//ignore .db files
}
else {
$dir_array[] = $new_path;
}
if ($x > 1000) { break; };
$x++;
} }
closedir($dir_list);
unset($x, $file);
} }
unset($dir_list, $template_path);
} }
//asort($dir_array); //loop through the provision templates
foreach ($dir_array as $new_path){ foreach ($dir_array as &$template_path) {
$level = explode('/',$new_path); if (is_dir($template_path)) continue;
if (is_dir($new_path)) { if (!file_exists($template_path)) continue;
$dir_name = end($level);
//$file_list .= "$dir_name\n";
//$dir_list .= recur_dir($new_path);
}
else {
$file_name = end($level);
//debug information
//$file_size = round(filesize($new_path)/1024, 2);
//echo $this->template_dir."/".$device_template."/".$file_name." $file_size\n";
//write the configuration to the directory
if (strlen($provision["path"]) > 0) {
$dir_array = explode(";", $provision["path"]);
foreach($dir_array as $directory) {
if (file_exists($this->template_dir."/".$device_template."/".$file_name)) { //template file name
//output template to string for header processing $file_name = basename($template_path);
//output template to string for header processing
$prov->domain_uuid = $domain_uuid;
$this->mac = $device_mac_address;
$this->file = $file_name;
$file_contents = $this->render();
//format the mac address //configure device object
$mac = $this->format_mac($device_mac_address, $device_vendor); $this->domain_uuid = $domain_uuid;
$this->mac = $device_mac_address;
$this->file = $file_name;
//replace {$mac} in the file name //format the mac address
$file_name = str_replace("{\$mac}", $mac, $file_name); $mac = $this->format_mac($device_mac_address, $device_vendor);
//write the file //replace {$mac} in the file name
//echo $directory.'/'.$file_name."\n"; $file_name = str_replace("{\$mac}", $mac, $file_name);
$fh = fopen($directory.'/'.$file_name,"w") or die("Unable to write to $directory for provisioning. Make sure the path exists and permissons are set correctly.");
fwrite($fh, $file_contents); //render and write configuration to file
fclose($fh); $provision_dir_array = explode(";", $provision["path"]);
} foreach($provision_dir_array as $directory) {
} //destinatino file path
unset($file_name); $dest_path = path_join($directory, $file_name);
if ($device_enabled == 'true'){
//output template to string for header processing
$file_contents = $this->render();
//write the file
$fh = fopen($dest_path,"w") or die("Unable to write to $directory for provisioning. Make sure the path exists and permissons are set correctly.");
fwrite($fh, $file_contents);
fclose($fh);
} }
else{ // device disabled
//remove only files with `{$mac}` name
if(strpos($template_path, '{$mac}') !== false){
unlink($dest_path);
}
}
unset($dest_path);
} }
//unset variables
unset($file_name, $provision_dir_array);
} //end for each } //end for each
closedir($dir_list);
//echo "<hr size='1'>\n"; //unset variables
unset($dir_array);
} }
unset ($prep_statement);
} //end write function } //end write function
} //end provision class } //end provision class
?> ?>
@@ -3,6 +3,8 @@
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\' ';
$prep = $db->prepare(check_sql($sql)); $prep = $db->prepare(check_sql($sql));
if ($prep) { if ($prep) {
$prep->bindParam(':mac', $mac); $prep->bindParam(':mac', $mac);
@@ -21,6 +23,8 @@ function device_by_ext($db, $ext, $domain) {
$sql .= 'where t2.user_id=:ext '; $sql .= 'where t2.user_id=:ext ';
$sql .= 'and t3.domain_name=:domain '; $sql .= 'and t3.domain_name=:domain ';
$sql .= 'and t3.domain_enabled = \'true\' '; $sql .= 'and t3.domain_enabled = \'true\' ';
$sql .= 'and t1.device_enabled = \'true\' ';
$prep = $db->prepare(check_sql($sql)); $prep = $db->prepare(check_sql($sql));
if ($prep) { if ($prep) {
$prep->bindParam(':ext', $ext); $prep->bindParam(':ext', $ext);