Repository navigation
Expand file tree
/
Copy pathphpbb_test.php
More file actions
248 lines (221 loc) · 9.27 KB
/
Copy pathphpbb_test.php
File metadata and controls
248 lines (221 loc) · 9.27 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
//
// phpbb_integration, the connection test of its page in the control panel
//
// not for directly open
if (!defined('IN_ADMIN')) {
exit();
}
if (!function_exists('phpbb_integration_test')) {
/**
* can Kleeja sign in the members of the forum with these settings: the forum address, kjauth.php in the forum folder,
* the answer of kjauth.php, its key and the founders of the forum. Only kjauth.php is written, when a folder is set
* @param array $settings as phpbb_settings() gives them
* @return array the steps, each one has its status (ok, warning, error or skipped), title, detail and hint, with HTML
*/
function phpbb_integration_test(array $settings): array
{
$steps = [];
foreach (['ADDRESS', 'FILE', 'CONNECT', 'KEY', 'FOUNDERS'] as $name) {
$steps[] = [
'status' => 'skipped',
'title' => phpbb_integration_word('PHPBB_INTR_STEP_' . $name),
'detail' => phpbb_integration_word('PHPBB_INTR_TEST_SKIPPED'),
'hint' => '',
];
}
// the forum address
$forum = phpbb_forum_address($settings);
if ($forum === '') {
$steps[0] = phpbb_test_result(
$steps[0],
'error',
phpbb_integration_word('PHPBB_INTR_ERR_LINK'),
'PHPBB_INTR_STEP_ADDRESS_HINT',
);
return $steps;
}
$bridge = $forum . '/kjauth.php';
$steps[0] = phpbb_test_result(
$steps[0],
'ok',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_ADDRESS_OK'), phpbb_test_code($bridge)),
);
// kjauth.php in the forum folder, Kleeja writes it there when the forum is on this server
$written = phpbb_write_bridge($settings);
$file = phpbb_test_code($written['file']);
$steps[1] = match ($written['status']) {
'written' => phpbb_test_result(
$steps[1],
'ok',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_FILE_WRITTEN'), $file),
),
'current' => phpbb_test_result(
$steps[1],
'ok',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_FILE_CURRENT'), $file),
),
'no_folder' => phpbb_test_result(
$steps[1],
'skipped',
phpbb_integration_word('PHPBB_INTR_STEP_FILE_NO_FOLDER'),
),
'not_phpbb' => phpbb_test_result(
$steps[1],
'warning',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_FILE_NOT_PHPBB'), $file),
'PHPBB_INTR_STEP_FILE_HINT',
),
default => phpbb_test_result(
$steps[1],
'warning',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_FILE_NOT_WRITABLE'), $file),
'PHPBB_INTR_STEP_FILE_HINT',
),
};
// the answer of kjauth.php. The server of the forum may cache PHP files, and run the copy of kjauth.php
// it had before Kleeja wrote the new one for a few seconds, so an old answer is asked again for a while
$in_folder = in_array($written['status'], ['written', 'current'], true);
$reply = phpbb_bridge_request($settings, 'test');
for ($wait = 0; $in_folder && $wait < PHPBB_BRIDGE_CACHE_WAIT && phpbb_test_is_old($reply); $wait++) {
sleep(1);
$reply = phpbb_bridge_request($settings, 'test');
}
$answer = $reply['answer'];
$status = phpbb_test_code('HTTP ' . $reply['status']);
// the file of the forum folder is the right one, the forum answers with another
$old_hint = $in_folder ? 'PHPBB_INTR_STEP_CACHED_HINT' : 'PHPBB_INTR_STEP_CONNECT_OLD_HINT';
$failed = match (true) {
$reply['error'] === 'no_client' => [
phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_NO_CLIENT'),
'PHPBB_INTR_STEP_CONNECT_NO_CLIENT_HINT',
],
$reply['error'] !== '' => [
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_FAILED'), phpbb_test_code($bridge)) .
($reply['detail'] !== '' ? ' ' . phpbb_test_code($reply['detail']) : ''),
'PHPBB_INTR_STEP_CONNECT_SERVER_HINT',
],
$reply['status'] >= 300 && $reply['status'] < 400 => [
sprintf(
phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_REDIRECT'),
phpbb_test_code($reply['redirect'] ?: '?'),
),
'PHPBB_INTR_STEP_CONNECT_REDIRECT_HINT',
],
$reply['status'] === 404 => [
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_MISSING'), phpbb_test_code($bridge)),
'PHPBB_INTR_STEP_CONNECT_MISSING_HINT',
],
// the first versions of kjauth.php answer with a number
$answer === null && (bool) preg_match('/^\d{4}$/D', trim($reply['body'])) => [
phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_OLD'),
$old_hint,
],
$answer === null => [
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_UNKNOWN'), $status),
'PHPBB_INTR_STEP_CONNECT_UNKNOWN_HINT',
],
(int) $answer['kjauth'] !== PHPBB_BRIDGE_VERSION => [
sprintf(
phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_VERSION'),
phpbb_test_code((string) (int) $answer['kjauth']),
phpbb_test_code((string) PHPBB_BRIDGE_VERSION),
),
$old_hint,
],
default => null,
};
if ($failed !== null) {
$steps[2] = phpbb_test_result($steps[2], 'error', $failed[0], $failed[1]);
return $steps;
}
$steps[2] = phpbb_test_result(
$steps[2],
'ok',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_OK'), phpbb_test_code($bridge)),
);
// the key
if (($answer['status'] ?? '') !== 'ok') {
$steps[3] =
($answer['error'] ?? '') === 'key'
? phpbb_test_result(
$steps[3],
'error',
phpbb_integration_word('PHPBB_INTR_STEP_KEY_WRONG'),
$in_folder ? 'PHPBB_INTR_STEP_CACHED_HINT' : 'PHPBB_INTR_STEP_KEY_WRONG_HINT',
)
: phpbb_test_result(
$steps[3],
'error',
sprintf(
phpbb_integration_word('PHPBB_INTR_STEP_CONNECT_UNKNOWN'),
phpbb_test_code((string) ($answer['error'] ?? '?')),
),
'PHPBB_INTR_STEP_CONNECT_OLD_HINT',
);
return $steps;
}
$steps[3] = phpbb_test_result(
$steps[3],
'ok',
sprintf(
phpbb_integration_word('PHPBB_INTR_STEP_KEY_OK'),
phpbb_test_code((string) ($answer['phpbb'] ?? '?')),
),
);
// the members who will open the control panel
$founders = (int) ($answer['founders'] ?? 0);
$steps[4] = $founders
? phpbb_test_result(
$steps[4],
'ok',
sprintf(phpbb_integration_word('PHPBB_INTR_STEP_FOUNDERS_OK'), number_format($founders)),
)
: phpbb_test_result(
$steps[4],
'error',
phpbb_integration_word('PHPBB_INTR_STEP_FOUNDERS_NONE'),
'PHPBB_INTR_STEP_FOUNDERS_NONE_HINT',
);
return $steps;
}
/**
* kjauth.php answered as an older copy of itself: with the number of the first versions, another version,
* or a refusal of the key
* @param array $reply as phpbb_bridge_request() gives it
* @return bool
*/
function phpbb_test_is_old(array $reply): bool
{
$answer = $reply['answer'];
if ($answer === null) {
return $reply['error'] === '' && (bool) preg_match('/^\d{4}$/D', trim($reply['body']));
}
return (int) $answer['kjauth'] !== PHPBB_BRIDGE_VERSION || ($answer['error'] ?? '') === 'key';
}
/**
* a step of the test with what it found
* @param array $step
* @param string $status ok, warning, error or skipped
* @param string $detail with HTML
* @param string $hint the word of the plugin that says how to fix it, if any
* @return array
*/
function phpbb_test_result(array $step, string $status, string $detail, string $hint = ''): array
{
return [
'status' => $status,
'detail' => $detail,
'hint' => $hint === '' ? '' : phpbb_integration_word($hint),
] + $step;
}
/**
* a value in the detail of a step, a path or an address reads from left to right in Arabic too
* @param string $text
* @return string
*/
function phpbb_test_code(string $text): string
{
return '<code dir="ltr">' . htmlspecialchars($text, ENT_QUOTES) . '</code>';
}
}