Skip to content

Commit

Permalink
Replace the common pattern of hunting for .ini
Browse files Browse the repository at this point in the history
* It's a little much to type and fix
* Moved to a static helper
* Start with the most likely to overwrite otherwise we were sometimes
parsing three files
  • Loading branch information
Stoyan Stefanov committed Jan 19, 2023
1 parent 372289c commit 2cef05b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
23 changes: 4 additions & 19 deletions www/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,10 @@
$url = htmlspecialchars($req_url);
}
$placeholder = 'Enter a website URL...';
$profile_file = SETTINGS_PATH . '/profiles.ini';
if (file_exists(SETTINGS_PATH . '/common/profiles.ini')) {
$profile_file = SETTINGS_PATH . '/common/profiles.ini';
}
if (file_exists(SETTINGS_PATH . '/server/profiles.ini')) {
$profile_file = SETTINGS_PATH . '/server/profiles.ini';
}
$profiles = parse_ini_file($profile_file, true);
$connectivity_file = SETTINGS_PATH . '/connectivity.ini.sample';
if (file_exists(SETTINGS_PATH . '/connectivity.ini')) {
$connectivity_file = SETTINGS_PATH . '/connectivity.ini';
}
if (file_exists(SETTINGS_PATH . '/common/connectivity.ini')) {
$connectivity_file = SETTINGS_PATH . '/common/connectivity.ini';
}
if (file_exists(SETTINGS_PATH . '/server/connectivity.ini')) {
$connectivity_file = SETTINGS_PATH . '/server/connectivity.ini';
}
$connectivity = parse_ini_file($connectivity_file, true);

$profiles = IniReader::parse('profiles.ini', true);
$connectivity = IniReader::parse('connectivity.ini', true, true);

$mobile_devices = LoadMobileDevices();

if (isset($_REQUEST['connection']) && isset($connectivity[$_REQUEST['connection']])) {
Expand Down
26 changes: 26 additions & 0 deletions www/src/Util/IniReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,30 @@ public static function getExtensions(): array
}
return [];
}
/**
* Return a parsed ini file by looking ar the default (even a sample) location
* and any overwrites in /common and /server
*
* @param string $filename E.g. "locations.ini"
* @param bool $processSections Same as the second param to `parse_ini_file`
* @param bool $allowSample If true also look for e.g. "locations.ini.sample"
* @return array|null Parsed ini or null
*/
public static function parse($filename, $processSections = false, $allowSample = false) {
$paths = [
realpath(SETTINGS_PATH . '/server/' . $filename),
realpath(SETTINGS_PATH . '/common/' . $filename),
realpath(SETTINGS_PATH . '/' . $filename),
];
if ($allowSample) {
$paths[] = realpath(SETTINGS_PATH . '/' . $filename . '.sample');
}

foreach ($paths as $path) {
if ($path && file_exists($path)) {
return parse_ini_file($path, $processSections);
}
}
return null;
}
}

0 comments on commit 2cef05b

Please sign in to comment.