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(&$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(&$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            $nclear = count($clear);
63
64            if (!empty($clear)) {
65
66                foreach( $head as $outerType => $list ) {
67
68                    $nlink = count($list);
69                    // process link tags
70                    for ($i = 0; $i < $nlink; $i++) {
71                        for ($y = 0; $y < $nclear; $y++) {
72                            if ($clear[$y]['cond']) {
73	                        if (!preg_match('/' . $clear[$y]['cond'] . '/', $ID)) {
74	                            continue;
75	                        }
76	                    }
77
78	                    $unset = true;
79	                    foreach ($clear[$y] as $type => $value) {
80	                        if ($type == 'cond') continue;
81	                        if (trim($head[$outerType][$i][$type]) != trim($value)) $unset = false;
82	                    }
83	                    if ($unset) {
84	                        unset($head[$outerType][$i]);
85	                    }
86	                }
87	            }
88                }
89            }
90        }
91
92        $replace = array('@AUTHOR@'       => $INFO['meta']['creator'],
93                         '@ID@'           => $INFO['id'],
94                         '@CREATED@'      => date('Y-m-d\TH:i:sO',$INFO['meta']['date']['created']),
95                         '@LASTMOD@'      => date('Y-m-d\TH:i:sO',$INFO['lastmod']),
96                         '@ABSTRACT@'     => preg_replace("/\s+/", ' ', $INFO['meta']['description']['abstract']),
97                         '@TITLE@'        => $INFO['meta']['title'],
98                         '@RELATION@'     => @implode(', ', @array_keys($INFO['meta']['relation']['references'])),
99                         '@CONTRIBUTORS@' => @implode(', ', @array_values($INFO['meta']['contributor']))
100                         );
101
102        // apply new headers skip if conditions aren't met or header value is empty
103        if (!empty($headers)) {
104            $types = array_keys($headers);
105            foreach ($types as $type) {
106                foreach ($headers[$type] as $header) {
107                    $skip = false;
108
109                    if ($header['cond']) {
110                        if (preg_match('/'.$header['cond'].'/', $ID)) {
111                            unset($header['cond']);
112                        } else{
113                            $skip = true;
114                        }
115                    }
116
117                    foreach ($header as $attr => $value) {
118                        $value = str_replace(array_keys($replace), array_values($replace), $value);
119                        if (empty($value)) {
120                            $skip = true;
121                        }else{
122                            $header[$attr] = $value;
123                        }
124                    }
125
126                    if (!$skip) $head[$type][] = $header;
127
128                }
129            }
130        }
131
132        return true;
133    }
134}
135
136