ConfigForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * @todo Rewrite to use Interchange objects
  4. */
  5. class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
  6. {
  7. /**
  8. * Printers for specific fields
  9. */
  10. protected $fields = array();
  11. /**
  12. * Documentation URL, can have fragment tagged on end
  13. */
  14. protected $docURL;
  15. /**
  16. * Name of form element to stuff config in
  17. */
  18. protected $name;
  19. /**
  20. * Whether or not to compress directive names, clipping them off
  21. * after a certain amount of letters. False to disable or integer letters
  22. * before clipping.
  23. */
  24. protected $compress = false;
  25. /**
  26. * @param $name Form element name for directives to be stuffed into
  27. * @param $doc_url String documentation URL, will have fragment tagged on
  28. * @param $compress Integer max length before compressing a directive name, set to false to turn off
  29. */
  30. public function __construct(
  31. $name, $doc_url = null, $compress = false
  32. ) {
  33. parent::__construct();
  34. $this->docURL = $doc_url;
  35. $this->name = $name;
  36. $this->compress = $compress;
  37. // initialize sub-printers
  38. $this->fields[0] = new HTMLPurifier_Printer_ConfigForm_default();
  39. $this->fields[HTMLPurifier_VarParser::BOOL] = new HTMLPurifier_Printer_ConfigForm_bool();
  40. }
  41. /**
  42. * Sets default column and row size for textareas in sub-printers
  43. * @param $cols Integer columns of textarea, null to use default
  44. * @param $rows Integer rows of textarea, null to use default
  45. */
  46. public function setTextareaDimensions($cols = null, $rows = null) {
  47. if ($cols) $this->fields['default']->cols = $cols;
  48. if ($rows) $this->fields['default']->rows = $rows;
  49. }
  50. /**
  51. * Retrieves styling, in case it is not accessible by webserver
  52. */
  53. public static function getCSS() {
  54. return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
  55. }
  56. /**
  57. * Retrieves JavaScript, in case it is not accessible by webserver
  58. */
  59. public static function getJavaScript() {
  60. return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
  61. }
  62. /**
  63. * Returns HTML output for a configuration form
  64. * @param $config Configuration object of current form state, or an array
  65. * where [0] has an HTML namespace and [1] is being rendered.
  66. * @param $allowed Optional namespace(s) and directives to restrict form to.
  67. */
  68. public function render($config, $allowed = true, $render_controls = true) {
  69. if (is_array($config) && isset($config[0])) {
  70. $gen_config = $config[0];
  71. $config = $config[1];
  72. } else {
  73. $gen_config = $config;
  74. }
  75. $this->config = $config;
  76. $this->genConfig = $gen_config;
  77. $this->prepareGenerator($gen_config);
  78. $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
  79. $all = array();
  80. foreach ($allowed as $key) {
  81. list($ns, $directive) = $key;
  82. $all[$ns][$directive] = $config->get($ns .'.'. $directive);
  83. }
  84. $ret = '';
  85. $ret .= $this->start('table', array('class' => 'hp-config'));
  86. $ret .= $this->start('thead');
  87. $ret .= $this->start('tr');
  88. $ret .= $this->element('th', 'Directive', array('class' => 'hp-directive'));
  89. $ret .= $this->element('th', 'Value', array('class' => 'hp-value'));
  90. $ret .= $this->end('tr');
  91. $ret .= $this->end('thead');
  92. foreach ($all as $ns => $directives) {
  93. $ret .= $this->renderNamespace($ns, $directives);
  94. }
  95. if ($render_controls) {
  96. $ret .= $this->start('tbody');
  97. $ret .= $this->start('tr');
  98. $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
  99. $ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
  100. $ret .= '[<a href="?">Reset</a>]';
  101. $ret .= $this->end('td');
  102. $ret .= $this->end('tr');
  103. $ret .= $this->end('tbody');
  104. }
  105. $ret .= $this->end('table');
  106. return $ret;
  107. }
  108. /**
  109. * Renders a single namespace
  110. * @param $ns String namespace name
  111. * @param $directive Associative array of directives to values
  112. */
  113. protected function renderNamespace($ns, $directives) {
  114. $ret = '';
  115. $ret .= $this->start('tbody', array('class' => 'namespace'));
  116. $ret .= $this->start('tr');
  117. $ret .= $this->element('th', $ns, array('colspan' => 2));
  118. $ret .= $this->end('tr');
  119. $ret .= $this->end('tbody');
  120. $ret .= $this->start('tbody');
  121. foreach ($directives as $directive => $value) {
  122. $ret .= $this->start('tr');
  123. $ret .= $this->start('th');
  124. if ($this->docURL) {
  125. $url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
  126. $ret .= $this->start('a', array('href' => $url));
  127. }
  128. $attr = array('for' => "{$this->name}:$ns.$directive");
  129. // crop directive name if it's too long
  130. if (!$this->compress || (strlen($directive) < $this->compress)) {
  131. $directive_disp = $directive;
  132. } else {
  133. $directive_disp = substr($directive, 0, $this->compress - 2) . '...';
  134. $attr['title'] = $directive;
  135. }
  136. $ret .= $this->element(
  137. 'label',
  138. $directive_disp,
  139. // component printers must create an element with this id
  140. $attr
  141. );
  142. if ($this->docURL) $ret .= $this->end('a');
  143. $ret .= $this->end('th');
  144. $ret .= $this->start('td');
  145. $def = $this->config->def->info["$ns.$directive"];
  146. if (is_int($def)) {
  147. $allow_null = $def < 0;
  148. $type = abs($def);
  149. } else {
  150. $type = $def->type;
  151. $allow_null = isset($def->allow_null);
  152. }
  153. if (!isset($this->fields[$type])) $type = 0; // default
  154. $type_obj = $this->fields[$type];
  155. if ($allow_null) {
  156. $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
  157. }
  158. $ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
  159. $ret .= $this->end('td');
  160. $ret .= $this->end('tr');
  161. }
  162. $ret .= $this->end('tbody');
  163. return $ret;
  164. }
  165. }
  166. /**
  167. * Printer decorator for directives that accept null
  168. */
  169. class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer {
  170. /**
  171. * Printer being decorated
  172. */
  173. protected $obj;
  174. /**
  175. * @param $obj Printer to decorate
  176. */
  177. public function __construct($obj) {
  178. parent::__construct();
  179. $this->obj = $obj;
  180. }
  181. public function render($ns, $directive, $value, $name, $config) {
  182. if (is_array($config) && isset($config[0])) {
  183. $gen_config = $config[0];
  184. $config = $config[1];
  185. } else {
  186. $gen_config = $config;
  187. }
  188. $this->prepareGenerator($gen_config);
  189. $ret = '';
  190. $ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
  191. $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
  192. $ret .= $this->text(' Null/Disabled');
  193. $ret .= $this->end('label');
  194. $attr = array(
  195. 'type' => 'checkbox',
  196. 'value' => '1',
  197. 'class' => 'null-toggle',
  198. 'name' => "$name"."[Null_$ns.$directive]",
  199. 'id' => "$name:Null_$ns.$directive",
  200. 'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
  201. );
  202. if ($this->obj instanceof HTMLPurifier_Printer_ConfigForm_bool) {
  203. // modify inline javascript slightly
  204. $attr['onclick'] = "toggleWriteability('$name:Yes_$ns.$directive',checked);toggleWriteability('$name:No_$ns.$directive',checked)";
  205. }
  206. if ($value === null) $attr['checked'] = 'checked';
  207. $ret .= $this->elementEmpty('input', $attr);
  208. $ret .= $this->text(' or ');
  209. $ret .= $this->elementEmpty('br');
  210. $ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
  211. return $ret;
  212. }
  213. }
  214. /**
  215. * Swiss-army knife configuration form field printer
  216. */
  217. class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
  218. public $cols = 18;
  219. public $rows = 5;
  220. public function render($ns, $directive, $value, $name, $config) {
  221. if (is_array($config) && isset($config[0])) {
  222. $gen_config = $config[0];
  223. $config = $config[1];
  224. } else {
  225. $gen_config = $config;
  226. }
  227. $this->prepareGenerator($gen_config);
  228. // this should probably be split up a little
  229. $ret = '';
  230. $def = $config->def->info["$ns.$directive"];
  231. if (is_int($def)) {
  232. $type = abs($def);
  233. } else {
  234. $type = $def->type;
  235. }
  236. if (is_array($value)) {
  237. switch ($type) {
  238. case HTMLPurifier_VarParser::LOOKUP:
  239. $array = $value;
  240. $value = array();
  241. foreach ($array as $val => $b) {
  242. $value[] = $val;
  243. }
  244. case HTMLPurifier_VarParser::ALIST:
  245. $value = implode(PHP_EOL, $value);
  246. break;
  247. case HTMLPurifier_VarParser::HASH:
  248. $nvalue = '';
  249. foreach ($value as $i => $v) {
  250. $nvalue .= "$i:$v" . PHP_EOL;
  251. }
  252. $value = $nvalue;
  253. break;
  254. default:
  255. $value = '';
  256. }
  257. }
  258. if ($type === HTMLPurifier_VarParser::MIXED) {
  259. return 'Not supported';
  260. $value = serialize($value);
  261. }
  262. $attr = array(
  263. 'name' => "$name"."[$ns.$directive]",
  264. 'id' => "$name:$ns.$directive"
  265. );
  266. if ($value === null) $attr['disabled'] = 'disabled';
  267. if (isset($def->allowed)) {
  268. $ret .= $this->start('select', $attr);
  269. foreach ($def->allowed as $val => $b) {
  270. $attr = array();
  271. if ($value == $val) $attr['selected'] = 'selected';
  272. $ret .= $this->element('option', $val, $attr);
  273. }
  274. $ret .= $this->end('select');
  275. } elseif (
  276. $type === HTMLPurifier_VarParser::TEXT ||
  277. $type === HTMLPurifier_VarParser::ITEXT ||
  278. $type === HTMLPurifier_VarParser::ALIST ||
  279. $type === HTMLPurifier_VarParser::HASH ||
  280. $type === HTMLPurifier_VarParser::LOOKUP
  281. ) {
  282. $attr['cols'] = $this->cols;
  283. $attr['rows'] = $this->rows;
  284. $ret .= $this->start('textarea', $attr);
  285. $ret .= $this->text($value);
  286. $ret .= $this->end('textarea');
  287. } else {
  288. $attr['value'] = $value;
  289. $attr['type'] = 'text';
  290. $ret .= $this->elementEmpty('input', $attr);
  291. }
  292. return $ret;
  293. }
  294. }
  295. /**
  296. * Bool form field printer
  297. */
  298. class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
  299. public function render($ns, $directive, $value, $name, $config) {
  300. if (is_array($config) && isset($config[0])) {
  301. $gen_config = $config[0];
  302. $config = $config[1];
  303. } else {
  304. $gen_config = $config;
  305. }
  306. $this->prepareGenerator($gen_config);
  307. $ret = '';
  308. $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
  309. $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
  310. $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
  311. $ret .= $this->text(' Yes');
  312. $ret .= $this->end('label');
  313. $attr = array(
  314. 'type' => 'radio',
  315. 'name' => "$name"."[$ns.$directive]",
  316. 'id' => "$name:Yes_$ns.$directive",
  317. 'value' => '1'
  318. );
  319. if ($value === true) $attr['checked'] = 'checked';
  320. if ($value === null) $attr['disabled'] = 'disabled';
  321. $ret .= $this->elementEmpty('input', $attr);
  322. $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
  323. $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
  324. $ret .= $this->text(' No');
  325. $ret .= $this->end('label');
  326. $attr = array(
  327. 'type' => 'radio',
  328. 'name' => "$name"."[$ns.$directive]",
  329. 'id' => "$name:No_$ns.$directive",
  330. 'value' => '0'
  331. );
  332. if ($value === false) $attr['checked'] = 'checked';
  333. if ($value === null) $attr['disabled'] = 'disabled';
  334. $ret .= $this->elementEmpty('input', $attr);
  335. $ret .= $this->end('div');
  336. return $ret;
  337. }
  338. }
  339. // vim: et sw=4 sts=4