-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrze-sso.php
191 lines (165 loc) · 5.8 KB
/
rrze-sso.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/*
Plugin Name: RRZE SSO
Plugin URI: https://github.com/RRZE-Webteam/rrze-sso
Version: 1.6.12
Description: Single-Sign-On (SSO) SAML-Integrations-Plugin für WordPress.
Author: RRZE-Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License Version 3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: rrze-sso
Domain Path: /languages
Requires at least: 6.7
Requires PHP: 8.2
*/
namespace RRZE\SSO;
defined('ABSPATH') || exit;
const RRZE_PHP_VERSION = '8.2';
const RRZE_WP_VERSION = '6.7';
/**
* SPL Autoloader (PSR-4).
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) {
$prefix = __NAMESPACE__;
$baseDir = __DIR__ . '/includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relativeClass = substr($class, $len);
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Register plugin hooks.
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
// Load the plugin's text domain for localization.
add_action('init', fn() => load_plugin_textdomain('rrze-sso', false, dirname(plugin_basename(__FILE__)) . '/languages'));
/**
* Activation callback function.
*/
function activation()
{
//
}
/**
* Deactivation callback function.
*/
function deactivation()
{
$optionGroup = Options::getOptionGroup();
$optionName = Options::getOptionName();
$options = Options::getOptions();
unregister_setting($optionGroup, $optionName);
if ($options->force_sso) {
$options->force_sso = 0;
$options = (array) $options;
update_site_option($optionName, $options);
}
}
/**
* Instantiate Plugin class.
* @return object Plugin
*/
function plugin()
{
static $instance;
if (null === $instance) {
$instance = new Plugin(__FILE__);
}
return $instance;
}
/**
* Instantiate SimpleSAML class.
* @return object SimpleSAML
*/
function simpleSAML()
{
static $instance;
if (null === $instance) {
$instance = new SimpleSAML();
}
return $instance;
}
/**
* Check system requirements for the plugin.
*
* This method checks if the server environment meets the minimum WordPress and PHP version requirements
* for the plugin to function properly.
*
* @return string An error message string if requirements are not met, or an empty string if requirements are satisfied.
*/
function systemRequirements(): string
{
// Get the global WordPress version.
global $wp_version;
// Get the PHP version.
$phpVersion = phpversion();
// Initialize an error message string.
$error = '';
// Check if the WordPress version is compatible with the plugin's requirement.
if (!is_wp_version_compatible(plugin()->getRequiresWP())) {
$error = sprintf(
/* translators: 1: Server WordPress version number, 2: Required WordPress version number. */
__('The server is running WordPress version %1$s. The Plugin requires at least WordPress version %2$s.', 'rrze-sso'),
$wp_version,
plugin()->getRequiresWP()
);
} elseif (!is_php_version_compatible(plugin()->getRequiresPHP())) {
// Check if the PHP version is compatible with the plugin's requirement.
$error = sprintf(
/* translators: 1: Server PHP version number, 2: Required PHP version number. */
__('The server is running PHP version %1$s. The Plugin requires at least PHP version %2$s.', 'rrze-sso'),
$phpVersion,
plugin()->getRequiresPHP()
);
}
// Return the error message string, which will be empty if requirements are satisfied.
return $error;
}
/**
* Handle the loading of the plugin.
*
* This function is responsible for initializing the plugin, loading text domains for localization,
* checking system requirements, and displaying error notices if necessary.
*/
function loaded()
{
// Trigger the 'loaded' method of the main plugin instance.
plugin()->loaded();
// Check system requirements and store any error messages.
if (systemRequirements()) {
// If there is an error, add an action to display an admin notice with the error message.
add_action('admin_init', function () {
$error = systemRequirements();
// Check if the current user has the capability to activate plugins.
if (current_user_can('activate_plugins')) {
// Get plugin data to retrieve the plugin's name.
$pluginName = plugin()->getName();
// Determine the admin notice tag based on network-wide activation.
$tag = is_plugin_active_for_network(plugin()->getBaseName()) ? 'network_admin_notices' : 'admin_notices';
// Add an action to display the admin notice.
add_action($tag, function () use ($pluginName, $error) {
printf(
'<div class="notice notice-error"><p>' .
/* translators: 1: The plugin name, 2: The error string. */
esc_html__('Plugins: %1$s: %2$s', 'rrze-sso') .
'</p></div>',
$pluginName,
$error
);
});
}
});
// Return to prevent further initialization if there is an error.
return;
}
// If there are no errors, create an instance of the 'Main' class and trigger its 'loaded' method.
(new Main)->loaded();
}