2007-05-28 02:20:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Printer.php';
|
|
|
|
|
|
|
|
class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Printers for specific fields
|
2007-05-28 03:49:06 +00:00
|
|
|
* @protected
|
2007-05-28 02:20:55 +00:00
|
|
|
*/
|
|
|
|
var $fields = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Documentation URL, can have fragment tagged on end
|
2007-05-28 03:49:06 +00:00
|
|
|
* @protected
|
2007-05-28 02:20:55 +00:00
|
|
|
*/
|
|
|
|
var $docURL;
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
2007-05-28 13:15:06 +00:00
|
|
|
* Name of form element to stuff config in
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
var $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name Form element name for directives to be stuffed into
|
2007-05-28 03:49:06 +00:00
|
|
|
* @param $doc_url String documentation URL, will have fragment tagged on
|
|
|
|
*/
|
2007-05-28 13:15:06 +00:00
|
|
|
function HTMLPurifier_Printer_ConfigForm($name, $doc_url = null) {
|
2007-05-28 02:20:55 +00:00
|
|
|
parent::HTMLPurifier_Printer();
|
|
|
|
$this->docURL = $doc_url;
|
2007-05-28 13:15:06 +00:00
|
|
|
$this->name = $name;
|
2007-05-28 02:20:55 +00:00
|
|
|
$this->fields['default'] = new HTMLPurifier_Printer_ConfigForm_default();
|
|
|
|
$this->fields['bool'] = new HTMLPurifier_Printer_ConfigForm_bool();
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Returns HTML output for a configuration form
|
|
|
|
* @param $config Configuration object of current form state
|
2007-06-25 18:38:39 +00:00
|
|
|
* @param $allowed Optional namespace(s) and directives to restrict form to.
|
2007-05-28 03:49:06 +00:00
|
|
|
*/
|
2007-06-25 18:38:39 +00:00
|
|
|
function render($config, $allowed = true, $render_controls = true) {
|
2007-05-28 02:20:55 +00:00
|
|
|
$this->config = $config;
|
2007-06-25 18:38:39 +00:00
|
|
|
$this->prepareGenerator($config);
|
|
|
|
|
|
|
|
$allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
|
|
|
|
$all = array();
|
|
|
|
foreach ($allowed as $key) {
|
|
|
|
list($ns, $directive) = $key;
|
|
|
|
$all[$ns][$directive] = $config->get($ns, $directive);
|
2007-05-28 03:49:06 +00:00
|
|
|
}
|
2007-06-25 18:38:39 +00:00
|
|
|
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret = '';
|
|
|
|
$ret .= $this->start('table', array('class' => 'hp-config'));
|
|
|
|
$ret .= $this->start('thead');
|
|
|
|
$ret .= $this->start('tr');
|
|
|
|
$ret .= $this->element('th', 'Directive');
|
|
|
|
$ret .= $this->element('th', 'Value');
|
|
|
|
$ret .= $this->end('tr');
|
|
|
|
$ret .= $this->end('thead');
|
|
|
|
foreach ($all as $ns => $directives) {
|
|
|
|
$ret .= $this->renderNamespace($ns, $directives);
|
|
|
|
}
|
2007-06-25 18:38:39 +00:00
|
|
|
if ($render_controls) {
|
|
|
|
$ret .= $this->start('tfoot');
|
|
|
|
$ret .= $this->start('tr');
|
|
|
|
$ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
|
|
|
|
$ret .= $this->elementEmpty('input', array('type' => 'Submit', 'value' => 'Submit'));
|
|
|
|
$ret .= '[<a href="?">Reset</a>]';
|
|
|
|
$ret .= $this->end('td');
|
|
|
|
$ret .= $this->end('tr');
|
|
|
|
$ret .= $this->end('tfoot');
|
|
|
|
}
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->end('table');
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Renders a single namespace
|
|
|
|
* @param $ns String namespace name
|
|
|
|
* @param $directive Associative array of directives to values
|
|
|
|
* @protected
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
function renderNamespace($ns, $directives) {
|
|
|
|
$ret = '';
|
|
|
|
$ret .= $this->start('tbody', array('class' => 'namespace'));
|
|
|
|
$ret .= $this->start('tr');
|
|
|
|
$ret .= $this->element('th', $ns, array('colspan' => 2));
|
|
|
|
$ret .= $this->end('tr');
|
|
|
|
$ret .= $this->end('tbody');
|
|
|
|
$ret .= $this->start('tbody');
|
|
|
|
foreach ($directives as $directive => $value) {
|
|
|
|
$ret .= $this->start('tr');
|
|
|
|
$ret .= $this->start('th');
|
2007-05-28 13:15:06 +00:00
|
|
|
if ($this->docURL) {
|
|
|
|
$url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
|
|
|
|
$ret .= $this->start('a', array('href' => $url));
|
|
|
|
}
|
2007-06-25 18:38:39 +00:00
|
|
|
$attr = array('for' => "{$this->name}:$ns.$directive");
|
|
|
|
// crop directive name if it's too long
|
|
|
|
if (strlen($directive) < 14) {
|
|
|
|
$directive_disp = $directive;
|
|
|
|
} else {
|
|
|
|
$directive_disp = substr($directive, 0, 12) . '...';
|
|
|
|
$attr['title'] = $directive;
|
|
|
|
}
|
|
|
|
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->element(
|
|
|
|
'label',
|
2007-06-25 18:38:39 +00:00
|
|
|
$directive_disp,
|
2007-05-28 13:15:06 +00:00
|
|
|
// component printers must create an element with this id
|
2007-06-25 18:38:39 +00:00
|
|
|
$attr
|
2007-05-28 02:20:55 +00:00
|
|
|
);
|
|
|
|
if ($this->docURL) $ret .= $this->end('a');
|
|
|
|
$ret .= $this->end('th');
|
|
|
|
|
|
|
|
$ret .= $this->start('td');
|
|
|
|
$def = $this->config->def->info[$ns][$directive];
|
|
|
|
$type = $def->type;
|
|
|
|
if (!isset($this->fields[$type])) $type = 'default';
|
|
|
|
$type_obj = $this->fields[$type];
|
|
|
|
if ($def->allow_null) {
|
|
|
|
$type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
|
|
|
|
}
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $type_obj->render($ns, $directive, $value, $this->name, $this->config);
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->end('td');
|
|
|
|
$ret .= $this->end('tr');
|
|
|
|
}
|
|
|
|
$ret .= $this->end('tbody');
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Printer decorator for directives that accept null
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer {
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Printer being decorated
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
var $obj;
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* @param $obj Printer to decorate
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) {
|
|
|
|
parent::HTMLPurifier_Printer();
|
|
|
|
$this->obj = $obj;
|
|
|
|
}
|
2007-05-28 13:15:06 +00:00
|
|
|
function render($ns, $directive, $value, $name, $config) {
|
2007-06-25 18:38:39 +00:00
|
|
|
$this->prepareGenerator($config);
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret = '';
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
|
|
|
|
$ret .= $this->text(' Null/Disabled');
|
|
|
|
$ret .= $this->end('label');
|
|
|
|
$attr = array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'value' => '1',
|
|
|
|
'class' => 'null-toggle',
|
2007-06-25 18:38:39 +00:00
|
|
|
'name' => "$name"."[Null_$ns.$directive]",
|
2007-05-28 13:15:06 +00:00
|
|
|
'id' => "$name:Null_$ns.$directive",
|
|
|
|
'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
|
2007-05-28 02:20:55 +00:00
|
|
|
);
|
|
|
|
if ($value === null) $attr['checked'] = 'checked';
|
|
|
|
$ret .= $this->elementEmpty('input', $attr);
|
|
|
|
$ret .= $this->text(' or ');
|
|
|
|
$ret .= $this->elementEmpty('br');
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $this->obj->render($ns, $directive, $value, $name, $config);
|
2007-05-28 02:20:55 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Swiss-army knife configuration form field printer
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
|
2007-05-28 13:15:06 +00:00
|
|
|
function render($ns, $directive, $value, $name, $config) {
|
2007-06-25 18:38:39 +00:00
|
|
|
$this->prepareGenerator($config);
|
2007-05-28 03:49:06 +00:00
|
|
|
// this should probably be split up a little
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret = '';
|
|
|
|
$def = $config->def->info[$ns][$directive];
|
|
|
|
if (is_array($value)) {
|
|
|
|
switch ($def->type) {
|
|
|
|
case 'lookup':
|
|
|
|
$array = $value;
|
|
|
|
$value = array();
|
|
|
|
foreach ($array as $val => $b) {
|
|
|
|
$value[] = $val;
|
|
|
|
}
|
|
|
|
case 'list':
|
|
|
|
$value = implode(',', $value);
|
|
|
|
break;
|
|
|
|
case 'hash':
|
|
|
|
$nvalue = '';
|
|
|
|
foreach ($value as $i => $v) {
|
2007-05-28 03:55:36 +00:00
|
|
|
$nvalue .= "$i:$v,";
|
2007-05-28 02:20:55 +00:00
|
|
|
}
|
|
|
|
$value = $nvalue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($def->type === 'mixed') {
|
|
|
|
return 'Not supported';
|
|
|
|
$value = serialize($value);
|
|
|
|
}
|
|
|
|
$attr = array(
|
2007-05-28 13:15:06 +00:00
|
|
|
'name' => "$name"."[$ns.$directive]",
|
|
|
|
'id' => "$name:$ns.$directive"
|
2007-05-28 02:20:55 +00:00
|
|
|
);
|
|
|
|
if ($value === null) $attr['disabled'] = 'disabled';
|
|
|
|
if (is_array($def->allowed)) {
|
|
|
|
$ret .= $this->start('select', $attr);
|
|
|
|
foreach ($def->allowed as $val => $b) {
|
|
|
|
$attr = array();
|
|
|
|
if ($value == $val) $attr['selected'] = 'selected';
|
|
|
|
$ret .= $this->element('option', $val, $attr);
|
|
|
|
}
|
|
|
|
$ret .= $this->end('select');
|
|
|
|
} else {
|
|
|
|
$attr['value'] = $value;
|
2007-06-25 18:38:39 +00:00
|
|
|
$attr['type'] = 'text';
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->elementEmpty('input', $attr);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* Bool form field printer
|
|
|
|
*/
|
2007-05-28 02:20:55 +00:00
|
|
|
class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
|
2007-05-28 13:15:06 +00:00
|
|
|
function render($ns, $directive, $value, $name, $config) {
|
2007-06-25 18:38:39 +00:00
|
|
|
$this->prepareGenerator($config);
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret = '';
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
|
2007-05-28 02:20:55 +00:00
|
|
|
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
|
|
|
|
$ret .= $this->text(' Yes');
|
|
|
|
$ret .= $this->end('label');
|
|
|
|
|
|
|
|
$attr = array(
|
|
|
|
'type' => 'radio',
|
2007-05-28 13:15:06 +00:00
|
|
|
'name' => "$name"."[$ns.$directive]",
|
|
|
|
'id' => "$name:Yes_$ns.$directive",
|
2007-05-28 02:20:55 +00:00
|
|
|
'value' => '1'
|
|
|
|
);
|
|
|
|
if ($value) $attr['checked'] = 'checked';
|
|
|
|
$ret .= $this->elementEmpty('input', $attr);
|
|
|
|
|
2007-05-28 13:15:06 +00:00
|
|
|
$ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
|
2007-05-28 02:20:55 +00:00
|
|
|
$ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
|
|
|
|
$ret .= $this->text(' No');
|
|
|
|
$ret .= $this->end('label');
|
|
|
|
|
|
|
|
$attr = array(
|
|
|
|
'type' => 'radio',
|
2007-05-28 13:15:06 +00:00
|
|
|
'name' => "$name"."[$ns.$directive]",
|
|
|
|
'id' => "$name:No_$ns.$directive",
|
2007-05-28 02:20:55 +00:00
|
|
|
'value' => '0'
|
|
|
|
);
|
|
|
|
if (!$value) $attr['checked'] = 'checked';
|
|
|
|
$ret .= $this->elementEmpty('input', $attr);
|
|
|
|
|
|
|
|
$ret .= $this->end('div');
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|