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 21:47:06 +03:00
committed by FusionPBX
parent 11e51de389
commit 9fe937d4d6
2 changed files with 99 additions and 77 deletions

View File

@@ -147,6 +147,9 @@ include "root.php";
case "snom":
$mac = strtolower($mac);
break;
case "escene":
$mac = strtolower($mac);
break;
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);
@@ -307,7 +310,6 @@ include "root.php";
if($provision['http_domain_filter'] == "true") {
$sql .= "AND domain_uuid=:domain_uuid ";
}
//$sql .= "WHERE device_mac_address= '$mac' ";
$prep_statement_2 = $this->db->prepare(check_sql($sql));
if ($prep_statement_2) {
//use the prepared statement
@@ -317,6 +319,15 @@ include "root.php";
}
$prep_statement_2->execute();
$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
$device_uuid = $row["device_uuid"];
$device_label = $row["device_label"];
@@ -337,6 +348,7 @@ include "root.php";
if (strlen($device_template) == 0) {
$sql = "SELECT * FROM v_devices ";
$sql .= "WHERE domain_uuid=:domain_uuid ";
$sql .= "AND device_enabled='true' ";
$sql .= "limit 1 ";
$prep_statement_3 = $this->db->prepare(check_sql($sql));
if ($prep_statement_3) {
@@ -431,6 +443,7 @@ include "root.php";
if (strlen($device_uuid) > 0) {
$sql = "SELECT * FROM v_devices ";
$sql .= "WHERE device_uuid = '".$device_uuid."' ";
$sql .= "AND device_enabled = 'true' ";
if($provision['http_domain_filter'] == "true") {
$sql .= "AND domain_uuid=:domain_uuid ";
}
@@ -458,13 +471,15 @@ include "root.php";
}
$prep_statement_4->execute();
$row = $prep_statement_4->fetch();
$device_label = $row["device_label"];
$device_firmware_version = $row["device_firmware_version"];
$device_user_uuid = $row["device_user_uuid"];
$device_enabled = $row["device_enabled"];
//keep the original template
$device_profile_uuid = $row["device_profile_uuid"];
$device_description = $row["device_description"];
if($row["device_enabled"] == "true") {
$device_label = $row["device_label"];
$device_firmware_version = $row["device_firmware_version"];
$device_user_uuid = $row["device_user_uuid"];
$device_enabled = $row["device_enabled"];
//keep the original template
$device_profile_uuid = $row["device_profile_uuid"];
$device_description = $row["device_description"];
}
}
unset($prep_statement_4);
}
@@ -945,27 +960,29 @@ include "root.php";
} //end render function
function write() {
//set default variables
$dir_count = 0;
$file_count = 0;
$row_count = 0;
$tmp_array = '';
$i = 0;
//build the provision array
$provision = Array();
foreach($_SESSION['provision'] as $key=>$val) {
if (strlen($val['var']) > 0) { $value = $val['var']; }
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 .= "where domain_uuid = '".$this->domain_uuid."' ";
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
unset ($prep_statement);
//process each device
foreach ($result as &$row) {
//get the values from the database and set as variables
$domain_uuid = $row["domain_uuid"];
@@ -981,81 +998,82 @@ include "root.php";
$device_password = $row["device_password"];
$device_description = $row["device_description"];
//loop through the provision template directory
clearstatcache();
$dir_list = '';
$file_list = '';
//loop through the provision template directory
$dir_array = array();
if (strlen($device_template) > 0) {
$dir_list = opendir($this->template_dir."/".$device_template);
$dir_array = array();
while (false !== ($file = readdir($dir_list))) {
if ($file != "." AND $file != ".."){
$new_path = $dir.'/'.$file;
$level = explode('/',$new_path);
if (substr($new_path, -4) == ".svn" ||
substr($new_path, -4) == ".git") {
//ignore .svn and .git dir and subdir
$template_path = path_join($this->template_dir, $device_template);
$dir_list = opendir($template_path);
if ($dir_list) {
$x = 0;
while (false !== ($file = readdir($dir_list))) {
$ignore = $file == "." || $file == ".." || substr($file, -3) == ".db" ||
substr($file, -4) == ".svn" || substr($file, -4) == ".git";
if (!$ignore) {
$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);
foreach ($dir_array as $new_path){
$level = explode('/',$new_path);
if (is_dir($new_path)) {
$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) {
//loop through the provision templates
foreach ($dir_array as &$template_path) {
if (is_dir($template_path)) continue;
if (!file_exists($template_path)) continue;
if (file_exists($this->template_dir."/".$device_template."/".$file_name)) {
//output template to string for header processing
//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();
//template file name
$file_name = basename($template_path);
//format the mac address
$mac = $this->format_mac($device_mac_address, $device_vendor);
//configure device object
$this->domain_uuid = $domain_uuid;
$this->mac = $device_mac_address;
$this->file = $file_name;
//replace {$mac} in the file name
$file_name = str_replace("{\$mac}", $mac, $file_name);
//format the mac address
$mac = $this->format_mac($device_mac_address, $device_vendor);
//write the file
//echo $directory.'/'.$file_name."\n";
$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);
fclose($fh);
}
}
unset($file_name);
//replace {$mac} in the file name
$file_name = str_replace("{\$mac}", $mac, $file_name);
//render and write configuration to file
$provision_dir_array = explode(";", $provision["path"]);
foreach($provision_dir_array as $directory) {
//destinatino file path
$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
closedir($dir_list);
//echo "<hr size='1'>\n";
//unset variables
unset($dir_array);
}
unset ($prep_statement);
} //end write function
} //end provision class
?>

View File

@@ -3,6 +3,8 @@
function device_by_mac($db, $mac) {
$sql = 'SELECT * FROM v_devices ';
$sql .= 'WHERE device_mac_address=:mac';
$sql .= 'AND device_enabled = \'true\' ';
$prep = $db->prepare(check_sql($sql));
if ($prep) {
$prep->bindParam(':mac', $mac);
@@ -21,6 +23,8 @@ function device_by_ext($db, $ext, $domain) {
$sql .= 'where t2.user_id=:ext ';
$sql .= 'and t3.domain_name=:domain ';
$sql .= 'and t3.domain_enabled = \'true\' ';
$sql .= 'and t1.device_enabled = \'true\' ';
$prep = $db->prepare(check_sql($sql));
if ($prep) {
$prep->bindParam(':ext', $ext);