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

View file

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