Optimise and add error handling to getFilesFromProvisioner

This commit is contained in:
steve-lad 2021-07-24 09:31:30 +02:00
parent 97cfa12fea
commit 75858d7cc1
4 changed files with 69 additions and 98 deletions

View file

@ -624,7 +624,10 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
$result = array(); $result = array();
if (!file_exists("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) { if (!file_exists("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) {
$this->getFileListFromProvisioner(); if (!$this->getFileListFromProvisioner()) {
// File does not exist and cannot get from internet.
return $result;
};
} }
$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml"); $tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml");

View file

@ -596,128 +596,97 @@ trait ajaxHelper {
public function getFilesFromProvisioner($request) { public function getFilesFromProvisioner($request) {
$filesToGet = array(); $filesToGet = array();
$totalFiles = 0;
$provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/"; $provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/";
if (!file_exists("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) { if (!file_exists("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) {
$this->getFileListFromProvisioner(); if (!$this->getFileListFromProvisioner()) {
return array('status' => false,
'message' => "{$provisionerUrl}tools/tftpbootFiles.xml cannot be found. Check your internet connection, and that this path exists",
'reload' => false);
}
} }
$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml"); $tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml");
switch ($request['type']) { switch ($request['type']) {
case 'firmware': case 'firmware':
$device = $request['device']; $device = $request['device'];
if (!is_dir("{$this->sccppath['tftp_firmware_path']}/{$device}")) {
mkdir("{$this->sccppath['tftp_firmware_path']}/{$device}", 0755);
}
$firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']"); $firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']");
$result = $firmwareDir[0]->xpath("//Directory[@name={$device}]"); $result = $firmwareDir[0]->xpath("//Directory[@name={$device}]");
$filesToGet = (array)$result[0]->FileName; $filesToGet['firmware'] = (array)$result[0]->FileName;
$totalFiles = count($filesToGet); $totalFiles += count($filesToGet['firmware']);
$filesRetrieved = 0; $srcDir['firmware'] = $provisionerUrl . (string)$result[0]->DirectoryPath;
$dstDir['firmware'] = "{$this->sccppath['tftp_firmware_path']}/{$device}";
foreach ($filesToGet as $srcFile) {
file_put_contents("{$this->sccppath['tftp_firmware_path']}/{$device}/{$srcFile}",
file_get_contents($provisionerUrl . (string)$result[0]->DirectoryPath . $srcFile));
$filesRetrieved ++;
$percentComplete = $filesRetrieved *100 / $totalFiles;
$data = "{$percentComplete},";
echo $data;
ob_flush();
flush();
}
$msg = "Firmware for {$device} has been successfully downloaded"; $msg = "Firmware for {$device} has been successfully downloaded";
break; break;
case 'locale': case 'locale':
$language = $request['locale']; $language = $request['locale'];
$totalFiles = 0;
if (!is_dir("{$this->sccppath['tftp_lang_path']}/{$language}")) {
mkdir("{$this->sccppath['tftp_lang_path']}/{$language}", 0755);
}
// Get locales // Get locales
$localeDir = $tftpBootXml->xpath("//Directory[@name='locales']"); $localeDir = $tftpBootXml->xpath("//Directory[@name='languages']");
$localeDir = $localeDir[0]->xpath("//Directory[@name='languages']");
$result = $localeDir[0]->xpath("//Directory[@name='{$language}']"); $result = $localeDir[0]->xpath("//Directory[@name='{$language}']");
$filesToGet['languages'] = (array)$result[0]->FileName; $filesToGet['language'] = (array)$result[0]->FileName;
$totalFiles += count($filesToGet['languages']); $totalFiles += count($filesToGet['language']);
$languagesSrcDir = (string)$result[0]->DirectoryPath; $srcDir['language'] = $provisionerUrl . (string)$result[0]->DirectoryPath;
$languagesDstDir = "{$this->sccppath['tftp_lang_path']}/{$language}"; $dstDir['language'] = "{$this->sccppath['tftp_lang_path']}/{$language}";
// Get countries. Country is a substring of locale with exception of korea // Get countries. Country is a substring of locale with exception of korea
$country = explode('_', $language); $country = explode('_', $language);
array_shift($country); array_shift($country);
$countryName = array_shift($country); $countryName = array_shift($country);
while (count($country)>=1) { while (count($country)>=1) {
$countryName .= '_' . array_shift($country); $countryName .= '_' . array_shift($country);
} }
if (!is_dir("{$this->sccppath['tftp_countries_path']}/{$countryName}")) {
mkdir("{$this->sccppath['tftp_countries_path']}/{$countryName}", 0755);
}
$countryDir = $tftpBootXml->xpath("//Directory[@name='locales']");
$countryDir = $countryDir[0]->xpath("//Directory[@name='countries']");
$result = $countryDir[0]->xpath("//Directory[@name='{$countryName}']");
$filesToGet['countries'] = (array)$result[0]->FileName;
$totalFiles += count($filesToGet['countries']);
$countriesSrcDir = (string)$result[0]->DirectoryPath;
$countriesDstDir = "{$this->sccppath['tftp_countries_path']}/{$countryName}";
$filesRetrieved = 0;
foreach (array('languages', 'countries') as $section){
$srcDir = ${"{$section}SrcDir"};
$dstDir = ${"{$section}DstDir"};
foreach ($filesToGet[$section] as $srcFile) {
file_put_contents("{$dstDir}/{$srcFile}",
file_get_contents($provisionerUrl . $srcDir . $srcFile));
$filesRetrieved ++;
$percentComplete = $filesRetrieved *100 / $totalFiles;
$data = "{$percentComplete},";
echo $data;
ob_flush();
flush();
}
}
$msg = "{$language} Locale and Country tones have been successfully downloaded"; $msg = "{$language} Locale and Country tones have been successfully downloaded";
break; //fall through intentionally to also get country files
case 'country':
$countryName = $request['country'];
$totalFiles = 0;
if (!is_dir("{$this->sccppath['tftp_countries_path']}/{$countryName}")) { case 'country':
mkdir("{$this->sccppath['tftp_countries_path']}/{$countryName}", 0755); if ($totalFiles == 0) {
//Request is for countries; if >0, have fallen through from locale
$countryName = $request['country'];
$msg = "{$countryName} country tones have been successfully downloaded";
} }
$result = $tftpBootXml->xpath("//Directory[@name='{$countryName}']"); $result = $tftpBootXml->xpath("//Directory[@name='{$countryName}']");
$filesToGet['countries'] = (array)$result[0]->FileName; $filesToGet['country'] = (array)$result[0]->FileName;
$totalFiles += count($filesToGet['countries']); $totalFiles += count($filesToGet['country']);
$countriesSrcDir = (string)$result[0]->DirectoryPath; $srcDir['country'] = $provisionerUrl . (string)$result[0]->DirectoryPath;
$countriesDstDir = "{$this->sccppath['tftp_countries_path']}/{$countryName}"; $dstDir['country'] = "{$this->sccppath['tftp_countries_path']}/{$countryName}";
$filesRetrieved = 0;
foreach (array('countries') as $section){
$srcDir = ${"{$section}SrcDir"};
$dstDir = ${"{$section}DstDir"};
foreach ($filesToGet[$section] as $srcFile) {
file_put_contents("{$dstDir}/{$srcFile}",
file_get_contents($provisionerUrl . $srcDir . $srcFile));
$filesRetrieved ++;
$percentComplete = $filesRetrieved *100 / $totalFiles;
$data = "{$percentComplete},";
echo $data;
ob_flush();
flush();
}
}
$msg = "{$countryName} country tones have been successfully downloaded";
break; break;
default: default:
return false; return array('status' => false, 'message' => 'Invalid request', 'reload' => false);
break; break;
} }
// Now get the files
$filesRetrieved = 0;
foreach (array('language','country', 'firmware') as $section){
if (!isset($dstDir[$section])) {
// No request for this section
continue;
}
$srcDir = $srcDir[$section];
$dstDir = $dstDir[$section];
if (!is_dir($dstDir)) {
mkdir($dstDir, 0755);
}
foreach ($filesToGet[$section] as $srcFile) {
try {
file_put_contents("{$dstDir}/{$srcFile}",
file_get_contents($srcDir. $srcFile));
} catch (\Exception $e) {
return array('status' => false,
'message' => "{$countriesSrcDir}{$srcFile} cannot be found. Check your internet connection, and that this path exists",
'reload' => false);
}
$filesRetrieved ++;
$percentComplete = $filesRetrieved *100 / $totalFiles;
$data = "{$percentComplete},";
echo $data;
ob_flush();
flush();
}
}
return array('status' => true, 'message' => $msg, 'reload' => true); return array('status' => true, 'message' => $msg, 'reload' => true);
} }

View file

@ -262,16 +262,15 @@ trait helperfunctions {
public function getFileListFromProvisioner() { public function getFileListFromProvisioner() {
$provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/"; $provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/";
// Get master tftpboot directory structure // Get master tftpboot directory structure
try {
file_put_contents("{$this->sccppath['tftp_path']}/masterFilesStructure.xml",file_get_contents("{$provisionerUrl}tools/tftpbootFiles.xml")); file_put_contents("{$this->sccppath['tftp_path']}/masterFilesStructure.xml",file_get_contents("{$provisionerUrl}tools/tftpbootFiles.xml"));
//$xmlData = simplexml_load_file("{$provisionerUrl}tools/tftpbootFiles.xml"); } catch (\Exception $e) {
return false;
}
return true; return true;
} }
public function initVarfromXml() { public function initVarfromXml() {
if ((array) $this->xml_data) { if ((array) $this->xml_data) {
foreach ($this->xml_data->xpath('//page_group') as $item) { foreach ($this->xml_data->xpath('//page_group') as $item) {

View file

@ -4,7 +4,7 @@
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
$requestType = 'firmware';
?> ?>
<div class="fpbx-container container-fluid"> <div class="fpbx-container container-fluid">
@ -32,7 +32,7 @@
<li><a class="dropitem" data-id="all" tabindex="-1" href="#"><span><?php echo _('Show All') ?></span></a></li> <li><a class="dropitem" data-id="all" tabindex="-1" href="#"><span><?php echo _('Show All') ?></span></a></li>
</ul> </ul>
</div> </div>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target=".get_ext_file"><i class="fa fa-bolt"></i> <?php echo _("Update Files from Provisioner"); ?> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target=".get_ext_file_<?php echo $requestType; ?>"><i class="fa fa-bolt"></i> <?php echo _("Update Files from Provisioner"); ?>
</button> </button>
</div> </div>
<table data-cookie="true" data-row-style="SetRowColor" data-cookie-id-table="sccp_model-all" data-url="ajax.php?module=sccp_manager&command=getDeviceModel&type=enabled" data-cache="false" data-show-refresh="true" data-toolbar="#toolbar-model" data-maintain-selected="true" data-show-columns="true" data-show-toggle="true" data-toggle="table" data-pagination="true" data-search="true" class="table table-condensed" id="table-models" data-id="model" data-unique-id="model"> <table data-cookie="true" data-row-style="SetRowColor" data-cookie-id-table="sccp_model-all" data-url="ajax.php?module=sccp_manager&command=getDeviceModel&type=enabled" data-cache="false" data-show-refresh="true" data-toolbar="#toolbar-model" data-maintain-selected="true" data-show-columns="true" data-show-toggle="true" data-toggle="table" data-pagination="true" data-search="true" class="table table-condensed" id="table-models" data-id="model" data-unique-id="model">
@ -149,7 +149,7 @@
</div> </div>
</div> </div>
<?php <?php
$requestType = 'firmware';
$selectArray = array(); $selectArray = array();
$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml"); $tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml");
$firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']"); $firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']");