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.
 
 
 
 
 

231 lines
6.2 KiB

  1. <?php
  2. use dokuwiki\Extension\AdminPlugin;
  3. use dokuwiki\StyleUtils;
  4. /**
  5. * DokuWiki Plugin styling (Admin Component)
  6. *
  7. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  8. * @author Andreas Gohr <andi@splitbrain.org>
  9. */
  10. class admin_plugin_styling extends AdminPlugin
  11. {
  12. public $ispopup = false;
  13. /**
  14. * @return int sort number in admin menu
  15. */
  16. public function getMenuSort()
  17. {
  18. return 1000;
  19. }
  20. /**
  21. * @return bool true if only access for superuser, false is for superusers and moderators
  22. */
  23. public function forAdminOnly()
  24. {
  25. return true;
  26. }
  27. /**
  28. * handle the different actions (also called from ajax)
  29. */
  30. public function handle()
  31. {
  32. global $INPUT;
  33. $run = $INPUT->extract('run')->str('run');
  34. if (!$run) return;
  35. if (!checkSecurityToken()) return;
  36. $run = 'run' . ucfirst($run);
  37. $this->$run();
  38. }
  39. /**
  40. * Render HTML output, e.g. helpful text and a form
  41. */
  42. public function html()
  43. {
  44. $class = 'nopopup';
  45. if ($this->ispopup) $class = 'ispopup page';
  46. echo '<div id="plugin__styling" class="' . $class . '">';
  47. echo '<h1>' . $this->getLang('menu') . '</h1>';
  48. $this->form();
  49. echo '</div>';
  50. }
  51. /**
  52. * Create the actual editing form
  53. */
  54. public function form()
  55. {
  56. global $conf;
  57. global $ID;
  58. $styleUtil = new StyleUtils($conf['template'], true, true);
  59. $styleini = $styleUtil->cssStyleini();
  60. $replacements = $styleini['replacements'];
  61. if ($this->ispopup) {
  62. $target = DOKU_BASE . 'lib/plugins/styling/popup.php';
  63. } else {
  64. $target = wl($ID, ['do' => 'admin', 'page' => 'styling']);
  65. }
  66. if (empty($replacements)) {
  67. echo '<p class="error">' . $this->getLang('error') . '</p>';
  68. } else {
  69. echo $this->locale_xhtml('intro');
  70. echo '<form class="styling" method="post" action="' . $target . '">';
  71. formSecurityToken();
  72. echo '<table><tbody>';
  73. foreach ($replacements as $key => $value) {
  74. $name = tpl_getLang($key);
  75. if (empty($name)) $name = $this->getLang($key);
  76. if (empty($name)) $name = $key;
  77. echo '<tr>';
  78. echo '<td><label for="tpl__' . hsc($key) . '">' . $name . '</label></td>';
  79. echo '<td><input type="' . $this->colorType($value) . '" name="tpl[' . hsc($key) . ']" ' .
  80. 'id="tpl__' . hsc($key) . '" value="' . hsc($this->colorValue($value)) . '" ' .
  81. 'dir="ltr" required="required"/></td>';
  82. echo '</tr>';
  83. }
  84. echo '</tbody></table>';
  85. echo '<p>';
  86. echo '<button type="submit" name="run[preview]" class="btn_preview primary">' .
  87. $this->getLang('btn_preview') . '</button> ';
  88. #FIXME only if preview.ini exists:
  89. echo '<button type="submit" name="run[reset]">' . $this->getLang('btn_reset') . '</button>';
  90. echo '</p>';
  91. echo '<p>';
  92. echo '<button type="submit" name="run[save]" class="primary">' . $this->getLang('btn_save') . '</button>';
  93. echo '</p>';
  94. echo '<p>';
  95. #FIXME only if local.ini exists:
  96. echo '<button type="submit" name="run[revert]">' . $this->getLang('btn_revert') . '</button>';
  97. echo '</p>';
  98. echo '</form>';
  99. echo tpl_locale_xhtml('style');
  100. }
  101. }
  102. /**
  103. * Adjust three char color codes to the 6 char one supported by browser's color input
  104. *
  105. * @param string $value
  106. * @return string
  107. */
  108. protected function colorValue($value)
  109. {
  110. if (preg_match('/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/', $value, $match)) {
  111. return '#' . $match[1] . $match[1] . $match[2] . $match[2] . $match[3] . $match[3];
  112. }
  113. return $value;
  114. }
  115. /**
  116. * Decide the input type based on the value
  117. *
  118. * @param string $value
  119. * @return string color|text
  120. */
  121. protected function colorType($value)
  122. {
  123. if (preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $value)) {
  124. return 'color';
  125. } else {
  126. return 'text';
  127. }
  128. }
  129. /**
  130. * saves the preview.ini (alos called from ajax directly)
  131. */
  132. public function runPreview()
  133. {
  134. global $conf;
  135. $ini = $conf['cachedir'] . '/preview.ini';
  136. io_saveFile($ini, $this->makeini());
  137. }
  138. /**
  139. * deletes the preview.ini
  140. */
  141. protected function runReset()
  142. {
  143. global $conf;
  144. $ini = $conf['cachedir'] . '/preview.ini';
  145. io_saveFile($ini, '');
  146. }
  147. /**
  148. * deletes the local style.ini replacements
  149. */
  150. protected function runRevert()
  151. {
  152. $this->replaceIni('');
  153. $this->runReset();
  154. }
  155. /**
  156. * save the local style.ini replacements
  157. */
  158. protected function runSave()
  159. {
  160. $this->replaceIni($this->makeini());
  161. $this->runReset();
  162. }
  163. /**
  164. * create the replacement part of a style.ini from submitted data
  165. *
  166. * @return string
  167. */
  168. protected function makeini()
  169. {
  170. global $INPUT;
  171. $ini = "[replacements]\n";
  172. $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
  173. $ini .= ";Any values in this section will be overwritten by that tool again\n";
  174. foreach ($INPUT->arr('tpl') as $key => $val) {
  175. $ini .= $key . ' = "' . addslashes($val) . '"' . "\n";
  176. }
  177. return $ini;
  178. }
  179. /**
  180. * replaces the replacement parts in the local ini
  181. *
  182. * @param string $new the new ini contents
  183. */
  184. protected function replaceIni($new)
  185. {
  186. global $conf;
  187. $ini = DOKU_CONF . "tpl/" . $conf['template'] . "/style.ini";
  188. if (file_exists($ini)) {
  189. $old = io_readFile($ini);
  190. $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
  191. $old = trim($old);
  192. } else {
  193. $old = '';
  194. }
  195. io_makeFileDir($ini);
  196. io_saveFile($ini, "$old\n\n$new");
  197. }
  198. }
  199. // vim:ts=4:sw=4:et: