-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPageProtectionPlus.php
More file actions
193 lines (164 loc) · 5.72 KB
/
Copy pathPageProtectionPlus.php
File metadata and controls
193 lines (164 loc) · 5.72 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
<?php
/**
* PageProtectionPlus extension.
*
* PHP version 5
*
* @category Encryption
* @package PageProtectionPlus
* @author Fabian Schmitt <fs@u4m.de>, Pawel Wilk <pw@gnu.org>
* @copyright 2006, 2007 Fabian Schmitt, Pawel Wilk
* @license http://www.gnu.org/licenses/gpl.html General Public License version 2 or higher
* @version 2.3b
* @link http://www.mediawiki.org/wiki/Extension:PPP
*/
define("PROTECT_TAG", "protect");
define("VAR_USERS", "{{{USERS}}}");
define("VAR_GROUPS", "{{{GROUPS}}}");
require_once("PageProtection.i18n.php");
$wgExtensionFunctions[] = "wfPageProtection";
/* register parser hook */
$wgExtensionCredits['parserhook'][] = array(
'name' => 'PPP',
'author' => 'Fabian Schmitt, Paweł Wilk',
'version' => '2.3b',
'url' => 'http://www.mediawiki.org/wiki/Extension:PPP' );
/* register special page hook */
$wgExtensionCredits['specialpage'][] = array(
'name' => 'PPP',
'author' => 'Paweł Wilk, Fabian Schmitt',
'version' => '2.3b',
'url' => 'http://www.mediawiki.org/wiki/Extension:PPP' );
/* Default groups that can read anything */
$wgPppDefaultGroups = array(
"sysop"
);
/**
* Extension-function. Registers special page for displaying ciphersuite
*/
if ( !function_exists( 'extAddSpecialPage' ) ) {
require( dirname(__FILE__) . '/../ExtensionFunctions.php' );
}
extAddSpecialPage( dirname(__FILE__) . '/SpecialCipherSuite.php', 'PageProtectionCipherSuite', 'SpecialPageProtectionCipherSuite' );
/**
* Extension-function. Registers parser, hook, messages.
*/
function wfPageProtection() {
global $wgParser;
global $wgMessageCache, $wgPageProtectionMessages;
foreach( $wgPageProtectionMessages as $key => $value ) {
$wgMessageCache->addMessages( $wgPageProtectionMessages[$key], $key );
}
$wgParser->setHook( PROTECT_TAG, "protectPage" );
global $wgHooks;
$wgHooks['AlternateEdit'][] = 'protectedEdit';
$wgHooks['ArticleSave'][] = 'protectSave';
}
require_once("ProtectPage.php");
require_once("ErrorHandler.php");
/**
* Callback function for the hook to the protect-tag.
* @param text Text to be protected
* @param params Parameters supplied to the tag
* @param parser Global parser-object
* @return If current user is allowed to read the page, $text will be returned.
* Otherwise, an error-page will be returned.
*/
function protectPage( $text, $params, &$parser) {
$parser->disableCache();
global $wgUser;
global $wgOut;
try {
$protect = new ProtectPage($parser);
$protect->initShow($params["users"], $params["groups"], $params["show"]);
if ($protect->hasAccess($wgUser)) {
global $wgRequest;
try {
$rtext = $protect->mEnc->Decrypt($text);
$rtext = $protect->parseTag($rtext);
} catch (Exception $e) {
$emsg = $e->getMessage();
$etrace = $e->getTraceAsString();
/* nasty hack, because we would like to be */
/* backward compatible and we cannot check */
/* whether some part was encrypted or not */
/* when doing page preview. also there is */
/* no such hook as AlternatePreview. */
/* as a result we may experience doubled */
/* decryption action, which tries to */
/* decrypt already decrypted data causing */
/* error in the wrapper */
/* FIXME: find another way to determine we're in preview */
/* rather than looking at the exception's trace */
if (strpos($emsg, 'Error tail') > 10 &&
strpos($etrace, 'EditPage->getPreviewText') > 10) {
$rtext = $text;
$rtext = $protect->parseTag($rtext);
} else {
throw $e; /* not that type of error, pass it */
}
}
return ($rtext);
}
$show = $params["show"];
$page = null;
if (isset($params["errorpage"])) {
$page = $params["errorpage"];
}
$err = new ErrorHandler($protect->mAccess,
$protect,
$show,
$page);
return $err->showError($text);
} catch (Exception $e) {
$err = new ErrorHandler($protect->mAccess, $protect);
return $err->InternalError('decrypt_error', $e->getMessage());
}
}
/**
* Callback function for the hook to ArticleSave. Encrypts the
* data between protect-tags and ensures the current user is in
* list of permitted users.
*/
function protectSave(&$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor) {
global $wgParser;
try {
$protect = new ProtectPage($wgParser);
$protect->mParser = new ProtectionParser($text, $protect->mEnc);
$protect->mParser->parseText(false);
$protect->encryptTags($text, $user->getName(), false);
} catch (Exceptions $e) {
$err = new ErrorHandler($protect->mAccess, $protect);
return $err->InternalError('encrypt_error', $e->getMessage());
}
return true;
}
/**
* Callback-function for hook to 'AlternateEdit'. Checks if the current user
* is allowed to edit the current article / section and if so returns the
* current editpage-object.
* @param editpage EditPage-object
* @return editpage if user is allowed to edit the section, null otherwise.
*/
function protectedEdit($editpage) {
global $wgUser, $wgParser;
try {
$protect = new ProtectPage($wgParser);
$protect->initEdit($editpage);
if (!$protect->mParser->isProtected() ) {
return $editpage;
}
if ($protect->mAccess->hasAccess($wgUser) ) {
$editpage->mArticle->mContent = $protect->decryptPage();
return true;
} else {
$err = new ErrorHandler($protect->mAccess, $wgParser);
return $err->stopEditing();
}
} catch (Exception $e) {
$err = new ErrorHandler($protect->mAccess, $wgParser);
return $err->InternalError('decrypt_error', $e->getMessage());
}
return false;
}
?>