You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

199 lines
5.6 KiB

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