-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrivatePageProtection.php
336 lines (264 loc) · 9.4 KB
/
PrivatePageProtection.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* PrivatePageProtection extension - implements per page acccess restrictions based on user group.
* Which groups are authorized for viewing is defined on-page, using a parser function.
*
* @file
* @ingroup Extensions
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @license GNU General Public Licence 2.0 or later
*/
/*
* WARNING: you can use this extension to deny read access to some pages. Keep in mind that this
* may be circumvented in several ways. This extension doesn't try to
* plug such holes. Also note that pages that are not readable will still be shown in listings,
* such as the search page, categories, etc.
*
* Known ways to access "hidden" pages:
* - transcluding as template. can be avoided using $wgNonincludableNamespaces.
* Some search messages may reveal the page existance by producing links to it (MediaWiki:searchsubtitle,
* MediaWiki:noexactmatch, MediaWiki:searchmenu-exists, MediaWiki:searchmenu-new...).
*
* NOTE: you cannot GRANT access to things forbidden by $wgGroupPermissions. You can only DENY access
* granted there.
*/
if (!defined('MEDIAWIKI')) {
echo("This file is an extension to the MediaWiki software and cannot be used standalone.\n");
die(1);
}
$wgExtensionFunctions[] = 'wfSetupPrivatePageProtection';
$wgExtensionCredits['parserfunction'][] = array(
'path' => __FILE__,
'name' => 'PrivatePageProtection',
'author' => array('Daniel Kinzler', 'Denisov Denis'),
'url' => 'http://mediawiki.org/wiki/Extension:PrivatePageProtection',
'descriptionmsg' => 'privatepp-desc',
);
$wgExtensionMessagesFiles[] = dirname(__FILE__) . '/PrivatePageProtection.i18n.php';
$wgExtensionMessagesFiles[] = dirname(__FILE__) . '/PrivatePageProtection.i18n.magic.php';
class PrivatePageProtection
{
static public $denyRecursive;
static public $groups;
function __construct()
{
global $wgHooks;
$wgHooks['getUserPermissionsErrorsExpensive'][] = $this;
$wgHooks['ParserFirstCallInit'][] = $this;
$wgHooks['ArticleSave'][] = $this;
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = $this;
}
function __toString()
{
return __CLASS__;
}
function onParserFirstCallInit(&$parser)
{
$parser->setFunctionHook('allow-groups', array(__CLASS__, 'privateppRenderTag'));
return true;
}
function ongetUserPermissionsErrorsExpensive($title, $user, $action, &$result)
{
self::$groups = $this->privateppGetAllowedGroups($title);
$result = $this->privateppGetAccessError(self::$groups, $user);
if ($result) {
return false;
}
return true;
}
function onArticleSave(&$wikipage, &$user, &$text, &$summary, $minor, $watchthis, $sectionanchor, &$flags, &$status)
{
$editInfo = $wikipage->prepareTextForEdit($text, null, $user);
$groups = $editInfo->output->getProperty('ppp_allowed_groups');
// $err = $this->privateppGetAccessError($groups, $user);
$err = $this->getSaveAccess($groups, $user);
if (!$err) {
return true;
}
$err[0] = 'privatepp-lockout-prevented'; #override message key
throw new PermissionsError('edit', array($err));
}
function onSkinTemplateOutputPageBeforeExec($sk, &$tpl)
{
if (RequestContext::getMain()->getOutput()->isArticle() and !empty(self::$groups)) {
$data = $tpl->data['credits'];
$message = wfMessage('privatepp-notify', implode(', ', self::$groups), count(self::$groups))->inContentLanguage()->parse();
$data .= "<p>$message</p>";
$tpl->set('credits', $data);
}
return true;
}
static public function privateppRenderTag($parser, $param1 = '', $param2 = '')
{
$args = func_get_args();
$out = $parser->getOutput();
if (count($args) <= 1) {
return true;
}
$groups = array();
for ($i = 1; $i < count($args); $i++) {
$param = strtolower(trim($args[$i]));
if ($param == 'deny-recursive') {
self::$denyRecursive = true;
$out->setProperty('ppp_deny_recursive', self::$denyRecursive);
} else {
$groups[] = $param;
}
}
$groups = implode("|", $groups);
$ppp = $out->getProperty('ppp_allowed_groups');
if ($ppp) {
$groups = $ppp . '|' . $groups;
}
$out->setProperty('ppp_allowed_groups', $groups);
return array('text' => '', 'ishtml' => true, 'inline' => true);
}
/**
* Returns a list of allowed groups for the given page.
*/
public function privateppGetAllowedGroups($title)
{
$id = $title->getArticleID();
$dbr = wfGetDB(DB_SLAVE);
if ($id == 0) {
return array();
}
$page = array($title->getText() => $id);
$Ids = $page;
$out = RequestContext::getMain()->getOutput();
$categories = $this->getCategories($out->getTitle());
if (!empty($categories)) {
$res = $dbr->select(array('page'),
array('page_id', 'page_title'),
array('page_title' => $categories),
__METHOD__);
foreach ($res as $row) {
if ($row->page_title) {
$Ids[$row->page_title] = intval($row->page_id);
}
}
$dbr->freeResult($res);
}
$res = $dbr->select(array('page_props'),
array('pp_value'),
array('pp_page' => array_values($Ids), 'pp_propname' => 'ppp_deny_recursive'),
__METHOD__);
$denyRecursive = array();
if ($res !== false) {
foreach ($res as $row) {
$denyRecursive[] = $row->pp_value;
}
}
$dbr->freeResult($res);
if (!empty($denyRecursive)) {
$structure = $out->getTitle()->getParentCategoryTree();
$categoriesTree = $this->array_values_recursive(array_slice($structure, 0, 1));
$categoriesTree = array_unique($categoriesTree);
$categories = $page;
foreach ($categoriesTree as $category) {
if (strpos($category, ':')) {
$category = explode(':', $category);
$categories[$category[1]] = null;
}
}
$Ids = array_intersect_key($Ids, $categories);
}
$res = $dbr->select(array('page_props'),
array('pp_value'),
array('pp_page' => array_values($Ids), 'pp_propname' => 'ppp_allowed_groups'),
__METHOD__);
$groups = array();
if ($res !== false) {
foreach ($res as $row) {
$groups[] = $row->pp_value;
}
}
$groups = array_unique($groups);
#TODO: use object cache?! get from parser cache?!
return $groups;
}
function privateppGetAccessError($groups, $user)
{
global $wgLang;
if (!$groups) {
return null;
}
if (is_string($groups)) {
$groups = explode('|', $groups);
}
$ugroups = $user->getEffectiveGroups(true);
# Sysop super permissions
if (in_array('sysop', $ugroups)) {
$groups[] = 'sysop';
}
$match = array_intersect($ugroups, $groups);
if ($match) {
# group is allowed - keep processing
return null;
} else {
# group is denied - abort
$groupLinks = array_map(array('User', 'makeGroupLinkWiki'), $groups);
$err = array(
'badaccess',
// $wgLang->commaList($groupLinks),
// count($groups)
);
return $err;
}
}
public function getSaveAccess($groups, $user)
{
global $wgLang;
if (!$groups) {
return null;
}
if (is_string($groups)) {
$groups = explode('|', $groups);
}
$ugroups = $user->getEffectiveGroups(true);
if (in_array('locker', $ugroups)) {
# group is allowed - keep processing
return null;
} else {
# group is denied - abort
$groupLinks = array_map(array('User', 'makeGroupLinkWiki'), $groups);
$err = array(
'badaccess-groups',
$wgLang->commaList($groupLinks),
count($groups)
);
return $err;
}
}
function getCategories(Title $title)
{
$structure = $title->getParentCategoryTree();
$categoriesTree = $this->array_values_recursive($structure);
$categoriesTree = array_unique($categoriesTree);
$categories = array();
foreach ($categoriesTree as $category) {
if (strpos($category, ':')) {
$category = explode(':', $category);
$categories[] = $category[1];
}
}
return $categories;
}
function array_values_recursive($array)
{
$arrayKeys = array();
foreach ($array as $key => $value) {
$arrayKeys[] = $key;
if (!empty($value)) {
$arrayKeys = array_merge($arrayKeys, $this->array_values_recursive($value));
}
}
return $arrayKeys;
}
}
function wfSetupPrivatePageProtection()
{
global $wgPrivatePageProtection;
$wgPrivatePageProtection = new PrivatePageProtection;
}