1<?php
2
3/**
4 * DokuWiki Action Plugin MetaHeaders
5 *
6 *
7 * LICENSE: This file is open source software (OSS) and may be copied under
8 *          certain conditions. See COPYING file for details or try to contact
9 *          the author(s) of this file in doubt.
10 *
11 * @license GPLv2 (http://www.gnu.org/licenses/gpl2.html)
12 * @author ARSAVA <dokuwiki@dev.arsava.com>
13 * @author Michael Klier <chi@chimeric.de> (creator and previous maintainer)
14 * @link https://www.dokuwiki.org/plugin:metaheaders
15 * @link https://www.dokuwiki.org/devel:plugins
16 * @link https://www.dokuwiki.org/devel:coding_style
17 * @link https://www.dokuwiki.org/devel:environment
18 */
19
20
21//check if we are running within the DokuWiki environment
22if (!defined("DOKU_INC")){
23    die();
24}
25
26
27if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
28require_once(DOKU_PLUGIN.'action.php');
29
30
31/**
32 * All DokuWiki plugins to extend the admin function
33 * need to inherit from this class.
34 */
35class action_plugin_metaheaders extends DokuWiki_Action_Plugin {
36
37    function register(Doku_Event_Handler $controller) {
38        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaheaders');
39    }
40
41    /**
42     * Modifies the meta headers before their send to the browser.
43     *
44     * @author Michael Klier <chi@chimeric.de>
45     */
46    function metaheaders(Doku_Event $event, $param) {
47        global $ID;
48        global $INFO;
49        global $ACT;
50        global $clear;
51        global $headers;
52
53        if ($ACT != 'show' || !page_exists($ID)) return;
54
55        $head =& $event->data;
56
57        $headerconf = DOKU_CONF.'metaheaders.conf.php';
58
59        if (@file_exists($headerconf)) {
60
61            require_once($headerconf);
62
63            if (!empty($clear)) {
64	            $nclear = count($clear);
65                foreach( $head as $outerType => $list ) {
66
67                    $nlink = count($list);
68                    // process link tags
69                    for ($i = 0; $i < $nlink; $i++) {
70                        for ($y = 0; $y < $nclear; $y++) {
71                            if ( $clear[$y]['cond'] ?? false) {
72		                        if (!preg_match('/' . $clear[$y]['cond'] . '/', $ID)) {
73		                            continue;
74		                        }
75		                    }
76
77		                    $unset = true;
78		                    foreach ($clear[$y] as $type => $value) {
79		                        if ($type == 'cond') continue;
80
81		                        $headerVal = trim($head[$outerType][$i][$type] ?? '');
82		                        if ( substr($type, 0, 1) == '%' ) {
83			                        $type = substr($type, 1 );
84									$headerVal = trim($head[$outerType][$i][$type] ?? '');
85			                        if ( !preg_match(trim($value), $headerVal ) ) {
86				                        $unset = false;
87				                    }
88		                        } else
89		                        if ($headerVal != trim($value)) {
90			                        $unset = false;
91								}
92		                    }
93		                    if ($unset) {
94		                        unset($head[$outerType][$i]);
95		                    }
96		                }
97		            }
98                }
99            }
100        }
101
102        $replace = array('@AUTHOR@'       => $INFO['meta']['creator'],
103                         '@ID@'           => $INFO['id'],
104                         '@CREATED@'      => date('Y-m-d\TH:i:sO',$INFO['meta']['date']['created']),
105                         '@LASTMOD@'      => date('Y-m-d\TH:i:sO',$INFO['lastmod']),
106                         '@ABSTRACT@'     => preg_replace("/\s+/", ' ', $INFO['meta']['description']['abstract']),
107                         '@TITLE@'        => $INFO['meta']['title'],
108                         '@RELATION@'     => @implode(', ', @array_keys($INFO['meta']['relation']['references']?:[])),
109                         '@CONTRIBUTORS@' => @implode(', ', @array_values($INFO['meta']['contributor']?:[]))
110                         );
111
112        // apply new headers skip if conditions aren't met or header value is empty
113        if (!empty($headers)) {
114            $types = array_keys($headers);
115            foreach ($types as $type) {
116                foreach ($headers[$type] as $header) {
117                    $skip = false;
118
119                    if ($header['cond']) {
120                        if (preg_match('/'.$header['cond'].'/', $ID)) {
121                            unset($header['cond']);
122                        } else{
123                            $skip = true;
124                        }
125                    }
126
127                    foreach ($header as $attr => $value) {
128                        $value = str_replace(array_keys($replace), array_values($replace), $value);
129                        if (empty($value)) {
130                            $skip = true;
131                        }else{
132                            $header[$attr] = $value;
133                        }
134                    }
135
136                    if (!$skip) $head[$type][] = $header;
137
138                }
139            }
140        }
141
142        return true;
143    }
144}
145
146