-
Notifications
You must be signed in to change notification settings - Fork 0
/
souin.php
196 lines (169 loc) · 4.68 KB
/
souin.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
192
193
194
195
196
<?php
require_once(__DIR__ . '/form/SouinConfiguration.php');
require_once(__DIR__ . '/install.php');
/**
* Plugin Name: Souin Cache system Plugin
* Plugin URI: https://github.com/darkweak/souin
* Description: High performance caching solution using Souin.
* Version: 1.0.0
* Author: darkweak
* Author URI: https://github.com/darkweak
**/
const PLUGIN = 'souinPlugin';
const SECTION = 'souin_configuration_section';
const OPT_NAME = 'souin_values';
function toObject($array)
{
$obj = new stdClass;
foreach ($array as $k => $v) {
if (strlen($k)) {
if (is_array($v)) {
$obj->{$k} = toObject($v);
} else {
$obj->{$k} = $v;
}
}
}
return $obj;
}
/**
* @param $configuration
* @return string
*/
function parseToYAML($configuration)
{
$yml = '';
$enabledSecurity = (bool)$configuration->api->security->enable;
$enabledSouin = (bool)$configuration->api->souin->enable;
$isSecureSouin = (bool)$configuration->api->souin->security;
$yml .= <<<YAML
api:
basepath: {$configuration->api->basepath}
security:
basepath: {$configuration->api->security->basepath}
enable: $enabledSecurity
secret: {$configuration->api->security->secret}
YAML;
if (\count($configuration->api->security->users) > 0) {
$yml .= <<<YAML
users:
YAML;
foreach ($configuration->api->security->users as $user) {
if (!$user->username || !$user->password) {
continue;
}
$yml .= <<<YAML
- username: {$user->username}
password: {$user->password}
YAML;
}
}
$yml .= <<<YAML
souin:
basepath: {$configuration->api->souin->basepath}
enable: $enabledSouin
security: $isSecureSouin
default_cache:
port:
web: {$configuration->default_cache->port->web}
tls: {$configuration->default_cache->port->tls}
regex:
exclude: {$configuration->default_cache->regex->exclude}
ttl: {$configuration->default_cache->ttl}
log_level: {$configuration->log_level}
reverse_proxy_url: {$configuration->reverse_proxy_url}
YAML;
if (\count($configuration->ykeys) > 0) {
$yml .= <<<YAML
ykeys:
YAML;
foreach ($configuration->ykeys as $ykey) {
if (!$ykey->name) {
continue;
}
$yml .= <<<YAML
{$ykey->name}:
url: {$ykey->url}
YAML;
}
}
return $yml;
}
function parseRepeatableFields($haystack, $length = 2)
{
$stack = [];
for ($i = 0; $i < \count($haystack) / $length; $i++) {
$stack[] = (object)array_merge(
$haystack[($i * $length)],
$haystack[($i * $length) + 1] ?: []
);
}
return $stack;
}
function souin_admin_menu()
{
global $wpdb;
if (isset($_POST['configuration'])) {
$table_name = $wpdb->prefix . 'souin';
$c = $_POST['configuration'];
$c['api']['security']['users'] = parseRepeatableFields($c['api']['security']['users']);
$c['ykeys'] = parseRepeatableFields($c['ykeys']);
$souin = toObject($c);
$configuration = new SouinConfiguration($souin);
\file_put_contents('/var/www/html/souin.yml', parseToYAML($souin));
$wpdb->update(
$table_name,
[
'configuration' => \serialize($configuration),
],
['id' => 1]
);
}
add_action('admin_enqueue_scripts', 'wpse_repeatable_scripts');
register_setting(PLUGIN, OPT_NAME);
$souinTable = $wpdb->prefix . 'souin';
add_options_page(
'Souin Cache Plugin',
'Souin Cache Plugin',
'manage_options',
'page_slug',
'souin_options_page'
);
$configuration = \unserialize($wpdb->get_results("SELECT configuration FROM $souinTable WHERE id = 1")[0]->configuration);
if (!$configuration) {
$configuration = new SouinConfiguration();
}
$configuration->register('', '');
$configuration->renderField();
}
function wpse_repeatable_scripts()
{
wp_enqueue_script(
'jquery-repeatable',
'/wp-content/plugins/souin/js/repeatable-fields.js',
['jquery', 'jquery-ui-core', 'jquery-ui-sortable'],
'20120206',
true
);
wp_enqueue_script(
'jquery-repeatable-init',
'/wp-content/plugins/souin/js/repeatable-init.js',
['jquery', 'jquery-ui-core', 'jquery-ui-sortable'],
'20120206',
true
);
}
function souin_options_page()
{
?>
<form method='post'>
<h2>Sitepoint Settings API Admin Page</h2>
<?php
settings_fields(PLUGIN);
do_settings_sections(PLUGIN);
submit_button();
?>
</form>
<?php
}
add_action('admin_menu', 'souin_admin_menu');