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         = new cache($cache_key, '.js');
82$cache->_event = 'ICONIFY_CACHE';
83$cache_files   = $params;
84$cache_files[] = __FILE__;
85$cache_ok      = $cache->useCache(array('files' => $cache_files));
86$content_type  = 'application/javascript; charset=utf-8';
87
88if ($params['icon']) {
89    $content_type                             = 'image/svg+xml; charset=utf-8';
90    list($params['prefix'], $params['icons']) = explode('-', str_replace('.svg', '', $params['icon']), 2);
91}
92
93header("Content-Type: $content_type");
94
95http_cached($cache->cache, $cache_ok);
96
97$collection_file = "$iconify_dir/" . $params['prefix'] . ".json";
98
99if (!file_exists($collection_file)) {
100    header('Content-Type: text/plain; charset=utf-8', true);
101    http_status(404);
102    print "Not Found";
103    exit;
104}
105
106$collection_data = json_decode(io_readFile($collection_file), true);
107
108$iconify_data = array(
109    'prefix'  => $params['prefix'],
110    'icons'   => array(),
111    'aliases' => array(),
112);
113
114foreach (explode(',', $params['icons']) as $icon) {
115
116    if (isset($collection_data['aliases'][$icon])) {
117        $iconify_data['aliases'][$icon] = $collection_data['aliases'][$icon];
118        $icon                           = $collection_data['aliases'][$icon]['parent'];
119    }
120
121    if (!$icon_data = $collection_data['icons'][$icon]) {
122        continue;
123    }
124
125    $iconify_data['icons'][$icon] = $icon_data;
126
127    if ($params['width']) {
128        $iconify_data['icons'][$icon]['width'] = $params['width'];
129    }
130    if ($params['height']) {
131        $iconify_data['icons'][$icon]['height'] = $params['height'];
132    }
133
134}
135
136foreach (array('width', 'height', 'top', 'left', 'inlineHeight', 'inlineTop', 'verticalAlign') as $property) {
137    if (isset($collection_data[$property])) {
138        $iconify_data[$property] = $collection_data[$property];
139    }
140}
141
142if ($params['callback']) {
143
144    $content = $params['callback'] . "(" . json_encode($iconify_data) . ");";
145
146} elseif ($params['icon']) {
147
148    $icon   = $params['icons'];
149    $width  = '1em';
150    $height = '1em';
151    $fill   = '';
152
153    if (isset($iconify_data['aliases'][$icon])) {
154        $icon = $iconify_data['aliases'][$icon]['parent'];
155    }
156
157    if (!isset($iconify_data['icons'][$icon])) {
158        header('Content-Type: text/plain; charset=utf-8', true);
159        http_status(404);
160        print "Not Found";
161        exit;
162    }
163
164    if ($params['width']) {
165        $width  = $params['width'];
166        $height = $params['width'];
167    }
168
169    if ($params['height']) {
170        $width  = $params['height'];
171        $height = $params['height'];
172    }
173
174    if ($params['color']) {
175        $fill = $params['color'];
176    }
177
178    # TODO add "rotate" support
179
180    $body = $iconify_data['icons'][$icon]['body'];
181
182    if ($fill) {
183        $body = str_replace('currentColor', $fill, $body);
184    }
185
186    $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>';
187    $content = sprintf($svg, $width, $height, $body);
188
189} else {
190
191    $content = "SimpleSVG._loaderCallback(" . json_encode($iconify_data) . ");";
192
193}
194
195http_cached_finish($cache->cache, $content);
196#print $content;
197