From 1b32e0977e6b741ff61919282b1eabdaf5f76d3a Mon Sep 17 00:00:00 2001 From: steve-lad <72376554+steve-lad@users.noreply.github.com> Date: Thu, 15 Jul 2021 12:12:07 +0200 Subject: [PATCH] Optimise locale (language) management Optimise function calls Use array functions instead of loops findInstalledlangs is currently called by the construct - as this is called frequently, maximise its efficiency --- Sccp_manager.class.php | 51 ++++++++++++----------------- sccpManClasses/extconfigs.class.php | 5 --- sccpManClasses/formcreate.class.php | 4 +-- sccpManTraits/ajaxHelper.php | 10 +++--- sccpManTraits/helperFunctions.php | 12 +++---- views/advserver.model.php | 3 +- views/formShowSysDefs.php | 3 +- 7 files changed, 36 insertions(+), 52 deletions(-) diff --git a/Sccp_manager.class.php b/Sccp_manager.class.php index ea49bd2..79ec882 100644 --- a/Sccp_manager.class.php +++ b/Sccp_manager.class.php @@ -84,12 +84,11 @@ namespace FreePBX\modules; class Sccp_manager extends \FreePBX_Helpers implements \BMO { /* Field Values for type seq */ - private $SCCP_LANG_DICTIONARY = 'be-sccp.jar'; // CISCO LANG file search in /tftp-path private $pagedata = null; private $sccp_driver_ver = '11.4'; // Ver fore SCCP.CLASS.PHP public $sccp_manager_ver = '14.1.0.0'; public $sccp_branch = 'm'; // Ver fore SCCP.CLASS.PHP - private $tftpLang = array(); + private $installedLangs = array(); private $hint_context = array('default' => '@ext-local'); /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Get it from Config !!! private $val_null = 'NONE'; /// REPLACE to null Field @@ -140,7 +139,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO { $this->sccpvalues = $this->dbinterface->get_db_SccpSetting(); //Initialise core settings $this->initializeSccpPath(); //Set required Paths $this->updateTimeZone(); // Get timezone from FreePBX - $this->initTftpLang(); + $this->findInstLangs(); $this->saveSccpSettings(); } @@ -173,7 +172,7 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO { 'h_show' => $show_Header, 'form_prefix' => $form_prefix, 'fvalues' => $form_values, - 'tftpLang' => $this->tftpLang, + 'installedLangs' => $this->installedLangs, 'chanSccpHelp' => $this->sccpHelpInfo, 'sccp_defaults' => $this->sccpvalues ) @@ -749,34 +748,26 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO { /** * Retrieve Active Codecs - * return finds Languageg pack + * return finds Language pack */ - private function initTftpLang() { + private function findInstLangs() { + //locales and languages are installed in the tftp_lang_path $result = array(); - if (empty($this->sccppath["tftp_path"]) || empty($this->sccppath["tftp_lang_path"])) { - return $result; - } - $dir = $this->sccppath["tftp_lang_path"]; + $langDir = $this->sccppath["tftp_lang_path"]; + $localeJar = 'be-sccp.jar'; // This jar should exist if the locale is populated + $langArr = $this->extconfigs->getExtConfig('sccp_lang'); + $localeArray = array_combine(array_keys($langArr),array_column($langArr, 'locale')); - $cdir = scandir($dir); - foreach ($cdir as $key => $value) { - if (!in_array($value, array(".", ".."))) { - if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { - $filename = $dir . DIRECTORY_SEPARATOR . $value . DIRECTORY_SEPARATOR . $this->SCCP_LANG_DICTIONARY; - if (file_exists($filename)) { - $lang_ar = $this->extconfigs->getExtConfig('sccp_lang'); - foreach ($lang_ar as $lang_key => $lang_value) { - if ($lang_value['locale'] == $value) { - $result[$lang_key] = $value; - } - } -// $result[] = $value; - } + foreach (array_diff(scandir($langDir),array('.', '..')) as $subDir) { + if (is_dir($langDir . DIRECTORY_SEPARATOR . $subDir)) { + $filename = $langDir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . $localeJar; + if (file_exists($filename)) { + $result = array_merge(array_intersect($localeArray,array($subDir)),$result); } } } - $this->tftpLang = $result; + $this->installedLangs = $result; } /* @@ -785,11 +776,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO { private function initializeTFtpLanguagePath() { $dir = $this->sccppath["tftp_lang_path"]; - foreach ($this->extconfigs->getExtConfig('sccp_lang') as $lang_key => $lang_value) { - $filename = $dir . DIRECTORY_SEPARATOR . $lang_value['locale']; - if (!file_exists($filename)) { - if (!mkdir($filename, 0777, true)) { - die('Error creating tftp language directory'); + foreach ($this->extconfigs->getExtConfig('sccp_lang') as $langKey => $langValueArr) { + $localeDir = $dir . DIRECTORY_SEPARATOR . $langValueArr['locale']; + if (!is_dir($localeDir)) { + if (!mkdir($localeDir, 0755, true)) { + die("Error creating $localeDir directory"); } } } diff --git a/sccpManClasses/extconfigs.class.php b/sccpManClasses/extconfigs.class.php index 481803f..9c8d923 100644 --- a/sccpManClasses/extconfigs.class.php +++ b/sccpManClasses/extconfigs.class.php @@ -1,14 +1,9 @@ paren_class = $parent_class; diff --git a/sccpManClasses/formcreate.class.php b/sccpManClasses/formcreate.class.php index f7eff97..ee49732 100644 --- a/sccpManClasses/formcreate.class.php +++ b/sccpManClasses/formcreate.class.php @@ -482,7 +482,7 @@ class formcreate name; $res_id = $npref.$res_n; @@ -502,7 +502,7 @@ class formcreate $select_opt= $syslangs; break; case 'SLT': - $select_opt= $tftpLang; + $select_opt= $installedLangs; break; case 'SLZ': $timeZoneOffsetList = array('-12' => 'GMT -12', '-11' => 'GMT -11', '-10' => 'GMT -10', '-09' => 'GMT -9', diff --git a/sccpManTraits/ajaxHelper.php b/sccpManTraits/ajaxHelper.php index 67c5775..68ce488 100644 --- a/sccpManTraits/ajaxHelper.php +++ b/sccpManTraits/ajaxHelper.php @@ -397,19 +397,17 @@ trait ajaxHelper { $save_codec = array(); $count_mods = 0; $dbSaveArray = array(); - $integer_msg = _("%s must be a non-negative integer"); $errors = array(); $i = 0; - $action = isset($request['sccp_createlangdir']) ? $request['sccp_createlangdir'] : ''; + + if (isset($request["{$hdr_prefix}createlangdir"]) && ($request["{$hdr_prefix}createlangdir"] == 'yes')) { + $this->initializeTFtpLanguagePath(); + } // if uncheck all codecs, audiocodecs key is missing so nothing changes in db. // Unsetting all codecs will now return to chan-sccp defaults. if (!isset($request['audiocodecs'])) { $request['audiocodecs'] = array_fill_keys(explode(',',$this->sccpvalues['allow']['systemdefault']),true); } - if ($action == 'yes') { - $this->initializeTFtpLanguagePath(); - } - foreach ($request as $key => $value) { // Originally saved all to sccpvalues. Now will save to db defaults if appropriate // TODO: Need to verify the tables defined in showGroup - some options maybe diff --git a/sccpManTraits/helperFunctions.php b/sccpManTraits/helperFunctions.php index 6f4e4f1..f911d96 100644 --- a/sccpManTraits/helperFunctions.php +++ b/sccpManTraits/helperFunctions.php @@ -271,7 +271,7 @@ trait helperfunctions { } - public function getFilesFromProvisioner($type = "",$name = "",$device = "") { + public function getFilesFromProvisioner($type = '',$name = '',$device = '') { $filesToGet = array(); $provisionerUrl = "https://github.com/dkgroot/provision_sccp/raw/master/"; if (!$tftpBootXml = simplexml_load_file("{$this->sccppath['tftp_path']}/masterFilesStructure.xml")) { @@ -280,15 +280,15 @@ trait helperfunctions { } switch ($type) { case 'firmware': - if (!is_dir("{$this->sccppath['tftp_path']}/firmware/{$device}")) { - mkdir("{$this->sccppath['tftp_path']}/firmware/{$device}", 0755); + if (!is_dir("{$this->sccppath['tftp_firmware_path']}/{$device}")) { + mkdir("{$this->sccppath['tftp_firmware_path']}/{$device}", 0755); } $firmwareDir = $tftpBootXml->xpath("//Directory[@name='firmware']"); $result = $firmwareDir[0]->xpath("//Directory[@name={$device}]"); $filesToGet = (array)$result[0]->FileName; foreach ($filesToGet as $srcFile) { - file_put_contents("{$this->sccppath['tftp_path']}/firmware/{$device}/{$srcFile}", - file_get_contents("{$provisionerUrl}" . (string)$result[0]->DirectoryPath . $srcFile)); + file_put_contents("{$this->sccppath['tftp_firmware_path']}/{$device}/{$srcFile}", + file_get_contents($provisionerUrl . (string)$result[0]->DirectoryPath . $srcFile)); } return "thanks for trying Diederik :-)"; break; @@ -298,8 +298,6 @@ trait helperfunctions { break; } } - file_get_contents(https://github.com/dkgroot/provision_sccp/raw/master/tftpboot/firmware/7911/SCCP11.9-4-2SR3-1S.loads): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found - File:/var/www/html/admin/modules/sccp_manager/sccpManTraits/helperFunctions.php:291 public function initVarfromXml() { if ((array) $this->xml_data) { diff --git a/views/advserver.model.php b/views/advserver.model.php index 3361159..f24c682 100644 --- a/views/advserver.model.php +++ b/views/advserver.model.php @@ -162,7 +162,8 @@
This site, dkgroot/provision_sccp, is unrelated to Sccp_Manager, and the files found cannot be warrantied
- If you accept this, please select the device type that you want to try to get files for and then Get Files From Provisioner

" ?> + If you accept this, please select the device type that you want to try to get files for and then Get Files From Provisioner

+ Please be patient - this may take some time depending on your internet link

" ?>
diff --git a/views/formShowSysDefs.php b/views/formShowSysDefs.php index a47dba2..402b48e 100644 --- a/views/formShowSysDefs.php +++ b/views/formShowSysDefs.php @@ -81,7 +81,7 @@ foreach ($items as $child) { case 'SLA': case 'SLZ': case 'SL': - \FreePbx::sccp_manager()->formcreate->addElementSL($child, $fvalues, $sccp_defaults,$npref, $tftpLang); + \FreePbx::sccp_manager()->formcreate->addElementSL($child, $fvalues, $sccp_defaults,$npref, $installedLangs); break; case 'SDM': case 'SDMS': @@ -105,3 +105,4 @@ if ($h_show==1) { echo ''; } ?> +installedLangs