forked from arsava/dokuwiki-plugin-metaheaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
145 lines (119 loc) · 5 KB
/
action.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
<?php
/**
* DokuWiki Action Plugin MetaHeaders
*
*
* LICENSE: This file is open source software (OSS) and may be copied under
* certain conditions. See COPYING file for details or try to contact
* the author(s) of this file in doubt.
*
* @license GPLv2 (http://www.gnu.org/licenses/gpl2.html)
* @author ARSAVA <[email protected]>
* @author Michael Klier <[email protected]> (creator and previous maintainer)
* @link https://www.dokuwiki.org/plugin:metaheaders
* @link https://www.dokuwiki.org/devel:plugins
* @link https://www.dokuwiki.org/devel:coding_style
* @link https://www.dokuwiki.org/devel:environment
*/
//check if we are running within the DokuWiki environment
if (!defined("DOKU_INC")){
die();
}
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class.
*/
class action_plugin_metaheaders extends DokuWiki_Action_Plugin {
function register(Doku_Event_Handler $controller) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaheaders');
}
/**
* Modifies the meta headers before their send to the browser.
*
* @author Michael Klier <[email protected]>
*/
function metaheaders(Doku_Event $event, $param) {
global $ID;
global $INFO;
global $ACT;
global $clear;
global $headers;
if ($ACT != 'show' || !page_exists($ID)) return;
$head =& $event->data;
$headerconf = DOKU_CONF.'metaheaders.conf.php';
if (@file_exists($headerconf)) {
require_once($headerconf);
if (!empty($clear)) {
$nclear = count($clear);
foreach( $head as $outerType => $list ) {
$nlink = count($list);
// process link tags
for ($i = 0; $i < $nlink; $i++) {
for ($y = 0; $y < $nclear; $y++) {
if ( $clear[$y]['cond'] ?? false) {
if (!preg_match('/' . $clear[$y]['cond'] . '/', $ID)) {
continue;
}
}
$unset = true;
foreach ($clear[$y] as $type => $value) {
if ($type == 'cond') continue;
$headerVal = trim($head[$outerType][$i][$type] ?? '');
if ( substr($type, 0, 1) == '%' ) {
$type = substr($type, 1 );
$headerVal = trim($head[$outerType][$i][$type] ?? '');
if ( !preg_match(trim($value), $headerVal ) ) {
$unset = false;
}
} else
if ($headerVal != trim($value)) {
$unset = false;
}
}
if ($unset) {
unset($head[$outerType][$i]);
}
}
}
}
}
}
$replace = array('@AUTHOR@' => $INFO['meta']['creator'],
'@ID@' => $INFO['id'],
'@CREATED@' => date('Y-m-d\TH:i:sO',$INFO['meta']['date']['created']),
'@LASTMOD@' => date('Y-m-d\TH:i:sO',$INFO['lastmod']),
'@ABSTRACT@' => preg_replace("/\s+/", ' ', $INFO['meta']['description']['abstract']),
'@TITLE@' => $INFO['meta']['title'],
'@RELATION@' => @implode(', ', @array_keys($INFO['meta']['relation']['references']?:[])),
'@CONTRIBUTORS@' => @implode(', ', @array_values($INFO['meta']['contributor']?:[]))
);
// apply new headers skip if conditions aren't met or header value is empty
if (!empty($headers)) {
$types = array_keys($headers);
foreach ($types as $type) {
foreach ($headers[$type] as $header) {
$skip = false;
if ($header['cond']) {
if (preg_match('/'.$header['cond'].'/', $ID)) {
unset($header['cond']);
} else{
$skip = true;
}
}
foreach ($header as $attr => $value) {
$value = str_replace(array_keys($replace), array_values($replace), $value);
if (empty($value)) {
$skip = true;
}else{
$header[$attr] = $value;
}
}
if (!$skip) $head[$type][] = $header;
}
}
}
return true;
}
}