1<?php
2
3/**
4 * DokuWiki Bootstrap3 Template: Iconify compatible API
5 *
6 * @link     http://dokuwiki.org/template:bootstrap3
7 * @author   Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 */
10
11# NOTE Some Linux distributon change the location of DokuWiki core libraries (DOKU_INC)
12#
13#      Bitnami (Docker)         /opt/bitnami/dokuwiki
14#      LinuxServer.io (Docker)  /app/dokuwiki
15#      Arch Linux               /usr/share/webapps/dokuwiki
16#      Debian/Ubuntu            /usr/share/dokuwiki
17#
18# NOTE If DokuWiki core libraries (DOKU_INC) is in another location you can
19#      create a PHP file in bootstrap3 root directory called "doku_inc.php" with
20#      this content:
21#
22#           <?php define('DOKU_INC', '/path/dokuwiki/');
23#
24#      (!) This file will be deleted on every upgrade of template
25
26
27$doku_inc_dirs = array(
28    '/opt/bitnami/dokuwiki',                      # Bitnami (Docker)
29    '/usr/share/webapps/dokuwiki',                # Arch Linux
30    '/usr/share/dokuwiki',                        # Debian/Ubuntu
31    '/app/dokuwiki',                              # LinuxServer.io (Docker),
32    realpath(dirname(__FILE__) . '/../../../'),   # Default DokuWiki path
33);
34
35# Load doku_inc.php file
36#
37if (file_exists(dirname(__FILE__) . '/doku_inc.php')) {
38    require_once dirname(__FILE__) . '/doku_inc.php';
39}
40
41if (! defined('DOKU_INC')) {
42    foreach ($doku_inc_dirs as $dir) {
43        if (! defined('DOKU_INC') && @file_exists("$dir/inc/init.php")) {
44            define('DOKU_INC', "$dir/");
45        }
46    }
47}
48
49// we do not use a session or authentication here (better caching)
50
51if (!defined('NOSESSION')) {
52    define('NOSESSION', true);
53}
54
55if (!defined('NL')) {
56    define('NL', "\n");
57}
58
59// we gzip ourself here
60
61if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) {
62    define('DOKU_DISABLE_GZIP_OUTPUT', 1);
63}
64
65require_once DOKU_INC . 'inc/init.php';
66
67global $INPUT;
68
69$params = array(
70    'prefix'   => hsc($INPUT->str('prefix')),
71    'icons'    => hsc($INPUT->str('icons')),
72    'callback' => hsc($INPUT->str('callback')),
73    'width'    => hsc($INPUT->str('width')),
74    'height'   => hsc($INPUT->str('height')),
75    'icon'     => hsc($INPUT->str('icon')),
76    'color'    => hsc($INPUT->str('color')),
77);
78
79$iconify_dir   = dirname(__FILE__) . '/assets/iconify/json';
80$cache_key     = md5(serialize($params) . $conf['template'] . filemtime(__FILE__));
81$cache_files   = $params;
82$cache_files[] = __FILE__;
83$content_type  = 'application/javascript; charset=utf-8';
84
85$cache = new dokuwiki\Cache\Cache($cache_key, '.js');
86$cache->setEvent('ICONIFY_CACHE');
87$cache_ok = $cache->useCache(array('files' => $cache_files));
88
89
90if ($params['icon']) {
91    $content_type                             = 'image/svg+xml; charset=utf-8';
92    list($params['prefix'], $params['icons']) = explode('-', str_replace('.svg', '', $params['icon']), 2);
93}
94
95header("Content-Type: $content_type");
96
97http_cached($cache->cache, $cache_ok);
98
99$collection_file = "$iconify_dir/" . $params['prefix'] . ".json";
100
101if (!file_exists($collection_file)) {
102    header('Content-Type: text/plain; charset=utf-8', true);
103    http_status(404);
104    print "Not Found";
105    exit;
106}
107
108$collection_data = json_decode(io_readFile($collection_file), true);
109
110$iconify_data = array(
111    'prefix'  => $params['prefix'],
112    'icons'   => array(),
113    'aliases' => array(),
114);
115
116foreach (explode(',', $params['icons']) as $icon) {
117
118    if (isset($collection_data['aliases'][$icon])) {
119        $iconify_data['aliases'][$icon] = $collection_data['aliases'][$icon];
120        $icon                           = $collection_data['aliases'][$icon]['parent'];
121    }
122
123    if (!$icon_data = $collection_data['icons'][$icon]) {
124        continue;
125    }
126
127    $iconify_data['icons'][$icon] = $icon_data;
128
129    if ($params['width']) {
130        $iconify_data['icons'][$icon]['width'] = $params['width'];
131    }
132    if ($params['height']) {
133        $iconify_data['icons'][$icon]['height'] = $params['height'];
134    }
135
136}
137
138foreach (array('width', 'height', 'top', 'left', 'inlineHeight', 'inlineTop', 'verticalAlign') as $property) {
139    if (isset($collection_data[$property])) {
140        $iconify_data[$property] = $collection_data[$property];
141    }
142}
143
144if ($params['callback']) {
145
146    $content = $params['callback'] . "(" . json_encode($iconify_data) . ");";
147
148} elseif ($params['icon']) {
149
150    $icon   = $params['icons'];
151    $width  = '1em';
152    $height = '1em';
153    $fill   = '';
154
155    if (isset($iconify_data['aliases'][$icon])) {
156        $icon = $iconify_data['aliases'][$icon]['parent'];
157    }
158
159    if (!isset($iconify_data['icons'][$icon])) {
160        header('Content-Type: text/plain; charset=utf-8', true);
161        http_status(404);
162        print "Not Found";
163        exit;
164    }
165
166    if ($params['width']) {
167        $width  = $params['width'];
168        $height = $params['width'];
169    }
170
171    if ($params['height']) {
172        $width  = $params['height'];
173        $height = $params['height'];
174    }
175
176    if ($params['color']) {
177        $fill = $params['color'];
178    }
179
180    # TODO add "rotate" support
181
182    $body = $iconify_data['icons'][$icon]['body'];
183
184    if ($fill) {
185        $body = str_replace('currentColor', $fill, $body);
186    }
187
188    $svg     = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%s" height="%s" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);">%s</svg>';
189    $content = sprintf($svg, $width, $height, $body);
190
191} else {
192
193    $content = "SimpleSVG._loaderCallback(" . json_encode($iconify_data) . ");";
194
195}
196
197http_cached_finish($cache->cache, $content);
198#print $content;
199