1<?php
2/**
3 * Variable Plugin: allows to insert dynamic variables
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <wikidesign@gmail.com>
7 */
8
9class syntax_plugin_var extends DokuWiki_Syntax_Plugin {
10    protected $aliasSeps = '._-';
11
12    /// Added in 2015-06 release to implement
13    static $wikiVERSION;
14
15    function getType() { return 'substition'; }
16    function getSort() { return 99; }
17    function connectTo($mode) { $this->Lexer->addSpecialPattern('@\w{2,6}['.$this->aliasSeps.']?@', $mode, 'plugin_var'); }
18
19    function handle($match, $state, $pos, Doku_Handler $handler) {
20        $match = substr($match, 1, -1); // strip markup
21        return array($match);
22    }
23
24    function wikiLink($id, $namespace = false)
25    {
26        if ($namespace)
27            $id = getNS($id);
28        $link = wl($id, '', true);
29        if ($namespace)
30            $link .= '/';
31        return $link;
32    }
33
34    function render($mode, Doku_Renderer $renderer, $data) {
35        global $ID;
36        global $INFO;
37        global $conf;
38
39        $meta = $data[0];
40        $length = strlen($meta);
41        $aliasSep = '.';
42        $part = substr($meta, 0, -1);
43        if ($part == 'ALIAS' && strpos ($this->aliasSeps, $meta[$length-1]) !== false) {
44            $aliasSep = $meta[$length-1];
45            $meta = $part;
46        }
47        $metadata = $mode == 'metadata';
48        $nocache = false;
49        switch ($meta) {
50            case 'ID':
51                $xhtml = $ID;
52                $meta  = $xhtml;
53                break;
54            case 'NS':
55                $xhtml = getNS($ID);
56                $meta  = $xhtml;
57                break;
58            case 'NSL':
59                $meta  = $this->wikiLink($ID, true);
60                $nocache = true;
61                if (!$metadata)
62                    $renderer->externallink($meta);
63                break;
64            case 'PAGE':
65                $xhtml = strtr(noNS($ID),'_',' ');
66                $meta  = $xhtml;
67                break;
68            case 'PAGEL':
69                $meta  = $this->wikiLink($ID);
70                $nocache = true;
71                if (!$metadata)
72                    $renderer->externallink($meta);
73                break;
74            case 'USER':
75                $xhtml   = $_SERVER['REMOTE_USER'];
76                $nocache = true;
77                break;
78            case 'ALIAS':
79                $xhtml   = ($_SERVER['REMOTE_USER'] ? str_replace(' ', $aliasSep, $INFO['userinfo']['name']) : $_SERVER['REMOTE_USER']);
80                $nocache = true;
81                break;
82            case 'NAME':
83                $xhtml   = ($_SERVER['REMOTE_USER'] ? $INFO['userinfo']['name'] : clientIP());
84                $nocache = true;
85                break;
86            case 'MAIL':
87                $xhtml   = ($_SERVER['REMOTE_USER'] ? $INFO['userinfo']['mail'] : '');
88                $nocache = true;
89                break;
90            case 'DATE':
91                $xhtml   = strftime($conf['dformat']);
92                $nocache = true;
93                break;
94            case 'YEAR':
95                $xhtml = date('Y');
96                break;
97            case 'MONTH':
98                $xhtml = date('m');
99                $nocache = true;
100                break;
101            case 'SMONTH':
102                $xhtml = date('n');
103                $nocache = true;
104                break;
105            case 'DAY':
106                $xhtml   = date('d');
107                $nocache = true;
108                break;
109            case 'SDAY':
110                $xhtml   = date('j');
111                $nocache = true;
112                break;
113            case 'WIKI':
114                $xhtml = $conf['title'];
115                break;
116            case 'TITLE':
117                $xhtml = ($INFO['meta']['title']) ? $INFO['meta']['title'] : $meta;
118                $nocache = true;
119                break;
120            case 'SERVER':
121                $xhtml = ($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $meta;
122                $nocache = true;
123                break;
124            case 'VER':
125                $xhtml = self::$wikiVERSION;
126                break;
127            case 'VERR':
128                list($vrel,$vdate,$vname) = explode(' ', self::$wikiVERSION);
129                $xhtml = trim($vdate);
130                break;
131            case 'VERN':
132                list($vrel,$vdate,$vname) = explode(' ', self::$wikiVERSION);
133                $xhtml = trim(substr($vname, 1, -1));
134                break;
135            default:
136                // for unknown match render original
137                $xhtml = "@{$meta}@";
138                break;
139        }
140
141        if ($metadata) {
142            $renderer->cdata($meta);
143        } else {
144            if ($xhtml != null) {
145                $renderer->cdata($xhtml);
146            }
147            if ($nocache) {
148                $renderer->nocache();
149            }
150        }
151
152        return true;
153    }
154}
155syntax_plugin_var::$wikiVERSION = getVersion();
156
157// vim:ts=4:sw=4:et:enc=utf-8:
158