forked from WolfgangFahl/PDFEmbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDFEmbed.hooks.php
306 lines (268 loc) · 10.3 KB
/
PDFEmbed.hooks.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
<?php
/**
* PDFEmbed
* PDFEmbed Hooks
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link https://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
use MediaWiki\MediaWikiServices;
class PDFEmbed
{
/**
* Sets up this extensions parser functions.
*
* @access public
* @param
* object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit(Parser &$parser): bool
{
$parser->setHook('pdf', 'PDFEmbed::generateTag');
return true;
}
/**
* disable the cache
*
* @param Parser $parser
*/
static public function disableCache(Parser &$parser)
{
// see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/MagicNoCache/+/refs/heads/master/src/MagicNoCacheHooks.php
global $wgOut;
$parser->getOutput()->updateCacheExpiry(0);
if (method_exists($wgOut, 'disableClientCache')) {
$wgOut->disableClientCache();
} else {
$wgOut->enableClientCache(false);
}
}
/**
* remove the File: prefix depending on the language or in english default form
*
* @param
* filename - the filename for which to fix the prefix
* @return string - the filename without the File: / Media: or i18n File/Media prefix
*/
static public function removeFilePrefix($filename): string
{
$mwServices = MediaWikiServices::getInstance();
if (method_exists($mwServices, "getContentLanguage")) {
$contentLang = $mwServices->getContentLanguage();
# there are four possible prefixes: 'File' and 'Media' in English and in the wiki's language
$ns_media_wiki_lang = $contentLang->getFormattedNsText(NS_MEDIA);
$ns_file_wiki_lang = $contentLang->getFormattedNsText(NS_FILE);
if (method_exists($mwServices, "getLanguageFactory")) {
$langFactory = $mwServices->getLanguageFactory();
$lang = $langFactory->getLanguage('en');
$ns_media_lang_en = $lang->getFormattedNsText(NS_MEDIA);
$ns_file_lang_en = $lang->getFormattedNsText(NS_FILE);
$filename = preg_replace("/^($ns_media_wiki_lang|$ns_file_wiki_lang|$ns_media_lang_en|$ns_file_lang_en):/", '', $filename);
} else {
$filename = preg_replace("/^($ns_media_wiki_lang|$ns_file_wiki_lang):/", '', $filename);
}
}
return $filename;
}
/**
* Generates the PDF object tag.
*
* @access public
* @param
* string Namespace prefixed article of the PDF file to display.
* @param
* array Arguments on the tag.
* @param
* object Parser object.
* @param
* object PPFrame object.
* @return string HTML
*/
static public function generateTag($obj, $args = [], ?Parser $parser, ?PPFrame $frame): string
{
global $wgPdfEmbed, $wgRequest, $wgPDF;
// disable the cache
PDFEmbed::disableCache($parser);
// grab the uri by parsing to html
$html = $parser->recursiveTagParse($obj, $frame);
// check the action which triggered us
$requestAction = $wgRequest->getVal('action');
if ($requestAction === null) {
// https://www.mediawiki.org/wiki/Manual:UserFactory.php
$revUserName = $parser->getRevisionUser();
if (empty($revUserName)) {
return self::error('embed_pdf_invalid_user');
}
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
$user = $userFactory->newFromName($revUserName);
}
// depending on the action get the responsible user
if ($requestAction === 'edit' || $requestAction === 'submit') {
$user = RequestContext::getMain()->getUser();
}
if (!($user instanceof User &&
MediaWikiServices::getInstance()->getPermissionManager()->userHasRight($user, 'embed_pdf')
)) {
$parser->addTrackingCategory("pdfembed-permission-problem-category");
return self::error('embed_pdf_no_permission', wfMessage('right-embed_pdf'));
}
// we don't want the html but just the href of the link
// so we might reverse some of the parsing again by examining the html
// whether it contains an anchor <a href= ...
if (strpos($html, '<a') !== false) {
$anchor = new SimpleXMLElement($html);
// is there a href element?
if (isset($anchor['href'])) {
// that's what we want ...
$html = $anchor['href'];
}
}
if (array_key_exists('width', $args)) {
$widthStr = $parser->recursiveTagParse($args['width'], $frame);
} else {
$widthStr = $wgPdfEmbed['width'];
}
if (array_key_exists('height', $args)) {
$heightStr = $parser->recursiveTagParse($args['height'], $frame);
} else {
$heightStr = $wgPdfEmbed['height'];
}
if (array_key_exists('page', $args)) {
$page = intval($parser->recursiveTagParse($args['page'], $frame));
} else {
$page = 1;
}
if (!preg_match('~^\d+~', $widthStr)) {
return self::error("embed_pdf_invalid_width", $widthStr);
} elseif (!preg_match('~^\d+~', $heightStr)) {
return self::error("embed_pdf_invalid_height", $heightStr);
}
$width = intVal($widthStr);
$height = intVal($heightStr);
if (array_key_exists('iframe', $args)) {
$iframe = $parser->recursiveTagParse($args['iframe'], $frame);
} else {
$iframe = $wgPdfEmbed['iframe'];
}
# if there are no slashes in the name we assume this
# might be a pointer to a file
if (preg_match('~^([^\/]+\.pdf)(#[0-9]+)?$~', $html, $matches)) {
# re contains the groups
$filename = $matches[1];
if (count($matches) == 3) {
$page = $matches[2];
}
$filename = self::removeFilePrefix($filename);
$pdfFile = MediaWikiServices::getInstance()->getRepoGroup()->findFile($filename);
if ($pdfFile !== false) {
$url = $pdfFile->getFullUrl();
return self::embed($url, $width, $height, $page, $iframe);
} else {
return self::error('embed_pdf_invalid_file', $filename);
}
} else {
// parse the given url
$domain = parse_url($html);
// check that the parsing worked and retrieve a valid host
// no relative urls are allowed ...
if ($domain === false || (!isset($domain['host']))) {
if (!isset($domain['host'])) {
return self::error("embed_pdf_invalid_relative_domain", $html);
}
return self::error("embed_pdf_invalid_url", $html);
}
if (isset($wgPDF)) {
foreach ($wgPDF['black'] as $x => $y) {
$wgPDF['black'][$x] = strtolower($y);
}
foreach ($wgPDF['white'] as $x => $y) {
$wgPDF['white'][$x] = strtolower($y);
}
$host = strtolower($domain['host']);
$whitelisted = false;
if (in_array($host, $wgPDF['white'])) {
$whitelisted = true;
}
if ($wgPDF['white'] != array() && !$whitelisted) {
return self::error("embed_pdf_domain_not_white", $host);
}
if (!$whitelisted) {
if (in_array($host, $wgPDF['black'])) {
return self::error("embed_pdf_domain_black", $host);
}
}
}
# check that url is valid
if (filter_var($html, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
return self::embed($html, $width, $height, $page, $iframe);
} else {
return self::error('embed_pdf_invalid_url', $html);
}
}
}
/**
* Returns an HTML node for the given file as string.
*
* @access private
* @param
* URL url to embed.
* @param
* integer width of the iframe.
* @param
* integer height of the iframe.
* @param
* integer page of the pdf file.
* @param
* boolean iframe - True if an iframe should be returned else an object is returned
* @return string HTML code for iframe.
*/
static private function embed($url, $width, $height, $page, $iframe): string
{
# secure and concatenate the url
$pdfSafeUrl = htmlentities($url) . '#page=' . $page;
# check the embed mode and return a proper HTML element
if ($iframe) {
return Html::rawElement('iframe', [
'class' => 'pdf-embed',
'width' => $width,
'height' => $height,
'src' => $pdfSafeUrl,
'style' => 'max-width: 100%;'
]);
} else {
# object mode (default)
return Html::rawElement('object', [
'class' => 'pdf-embed',
'width' => $width,
'height' => $height,
'data' => $pdfSafeUrl,
'style' => 'max-width: 100%;',
'type' => 'application/pdf'
], Html::rawElement(
'a',
[
'href' => $pdfSafeUrl
],
'load PDF' // i18n?
));
}
}
/**
* Returns a standard error message.
*
* @access private
* @param
* string Error message key to display.
* @param
* params any parameters for the error message
* @return string HTML error message.
*/
static private function error($messageKey, ...$params): string
{
return Xml::span(wfMessage($messageKey, $params)->plain(), 'error');
}
}