admin.php 6.1 KB

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