-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatMicrosoftProtectedLinks.module.php
More file actions
73 lines (60 loc) · 2.07 KB
/
FormatMicrosoftProtectedLinks.module.php
File metadata and controls
73 lines (60 loc) · 2.07 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
<?php namespace ProcessWire;
/**
* FormatMicrosoftProtectedLinks module for ProcessWire
*
* Hooks page save to find and replaces Microsoft protected links from
* Outlook/Teams/Office with their original links
*
* @author Australian Antarctic Division
* @copyright 2026 Commonwealth of Australia
*/
use DOMDocument;
libxml_use_internal_errors(true);
class FormatMicrosoftProtectedLinks extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return [
'title' => 'Replace Outlook Protected Links',
'version' => '100',
'summary' => 'Replace protected links from Outlook/Teams/Microsoft Office with the original link.',
'author' => 'Australian Antarctic Division',
'icon' => 'link',
'autoload' => true
];
}
public function ready() {
$this->addHookBefore('Pages::save', $this, 'formatLinks');
}
public function formatLinks(HookEvent $event) {
$page = $event->arguments(0);
// get fields as set in module configuration
$fieldsToCheck = $this->formatFields;
// for each set field, run format links and save value back to page
foreach ($fieldsToCheck as $toCheck) {
$field = $page->getField($toCheck);
$str = $page->get("$field");
if (empty($str)) {
continue;
}
$dom = new DOMDocument();
if ($dom->loadHTML('<?xml encoding="utf-8" ?>' . $str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD) === false) {
continue; // error loading markup from text/textarea field, exit here
}
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$parsedURL = parse_url($anchor->getAttribute('href'));
if ($parsedURL === false) {
continue; // malformed URL, skip
}
if (preg_match("/safelinks\.(protection\.outlook|office)\.com$/", $parsedURL['host'])) {
parse_str($parsedURL['query'], $parsedQuery);
if (($href = $parsedQuery['url'] ?? '') !== '') {
$anchor->setAttribute('href', $href);
}
}
}
$str = str_replace('<?xml encoding="utf-8" ?>', '', $dom->saveHtml());
$page->$field = $str;
}
$event->arguments(0, $page);
}
}