HTMLPurifier 4.4.0
|
00001 <?php 00002 00006 class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer 00007 { 00008 00012 protected $fields = array(); 00013 00017 protected $docURL; 00018 00022 protected $name; 00023 00029 protected $compress = false; 00030 00036 public function __construct( 00037 $name, $doc_url = null, $compress = false 00038 ) { 00039 parent::__construct(); 00040 $this->docURL = $doc_url; 00041 $this->name = $name; 00042 $this->compress = $compress; 00043 // initialize sub-printers 00044 $this->fields[0] = new HTMLPurifier_Printer_ConfigForm_default(); 00045 $this->fields[HTMLPurifier_VarParser::BOOL] = new HTMLPurifier_Printer_ConfigForm_bool(); 00046 } 00047 00053 public function setTextareaDimensions($cols = null, $rows = null) { 00054 if ($cols) $this->fields['default']->cols = $cols; 00055 if ($rows) $this->fields['default']->rows = $rows; 00056 } 00057 00061 public static function getCSS() { 00062 return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css'); 00063 } 00064 00068 public static function getJavaScript() { 00069 return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js'); 00070 } 00071 00078 public function render($config, $allowed = true, $render_controls = true) { 00079 if (is_array($config) && isset($config[0])) { 00080 $gen_config = $config[0]; 00081 $config = $config[1]; 00082 } else { 00083 $gen_config = $config; 00084 } 00085 00086 $this->config = $config; 00087 $this->genConfig = $gen_config; 00088 $this->prepareGenerator($gen_config); 00089 00090 $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $config->def); 00091 $all = array(); 00092 foreach ($allowed as $key) { 00093 list($ns, $directive) = $key; 00094 $all[$ns][$directive] = $config->get($ns .'.'. $directive); 00095 } 00096 00097 $ret = ''; 00098 $ret .= $this->start('table', array('class' => 'hp-config')); 00099 $ret .= $this->start('thead'); 00100 $ret .= $this->start('tr'); 00101 $ret .= $this->element('th', 'Directive', array('class' => 'hp-directive')); 00102 $ret .= $this->element('th', 'Value', array('class' => 'hp-value')); 00103 $ret .= $this->end('tr'); 00104 $ret .= $this->end('thead'); 00105 foreach ($all as $ns => $directives) { 00106 $ret .= $this->renderNamespace($ns, $directives); 00107 } 00108 if ($render_controls) { 00109 $ret .= $this->start('tbody'); 00110 $ret .= $this->start('tr'); 00111 $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls')); 00112 $ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit')); 00113 $ret .= '[<a href="?">Reset</a>]'; 00114 $ret .= $this->end('td'); 00115 $ret .= $this->end('tr'); 00116 $ret .= $this->end('tbody'); 00117 } 00118 $ret .= $this->end('table'); 00119 return $ret; 00120 } 00121 00127 protected function renderNamespace($ns, $directives) { 00128 $ret = ''; 00129 $ret .= $this->start('tbody', array('class' => 'namespace')); 00130 $ret .= $this->start('tr'); 00131 $ret .= $this->element('th', $ns, array('colspan' => 2)); 00132 $ret .= $this->end('tr'); 00133 $ret .= $this->end('tbody'); 00134 $ret .= $this->start('tbody'); 00135 foreach ($directives as $directive => $value) { 00136 $ret .= $this->start('tr'); 00137 $ret .= $this->start('th'); 00138 if ($this->docURL) { 00139 $url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL); 00140 $ret .= $this->start('a', array('href' => $url)); 00141 } 00142 $attr = array('for' => "{$this->name}:$ns.$directive"); 00143 00144 // crop directive name if it's too long 00145 if (!$this->compress || (strlen($directive) < $this->compress)) { 00146 $directive_disp = $directive; 00147 } else { 00148 $directive_disp = substr($directive, 0, $this->compress - 2) . '...'; 00149 $attr['title'] = $directive; 00150 } 00151 00152 $ret .= $this->element( 00153 'label', 00154 $directive_disp, 00155 // component printers must create an element with this id 00156 $attr 00157 ); 00158 if ($this->docURL) $ret .= $this->end('a'); 00159 $ret .= $this->end('th'); 00160 00161 $ret .= $this->start('td'); 00162 $def = $this->config->def->info["$ns.$directive"]; 00163 if (is_int($def)) { 00164 $allow_null = $def < 0; 00165 $type = abs($def); 00166 } else { 00167 $type = $def->type; 00168 $allow_null = isset($def->allow_null); 00169 } 00170 if (!isset($this->fields[$type])) $type = 0; // default 00171 $type_obj = $this->fields[$type]; 00172 if ($allow_null) { 00173 $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj); 00174 } 00175 $ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config)); 00176 $ret .= $this->end('td'); 00177 $ret .= $this->end('tr'); 00178 } 00179 $ret .= $this->end('tbody'); 00180 return $ret; 00181 } 00182 00183 } 00184 00188 class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer { 00192 protected $obj; 00196 public function __construct($obj) { 00197 parent::__construct(); 00198 $this->obj = $obj; 00199 } 00200 public function render($ns, $directive, $value, $name, $config) { 00201 if (is_array($config) && isset($config[0])) { 00202 $gen_config = $config[0]; 00203 $config = $config[1]; 00204 } else { 00205 $gen_config = $config; 00206 } 00207 $this->prepareGenerator($gen_config); 00208 00209 $ret = ''; 00210 $ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive")); 00211 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose')); 00212 $ret .= $this->text(' Null/Disabled'); 00213 $ret .= $this->end('label'); 00214 $attr = array( 00215 'type' => 'checkbox', 00216 'value' => '1', 00217 'class' => 'null-toggle', 00218 'name' => "$name"."[Null_$ns.$directive]", 00219 'id' => "$name:Null_$ns.$directive", 00220 'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!! 00221 ); 00222 if ($this->obj instanceof HTMLPurifier_Printer_ConfigForm_bool) { 00223 // modify inline javascript slightly 00224 $attr['onclick'] = "toggleWriteability('$name:Yes_$ns.$directive',checked);toggleWriteability('$name:No_$ns.$directive',checked)"; 00225 } 00226 if ($value === null) $attr['checked'] = 'checked'; 00227 $ret .= $this->elementEmpty('input', $attr); 00228 $ret .= $this->text(' or '); 00229 $ret .= $this->elementEmpty('br'); 00230 $ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config)); 00231 return $ret; 00232 } 00233 } 00234 00238 class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer { 00239 public $cols = 18; 00240 public $rows = 5; 00241 public function render($ns, $directive, $value, $name, $config) { 00242 if (is_array($config) && isset($config[0])) { 00243 $gen_config = $config[0]; 00244 $config = $config[1]; 00245 } else { 00246 $gen_config = $config; 00247 } 00248 $this->prepareGenerator($gen_config); 00249 // this should probably be split up a little 00250 $ret = ''; 00251 $def = $config->def->info["$ns.$directive"]; 00252 if (is_int($def)) { 00253 $type = abs($def); 00254 } else { 00255 $type = $def->type; 00256 } 00257 if (is_array($value)) { 00258 switch ($type) { 00259 case HTMLPurifier_VarParser::LOOKUP: 00260 $array = $value; 00261 $value = array(); 00262 foreach ($array as $val => $b) { 00263 $value[] = $val; 00264 } 00265 case HTMLPurifier_VarParser::ALIST: 00266 $value = implode(PHP_EOL, $value); 00267 break; 00268 case HTMLPurifier_VarParser::HASH: 00269 $nvalue = ''; 00270 foreach ($value as $i => $v) { 00271 $nvalue .= "$i:$v" . PHP_EOL; 00272 } 00273 $value = $nvalue; 00274 break; 00275 default: 00276 $value = ''; 00277 } 00278 } 00279 if ($type === HTMLPurifier_VarParser::MIXED) { 00280 return 'Not supported'; 00281 $value = serialize($value); 00282 } 00283 $attr = array( 00284 'name' => "$name"."[$ns.$directive]", 00285 'id' => "$name:$ns.$directive" 00286 ); 00287 if ($value === null) $attr['disabled'] = 'disabled'; 00288 if (isset($def->allowed)) { 00289 $ret .= $this->start('select', $attr); 00290 foreach ($def->allowed as $val => $b) { 00291 $attr = array(); 00292 if ($value == $val) $attr['selected'] = 'selected'; 00293 $ret .= $this->element('option', $val, $attr); 00294 } 00295 $ret .= $this->end('select'); 00296 } elseif ( 00297 $type === HTMLPurifier_VarParser::TEXT || 00298 $type === HTMLPurifier_VarParser::ITEXT || 00299 $type === HTMLPurifier_VarParser::ALIST || 00300 $type === HTMLPurifier_VarParser::HASH || 00301 $type === HTMLPurifier_VarParser::LOOKUP 00302 ) { 00303 $attr['cols'] = $this->cols; 00304 $attr['rows'] = $this->rows; 00305 $ret .= $this->start('textarea', $attr); 00306 $ret .= $this->text($value); 00307 $ret .= $this->end('textarea'); 00308 } else { 00309 $attr['value'] = $value; 00310 $attr['type'] = 'text'; 00311 $ret .= $this->elementEmpty('input', $attr); 00312 } 00313 return $ret; 00314 } 00315 } 00316 00320 class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer { 00321 public function render($ns, $directive, $value, $name, $config) { 00322 if (is_array($config) && isset($config[0])) { 00323 $gen_config = $config[0]; 00324 $config = $config[1]; 00325 } else { 00326 $gen_config = $config; 00327 } 00328 $this->prepareGenerator($gen_config); 00329 $ret = ''; 00330 $ret .= $this->start('div', array('id' => "$name:$ns.$directive")); 00331 00332 $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive")); 00333 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose')); 00334 $ret .= $this->text(' Yes'); 00335 $ret .= $this->end('label'); 00336 00337 $attr = array( 00338 'type' => 'radio', 00339 'name' => "$name"."[$ns.$directive]", 00340 'id' => "$name:Yes_$ns.$directive", 00341 'value' => '1' 00342 ); 00343 if ($value === true) $attr['checked'] = 'checked'; 00344 if ($value === null) $attr['disabled'] = 'disabled'; 00345 $ret .= $this->elementEmpty('input', $attr); 00346 00347 $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive")); 00348 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose')); 00349 $ret .= $this->text(' No'); 00350 $ret .= $this->end('label'); 00351 00352 $attr = array( 00353 'type' => 'radio', 00354 'name' => "$name"."[$ns.$directive]", 00355 'id' => "$name:No_$ns.$directive", 00356 'value' => '0' 00357 ); 00358 if ($value === false) $attr['checked'] = 'checked'; 00359 if ($value === null) $attr['disabled'] = 'disabled'; 00360 $ret .= $this->elementEmpty('input', $attr); 00361 00362 $ret .= $this->end('div'); 00363 00364 return $ret; 00365 } 00366 } 00367 00368 // vim: et sw=4 sts=4