-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSO365Embed.hooks.php
259 lines (227 loc) · 9.05 KB
/
MSO365Embed.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
<?php
/**
* MSO365Embed
* MSO365Embed Hooks
*
* @author Spas Z. Spasov (source of the idea: https://github.com/WolfgangFahl/PDFEmbed)
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package MSO365Embed
* @link https://github.com/metalevel-tech/mw-MSO365Embed
*
*/
use MediaWiki\MediaWikiServices;
class MSO365Embed
{
/**
*
* 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)
{
$parser->setHook('MSO365', 'MSO365Embed::generateTag');
return true;
}
/**
* disable the cache, (probably we don't need it, because the file is cached by office.live for few hours)
*
* @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)
{
$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 MSO365/mso365 object tag.
*
* @access public
*
* @param
* string Namespace prefixed article of the MSO365 file to display.
* @param
* array Arguments on the tag.
* @param
* object Parser object.
* @param
* object PPFrame object.
*
* @return string HTML
*/
static public function generateTag($input, array $args, Parser $parser, PPFrame $frame)
{
global $wgMSO365Embed, $wgRequest;
// disable the cache
MSO365Embed::disableCache($parser);
// grab the uri by parsing to html
$html = $parser->recursiveTagParse($input);
// check the action which triggered us
$requestAction = $wgRequest->getVal('action');
// depending on the action get the responsible user
if ($requestAction == 'edit' || $requestAction == 'submit') {
$user = RequestContext::getMain()->getUser();
} else {
// https://www.mediawiki.org/wiki/Manual:UserFactory.php
$revUserName = $parser->getRevisionUser();
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
$user = $userFactory->newFromName($revUserName);
}
if ($user === false) {
return self::error('embed_mso365_invalid_user');
}
if (!MediaWikiServices::getInstance()->getPermissionManager()->userHasRight($user, 'embed_MSO365')) {
return self::error('embed_mso365_no_permission');
}
// 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'];
}
}
// Handle the arguments of the current <mso365> tag
(isset($args['style'])) ? $style = $args['style'] : $style = $wgMSO365Embed['style'];
(isset($args['width'])) ? $width = $args['width'] : $width = $wgMSO365Embed['width'];
(isset($args['height'])) ? $height = $args['height'] : $height = $wgMSO365Embed['height'];
(isset($args['action'])) ? $action = $args['action'] : $action = $wgMSO365Embed['action'];
(isset($args['iframe'])) ? $iframe = $args['iframe'] : $iframe = $wgMSO365Embed['iframe'];
// if there are no slashes in the name we assume this might be a pointer to a file
if (preg_match('~^([^\/]+\.(docx|docm|xlsx|xlsm|pptm|pptx|ppsx|ppsm))(#[0-9]+)?$~', $html, $matches)) {
// re contains the groups
$filename = $matches[1];
$filename = self::removeFilePrefix($filename);
$MSO365File = MediaWikiServices::getInstance()->getRepoGroup()->findFile($filename);
if ($MSO365File !== false) {
$url = $MSO365File->getFullUrl();
return self::embed($url, $width, $height, $style, $action, $iframe);
} else {
return self::error('embed_mso365_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_mso365_invalid_relative_domain: ", $html);
}
return self::error("embed_mso365_invalid_url", $html);
}
// check that url is valid
if (filter_var($html, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
return self::embed($html, $width, $height, $style, $action, $iframe);
} else {
return self::error('embed_mso365_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 style of the div container.
*
* @return string HTML code for iframe.
*/
static private function embed($url, $width, $height, $style, $action, $iframe)
{
$divStyle = 'position: relative; overflow: hidden; width: ' . $width . '; height: ' . $height . '; ' . $style;
$iframeStyle = 'position: absolute; left: -.2%; top: -.2%;';
// compose url
$FileUrl = htmlentities($url);
$ViewOfficeLiveUrl = 'https://view.officeapps.live.com/op/' . $action . '.aspx?src=';
$MSO365SafeUrl = $ViewOfficeLiveUrl . $FileUrl;
// return a proper HTML element
if ($iframe) {
$output = Html::rawElement('iframe', [
'class' => 'mso365-embed',
'width' => '100.4%',
'height' => '100.4%',
'src' => $MSO365SafeUrl,
'frameborder' => '0',
'style' => $iframeStyle
]);
} else {
$output = Html::rawElement('object', [
'class' => 'mso365-embed',
'width' => '100.4%',
'height' => '100.4%',
'data' => $MSO365SafeUrl,
'type' => 'application/xml',
'frameborder' => '0',
'style' => $iframeStyle
]);
}
return Html::rawElement('div', [
'class' => 'mso365-div',
'width' => $width,
'height' => $height,
'style' => $divStyle
], $output);
}
/**
* 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)
{
return Xml::span(wfMessage($messageKey, $params)->plain(), 'error');
}
}