Fix the file_type detection

- Get the default device template file ealier in the render method. This wil fix retreiving the file type.
- Rename the file variable to the device_file
- Pass in the domain_uuid, domain_name, and user_id when the object is initialized.
This commit is contained in:
markjcrane
2025-12-22 21:09:36 -07:00
parent dfa0d4a215
commit 80fe26f4dc
2 changed files with 38 additions and 40 deletions

View File

@@ -415,10 +415,9 @@
ob_start(); ob_start();
//output template to string for header processing //output template to string for header processing
$prov = new provision(['settings'=>$settings]); $prov = new provision(['settings'=>$settings, 'domain_uuid'=>$domain_uuid, 'domain_name'=>$domain_name, 'user_uuid'=>$_SESSION['user_uuid']]);
$prov->domain_uuid = $domain_uuid;
$prov->device_address = $device_address; $prov->device_address = $device_address;
$prov->file = $file; $prov->device_file = $file;
$file_contents = $prov->render(); $file_contents = $prov->render();
//clean the output buffer //clean the output buffer

View File

@@ -57,7 +57,7 @@ class provision {
public $template_dir; public $template_dir;
public $device_address; public $device_address;
public $device_template; public $device_template;
public $file; public $device_file;
/** /**
* Set in the constructor. Must be a database object and cannot be null. * Set in the constructor. Must be a database object and cannot be null.
@@ -374,10 +374,6 @@ class provision {
$device_template = $this->device_template; $device_template = $this->device_template;
$template_dir = $this->template_dir; $template_dir = $this->template_dir;
$device_address = $this->device_address; $device_address = $this->device_address;
$file = $this->file;
//get the file type
$this->file_type = strtolower(pathinfo($this->file ?? '', PATHINFO_EXTENSION));
// set the device address to lower case to be consistent with the database // set the device address to lower case to be consistent with the database
$device_address = strtolower($device_address); $device_address = strtolower($device_address);
@@ -392,7 +388,7 @@ class provision {
// remove ../ and slashes in the file name // remove ../ and slashes in the file name
$search = ['..', '/', '\\', '/./', '//']; $search = ['..', '/', '\\', '/./', '//'];
$file = str_replace($search, '', $file); $this->device_file = str_replace($search, '', $this->device_file);
// get the domain_name // get the domain_name
if (empty($domain_name)) { if (empty($domain_name)) {
@@ -740,6 +736,28 @@ class provision {
$device_template = $domain_name . '/' . $device_template; $device_template = $domain_name . '/' . $device_template;
} }
// get the default template file
if (empty($this->device_file)) {
if (file_exists($template_dir . '/' . $device_template . '/{$address}')) {
$this->device_file = '{$address}';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$address}.xml')) {
$this->device_file = '{$address}.xml';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}')) {
$this->device_file = '{$mac}';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}.xml')) {
$this->device_file = '{$mac}.xml';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}.cfg')) {
$this->device_file = '{$mac}.cfg';
} else {
// the provisioning template was not found return an error
$this->http_error('404');
exit;
}
}
//set the template file type
$this->file_type = strtolower(pathinfo($this->device_file ?? '', PATHINFO_EXTENSION));
// initialize a template object // initialize a template object
$view = new template(); $view = new template();
$view->engine = $this->settings->get('provision', 'template_engine', 'smarty'); $view->engine = $this->settings->get('provision', 'template_engine', 'smarty');
@@ -1437,38 +1455,21 @@ class provision {
} }
} }
// if $file is not provided then look for a default file that exists
if (empty($file)) {
if (file_exists($template_dir . '/' . $device_template . '/{$address}')) {
$file = '{$address}';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$address}.xml')) {
$file = '{$address}.xml';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}')) {
$file = '{$mac}';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}.xml')) {
$file = '{$mac}.xml';
} elseif (file_exists($template_dir . '/' . $device_template . '/{$mac}.cfg')) {
$file = '{$mac}.cfg';
} else {
$this->http_error('404');
exit;
}
} else {
// make sure the file exists // make sure the file exists
if (!file_exists($template_dir . '/' . $device_template . '/' . $file)) { if (!file_exists($template_dir . '/' . $device_template . '/' . $this->device_file)) {
$this->http_error('404'); $this->http_error('404');
if ($this->settings->get('provision', 'debug', false)) { if ($this->settings->get('provision', 'debug', false)) {
echo ":$template_dir/$device_template/$file<br/>"; echo ":$template_dir/$device_template/" . $this->device_file . "<br/>";
echo "template_dir: $template_dir<br/>"; echo "template_dir: $template_dir<br/>";
echo "device_template: $device_template<br/>"; echo "device_template: $device_template<br/>";
echo "file: $file"; echo "file: $this->device_file";
} }
exit; exit;
} }
}
// output template to string for header processing // output template to string for header processing
$file_contents = $view->render($file); $file_contents = $view->render($this->device_file);
// log file for testing // log file for testing
if ($this->settings->get('provision', 'debug', false)) { if ($this->settings->get('provision', 'debug', false)) {
@@ -1479,8 +1480,6 @@ class provision {
fclose($fh); fclose($fh);
} }
$this->file = $file;
// returned the rendered template // returned the rendered template
return $file_contents; return $file_contents;
} // end render function } // end render function
@@ -1587,7 +1586,7 @@ class provision {
// configure device object // configure device object
$this->domain_uuid = $domain_uuid; $this->domain_uuid = $domain_uuid;
$this->device_address = $device_address; $this->device_address = $device_address;
$this->file = $file_name; $this->device_file = $file_name;
// format the device address // format the device address
$address_formatted = $this->format_address($device_address, $device_vendor); $address_formatted = $this->format_address($device_address, $device_vendor);