ConfigForm.php 15 KB

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