ConfigForm.php 14 KB

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