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.

452 satır
14KB

  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. if (is_array($v)) {
  306. // HACK
  307. $v = implode(";", $v);
  308. }
  309. $nvalue .= "$i:$v" . PHP_EOL;
  310. }
  311. $value = $nvalue;
  312. break;
  313. default:
  314. $value = '';
  315. }
  316. }
  317. if ($type === HTMLPurifier_VarParser::MIXED) {
  318. return 'Not supported';
  319. $value = serialize($value);
  320. }
  321. $attr = array(
  322. 'name' => "$name" . "[$ns.$directive]",
  323. 'id' => "$name:$ns.$directive"
  324. );
  325. if ($value === null) {
  326. $attr['disabled'] = 'disabled';
  327. }
  328. if (isset($def->allowed)) {
  329. $ret .= $this->start('select', $attr);
  330. foreach ($def->allowed as $val => $b) {
  331. $attr = array();
  332. if ($value == $val) {
  333. $attr['selected'] = 'selected';
  334. }
  335. $ret .= $this->element('option', $val, $attr);
  336. }
  337. $ret .= $this->end('select');
  338. } elseif ($type === HTMLPurifier_VarParser::TEXT ||
  339. $type === HTMLPurifier_VarParser::ITEXT ||
  340. $type === HTMLPurifier_VarParser::ALIST ||
  341. $type === HTMLPurifier_VarParser::HASH ||
  342. $type === HTMLPurifier_VarParser::LOOKUP) {
  343. $attr['cols'] = $this->cols;
  344. $attr['rows'] = $this->rows;
  345. $ret .= $this->start('textarea', $attr);
  346. $ret .= $this->text($value);
  347. $ret .= $this->end('textarea');
  348. } else {
  349. $attr['value'] = $value;
  350. $attr['type'] = 'text';
  351. $ret .= $this->elementEmpty('input', $attr);
  352. }
  353. return $ret;
  354. }
  355. }
  356. /**
  357. * Bool form field printer
  358. */
  359. class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer
  360. {
  361. /**
  362. * @param string $ns
  363. * @param string $directive
  364. * @param string $value
  365. * @param string $name
  366. * @param HTMLPurifier_Config|array $config
  367. * @return string
  368. */
  369. public function render($ns, $directive, $value, $name, $config)
  370. {
  371. if (is_array($config) && isset($config[0])) {
  372. $gen_config = $config[0];
  373. $config = $config[1];
  374. } else {
  375. $gen_config = $config;
  376. }
  377. $this->prepareGenerator($gen_config);
  378. $ret = '';
  379. $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
  380. $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
  381. $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
  382. $ret .= $this->text(' Yes');
  383. $ret .= $this->end('label');
  384. $attr = array(
  385. 'type' => 'radio',
  386. 'name' => "$name" . "[$ns.$directive]",
  387. 'id' => "$name:Yes_$ns.$directive",
  388. 'value' => '1'
  389. );
  390. if ($value === true) {
  391. $attr['checked'] = 'checked';
  392. }
  393. if ($value === null) {
  394. $attr['disabled'] = 'disabled';
  395. }
  396. $ret .= $this->elementEmpty('input', $attr);
  397. $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
  398. $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
  399. $ret .= $this->text(' No');
  400. $ret .= $this->end('label');
  401. $attr = array(
  402. 'type' => 'radio',
  403. 'name' => "$name" . "[$ns.$directive]",
  404. 'id' => "$name:No_$ns.$directive",
  405. 'value' => '0'
  406. );
  407. if ($value === false) {
  408. $attr['checked'] = 'checked';
  409. }
  410. if ($value === null) {
  411. $attr['disabled'] = 'disabled';
  412. }
  413. $ret .= $this->elementEmpty('input', $attr);
  414. $ret .= $this->end('div');
  415. return $ret;
  416. }
  417. }
  418. // vim: et sw=4 sts=4