Add a provision write method to the provision function and use it to write the provisioning files for tftp/ftp provisioning.

This commit is contained in:
Mark Crane
2014-01-29 10:18:25 +00:00
parent 74e0552a1d
commit bc266b0c8a
3 changed files with 154 additions and 298 deletions

View File

@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2008-2013 All Rights Reserved.
Copyright (C) 2008-2014 All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
@@ -154,11 +154,9 @@ require_once "resources/require.php";
//output template to string for header processing
$prov = new provision;
//$prov->template_directory = $template_directory;
$prov->domain_uuid = $domain_uuid;
$prov->mac = $mac;
$prov->file = $file;
$prov->db = $db;
$file_contents = $prov->render();
//deliver the customized config over HTTP/HTTPS

View File

@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2008-2013 All Rights Reserved.
Copyright (C) 2008-2014 All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
@@ -25,6 +25,7 @@
include "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
include "resources/classes/template.php";
if (if_group("admin") || if_group("superadmin")) {
//access granted
}
@@ -33,261 +34,9 @@ else {
exit;
}
//set default variables
$dir_count = 0;
$file_count = 0;
$row_count = 0;
$tmp_array = '';
//get the hardware phone list
$sql = "select * from v_devices ";
$sql .= "where domain_uuid = '$domain_uuid' ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach ($result as &$row) {
$device_uuid = $row["device_uuid"];
$device_mac_address = $row["device_mac_address"];
$device_label = $row["device_label"];
$device_vendor = strtolower($row["device_vendor"]);
$device_model = $row["device_model"];
$device_firmware_version = $row["device_firmware_version"];
$device_provision_enable = $row["device_provision_enable"];
$device_template = $row["device_template"];
$device_username = $row["device_username"];
$device_password = $row["device_password"];
$device_time_zone = $row["device_time_zone"];
$device_description = $row["device_description"];
//use the mac address to find the vendor
if (strlen($device_vendor) == 0) {
switch (substr($device_mac_address, 0, 6)) {
case "00085d":
$device_vendor = "aastra";
break;
case "000e08":
$device_vendor = "linksys";
break;
case "0004f2":
$device_vendor = "polycom";
break;
case "00907a":
$device_vendor = "polycom";
break;
case "0080f0":
$device_vendor = "panasonic";
break;
case "001873":
$device_vendor = "cisco";
break;
case "a44c11":
$device_vendor = "cisco";
break;
case "0021A0":
$device_vendor = "cisco";
break;
case "30e4db":
$device_vendor = "cisco";
break;
case "002155":
$device_vendor = "cisco";
break;
case "68efbd":
$device_vendor = "cisco";
break;
case "00045a":
$device_vendor = "linksys";
break;
case "000625":
$device_vendor = "linksys";
break;
case "001565":
$device_vendor = "yealink";
break;
case "000413":
$device_vendor = "snom";
break;
case "000b82":
$device_vendor = "grandstream";
break;
case "00177d":
$device_vendor = "konftel";
break;
default:
$device_vendor = "";
}
}
//set the mac address in the correct format
switch ($device_vendor) {
case "aastra":
//upper case no formatting
$device_mac_address = strtoupper($device_mac_address);
break;
case "polycom":
//lower case no formatting
$device_mac_address = strtolower($device_mac_address);
$device_mac_address = str_replace("-", "", $device_mac_address);
break;
case "snom":
//upper case with dashes
$device_mac_address = strtoupper($device_mac_address);
$device_mac_address = str_replace("-", "", $device_mac_address);
default:
//lower case with dashes
$device_mac_address = strtolower($device_mac_address);
$device_mac_address = substr($device_mac_address, 0,2).'-'.substr($device_mac_address, 2,2).'-'.substr($device_mac_address, 4,2).'-'.substr($device_mac_address, 6,2).'-'.substr($device_mac_address, 8,2).'-'.substr($device_mac_address, 10,2);
}
//loop through the provision template directory
$provision_template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/".$device_template;
clearstatcache();
$dir_list = '';
$file_list = '';
$dir_list = opendir($provision_template_dir);
$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") {
//ignore .svn dir and subdir
}
elseif (substr($new_path, -3) == ".db") {
//ignore .db files
}
else {
$dir_array[] = $new_path;
}
if ($x > 1000) { break; };
$x++;
}
}
//asort($dir_array);
foreach ($dir_array as $new_path){
$level = explode('/',$new_path);
if (is_dir($new_path)) {
//$mod_array[] = array(
//'level'=>count($level)-1,
//'path'=>$new_path,
//'name'=>end($level),
//'type'=>'dir',
//'mod_time'=>filemtime($new_path),
//'size'=>'');
//$mod_array[] = recur_dir($new_path);
$dir_name = end($level);
//$file_list .= "$dir_name\n";
//$dir_list .= recur_dir($new_path);
}
else {
//$mod_array[] = array(
//'level'=>count($level)-1,
//'path'=>$new_path,
//'name'=>end($level),
//'type'=>'dir',
//'mod_time'=>filemtime($new_path),
//'size'=>'');
//$mod_array[] = recur_dir($new_path);
$file_name = end($level);
$file_size = round(filesize($new_path)/1024, 2);
//get the contents of the template
$file_contents = file_get_contents($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/".$device_template ."/".$file_name);
//prepare the files
//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("{\$mac}", $device_mac_address, $file_contents);
$file_contents = str_replace("{v_label}", $device_label, $file_contents);
$file_contents = str_replace("{v_firmware_version}", $device_firmware_version, $file_contents);
$file_contents = str_replace("{domain_time_zone}", $device_time_zone, $file_contents);
$file_contents = str_replace("{domain_name}", $_SESSION['domain_name'], $file_contents);
$file_contents = str_replace("{v_server1_address}", $server1_address, $file_contents);
$file_contents = str_replace("{v_proxy1_address}", $proxy1_address, $file_contents);
//replace the dynamic provision variables that are defined in 'default settings' and 'domain settings';
//example: category=provision, subcategory=sip_transport, name=var, value=tls - used in the template as {v_sip_transport}
foreach($_SESSION['provision'] as $key=>$value) {
$file_contents = str_replace('{$'.$key.'}', $value['var'], $file_contents);
}
//create a mac address with back slashes for backwards compatability
$mac_dash = substr($device_mac_address, 0,2).'-'.substr($device_mac_address, 2,2).'-'.substr($device_mac_address, 4,2).'-'.substr($device_mac_address, 6,2).'-'.substr($device_mac_address, 8,2).'-'.substr($device_mac_address, 10,2);
//lookup the provisioning information for this MAC address.
$sql = "SELECT e.extension, e.password, e.effective_caller_id_name, d.device_extension_uuid, d.extension_uuid, d.device_line ";
$sql .= "FROM v_device_extensions as d, v_extensions as e ";
$sql .= "WHERE e.extension_uuid = d.extension_uuid ";
$sql .= "AND d.device_uuid = '".$device_uuid."' ";
$sql .= "AND d.domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= "and e.enabled = 'true' ";
$sub_prep_statement = $db->prepare(check_sql($sql));
$sub_prep_statement->execute();
$sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($sub_result as $field) {
$line_number = $field['device_line'];
$file_contents = str_replace("{v_line".$line_number."_server_address}", $_SESSION['domain_name'], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_displayname}", $field["effective_caller_id_name"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_shortname}", $field["extension"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_user_id}", $field["extension"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_user_password}", $field["password"], $file_contents);
}
unset ($sub_prep_statement);
//get the provisioning information from device lines table
$sql = "SELECT * FROM v_device_lines ";
$sql .= "WHERE device_uuid = '".$device_uuid."' ";
$sql .= "AND domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sub_prep_statement = $db->prepare(check_sql($sql));
$sub_prep_statement->execute();
$sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach($sub_result as $field) {
$line_number = $field['line_number'];
$file_contents = str_replace("{v_line".$line_number."_server_address}", $field["server_address"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_outbound_proxy}", $field["outbound_proxy"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_displayname}", $field["display_name"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_user_id}", $field["user_id"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_auth_id}", $field["auth_id"], $file_contents);
$file_contents = str_replace("{v_line".$line_number."_user_password}", $field["password"], $file_contents);
}
unset ($sub_prep_statement);
//cleanup any remaining variables
for ($i = 1; $i <= 100; $i++) {
$file_contents = str_replace("{v_line".$i."_server_address}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_outbound_proxy}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_displayname}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_shortname}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_user_id}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_auth_id}", "", $file_contents);
$file_contents = str_replace("{v_line".$i."_user_password}", "", $file_contents);
}
//replace {$mac} in the file name
if ($device_vendor == "aastra" || $device_vendor == "cisco") {
//upper case the mac address for aastra phones
$file_name = str_replace("{\$mac}", strtoupper($device_mac_address), $file_name);
}
else {
//all other phones
$file_name = str_replace("{\$mac}", $device_mac_address, $file_name);
}
//write the configuration to the directory
if (strlen($_SESSION['switch']['provision']['dir']) > 0) {
$dir_array = explode(";", $_SESSION['switch']['provision']['dir']);
foreach($dir_array as $directory) {
//echo $directory.'/'.$file_name."<br />\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);
}
}
} //end for each
closedir($dir_list);
}
unset ($prep_statement);
//output template to string for header processing
$prov = new provision;
$prov->domain_uuid = $domain_uuid;
$response = $prov->write();
?>

View File

@@ -30,11 +30,16 @@ include "root.php";
public $db;
public $domain_uuid;
public $domain_name;
public $device_template;
public $template_directory;
public $template_dir;
public function __construct() {
//get the database object
global $db;
$this->db = $db;
//set the default template directory
if (strlen($this->template_dir) == 0) {
$this->template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision";
}
}
public function __destruct() {
@@ -49,39 +54,35 @@ include "root.php";
//define the function which checks to see if the mac address exists in devices
private function mac_exists($mac) {
$sql = "SELECT count(*) as count FROM v_devices ";
//$sql .= "WHERE domain_uuid=:domain_uuid ";
$sql .= "WHERE device_mac_address=:mac ";
$prep_statement = $this->db->prepare(check_sql($sql));
if ($prep_statement) {
//$prep_statement->bindParam(':domain_uuid', $domain_uuid);
$prep_statement->bindParam(':mac', $mac);
$prep_statement->execute();
$row = $prep_statement->fetch();
$count = $row['count'];
if ($row['count'] > 0) {
return true;
//check in the devices table for a specific mac address
$sql = "SELECT count(*) as count FROM v_devices ";
//$sql .= "WHERE domain_uuid=:domain_uuid ";
$sql .= "WHERE device_mac_address=:mac ";
$prep_statement = $this->db->prepare(check_sql($sql));
if ($prep_statement) {
//$prep_statement->bindParam(':domain_uuid', $domain_uuid);
$prep_statement->bindParam(':mac', $mac);
$prep_statement->execute();
$row = $prep_statement->fetch();
$count = $row['count'];
if ($row['count'] > 0) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
function render() {
//create the database object
$database = new database;
if (!$this->db) {
$this->db = $database->connect();
}
//get the variables
$domain_uuid = $this->domain_uuid;
$device_template = $this->device_template;
$template_directory = $this->template_directory;
$template_dir = $this->template_dir;
$mac = $this->mac;
$file = $this->file;
@@ -97,12 +98,12 @@ include "root.php";
//get the device_template
//if (strlen($device_template) == 0) {
$sql = "SELECT * FROM v_devices ";
//$sql .= "WHERE device_mac_address=:mac ";
$sql .= "WHERE device_mac_address= '$mac' ";
$sql .= "WHERE device_mac_address=:mac ";
//$sql .= "WHERE device_mac_address= '$mac' ";
$prep_statement_2 = $this->db->prepare(check_sql($sql));
if ($prep_statement_2) {
//use the prepared statement
//$prep_statement_2->bindParam(':mac', $mac);
$prep_statement_2->bindParam(':mac', $mac);
$prep_statement_2->execute();
$row = $prep_statement_2->fetch();
//set the variables from values in the database
@@ -244,7 +245,7 @@ include "root.php";
else {
$view->engine = "smarty";
}
$view->template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/provision/".$device_template."/";
$view->template_dir = $template_dir ."/".$device_template."/";
$view->cache_dir = $_SESSION['server']['temp']['dir'];
$view->init();
@@ -382,35 +383,34 @@ include "root.php";
}
//set the template directory
$template_directory = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/templates/provision';
if (strlen($provision["template_directory"]) > 0) {
$template_directory = $provision["template_directory"];
if (strlen($provision["template_dir"]) > 0) {
$template_dir = $provision["template_dir"];
}
//if the domain name directory exists then only use templates from it
if (is_dir($template_directory.'/'.$domain_name)) {
if (is_dir($template_dir.'/'.$domain_name)) {
$device_template = $domain_name.'/'.$device_template;
}
//if $file is not provided then look for a default file that exists
if (strlen($file) == 0) {
if (file_exists($template_directory."/".$device_template ."/{\$mac}")) {
if (file_exists($template_dir."/".$device_template ."/{\$mac}")) {
$file = "{\$mac}";
}
elseif (file_exists($template_directory."/".$device_template ."/{\$mac}.xml")) {
elseif (file_exists($template_dir."/".$device_template ."/{\$mac}.xml")) {
$file = "{\$mac}.xml";
}
elseif (file_exists($template_directory."/".$device_template ."/{\$mac}.cfg")) {
elseif (file_exists($template_dir."/".$device_template ."/{\$mac}.cfg")) {
$file = "{\$mac}.cfg";
}
else {
echo "file not found 178 test";
echo "file not found";
exit;
}
}
else {
//make sure the file exists
if (!file_exists($template_directory."/".$device_template ."/".$file)) {
if (!file_exists($template_dir."/".$device_template ."/".$file)) {
echo "file not found";
exit;
}
@@ -431,6 +431,115 @@ include "root.php";
} //end render function
function write() {
//set default variables
$dir_count = 0;
$file_count = 0;
$row_count = 0;
$tmp_array = '';
$i = 0;
//get the devices
$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);
foreach ($result as &$row) {
//get the values from the database and set as variables
$device_uuid = $row["device_uuid"];
$device_mac_address = $row["device_mac_address"];
$device_label = $row["device_label"];
$device_vendor = strtolower($row["device_vendor"]);
$device_model = $row["device_model"];
$device_firmware_version = $row["device_firmware_version"];
$device_provision_enable = $row["device_provision_enable"];
$device_template = $row["device_template"];
$device_username = $row["device_username"];
$device_password = $row["device_password"];
$device_time_zone = $row["device_time_zone"];
$device_description = $row["device_description"];
//loop through the provision template directory
clearstatcache();
$dir_list = '';
$file_list = '';
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") {
//ignore .svn dir and subdir
}
elseif (substr($new_path, -3) == ".db") {
//ignore .db files
}
else {
$dir_array[] = $new_path;
}
if ($x > 1000) { break; };
$x++;
}
}
}
//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($_SESSION['switch']['provision']['dir']) > 0) {
$dir_array = explode(";", $_SESSION['switch']['provision']['dir']);
foreach($dir_array as $directory) {
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();
//replace {$mac} in the file name
if ($device_vendor == "aastra" || $device_vendor == "cisco") {
//upper case the mac address for aastra phones
$file_name = str_replace("{\$mac}", strtoupper($device_mac_address), $file_name);
}
else {
//all other phones
$file_name = str_replace("{\$mac}", $device_mac_address, $file_name);
}
//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);
}
}
} //end for each
closedir($dir_list);
//echo "<hr size='1'>\n";
}
unset ($prep_statement);
} //end write function
} //end provision class
?>