PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/include/htmlpurifier/library/HTMLPurifier/Printer/ConfigForm.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 368 lines | 274 code | 29 blank | 65 comment | 40 complexity | dba9c25885aabb97a253f866715c11a1 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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