3
require_once 'HTMLPurifier/Printer.php';
4
* @todo Rewrite to use Interchange objects
5
6
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
9
10
* Printers for specific fields
12
var $fields = array();
12
protected $fields = array();
15
15
* Documentation URL, can have fragment tagged on end
21
20
* Name of form element to stuff config in
27
25
* Whether or not to compress directive names, clipping them off
28
26
* after a certain amount of letters. False to disable or integer letters
32
var $compress = false;
29
protected $compress = false;
35
32
* @param $name Form element name for directives to be stuffed into
36
33
* @param $doc_url String documentation URL, will have fragment tagged on
37
34
* @param $compress Integer max length before compressing a directive name, set to false to turn off
39
function HTMLPurifier_Printer_ConfigForm(
36
public function __construct(
40
37
$name, $doc_url = null, $compress = false
42
parent::HTMLPurifier_Printer();
39
parent::__construct();
43
40
$this->docURL = $doc_url;
44
41
$this->name = $name;
45
42
$this->compress = $compress;
46
43
// initialize sub-printers
47
$this->fields['default'] = new HTMLPurifier_Printer_ConfigForm_default();
48
$this->fields['bool'] = new HTMLPurifier_Printer_ConfigForm_bool();
44
$this->fields[0] = new HTMLPurifier_Printer_ConfigForm_default();
45
$this->fields[HTMLPurifier_VarParser::BOOL] = new HTMLPurifier_Printer_ConfigForm_bool();
52
49
* Sets default column and row size for textareas in sub-printers
53
50
* @param $cols Integer columns of textarea, null to use default
54
51
* @param $rows Integer rows of textarea, null to use default
56
function setTextareaDimensions($cols = null, $rows = null) {
53
public function setTextareaDimensions($cols = null, $rows = null) {
57
54
if ($cols) $this->fields['default']->cols = $cols;
58
55
if ($rows) $this->fields['default']->rows = $rows;
62
59
* Retrieves styling, in case it is not accessible by webserver
61
public static function getCSS() {
65
62
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
69
66
* Retrieves JavaScript, in case it is not accessible by webserver
71
function getJavaScript() {
68
public static function getJavaScript() {
72
69
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
76
73
* Returns HTML output for a configuration form
77
* @param $config Configuration object of current form state
74
* @param $config Configuration object of current form state, or an array
75
* where [0] has an HTML namespace and [1] is being rendered.
78
76
* @param $allowed Optional namespace(s) and directives to restrict form to.
80
function render($config, $allowed = true, $render_controls = true) {
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;
81
86
$this->config = $config;
82
$this->prepareGenerator($config);
84
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
87
$this->genConfig = $gen_config;
88
$this->prepareGenerator($gen_config);
90
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
86
92
foreach ($allowed as $key) {
87
93
list($ns, $directive) = $key;
88
$all[$ns][$directive] = $config->get($ns, $directive);
94
$all[$ns][$directive] = $config->get($ns .'.'. $directive);
92
98
$ret .= $this->start('table', array('class' => 'hp-config'));
93
99
$ret .= $this->start('thead');
94
100
$ret .= $this->start('tr');
95
$ret .= $this->element('th', 'Directive');
96
$ret .= $this->element('th', 'Value');
101
$ret .= $this->element('th', 'Directive', array('class' => 'hp-directive'));
102
$ret .= $this->element('th', 'Value', array('class' => 'hp-value'));
97
103
$ret .= $this->end('tr');
98
104
$ret .= $this->end('thead');
99
105
foreach ($all as $ns => $directives) {
153
158
if ($this->docURL) $ret .= $this->end('a');
154
159
$ret .= $this->end('th');
156
161
$ret .= $this->start('td');
157
$def = $this->config->def->info[$ns][$directive];
159
if (!isset($this->fields[$type])) $type = 'default';
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
160
171
$type_obj = $this->fields[$type];
161
if ($def->allow_null) {
162
173
$type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
164
$ret .= $type_obj->render($ns, $directive, $value, $this->name, $this->config);
175
$ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
165
176
$ret .= $this->end('td');
166
177
$ret .= $this->end('tr');
168
179
$ret .= $this->end('tbody');
179
190
* Printer being decorated
183
194
* @param $obj Printer to decorate
185
function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) {
186
parent::HTMLPurifier_Printer();
196
public function __construct($obj) {
197
parent::__construct();
187
198
$this->obj = $obj;
189
function render($ns, $directive, $value, $name, $config) {
190
$this->prepareGenerator($config);
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);
192
210
$ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
193
211
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
201
219
'id' => "$name:Null_$ns.$directive",
202
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)";
204
226
if ($value === null) $attr['checked'] = 'checked';
205
227
$ret .= $this->elementEmpty('input', $attr);
206
228
$ret .= $this->text(' or ');
207
229
$ret .= $this->elementEmpty('br');
208
$ret .= $this->obj->render($ns, $directive, $value, $name, $config);
230
$ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
214
236
* Swiss-army knife configuration form field printer
216
238
class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
219
function render($ns, $directive, $value, $name, $config) {
220
$this->prepareGenerator($config);
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);
221
249
// this should probably be split up a little
223
$def = $config->def->info[$ns][$directive];
251
$def = $config->def->info["$ns.$directive"];
224
257
if (is_array($value)) {
225
switch ($def->type) {
259
case HTMLPurifier_VarParser::LOOKUP:
228
261
$value = array();
229
262
foreach ($array as $val => $b) {
265
case HTMLPurifier_VarParser::ALIST:
233
266
$value = implode(PHP_EOL, $value);
268
case HTMLPurifier_VarParser::HASH:
237
270
foreach ($value as $i => $v) {
238
271
$nvalue .= "$i:$v" . PHP_EOL;
282
318
* Bool form field printer
284
320
class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
285
function render($ns, $directive, $value, $name, $config) {
286
$this->prepareGenerator($config);
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);
288
330
$ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
290
332
$ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
291
333
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
292
334
$ret .= $this->text(' Yes');
293
335
$ret .= $this->end('label');
296
338
'type' => 'radio',
297
339
'name' => "$name"."[$ns.$directive]",
298
340
'id' => "$name:Yes_$ns.$directive",
301
if ($value) $attr['checked'] = 'checked';
343
if ($value === true) $attr['checked'] = 'checked';
344
if ($value === null) $attr['disabled'] = 'disabled';
302
345
$ret .= $this->elementEmpty('input', $attr);
304
347
$ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
305
348
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
306
349
$ret .= $this->text(' No');
307
350
$ret .= $this->end('label');
310
353
'type' => 'radio',
311
354
'name' => "$name"."[$ns.$directive]",
312
355
'id' => "$name:No_$ns.$directive",
315
if (!$value) $attr['checked'] = 'checked';
358
if ($value === false) $attr['checked'] = 'checked';
359
if ($value === null) $attr['disabled'] = 'disabled';
316
360
$ret .= $this->elementEmpty('input', $attr);
318
362
$ret .= $this->end('div');
368
// vim: et sw=4 sts=4