~ubuntu-branches/ubuntu/natty/moodle/natty

« back to all changes in this revision

Viewing changes to lib/htmlpurifier/HTMLPurifier/Printer/ConfigForm.php

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Muras
  • Date: 2010-10-30 12:19:28 UTC
  • mfrom: (1.1.12 upstream) (3.1.10 squeeze)
  • Revision ID: james.westby@ubuntu.com-20101030121928-qzobi6mctpnk4dif
Tags: 1.9.9.dfsg2-2
* Added Romanian translation
* Updated Japanese translation (closes: #596820)
* Backporting security fixes from Moodle 1.9.10 (closes: #601384)
   - Updated embedded CAS to 1.1.3
   - Added patch for MDL-24523:
     clean_text() not filtering text in markdown format
   - Added patch for MDL-24810 and upgraded customized HTML Purifier to 4.2.0 
   - Added patch for MDL-24258:
     students can delete their forum posts later than $CFG->maxeditingtime 
     under certain conditions
   - Added patch for MDL-23377:
     Can't delete quiz attempts in course without enrolled students

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
 
3
 
require_once 'HTMLPurifier/Printer.php';
4
 
 
 
3
/**
 
4
 * @todo Rewrite to use Interchange objects
 
5
 */
5
6
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
6
7
{
7
 
    
 
8
 
8
9
    /**
9
10
     * Printers for specific fields
10
 
     * @protected
11
11
     */
12
 
    var $fields = array();
13
 
    
 
12
    protected $fields = array();
 
13
 
14
14
    /**
15
15
     * Documentation URL, can have fragment tagged on end
16
 
     * @protected
17
16
     */
18
 
    var $docURL;
19
 
    
 
17
    protected $docURL;
 
18
 
20
19
    /**
21
20
     * Name of form element to stuff config in
22
 
     * @protected
23
21
     */
24
 
    var $name;
25
 
    
 
22
    protected $name;
 
23
 
26
24
    /**
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
29
27
     * before clipping.
30
 
     * @protected
31
28
     */
32
 
    var $compress = false;
33
 
    
 
29
    protected $compress = false;
 
30
 
34
31
    /**
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
38
35
     */
39
 
    function HTMLPurifier_Printer_ConfigForm(
 
36
    public function __construct(
40
37
        $name, $doc_url = null, $compress = false
41
38
    ) {
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();
49
46
    }
50
 
    
 
47
 
51
48
    /**
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
55
52
     */
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;
59
56
    }
60
 
    
 
57
 
61
58
    /**
62
59
     * Retrieves styling, in case it is not accessible by webserver
63
60
     */
64
 
    function getCSS() {
 
61
    public static function getCSS() {
65
62
        return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
66
63
    }
67
 
    
 
64
 
68
65
    /**
69
66
     * Retrieves JavaScript, in case it is not accessible by webserver
70
67
     */
71
 
    function getJavaScript() {
 
68
    public static function getJavaScript() {
72
69
        return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
73
70
    }
74
 
    
 
71
 
75
72
    /**
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.
79
77
     */
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];
 
81
            $config = $config[1];
 
82
        } else {
 
83
            $gen_config = $config;
 
84
        }
 
85
 
81
86
        $this->config = $config;
82
 
        $this->prepareGenerator($config);
83
 
        
84
 
        $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
 
87
        $this->genConfig = $gen_config;
 
88
        $this->prepareGenerator($gen_config);
 
89
 
 
90
        $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def);
85
91
        $all = array();
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);
89
95
        }
90
 
        
 
96
 
91
97
        $ret = '';
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) {
112
118
        $ret .= $this->end('table');
113
119
        return $ret;
114
120
    }
115
 
    
 
121
 
116
122
    /**
117
123
     * Renders a single namespace
118
124
     * @param $ns String namespace name
119
125
     * @param $directive Associative array of directives to values
120
 
     * @protected
121
126
     */
122
 
    function renderNamespace($ns, $directives) {
 
127
    protected function renderNamespace($ns, $directives) {
123
128
        $ret = '';
124
129
        $ret .= $this->start('tbody', array('class' => 'namespace'));
125
130
        $ret .= $this->start('tr');
135
140
                $ret .= $this->start('a', array('href' => $url));
136
141
            }
137
142
                $attr = array('for' => "{$this->name}:$ns.$directive");
138
 
                
 
143
 
139
144
                // crop directive name if it's too long
140
145
                if (!$this->compress || (strlen($directive) < $this->compress)) {
141
146
                    $directive_disp = $directive;
143
148
                    $directive_disp = substr($directive, 0, $this->compress - 2) . '...';
144
149
                    $attr['title'] = $directive;
145
150
                }
146
 
                
 
151
 
147
152
                $ret .= $this->element(
148
153
                    'label',
149
154
                    $directive_disp,
152
157
                );
153
158
            if ($this->docURL) $ret .= $this->end('a');
154
159
            $ret .= $this->end('th');
155
 
            
 
160
 
156
161
            $ret .= $this->start('td');
157
 
                $def = $this->config->def->info[$ns][$directive];
158
 
                $type = $def->type;
159
 
                if (!isset($this->fields[$type])) $type = 'default';
 
162
                $def = $this->config->def->info["$ns.$directive"];
 
163
                if (is_int($def)) {
 
164
                    $allow_null = $def < 0;
 
165
                    $type = abs($def);
 
166
                } else {
 
167
                    $type = $def->type;
 
168
                    $allow_null = isset($def->allow_null);
 
169
                }
 
170
                if (!isset($this->fields[$type])) $type = 0; // default
160
171
                $type_obj = $this->fields[$type];
161
 
                if ($def->allow_null) {
 
172
                if ($allow_null) {
162
173
                    $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
163
174
                }
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');
167
178
        }
168
179
        $ret .= $this->end('tbody');
169
180
        return $ret;
170
181
    }
171
 
    
 
182
 
172
183
}
173
184
 
174
185
/**
178
189
    /**
179
190
     * Printer being decorated
180
191
     */
181
 
    var $obj;
 
192
    protected $obj;
182
193
    /**
183
194
     * @param $obj Printer to decorate
184
195
     */
185
 
    function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) {
186
 
        parent::HTMLPurifier_Printer();
 
196
    public function __construct($obj) {
 
197
        parent::__construct();
187
198
        $this->obj = $obj;
188
199
    }
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];
 
204
        } else {
 
205
            $gen_config = $config;
 
206
        }
 
207
        $this->prepareGenerator($gen_config);
 
208
 
191
209
        $ret = '';
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!!!!
203
221
        );
 
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)";
 
225
        }
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));
209
231
        return $ret;
210
232
    }
211
233
}
214
236
 * Swiss-army knife configuration form field printer
215
237
 */
216
238
class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
217
 
    var $cols = 18;
218
 
    var $rows = 5;
219
 
    function render($ns, $directive, $value, $name, $config) {
220
 
        $this->prepareGenerator($config);
 
239
    public $cols = 18;
 
240
    public $rows = 5;
 
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];
 
245
        } else {
 
246
            $gen_config = $config;
 
247
        }
 
248
        $this->prepareGenerator($gen_config);
221
249
        // this should probably be split up a little
222
250
        $ret = '';
223
 
        $def = $config->def->info[$ns][$directive];
 
251
        $def = $config->def->info["$ns.$directive"];
 
252
        if (is_int($def)) {
 
253
            $type = abs($def);
 
254
        } else {
 
255
            $type = $def->type;
 
256
        }
224
257
        if (is_array($value)) {
225
 
            switch ($def->type) {
226
 
                case 'lookup':
 
258
            switch ($type) {
 
259
                case HTMLPurifier_VarParser::LOOKUP:
227
260
                    $array = $value;
228
261
                    $value = array();
229
262
                    foreach ($array as $val => $b) {
230
263
                        $value[] = $val;
231
264
                    }
232
 
                case 'list':
 
265
                case HTMLPurifier_VarParser::ALIST:
233
266
                    $value = implode(PHP_EOL, $value);
234
267
                    break;
235
 
                case 'hash':
 
268
                case HTMLPurifier_VarParser::HASH:
236
269
                    $nvalue = '';
237
270
                    foreach ($value as $i => $v) {
238
271
                        $nvalue .= "$i:$v" . PHP_EOL;
243
276
                    $value = '';
244
277
            }
245
278
        }
246
 
        if ($def->type === 'mixed') {
 
279
        if ($type === HTMLPurifier_VarParser::MIXED) {
247
280
            return 'Not supported';
248
281
            $value = serialize($value);
249
282
        }
252
285
            'id' => "$name:$ns.$directive"
253
286
        );
254
287
        if ($value === null) $attr['disabled'] = 'disabled';
255
 
        if (is_array($def->allowed)) {
 
288
        if (isset($def->allowed)) {
256
289
            $ret .= $this->start('select', $attr);
257
290
            foreach ($def->allowed as $val => $b) {
258
291
                $attr = array();
261
294
            }
262
295
            $ret .= $this->end('select');
263
296
        } elseif (
264
 
            $def->type == 'text' || $def->type == 'itext' ||
265
 
            $def->type == 'list' || $def->type == 'hash' || $def->type == 'lookup'
 
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
266
302
        ) {
267
303
            $attr['cols'] = $this->cols;
268
304
            $attr['rows'] = $this->rows;
282
318
 * Bool form field printer
283
319
 */
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];
 
325
        } else {
 
326
            $gen_config = $config;
 
327
        }
 
328
        $this->prepareGenerator($gen_config);
287
329
        $ret = '';
288
330
        $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
289
 
        
 
331
 
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');
294
 
        
 
336
 
295
337
        $attr = array(
296
338
            'type' => 'radio',
297
339
            'name' => "$name"."[$ns.$directive]",
298
340
            'id' => "$name:Yes_$ns.$directive",
299
341
            'value' => '1'
300
342
        );
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);
303
 
        
 
346
 
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');
308
 
        
 
351
 
309
352
        $attr = array(
310
353
            'type' => 'radio',
311
354
            'name' => "$name"."[$ns.$directive]",
312
355
            'id' => "$name:No_$ns.$directive",
313
356
            'value' => '0'
314
357
        );
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);
317
 
                
 
361
 
318
362
        $ret .= $this->end('div');
319
 
        
 
363
 
320
364
        return $ret;
321
365
    }
322
366
}
323
367
 
 
368
// vim: et sw=4 sts=4