diff --git a/app/provision/index.php b/app/provision/index.php index 42be2b36a1..fd6bab6125 100644 --- a/app/provision/index.php +++ b/app/provision/index.php @@ -415,10 +415,9 @@ ob_start(); //output template to string for header processing - $prov = new provision(['settings'=>$settings]); - $prov->domain_uuid = $domain_uuid; + $prov = new provision(['settings'=>$settings, 'domain_uuid'=>$domain_uuid, 'domain_name'=>$domain_name, 'user_uuid'=>$_SESSION['user_uuid']]); $prov->device_address = $device_address; - $prov->file = $file; + $prov->device_file = $file; $file_contents = $prov->render(); //clean the output buffer diff --git a/app/provision/resources/classes/provision.php b/app/provision/resources/classes/provision.php index fc71072caf..a5d84b9377 100644 --- a/app/provision/resources/classes/provision.php +++ b/app/provision/resources/classes/provision.php @@ -57,7 +57,7 @@ class provision { public $template_dir; public $device_address; public $device_template; - public $file; + public $device_file; /** * Set in the constructor. Must be a database object and cannot be null. @@ -374,10 +374,6 @@ class provision { $device_template = $this->device_template; $template_dir = $this->template_dir; $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 $device_address = strtolower($device_address); @@ -392,7 +388,7 @@ class provision { // remove ../ and slashes in the file name $search = ['..', '/', '\\', '/./', '//']; - $file = str_replace($search, '', $file); + $this->device_file = str_replace($search, '', $this->device_file); // get the domain_name if (empty($domain_name)) { @@ -740,6 +736,28 @@ class provision { $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 $view = new template(); $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 - if (!file_exists($template_dir . '/' . $device_template . '/' . $file)) { - $this->http_error('404'); - if ($this->settings->get('provision', 'debug', false)) { - echo ":$template_dir/$device_template/$file
"; - echo "template_dir: $template_dir
"; - echo "device_template: $device_template
"; - echo "file: $file"; - } - exit; + // make sure the file exists + if (!file_exists($template_dir . '/' . $device_template . '/' . $this->device_file)) { + $this->http_error('404'); + if ($this->settings->get('provision', 'debug', false)) { + echo ":$template_dir/$device_template/" . $this->device_file . "
"; + echo "template_dir: $template_dir
"; + echo "device_template: $device_template
"; + echo "file: $this->device_file"; } + exit; } + // output template to string for header processing - $file_contents = $view->render($file); + $file_contents = $view->render($this->device_file); // log file for testing if ($this->settings->get('provision', 'debug', false)) { @@ -1479,8 +1480,6 @@ class provision { fclose($fh); } - $this->file = $file; - // returned the rendered template return $file_contents; } // end render function @@ -1587,7 +1586,7 @@ class provision { // configure device object $this->domain_uuid = $domain_uuid; $this->device_address = $device_address; - $this->file = $file_name; + $this->device_file = $file_name; // format the device address $address_formatted = $this->format_address($device_address, $device_vendor);