Optimise validate part of getSccpModelInformation

Remove needless iterations and use array functions.
Use basename to remove file extension to facilitate this
This commit is contained in:
steve-lad 2021-08-01 11:57:03 +02:00
parent ab7b295b87
commit 28bb5a63da
2 changed files with 33 additions and 24 deletions

View file

@ -1078,11 +1078,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
case 'pro':
case 'on':
case 'internal':
$dir_list = $this->findAllFiles($dir, $file_ext, 'fileonly');
$dir_list = $this->findAllFiles($dir, $file_ext, 'fileBaseName');
break;
case 'off':
default: // Place in root TFTP dir
$dir_list = $this->findAllFiles($dir, $file_ext);
$dir_list = $this->findAllFiles($dir, $file_ext, 'dirFileBaseName');
break;
}
$raw_settings = $this->dbinterface->getDb_model_info($get, $format_list, $filter);
@ -1090,12 +1090,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
for ($i = 0; $i < count($raw_settings); $i++) {
if (!empty($raw_settings[$i]['loadimage'])) {
$raw_settings[$i]['validate'] = 'no;';
foreach ($dir_list as $filek) {
switch ($search_mode) {
case 'pro':
case 'on':
case 'internal':
if (strpos($filek, $raw_settings[$i]['loadimage']) !== false) {
if (in_array($raw_settings[$i]['loadimage'], $dir_list, true)) {
$raw_settings[$i]['validate'] = 'yes;';
}
break;
@ -1103,12 +1102,11 @@ class Sccp_manager extends \FreePBX_Helpers implements \BMO {
break;
case 'off':
default: // Place in root TFTP dir
if (strpos($filek, strtolower($dir . '/' . $raw_settings[$i]['loadimage'])) !== false) {
if (in_array("{$dir}/{$raw_settings[$i]['loadimage']}", $dir_list, true)) {
$raw_settings[$i]['validate'] = 'yes;';
}
break;
}
}
} else {
$raw_settings[$i]['validate'] = '-;';
}

View file

@ -120,21 +120,32 @@ trait helperfunctions {
}
foreach (array_diff(scandir($searchDir),array('.', '..')) as $value) {
if (is_file("$searchDir/$value")) {
$extFound = '';
$foundFile = true;
if (!empty($file_mask)) {
$foundFile = false;
foreach ($file_mask as $k) {
if (strpos($value, $k) !== false) {
$foundFile = true;
$extFound = $k;
break;
}
}
}
if ($foundFile) {
if ($mode == 'fileonly') {
switch ($mode) {
case 'fileonly':
$result[] = $value;
} else {
break;
case 'fileBaseName':
$result[] = basename("/$value", $extFound);
break;
case 'dirFileBaseName':
$result[] = $searchDir . "/" . basename("/$value", $extFound);
break;
default:
$result[] = "$searchDir/$value";
break;
}
}
continue;