-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.php
More file actions
224 lines (183 loc) · 6.69 KB
/
helper.php
File metadata and controls
224 lines (183 loc) · 6.69 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
<?php
if (!defined('DOKU_INC')) die();
class helper_plugin_extranet extends DokuWiki_Plugin
{
private function getConfiguredString(array $keys, string $fallback = ''): string
{
foreach ($keys as $key) {
$value = trim((string)$this->getConf($key));
if ($value !== '') return $value;
}
return $fallback;
}
public function getDefaultPolicy(): string
{
$mode = strtolower(trim((string)$this->getConf('default_policy')));
if (in_array($mode, ['allow', 'block', 'force_allow', 'force_block'], true)) {
return $mode;
}
return 'allow';
}
private function hasMacro(string $content, string $macro): bool
{
return (bool)preg_match('/~~\s*' . preg_quote($macro, '/') . '\s*~~/i', $content);
}
public function parseRuleList($raw): array
{
$parts = preg_split('/[\r\n,;]+/', (string)$raw);
$rules = [];
foreach ($parts as $part) {
$rule = trim((string)$part);
if ($rule !== '') $rules[] = $rule;
}
return $rules;
}
public function idMatchesRule(string $id, string $rule): bool
{
$rule = trim($rule);
if ($rule === '') return false;
if (strlen($rule) > 1 && $rule[0] === '/' && substr($rule, -1) === '/') {
return (bool)@preg_match($rule, $id);
}
if (substr($rule, -1) === ':') {
return strpos($id, $rule) === 0;
}
if (strpos($rule, '*') !== false) {
$regex = '/^' . str_replace('\\*', '.*', preg_quote($rule, '/')) . '$/';
return (bool)preg_match($regex, $id);
}
return $id === $rule;
}
public function matchesConfiguredList(string $id, string $confKey): bool
{
$rules = $this->parseRuleList($this->getConf($confKey));
foreach ($rules as $rule) {
if ($this->idMatchesRule($id, $rule)) return true;
}
return false;
}
public function hasConfiguredList(string $confKey): bool
{
return !empty($this->parseRuleList($this->getConf($confKey)));
}
private function hasFilter(): bool
{
if ($this->hasConfiguredList('filter_list')) return true;
return trim((string)$this->getConf('filter_regex')) !== '';
}
private function isInFilter(string $id): bool
{
// filter_list: exact pages, namespace prefixes and wildcards only — no regex entries
foreach ($this->parseRuleList($this->getConf('filter_list')) as $rule) {
$isRegex = strlen($rule) > 1 && $rule[0] === '/' && substr($rule, -1) === '/';
if (!$isRegex && $this->idMatchesRule($id, $rule)) return true;
}
// filter_regex: dedicated regex field
$regex = trim((string)$this->getConf('filter_regex'));
if ($regex !== '' && @preg_match($regex, $id)) return true;
return false;
}
public function getRequestMatchKey(): string
{
return $this->getConfiguredString(['request_match_key', 'server_ip_key'], 'REMOTE_ADDR');
}
public function getExtranetMatchList(): string
{
return $this->getConfiguredString(['extranet_match_list', 'extranet_ip_list']);
}
public function getExtranetMatchRegex(): string
{
return $this->getConfiguredString(['extranet_match_regex', 'extranet_ip_regex']);
}
/**
* Build candidate values from the configured request marker.
*
* A proxy header may contain a single marker ("extranet"), a hostname,
* or a comma-separated list such as X-Forwarded-For. We accept both the
* full raw value and each comma-separated token.
*
* @return string[]
*/
private function getRequestMatchCandidates(): array
{
$requestKey = $this->getRequestMatchKey();
$rawValue = trim((string)($_SERVER[$requestKey] ?? ''));
if ($rawValue === '') return [];
$candidates = [$rawValue];
if (strpos($rawValue, ',') !== false) {
foreach (explode(',', $rawValue) as $part) {
$part = trim((string)$part);
if ($part !== '') $candidates[] = $part;
}
}
return array_values(array_unique($candidates));
}
public function isExtranetRequest(): bool
{
$candidates = $this->getRequestMatchCandidates();
if ($candidates === []) return false;
$matchList = $this->getExtranetMatchList();
if ($matchList !== '') {
foreach (explode(',', $matchList) as $configuredValue) {
$configuredValue = trim((string)$configuredValue);
if ($configuredValue !== '' && in_array($configuredValue, $candidates, true)) {
return true;
}
}
}
$matchRegex = $this->getExtranetMatchRegex();
if ($matchRegex !== '') {
foreach ($candidates as $candidate) {
if ((bool)@preg_match($matchRegex, $candidate)) return true;
}
}
return false;
}
public function isClientFromExtranet(): bool
{
return $this->isExtranetRequest();
}
public function isMediaVisibleFromExtranet(string $mediaID): bool
{
if (!$this->hasFilter()) return true;
$inFilter = $this->isInFilter($mediaID);
$defaultPolicy = $this->getDefaultPolicy();
if ($defaultPolicy === 'force_allow' || $defaultPolicy === 'allow') {
return !$inFilter;
}
return $inFilter;
}
public function isPageVisibleFromExtranet(string $id, ?string $content = null): bool
{
if ($content === null) {
$content = rawWiki($id);
}
$content = (string)$content;
$hasFilter = $this->hasFilter();
$inFilter = $hasFilter && $this->isInFilter($id);
$defaultPolicy = $this->getDefaultPolicy();
if ($defaultPolicy === 'force_allow') {
return !$inFilter;
}
if ($defaultPolicy === 'force_block') {
return $inFilter;
}
$hasExtranet = $this->hasMacro($content, 'EXTRANET');
$hasNoExtranet = $this->hasMacro($content, 'NOEXTRANET');
if ($defaultPolicy === 'allow') {
return !$inFilter && !$hasNoExtranet;
}
// block
return $inFilter || $hasExtranet;
}
public function isPageAllowed(string $id, ?string $content = null): bool
{
if (!$this->isExtranetRequest()) return true;
return $this->isPageVisibleFromExtranet($id, $content);
}
public function isMediaAllowed(string $mediaID): bool
{
if (!$this->isExtranetRequest()) return true;
return $this->isMediaVisibleFromExtranet($mediaID);
}
}