Skip to content

Commit 5f6a6ff

Browse files
committed
refactor config loading logic, remove settings.php usage, and centralize default settings
1 parent ad8aaa2 commit 5f6a6ff

8 files changed

Lines changed: 83 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ After `cd`-ing into the configured phpList plugin directory:
1111
## Configuration
1212

1313
### You can configure saml from the phplist configuration page "SSO config" category (except for certificate which should be added manually in the cert folder)
14-
you will need to give write permission to the simplesaml/settings.php file
14+
on first install the plugin uses built-in defaults and then reads values from phpList configuration
1515

1616
https://resources.phplist.com/plugin/simplesaml#simplesamlphp_installation_check
1717

plugins/simplesaml.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require_once dirname(__FILE__, 2) . '/defaultplugin.php';
3+
require_once $_SERVER['DOCUMENT_ROOT'] . '/lists/admin/defaultplugin.php';
44
require_once __DIR__ . '/simplesaml/simplesamlphp/lib/_autoload.php';
55

66
use SimpleSAML\Auth\Simple;
@@ -92,23 +92,21 @@ class simplesaml extends phplistPlugin
9292
'category' => self::CONFIG_CATEGORY,
9393
],
9494
];
95-
private const SETTINGS_FILE_NAME= 'settings.php';
9695
function __construct()
9796
{
9897
if ( version_compare(PHP_VERSION, '7.4.0') >= 0) {
9998
require_once(__DIR__ . '/simplesaml/simplesamlphp/lib/_autoload.php');
10099
}
101100
parent::__construct();
102101
$this->tables = $GLOBALS['tables'];
103-
$filename = __DIR__ . '/simplesaml/' . self::SETTINGS_FILE_NAME;
104-
105-
$dataToWrite = [];
102+
$configuredDisplayName = getConfig($this->name);
103+
if ($configuredDisplayName !== false && $configuredDisplayName !== null && $configuredDisplayName !== '') {
104+
$this->settings[$this->name]['value'] = $configuredDisplayName;
105+
}
106106
foreach ($this->settings as $key => $setting) {
107-
$dataToWrite[$key] = !empty(getConfig($key)) ? getConfig($key) : $setting['value'];
107+
$this->settings[$key]['value'] = !empty(getConfig($key)) ? getConfig($key) : $setting['value'];
108108
}
109-
$this->settings[$this->name]['value'] = $dataToWrite[$this->name];
110109

111-
file_put_contents($filename, "<?php\n\nreturn " . var_export($dataToWrite, true) . ";\n");
112110
if ($this->settings['saml_secret_salt']['value'] == getConfig('saml_secret_salt')) {
113111
$GLOBALS['msg'] = ($GLOBALS['I18N']->get('Please change saml secret salt').'<br/>');
114112
}

plugins/simplesaml/settings.php

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
saml-private-key.pem
22
saml-certificate.pem
33
saml-remote-idp.crt
4+
*.crt

plugins/simplesaml/simplesamlphp/config/authsources.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
$settings = include __DIR__ . '/../../settings.php';
3+
require_once __DIR__ . '/phplist-settings.php';
4+
$settings = simplesamlLoadSettings();
45

56
$config = [
67
/*

plugins/simplesaml/simplesamlphp/config/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66

77
$httpUtils = new \SimpleSAML\Utils\HTTP();
8-
$settings = include __DIR__ . '/../../settings.php';
8+
require_once __DIR__ . '/phplist-settings.php';
9+
$settings = simplesamlLoadSettings();
910

1011
$config = [
1112

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
function simplesamlDefaultSettings(): array
4+
{
5+
return [
6+
'simplesaml' => 'Saml',
7+
'hide_default_login' => false,
8+
'saml_idp' => 'https://sso.phplist.com:8443/realms/master',
9+
'saml_entity_id' => 'phplisttest',
10+
'saml_realm' => 'master',
11+
'saml_trusted_url_domains' => 'localhost',
12+
'saml_session_cookie_domain' => '.localhost',
13+
'saml_session_save_path' => '/var/lib/php/sessions',
14+
'saml_secret_salt' => 'defaultsecretsalt',
15+
'saml_admin_password' => '123',
16+
];
17+
}
18+
19+
function simplesamlHasConfigValue($value): bool
20+
{
21+
return !($value === false || $value === null || $value === '');
22+
}
23+
24+
function simplesamlBootstrapPhpList(): void
25+
{
26+
if (function_exists('getConfig')) {
27+
return;
28+
}
29+
30+
$documentRoot = rtrim((string) ($_SERVER['DOCUMENT_ROOT'] ?? ''), '/');
31+
$candidates = [
32+
$documentRoot . '/lists/admin/defaultconfig.php',
33+
dirname(__DIR__, 4) . '/defaultconfig.php',
34+
dirname(__DIR__, 5) . '/admin/defaultconfig.php',
35+
dirname(__DIR__, 6) . '/lists/admin/defaultconfig.php',
36+
];
37+
38+
foreach (array_unique($candidates) as $candidate) {
39+
if ($candidate === '' || strpos($candidate, '//') !== false) {
40+
continue;
41+
}
42+
if (is_file($candidate)) {
43+
require_once $candidate;
44+
}
45+
if (function_exists('getConfig')) {
46+
return;
47+
}
48+
}
49+
}
50+
51+
function simplesamlLoadSettings(): array
52+
{
53+
$settings = simplesamlDefaultSettings();
54+
simplesamlBootstrapPhpList();
55+
56+
if (!function_exists('getConfig')) {
57+
return $settings;
58+
}
59+
60+
foreach ($settings as $key => $defaultValue) {
61+
$configured = getConfig($key);
62+
if (simplesamlHasConfigValue($configured)) {
63+
$settings[$key] = $configured;
64+
}
65+
}
66+
67+
return $settings;
68+
}

plugins/simplesaml/simplesamlphp/metadata/saml20-idp-remote.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
$settings = include __DIR__ . '/../../settings.php';
4-
$key = $settings['saml_idp'] ?? 'https://sso.phplist.com:8443/realms/phplist';
3+
require_once __DIR__ . '/../config/phplist-settings.php';
4+
$settings = simplesamlLoadSettings();
5+
$key = $settings['saml_idp'];
56

67
/**
78
* SAML 2.0 remote IdP metadata for SimpleSAMLphp.

0 commit comments

Comments
 (0)