Skip to content

Commit

Permalink
Merge pull request #2664 from WPO-Foundation/ini
Browse files Browse the repository at this point in the history
Replace the common pattern of hunting for .ini
  • Loading branch information
stoyan authored Jan 26, 2023
2 parents af6a99a + 045946b commit ff98065
Show file tree
Hide file tree
Showing 2 changed files with 31 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
27 changes: 27 additions & 0 deletions www/src/Util/IniReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,31 @@ 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 ff98065

Please sign in to comment.