3 require_once 'HTMLPurifier/Printer.php';
5 class HTMLPurifier_Printer_ConfigForm
extends HTMLPurifier_Printer
9 * Printers for specific fields
12 var $fields = array();
15 * Documentation URL, can have fragment tagged on end
21 * Name of form element to stuff config in
27 * Whether or not to compress directive names, clipping them off
28 * after a certain amount of letters. False to disable or integer letters
32 var $compress = false;
35 * @param $name Form element name for directives to be stuffed into
36 * @param $doc_url String documentation URL, will have fragment tagged on
37 * @param $compress Integer max length before compressing a directive name, set to false to turn off
39 function HTMLPurifier_Printer_ConfigForm(
40 $name, $doc_url = null, $compress = false
42 parent
::HTMLPurifier_Printer();
43 $this->docURL
= $doc_url;
45 $this->compress
= $compress;
46 // initialize sub-printers
47 $this->fields
['default'] = new HTMLPurifier_Printer_ConfigForm_default();
48 $this->fields
['bool'] = new HTMLPurifier_Printer_ConfigForm_bool();
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
56 function setTextareaDimensions($cols = null, $rows = null) {
57 if ($cols) $this->fields
['default']->cols
= $cols;
58 if ($rows) $this->fields
['default']->rows
= $rows;
62 * Retrieves styling, in case it is not accessible by webserver
65 return file_get_contents(HTMLPURIFIER_PREFIX
. '/HTMLPurifier/Printer/ConfigForm.css');
69 * Retrieves JavaScript, in case it is not accessible by webserver
71 function getJavaScript() {
72 return file_get_contents(HTMLPURIFIER_PREFIX
. '/HTMLPurifier/Printer/ConfigForm.js');
76 * Returns HTML output for a configuration form
77 * @param $config Configuration object of current form state
78 * @param $allowed Optional namespace(s) and directives to restrict form to.
80 function render($config, $allowed = true, $render_controls = true) {
81 $this->config
= $config;
82 $this->prepareGenerator($config);
84 $allowed = HTMLPurifier_Config
::getAllowedDirectivesForForm($allowed);
86 foreach ($allowed as $key) {
87 list($ns, $directive) = $key;
88 $all[$ns][$directive] = $config->get($ns, $directive);
92 $ret .= $this->start('table', array('class' => 'hp-config'));
93 $ret .= $this->start('thead');
94 $ret .= $this->start('tr');
95 $ret .= $this->element('th', 'Directive');
96 $ret .= $this->element('th', 'Value');
97 $ret .= $this->end('tr');
98 $ret .= $this->end('thead');
99 foreach ($all as $ns => $directives) {
100 $ret .= $this->renderNamespace($ns, $directives);
102 if ($render_controls) {
103 $ret .= $this->start('tbody');
104 $ret .= $this->start('tr');
105 $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
106 $ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
107 $ret .= '[<a href="?">Reset</a>]';
108 $ret .= $this->end('td');
109 $ret .= $this->end('tr');
110 $ret .= $this->end('tbody');
112 $ret .= $this->end('table');
117 * Renders a single namespace
118 * @param $ns String namespace name
119 * @param $directive Associative array of directives to values
122 function renderNamespace($ns, $directives) {
124 $ret .= $this->start('tbody', array('class' => 'namespace'));
125 $ret .= $this->start('tr');
126 $ret .= $this->element('th', $ns, array('colspan' => 2));
127 $ret .= $this->end('tr');
128 $ret .= $this->end('tbody');
129 $ret .= $this->start('tbody');
130 foreach ($directives as $directive => $value) {
131 $ret .= $this->start('tr');
132 $ret .= $this->start('th');
134 $url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL
);
135 $ret .= $this->start('a', array('href' => $url));
137 $attr = array('for' => "{$this->name}:$ns.$directive");
139 // crop directive name if it's too long
140 if (!$this->compress ||
(strlen($directive) < $this->compress
)) {
141 $directive_disp = $directive;
143 $directive_disp = substr($directive, 0, $this->compress
- 2) . '...';
144 $attr['title'] = $directive;
147 $ret .= $this->element(
150 // component printers must create an element with this id
153 if ($this->docURL
) $ret .= $this->end('a');
154 $ret .= $this->end('th');
156 $ret .= $this->start('td');
157 $def = $this->config
->def
->info
[$ns][$directive];
159 if (!isset($this->fields
[$type])) $type = 'default';
160 $type_obj = $this->fields
[$type];
161 if ($def->allow_null
) {
162 $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
164 $ret .= $type_obj->render($ns, $directive, $value, $this->name
, $this->config
);
165 $ret .= $this->end('td');
166 $ret .= $this->end('tr');
168 $ret .= $this->end('tbody');
175 * Printer decorator for directives that accept null
177 class HTMLPurifier_Printer_ConfigForm_NullDecorator
extends HTMLPurifier_Printer
{
179 * Printer being decorated
183 * @param $obj Printer to decorate
185 function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) {
186 parent
::HTMLPurifier_Printer();
189 function render($ns, $directive, $value, $name, $config) {
190 $this->prepareGenerator($config);
192 $ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
193 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
194 $ret .= $this->text(' Null/Disabled');
195 $ret .= $this->end('label');
197 'type' => 'checkbox',
199 'class' => 'null-toggle',
200 'name' => "$name"."[Null_$ns.$directive]",
201 'id' => "$name:Null_$ns.$directive",
202 'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
204 if ($value === null) $attr['checked'] = 'checked';
205 $ret .= $this->elementEmpty('input', $attr);
206 $ret .= $this->text(' or ');
207 $ret .= $this->elementEmpty('br');
208 $ret .= $this->obj
->render($ns, $directive, $value, $name, $config);
214 * Swiss-army knife configuration form field printer
216 class HTMLPurifier_Printer_ConfigForm_default
extends HTMLPurifier_Printer
{
219 function render($ns, $directive, $value, $name, $config) {
220 $this->prepareGenerator($config);
221 // this should probably be split up a little
223 $def = $config->def
->info
[$ns][$directive];
224 if (is_array($value)) {
225 switch ($def->type
) {
229 foreach ($array as $val => $b) {
233 $value = implode(PHP_EOL
, $value);
237 foreach ($value as $i => $v) {
238 $nvalue .= "$i:$v" . PHP_EOL
;
246 if ($def->type
=== 'mixed') {
247 return 'Not supported';
248 $value = serialize($value);
251 'name' => "$name"."[$ns.$directive]",
252 'id' => "$name:$ns.$directive"
254 if ($value === null) $attr['disabled'] = 'disabled';
255 if (is_array($def->allowed
)) {
256 $ret .= $this->start('select', $attr);
257 foreach ($def->allowed
as $val => $b) {
259 if ($value == $val) $attr['selected'] = 'selected';
260 $ret .= $this->element('option', $val, $attr);
262 $ret .= $this->end('select');
264 $def->type
== 'text' ||
$def->type
== 'itext' ||
265 $def->type
== 'list' ||
$def->type
== 'hash' ||
$def->type
== 'lookup'
267 $attr['cols'] = $this->cols
;
268 $attr['rows'] = $this->rows
;
269 $ret .= $this->start('textarea', $attr);
270 $ret .= $this->text($value);
271 $ret .= $this->end('textarea');
273 $attr['value'] = $value;
274 $attr['type'] = 'text';
275 $ret .= $this->elementEmpty('input', $attr);
282 * Bool form field printer
284 class HTMLPurifier_Printer_ConfigForm_bool
extends HTMLPurifier_Printer
{
285 function render($ns, $directive, $value, $name, $config) {
286 $this->prepareGenerator($config);
288 $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
290 $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
291 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
292 $ret .= $this->text(' Yes');
293 $ret .= $this->end('label');
297 'name' => "$name"."[$ns.$directive]",
298 'id' => "$name:Yes_$ns.$directive",
301 if ($value) $attr['checked'] = 'checked';
302 $ret .= $this->elementEmpty('input', $attr);
304 $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
305 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
306 $ret .= $this->text(' No');
307 $ret .= $this->end('label');
311 'name' => "$name"."[$ns.$directive]",
312 'id' => "$name:No_$ns.$directive",
315 if (!$value) $attr['checked'] = 'checked';
316 $ret .= $this->elementEmpty('input', $attr);
318 $ret .= $this->end('div');