4
* @todo Rewrite to use Interchange objects
6
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
10
* Printers for specific fields
12
protected $fields = array();
15
* Documentation URL, can have fragment tagged on end
20
* Name of form element to stuff config in
25
* Whether or not to compress directive names, clipping them off
26
* after a certain amount of letters. False to disable or integer letters
29
protected $compress = false;
32
* @param $name Form element name for directives to be stuffed into
33
* @param $doc_url String documentation URL, will have fragment tagged on
34
* @param $compress Integer max length before compressing a directive name, set to false to turn off
36
public function __construct(
37
$name, $doc_url = null, $compress = false
39
parent::__construct();
40
$this->docURL = $doc_url;
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();
49
* Sets default column and row size for textareas in sub-printers
50
* @param $cols Integer columns of textarea, null to use default
51
* @param $rows Integer rows of textarea, null to use default
53
public function setTextareaDimensions($cols = null, $rows = null) {
54
if ($cols) $this->fields['default']->cols = $cols;
55
if ($rows) $this->fields['default']->rows = $rows;
59
* Retrieves styling, in case it is not accessible by webserver
61
public static function getCSS() {
62
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
66
* Retrieves JavaScript, in case it is not accessible by webserver
68
public static function getJavaScript() {
69
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
73
* Returns HTML output for a configuration form
74
* @param $config Configuration object of current form state, or an array
75
* where [0] has an HTML namespace and [1] is being rendered.
76
* @param $allowed Optional namespace(s) and directives to restrict form to.
78
public function render($config, $allowed = true, $render_controls = true) {
79
if (is_array($config) && isset($config[0])) {
80
$gen_config = $config[0];
83
$gen_config = $config;
86
$this->config = $config;
87
$this->genConfig = $gen_config;
88
$this->prepareGenerator($gen_config);
90
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
92
foreach ($allowed as $key) {
93
list($ns, $directive) = $key;
94
$all[$ns][$directive] = $config->get($ns, $directive);
98
$ret .= $this->start('table', array('class' => 'hp-config'));
99
$ret .= $this->start('thead');
100
$ret .= $this->start('tr');
101
$ret .= $this->element('th', 'Directive', array('class' => 'hp-directive'));
102
$ret .= $this->element('th', 'Value', array('class' => 'hp-value'));
103
$ret .= $this->end('tr');
104
$ret .= $this->end('thead');
105
foreach ($all as $ns => $directives) {
106
$ret .= $this->renderNamespace($ns, $directives);
108
if ($render_controls) {
109
$ret .= $this->start('tbody');
110
$ret .= $this->start('tr');
111
$ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
112
$ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
113
$ret .= '[<a href="?">Reset</a>]';
114
$ret .= $this->end('td');
115
$ret .= $this->end('tr');
116
$ret .= $this->end('tbody');
118
$ret .= $this->end('table');
123
* Renders a single namespace
124
* @param $ns String namespace name
125
* @param $directive Associative array of directives to values
127
protected function renderNamespace($ns, $directives) {
129
$ret .= $this->start('tbody', array('class' => 'namespace'));
130
$ret .= $this->start('tr');
131
$ret .= $this->element('th', $ns, array('colspan' => 2));
132
$ret .= $this->end('tr');
133
$ret .= $this->end('tbody');
134
$ret .= $this->start('tbody');
135
foreach ($directives as $directive => $value) {
136
$ret .= $this->start('tr');
137
$ret .= $this->start('th');
139
$url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
140
$ret .= $this->start('a', array('href' => $url));
142
$attr = array('for' => "{$this->name}:$ns.$directive");
144
// crop directive name if it's too long
145
if (!$this->compress || (strlen($directive) < $this->compress)) {
146
$directive_disp = $directive;
148
$directive_disp = substr($directive, 0, $this->compress - 2) . '...';
149
$attr['title'] = $directive;
152
$ret .= $this->element(
155
// component printers must create an element with this id
158
if ($this->docURL) $ret .= $this->end('a');
159
$ret .= $this->end('th');
161
$ret .= $this->start('td');
162
$def = $this->config->def->info[$ns][$directive];
164
$allow_null = $def < 0;
168
$allow_null = isset($def->allow_null);
170
if (!isset($this->fields[$type])) $type = 0; // default
171
$type_obj = $this->fields[$type];
173
$type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
175
$ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
176
$ret .= $this->end('td');
177
$ret .= $this->end('tr');
179
$ret .= $this->end('tbody');
186
* Printer decorator for directives that accept null
188
class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer {
190
* Printer being decorated
194
* @param $obj Printer to decorate
196
public function __construct($obj) {
197
parent::__construct();
200
public function render($ns, $directive, $value, $name, $config) {
201
if (is_array($config) && isset($config[0])) {
202
$gen_config = $config[0];
203
$config = $config[1];
205
$gen_config = $config;
207
$this->prepareGenerator($gen_config);
210
$ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
211
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
212
$ret .= $this->text(' Null/Disabled');
213
$ret .= $this->end('label');
215
'type' => 'checkbox',
217
'class' => 'null-toggle',
218
'name' => "$name"."[Null_$ns.$directive]",
219
'id' => "$name:Null_$ns.$directive",
220
'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
222
if ($this->obj instanceof HTMLPurifier_Printer_ConfigForm_bool) {
223
// modify inline javascript slightly
224
$attr['onclick'] = "toggleWriteability('$name:Yes_$ns.$directive',checked);toggleWriteability('$name:No_$ns.$directive',checked)";
226
if ($value === null) $attr['checked'] = 'checked';
227
$ret .= $this->elementEmpty('input', $attr);
228
$ret .= $this->text(' or ');
229
$ret .= $this->elementEmpty('br');
230
$ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
236
* Swiss-army knife configuration form field printer
238
class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
241
public function render($ns, $directive, $value, $name, $config) {
242
if (is_array($config) && isset($config[0])) {
243
$gen_config = $config[0];
244
$config = $config[1];
246
$gen_config = $config;
248
$this->prepareGenerator($gen_config);
249
// this should probably be split up a little
251
$def = $config->def->info[$ns][$directive];
257
if (is_array($value)) {
259
case HTMLPurifier_VarParser::LOOKUP:
262
foreach ($array as $val => $b) {
265
case HTMLPurifier_VarParser::ALIST:
266
$value = implode(PHP_EOL, $value);
268
case HTMLPurifier_VarParser::HASH:
270
foreach ($value as $i => $v) {
271
$nvalue .= "$i:$v" . PHP_EOL;
279
if ($type === HTMLPurifier_VarParser::MIXED) {
280
return 'Not supported';
281
$value = serialize($value);
284
'name' => "$name"."[$ns.$directive]",
285
'id' => "$name:$ns.$directive"
287
if ($value === null) $attr['disabled'] = 'disabled';
288
if (isset($def->allowed)) {
289
$ret .= $this->start('select', $attr);
290
foreach ($def->allowed as $val => $b) {
292
if ($value == $val) $attr['selected'] = 'selected';
293
$ret .= $this->element('option', $val, $attr);
295
$ret .= $this->end('select');
297
$type === HTMLPurifier_VarParser::TEXT ||
298
$type === HTMLPurifier_VarParser::ITEXT ||
299
$type === HTMLPurifier_VarParser::ALIST ||
300
$type === HTMLPurifier_VarParser::HASH ||
301
$type === HTMLPurifier_VarParser::LOOKUP
303
$attr['cols'] = $this->cols;
304
$attr['rows'] = $this->rows;
305
$ret .= $this->start('textarea', $attr);
306
$ret .= $this->text($value);
307
$ret .= $this->end('textarea');
309
$attr['value'] = $value;
310
$attr['type'] = 'text';
311
$ret .= $this->elementEmpty('input', $attr);
318
* Bool form field printer
320
class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
321
public function render($ns, $directive, $value, $name, $config) {
322
if (is_array($config) && isset($config[0])) {
323
$gen_config = $config[0];
324
$config = $config[1];
326
$gen_config = $config;
328
$this->prepareGenerator($gen_config);
330
$ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
332
$ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
333
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
334
$ret .= $this->text(' Yes');
335
$ret .= $this->end('label');
339
'name' => "$name"."[$ns.$directive]",
340
'id' => "$name:Yes_$ns.$directive",
343
if ($value === true) $attr['checked'] = 'checked';
344
if ($value === null) $attr['disabled'] = 'disabled';
345
$ret .= $this->elementEmpty('input', $attr);
347
$ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
348
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
349
$ret .= $this->text(' No');
350
$ret .= $this->end('label');
354
'name' => "$name"."[$ns.$directive]",
355
'id' => "$name:No_$ns.$directive",
358
if ($value === false) $attr['checked'] = 'checked';
359
if ($value === null) $attr['disabled'] = 'disabled';
360
$ret .= $this->elementEmpty('input', $attr);
362
$ret .= $this->end('div');
4
* @todo Rewrite to use Interchange objects
6
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
10
* Printers for specific fields
12
protected $fields = array();
15
* Documentation URL, can have fragment tagged on end
20
* Name of form element to stuff config in
25
* Whether or not to compress directive names, clipping them off
26
* after a certain amount of letters. False to disable or integer letters
29
protected $compress = false;
32
* @param $name Form element name for directives to be stuffed into
33
* @param $doc_url String documentation URL, will have fragment tagged on
34
* @param $compress Integer max length before compressing a directive name, set to false to turn off
36
public function __construct(
37
$name, $doc_url = null, $compress = false
39
parent::__construct();
40
$this->docURL = $doc_url;
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();
49
* Sets default column and row size for textareas in sub-printers
50
* @param $cols Integer columns of textarea, null to use default
51
* @param $rows Integer rows of textarea, null to use default
53
public function setTextareaDimensions($cols = null, $rows = null) {
54
if ($cols) $this->fields['default']->cols = $cols;
55
if ($rows) $this->fields['default']->rows = $rows;
59
* Retrieves styling, in case it is not accessible by webserver
61
public static function getCSS() {
62
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
66
* Retrieves JavaScript, in case it is not accessible by webserver
68
public static function getJavaScript() {
69
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
73
* Returns HTML output for a configuration form
74
* @param $config Configuration object of current form state, or an array
75
* where [0] has an HTML namespace and [1] is being rendered.
76
* @param $allowed Optional namespace(s) and directives to restrict form to.
78
public function render($config, $allowed = true, $render_controls = true) {
79
if (is_array($config) && isset($config[0])) {
80
$gen_config = $config[0];
83
$gen_config = $config;
86
$this->config = $config;
87
$this->genConfig = $gen_config;
88
$this->prepareGenerator($gen_config);
90
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
92
foreach ($allowed as $key) {
93
list($ns, $directive) = $key;
94
$all[$ns][$directive] = $config->get($ns .'.'. $directive);
98
$ret .= $this->start('table', array('class' => 'hp-config'));
99
$ret .= $this->start('thead');
100
$ret .= $this->start('tr');
101
$ret .= $this->element('th', 'Directive', array('class' => 'hp-directive'));
102
$ret .= $this->element('th', 'Value', array('class' => 'hp-value'));
103
$ret .= $this->end('tr');
104
$ret .= $this->end('thead');
105
foreach ($all as $ns => $directives) {
106
$ret .= $this->renderNamespace($ns, $directives);
108
if ($render_controls) {
109
$ret .= $this->start('tbody');
110
$ret .= $this->start('tr');
111
$ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
112
$ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
113
$ret .= '[<a href="?">Reset</a>]';
114
$ret .= $this->end('td');
115
$ret .= $this->end('tr');
116
$ret .= $this->end('tbody');
118
$ret .= $this->end('table');
123
* Renders a single namespace
124
* @param $ns String namespace name
125
* @param $directive Associative array of directives to values
127
protected function renderNamespace($ns, $directives) {
129
$ret .= $this->start('tbody', array('class' => 'namespace'));
130
$ret .= $this->start('tr');
131
$ret .= $this->element('th', $ns, array('colspan' => 2));
132
$ret .= $this->end('tr');
133
$ret .= $this->end('tbody');
134
$ret .= $this->start('tbody');
135
foreach ($directives as $directive => $value) {
136
$ret .= $this->start('tr');
137
$ret .= $this->start('th');
139
$url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
140
$ret .= $this->start('a', array('href' => $url));
142
$attr = array('for' => "{$this->name}:$ns.$directive");
144
// crop directive name if it's too long
145
if (!$this->compress || (strlen($directive) < $this->compress)) {
146
$directive_disp = $directive;
148
$directive_disp = substr($directive, 0, $this->compress - 2) . '...';
149
$attr['title'] = $directive;
152
$ret .= $this->element(
155
// component printers must create an element with this id
158
if ($this->docURL) $ret .= $this->end('a');
159
$ret .= $this->end('th');
161
$ret .= $this->start('td');
162
$def = $this->config->def->info["$ns.$directive"];
164
$allow_null = $def < 0;
168
$allow_null = isset($def->allow_null);
170
if (!isset($this->fields[$type])) $type = 0; // default
171
$type_obj = $this->fields[$type];
173
$type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
175
$ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
176
$ret .= $this->end('td');
177
$ret .= $this->end('tr');
179
$ret .= $this->end('tbody');
186
* Printer decorator for directives that accept null
188
class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer {
190
* Printer being decorated
194
* @param $obj Printer to decorate
196
public function __construct($obj) {
197
parent::__construct();
200
public function render($ns, $directive, $value, $name, $config) {
201
if (is_array($config) && isset($config[0])) {
202
$gen_config = $config[0];
203
$config = $config[1];
205
$gen_config = $config;
207
$this->prepareGenerator($gen_config);
210
$ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
211
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
212
$ret .= $this->text(' Null/Disabled');
213
$ret .= $this->end('label');
215
'type' => 'checkbox',
217
'class' => 'null-toggle',
218
'name' => "$name"."[Null_$ns.$directive]",
219
'id' => "$name:Null_$ns.$directive",
220
'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
222
if ($this->obj instanceof HTMLPurifier_Printer_ConfigForm_bool) {
223
// modify inline javascript slightly
224
$attr['onclick'] = "toggleWriteability('$name:Yes_$ns.$directive',checked);toggleWriteability('$name:No_$ns.$directive',checked)";
226
if ($value === null) $attr['checked'] = 'checked';
227
$ret .= $this->elementEmpty('input', $attr);
228
$ret .= $this->text(' or ');
229
$ret .= $this->elementEmpty('br');
230
$ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
236
* Swiss-army knife configuration form field printer
238
class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
241
public function render($ns, $directive, $value, $name, $config) {
242
if (is_array($config) && isset($config[0])) {
243
$gen_config = $config[0];
244
$config = $config[1];
246
$gen_config = $config;
248
$this->prepareGenerator($gen_config);
249
// this should probably be split up a little
251
$def = $config->def->info["$ns.$directive"];
257
if (is_array($value)) {
259
case HTMLPurifier_VarParser::LOOKUP:
262
foreach ($array as $val => $b) {
265
case HTMLPurifier_VarParser::ALIST:
266
$value = implode(PHP_EOL, $value);
268
case HTMLPurifier_VarParser::HASH:
270
foreach ($value as $i => $v) {
271
$nvalue .= "$i:$v" . PHP_EOL;
279
if ($type === HTMLPurifier_VarParser::MIXED) {
280
return 'Not supported';
281
$value = serialize($value);
284
'name' => "$name"."[$ns.$directive]",
285
'id' => "$name:$ns.$directive"
287
if ($value === null) $attr['disabled'] = 'disabled';
288
if (isset($def->allowed)) {
289
$ret .= $this->start('select', $attr);
290
foreach ($def->allowed as $val => $b) {
292
if ($value == $val) $attr['selected'] = 'selected';
293
$ret .= $this->element('option', $val, $attr);
295
$ret .= $this->end('select');
297
$type === HTMLPurifier_VarParser::TEXT ||
298
$type === HTMLPurifier_VarParser::ITEXT ||
299
$type === HTMLPurifier_VarParser::ALIST ||
300
$type === HTMLPurifier_VarParser::HASH ||
301
$type === HTMLPurifier_VarParser::LOOKUP
303
$attr['cols'] = $this->cols;
304
$attr['rows'] = $this->rows;
305
$ret .= $this->start('textarea', $attr);
306
$ret .= $this->text($value);
307
$ret .= $this->end('textarea');
309
$attr['value'] = $value;
310
$attr['type'] = 'text';
311
$ret .= $this->elementEmpty('input', $attr);
318
* Bool form field printer
320
class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
321
public function render($ns, $directive, $value, $name, $config) {
322
if (is_array($config) && isset($config[0])) {
323
$gen_config = $config[0];
324
$config = $config[1];
326
$gen_config = $config;
328
$this->prepareGenerator($gen_config);
330
$ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
332
$ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
333
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
334
$ret .= $this->text(' Yes');
335
$ret .= $this->end('label');
339
'name' => "$name"."[$ns.$directive]",
340
'id' => "$name:Yes_$ns.$directive",
343
if ($value === true) $attr['checked'] = 'checked';
344
if ($value === null) $attr['disabled'] = 'disabled';
345
$ret .= $this->elementEmpty('input', $attr);
347
$ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
348
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
349
$ret .= $this->text(' No');
350
$ret .= $this->end('label');
354
'name' => "$name"."[$ns.$directive]",
355
'id' => "$name:No_$ns.$directive",
358
if ($value === false) $attr['checked'] = 'checked';
359
if ($value === null) $attr['disabled'] = 'disabled';
360
$ret .= $this->elementEmpty('input', $attr);
362
$ret .= $this->end('div');
368
// vim: et sw=4 sts=4