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-11-25 02:24:39 +00:00
|
|
|
protected $fields = array();
|
2007-05-28 02:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Documentation URL, can have fragment tagged on end
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $docURL;
|
2007-05-28 02:20:55 +00:00
|
|
|
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
2007-05-28 13:15:06 +00:00
|
|
|
* Name of form element to stuff config in
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $name;
|
2007-05-28 13:15:06 +00:00
|
|
|
|
2007-06-28 23:01:27 +00:00
|
|
|
/**
|
|
|
|
* Whether or not to compress directive names, clipping them off
|
2007-08-19 15:38:37 +00:00
|
|
|
* after a certain amount of letters. False to disable or integer letters
|
|
|
|
* before clipping.
|
2007-06-28 23:01:27 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $compress = false;
|
2007-06-28 23:01:27 +00:00
|
|
|
|
2007-05-28 13:15:06 +00:00
|
|
|
/**
|
|
|
|
* @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-06-28 23:01:27 +00:00
|
|
|
* @param $compress Integer max length before compressing a directive name, set to false to turn off
|
2007-05-28 03:49:06 +00:00
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct(
|
2007-06-29 02:16:47 +00:00
|
|
|
$name, $doc_url = null, $compress = false
|
|
|
|
) {
|
2007-11-29 04:29:51 +00:00
|
|
|
parent::__construct();
|
2007-05-28 02:20:55 +00:00
|
|
|
$this->docURL = $doc_url;
|
2007-05-28 13:15:06 +00:00
|
|
|
$this->name = $name;
|
2007-06-28 23:01:27 +00:00
|
|
|
$this->compress = $compress;
|
2007-08-19 15:38:37 +00:00
|
|
|
// initialize sub-printers
|
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-06-29 02:16:47 +00:00
|
|
|
/**
|
2007-08-19 15:38:37 +00:00
|
|
|
* Sets default column and row size for textareas in sub-printers
|
2007-06-29 02:16:47 +00:00
|
|
|
* @param $cols Integer columns of textarea, null to use default
|
|
|
|
* @param $rows Integer rows of textarea, null to use default
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function setTextareaDimensions($cols = null, $rows = null) {
|
2007-06-29 02:16:47 +00:00
|
|
|
if ($cols) $this->fields['default']->cols = $cols;
|
|
|
|
if ($rows) $this->fields['default']->rows = $rows;
|
|
|
|
}
|
|
|
|
|
2007-06-28 23:01:27 +00:00
|
|
|
/**
|
2007-08-19 15:38:37 +00:00
|
|
|
* Retrieves styling, in case it is not accessible by webserver
|
2007-06-28 23:01:27 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function getCSS() {
|
2007-07-30 16:56:50 +00:00
|
|
|
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-08-19 15:38:37 +00:00
|
|
|
* Retrieves JavaScript, in case it is not accessible by webserver
|
2007-06-28 23:01:27 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function getJavaScript() {
|
2007-07-30 16:56:50 +00:00
|
|
|
return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
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-11-25 02:24:39 +00:00
|
|
|
public 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) {
|
2007-08-19 16:24:55 +00:00
|
|
|
$ret .= $this->start('tbody');
|
2007-06-25 18:38:39 +00:00
|
|
|
$ret .= $this->start('tr');
|
|
|
|
$ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
|
2007-08-19 16:24:55 +00:00
|
|
|
$ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
|
2007-06-25 18:38:39 +00:00
|
|
|
$ret .= '[<a href="?">Reset</a>]';
|
|
|
|
$ret .= $this->end('td');
|
|
|
|
$ret .= $this->end('tr');
|
2007-08-19 16:24:55 +00:00
|
|
|
$ret .= $this->end('tbody');
|
2007-06-25 18:38:39 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected function renderNamespace($ns, $directives) {
|
2007-05-28 02:20:55 +00:00
|
|
|
$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");
|
2007-06-28 23:01:27 +00:00
|
|
|
|
2007-06-25 18:38:39 +00:00
|
|
|
// crop directive name if it's too long
|
2007-06-28 23:01:27 +00:00
|
|
|
if (!$this->compress || (strlen($directive) < $this->compress)) {
|
2007-06-25 18:38:39 +00:00
|
|
|
$directive_disp = $directive;
|
|
|
|
} else {
|
2007-06-28 23:01:27 +00:00
|
|
|
$directive_disp = substr($directive, 0, $this->compress - 2) . '...';
|
2007-06-25 18:38:39 +00:00
|
|
|
$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-11-25 02:24:39 +00:00
|
|
|
protected $obj;
|
2007-05-28 03:49:06 +00:00
|
|
|
/**
|
|
|
|
* @param $obj Printer to decorate
|
|
|
|
*/
|
2007-11-29 22:06:21 +00:00
|
|
|
public function __construct($obj) {
|
|
|
|
parent::__construct();
|
2007-05-28 02:20:55 +00:00
|
|
|
$this->obj = $obj;
|
|
|
|
}
|
2007-11-25 02:24:39 +00:00
|
|
|
public 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-11-25 02:24:39 +00:00
|
|
|
public $cols = 18;
|
|
|
|
public $rows = 5;
|
|
|
|
public 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':
|
2007-06-29 02:16:47 +00:00
|
|
|
$value = implode(PHP_EOL, $value);
|
2007-05-28 02:20:55 +00:00
|
|
|
break;
|
|
|
|
case 'hash':
|
|
|
|
$nvalue = '';
|
|
|
|
foreach ($value as $i => $v) {
|
2007-06-29 02:16:47 +00:00
|
|
|
$nvalue .= "$i:$v" . PHP_EOL;
|
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');
|
2007-06-29 02:16:47 +00:00
|
|
|
} elseif (
|
|
|
|
$def->type == 'text' || $def->type == 'itext' ||
|
|
|
|
$def->type == 'list' || $def->type == 'hash' || $def->type == 'lookup'
|
|
|
|
) {
|
|
|
|
$attr['cols'] = $this->cols;
|
|
|
|
$attr['rows'] = $this->rows;
|
|
|
|
$ret .= $this->start('textarea', $attr);
|
|
|
|
$ret .= $this->text($value);
|
|
|
|
$ret .= $this->end('textarea');
|
2007-05-28 02:20:55 +00:00
|
|
|
} 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-11-25 02:24:39 +00:00
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|