[ Index ] |
|
Code source de DokuWiki 2006-11-06 |
1 <?php 2 /** 3 * Configuration Class and generic setting classes 4 * 5 * @author Chris Smith <chris@jalakai.co.uk> 6 * @author Ben Coburn <btcoburn@silicodon.net> 7 */ 8 9 if (!class_exists('configuration')) { 10 11 class configuration { 12 13 var $_name = 'conf'; // name of the config variable found in the files (overridden by $config['varname']) 14 var $_format = 'php'; // format of the config file, supported formats - php (overridden by $config['format']) 15 var $_heading = ''; // heading string written at top of config file - don't include comment indicators 16 var $_loaded = false; // set to true after configuration files are loaded 17 var $_metadata = array(); // holds metadata describing the settings 18 var $setting = array(); // array of setting objects 19 var $locked = false; // configuration is considered locked if it can't be updated 20 21 // filenames, these will be eval()'d prior to use so maintain any constants in output 22 var $_default_file = ''; 23 var $_local_file = ''; 24 var $_protected_file = ''; 25 26 /** 27 * constructor 28 */ 29 function configuration($datafile) { 30 global $conf; 31 32 if (!@file_exists($datafile)) { 33 msg('No configuration metadata found at - '.htmlspecialchars($datafile),-1); 34 return; 35 } 36 include($datafile); 37 38 if (isset($config['varname'])) $this->_name = $config['varname']; 39 if (isset($config['format'])) $this->_format = $config['format']; 40 if (isset($config['heading'])) $this->_heading = $config['heading']; 41 42 if (isset($file['default'])) $this->_default_file = $file['default']; 43 if (isset($file['local'])) $this->_local_file = $file['local']; 44 if (isset($file['protected'])) $this->_protected_file = $file['protected']; 45 46 $this->locked = $this->_is_locked(); 47 48 $this->_metadata = array_merge($meta, $this->get_plugintpl_metadata($conf['template'])); 49 50 $this->retrieve_settings(); 51 } 52 53 function retrieve_settings() { 54 global $conf; 55 $no_default_check = array('setting_fieldset', 'setting_undefined', 'setting_no_class'); 56 57 if (!$this->_loaded) { 58 $default = array_merge($this->_read_config($this->_default_file), $this->get_plugintpl_default($conf['template'])); 59 $local = $this->_read_config($this->_local_file); 60 $protected = $this->_read_config($this->_protected_file); 61 62 $keys = array_merge(array_keys($this->_metadata),array_keys($default), array_keys($local), array_keys($protected)); 63 $keys = array_unique($keys); 64 65 foreach ($keys as $key) { 66 if (isset($this->_metadata[$key])) { 67 $class = $this->_metadata[$key][0]; 68 $class = ($class && class_exists('setting_'.$class)) ? 'setting_'.$class : 'setting'; 69 if ($class=='setting') { 70 $this->setting[] = new setting_no_class($key,$param); 71 } 72 73 $param = $this->_metadata[$key]; 74 array_shift($param); 75 } else { 76 $class = 'setting_undefined'; 77 $param = NULL; 78 } 79 80 if (!in_array($class, $no_default_check) && !isset($default[$key])) { 81 $this->setting[] = new setting_no_default($key,$param); 82 } 83 84 $this->setting[$key] = new $class($key,$param); 85 $this->setting[$key]->initialize($default[$key],$local[$key],$protected[$key]); 86 } 87 88 $this->_loaded = true; 89 } 90 } 91 92 function save_settings($id, $header='', $backup=true) { 93 94 if ($this->locked) return false; 95 96 $file = eval('return '.$this->_local_file.';'); 97 98 // backup current file (remove any existing backup) 99 if (@file_exists($file) && $backup) { 100 if (@file_exists($file.'.bak')) @unlink($file.'.bak'); 101 if (!io_rename($file, $file.'.bak')) return false; 102 } 103 104 if (!$fh = @fopen($file, 'wb')) { 105 io_rename($file.'.bak', $file); // problem opening, restore the backup 106 return false; 107 } 108 109 if (empty($header)) $header = $this->_heading; 110 111 $out = $this->_out_header($id,$header); 112 113 foreach ($this->setting as $setting) { 114 $out .= $setting->out($this->_name, $this->_format); 115 } 116 117 $out .= $this->_out_footer(); 118 119 @fwrite($fh, $out); 120 fclose($fh); 121 return true; 122 } 123 124 /** 125 * return an array of config settings 126 */ 127 function _read_config($file) { 128 129 if (!$file) return array(); 130 131 $config = array(); 132 $file = eval('return '.$file.';'); 133 134 if ($this->_format == 'php') { 135 136 $contents = @php_strip_whitespace($file); 137 $pattern = '/\$'.$this->_name.'\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$'.$this->_name.'|@include|$))/'; 138 $matches=array(); 139 preg_match_all($pattern,$contents,$matches,PREG_SET_ORDER); 140 141 for ($i=0; $i<count($matches); $i++) { 142 143 // correct issues with the incoming data 144 // FIXME ... for now merge multi-dimensional array indices using ____ 145 $key = preg_replace('/.\]\[./',CM_KEYMARKER,$matches[$i][1]); 146 147 // remove quotes from quoted strings & unescape escaped data 148 $value = preg_replace('/^(\'|")(.*)(?<!\\\\)\1$/','$2',$matches[$i][2]); 149 $value = strtr($value, array('\\\\'=>'\\','\\\''=>'\'','\\"'=>'"')); 150 151 $config[$key] = $value; 152 } 153 } 154 155 return $config; 156 } 157 158 function _out_header($id, $header) { 159 $out = ''; 160 if ($this->_format == 'php') { 161 $out .= '<'.'?php'."\n". 162 "/*\n". 163 " * ".$header." \n". 164 " * Auto-generated by ".$id." plugin \n". 165 " * Run for user: ".$_SERVER['REMOTE_USER']."\n". 166 " * Date: ".date('r')."\n". 167 " */\n\n"; 168 } 169 170 return $out; 171 } 172 173 function _out_footer() { 174 $out = ''; 175 if ($this->_format == 'php') { 176 if ($this->_protected_file) { 177 $out .= "\n@include(".$this->_protected_file.");\n"; 178 } 179 $out .= "\n// end auto-generated content\n"; 180 } 181 182 return $out; 183 } 184 185 // configuration is considered locked if there is no local settings filename 186 // or the directory its in is not writable or the file exists and is not writable 187 function _is_locked() { 188 if (!$this->_local_file) return true; 189 190 $local = eval('return '.$this->_local_file.';'); 191 192 if (!is_writable(dirname($local))) return true; 193 if (@file_exists($local) && !is_writable($local)) return true; 194 195 return false; 196 } 197 198 /** 199 * not used ... conf's contents are an array! 200 * reduce any multidimensional settings to one dimension using CM_KEYMARKER 201 */ 202 function _flatten($conf,$prefix='') { 203 204 $out = array(); 205 206 foreach($conf as $key => $value) { 207 if (!is_array($value)) { 208 $out[$prefix.$key] = $value; 209 continue; 210 } 211 212 $tmp = $this->_flatten($value,$prefix.$key.CM_KEYMARKER); 213 $out = array_merge($out,$tmp); 214 } 215 216 return $out; 217 } 218 219 /** 220 * load metadata for plugin and template settings 221 */ 222 function get_plugintpl_metadata($tpl){ 223 $file = '/conf/metadata.php'; 224 $class = '/conf/settings.class.php'; 225 $metadata = array(); 226 227 if ($dh = opendir(DOKU_PLUGIN)) { 228 while (false !== ($plugin = readdir($dh))) { 229 if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue; 230 if (is_file(DOKU_PLUGIN.$plugin)) continue; 231 232 if (@file_exists(DOKU_PLUGIN.$plugin.$file)){ 233 $meta = array(); 234 @include(DOKU_PLUGIN.$plugin.$file); 235 @include(DOKU_PLUGIN.$plugin.$class); 236 if (!empty($meta)) { 237 $metadata['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.'plugin_settings_name'] = array('fieldset'); 238 } 239 foreach ($meta as $key => $value){ 240 if ($value[0]=='fieldset') { continue; } //plugins only get one fieldset 241 $metadata['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value; 242 } 243 } 244 } 245 closedir($dh); 246 } 247 248 // the same for the active template 249 if (@file_exists(DOKU_TPLINC.$file)){ 250 $meta = array(); 251 @include(DOKU_TPLINC.$file); 252 @include(DOKU_TPLINC.$class); 253 if (!empty($meta)) { 254 $metadata['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.'template_settings_name'] = array('fieldset'); 255 } 256 foreach ($meta as $key => $value){ 257 if ($value[0]=='fieldset') { continue; } //template only gets one fieldset 258 $metadata['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value; 259 } 260 } 261 262 return $metadata; 263 } 264 265 /** 266 * load default settings for plugins and templates 267 */ 268 function get_plugintpl_default($tpl){ 269 $file = '/conf/default.php'; 270 $default = array(); 271 272 if ($dh = opendir(DOKU_PLUGIN)) { 273 while (false !== ($plugin = readdir($dh))) { 274 if (@file_exists(DOKU_PLUGIN.$plugin.$file)){ 275 $conf = array(); 276 @include(DOKU_PLUGIN.$plugin.$file); 277 foreach ($conf as $key => $value){ 278 $default['plugin'.CM_KEYMARKER.$plugin.CM_KEYMARKER.$key] = $value; 279 } 280 } 281 } 282 closedir($dh); 283 } 284 285 // the same for the active template 286 if (@file_exists(DOKU_TPLINC.$file)){ 287 $conf = array(); 288 @include(DOKU_TPLINC.$file); 289 foreach ($conf as $key => $value){ 290 $default['tpl'.CM_KEYMARKER.$tpl.CM_KEYMARKER.$key] = $value; 291 } 292 } 293 294 return $default; 295 } 296 297 } 298 } 299 300 if (!class_exists('setting')) { 301 class setting { 302 303 var $_key = ''; 304 var $_default = NULL; 305 var $_local = NULL; 306 var $_protected = NULL; 307 308 var $_pattern = ''; 309 var $_error = false; // only used by those classes which error check 310 var $_input = NULL; // only used by those classes which error check 311 312 function setting($key, $params=NULL) { 313 $this->_key = $key; 314 315 if (is_array($params)) { 316 foreach($params as $property => $value) { 317 $this->$property = $value; 318 } 319 } 320 } 321 322 /** 323 * receives current values for the setting $key 324 */ 325 function initialize($default, $local, $protected) { 326 if (isset($default)) $this->_default = $default; 327 if (isset($local)) $this->_local = $local; 328 if (isset($protected)) $this->_protected = $protected; 329 } 330 331 /** 332 * update setting with user provided value $input 333 * if value fails error check, save it 334 * 335 * @return true if changed, false otherwise (incl. on error) 336 */ 337 function update($input) { 338 if (is_null($input)) return false; 339 if ($this->is_protected()) return false; 340 341 $value = is_null($this->_local) ? $this->_default : $this->_local; 342 if ($value == $input) return false; 343 344 if ($this->_pattern && !preg_match($this->_pattern,$input)) { 345 $this->_error = true; 346 $this->_input = $input; 347 return false; 348 } 349 350 $this->_local = $input; 351 return true; 352 } 353 354 /** 355 * @return array(string $label_html, string $input_html) 356 */ 357 function html(&$plugin, $echo=false) { 358 $value = ''; 359 $disable = ''; 360 361 if ($this->is_protected()) { 362 $value = $this->_protected; 363 $disable = 'disabled="disabled"'; 364 } else { 365 if ($echo && $this->_error) { 366 $value = $this->_input; 367 } else { 368 $value = is_null($this->_local) ? $this->_default : $this->_local; 369 } 370 } 371 372 $key = htmlspecialchars($this->_key); 373 $value = htmlspecialchars($value); 374 375 $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 376 $input = '<textarea rows="3" cols="40" id="config__'.$key.'" name="config['.$key.']" class="edit" '.$disable.'>'.$value.'</textarea>'; 377 return array($label,$input); 378 } 379 380 /** 381 * generate string to save setting value to file according to $fmt 382 */ 383 function out($var, $fmt='php') { 384 385 if ($this->is_protected()) return ''; 386 if (is_null($this->_local) || ($this->_default == $this->_local)) return ''; 387 388 $out = ''; 389 390 if ($fmt=='php') { 391 // translation string needs to be improved FIXME 392 $tr = array("\n"=>'\n', "\r"=>'\r', "\t"=>'\t', "\\" => '\\\\', "'" => '\\\''); 393 $tr = array("\\" => '\\\\', "'" => '\\\''); 394 395 $out = '$'.$var."['".$this->_out_key()."'] = '".strtr($this->_local, $tr)."';\n"; 396 } 397 398 return $out; 399 } 400 401 function prompt(&$plugin) { 402 $prompt = $plugin->getLang($this->_key); 403 if (!$prompt) $prompt = htmlspecialchars(str_replace(array('____','_'),' ',$this->_key)); 404 return $prompt; 405 } 406 407 function is_protected() { return !is_null($this->_protected); } 408 function is_default() { return !$this->is_protected() && is_null($this->_local); } 409 function error() { return $this->_error; } 410 411 function _out_key() { return str_replace(CM_KEYMARKER,"']['",$this->_key); } 412 } 413 } 414 415 if (!class_exists('setting_string')) { 416 class setting_string extends setting { 417 function html(&$plugin, $echo=false) { 418 $value = ''; 419 $disable = ''; 420 421 if ($this->is_protected()) { 422 $value = $this->_protected; 423 $disable = 'disabled="disabled"'; 424 } else { 425 if ($echo && $this->_error) { 426 $value = $this->_input; 427 } else { 428 $value = is_null($this->_local) ? $this->_default : $this->_local; 429 } 430 } 431 432 $key = htmlspecialchars($this->_key); 433 $value = htmlspecialchars($value); 434 435 $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 436 $input = '<input id="config__'.$key.'" name="config['.$key.']" type="text" class="edit" value="'.$value.'" '.$disable.'/>'; 437 return array($label,$input); 438 } 439 } 440 } 441 442 if (!class_exists('setting_password')) { 443 class setting_password extends setting_string { 444 445 function update($input) { 446 if ($this->is_protected()) return false; 447 if (!$input) return false; 448 449 if ($this->_pattern && !preg_match($this->_pattern,$input)) { 450 $this->_error = true; 451 $this->_input = $input; 452 return false; 453 } 454 455 $this->_local = $input; 456 return true; 457 } 458 459 function html(&$plugin, $echo=false) { 460 461 $value = ''; 462 $disable = $this->is_protected() ? 'disabled="disabled"' : ''; 463 464 $key = htmlspecialchars($this->_key); 465 466 $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 467 $input = '<input id="config__'.$key.'" name="config['.$key.']" type="password" class="edit" value="" '.$disable.'/>'; 468 return array($label,$input); 469 } 470 } 471 } 472 473 if (!class_exists('setting_email')) { 474 class setting_email extends setting_string { 475 var $_pattern = '#^\s*(([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)(,\s*([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+))*)?\s*$#i'; 476 } 477 } 478 479 if (!class_exists('setting_numeric')) { 480 class setting_numeric extends setting_string { 481 // This allows for many PHP syntax errors... 482 // var $_pattern = '/^[-+\/*0-9 ]*$/'; 483 // much more restrictive, but should eliminate syntax errors. 484 var $_pattern = '/^[-]?[0-9]+(?:[-+*][0-9]+)*$/'; 485 //FIXME - make the numeric error checking better. 486 487 function out($var, $fmt='php') { 488 489 if ($this->is_protected()) return ''; 490 if (is_null($this->_local) || ($this->_default == $this->_local)) return ''; 491 492 $out = ''; 493 494 if ($fmt=='php') { 495 $out .= '$'.$var."['".$this->_out_key()."'] = ".$this->_local.";\n"; 496 } 497 498 return $out; 499 } 500 } 501 } 502 503 if (!class_exists('setting_onoff')) { 504 class setting_onoff extends setting_numeric { 505 506 function html(&$plugin) { 507 $value = ''; 508 $disable = ''; 509 510 if ($this->is_protected()) { 511 $value = $this->_protected; 512 $disable = ' disabled="disabled"'; 513 } else { 514 $value = is_null($this->_local) ? $this->_default : $this->_local; 515 } 516 517 $key = htmlspecialchars($this->_key); 518 $checked = ($value) ? ' checked="checked"' : ''; 519 520 $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 521 $input = '<div class="input"><input id="config__'.$key.'" name="config['.$key.']" type="checkbox" class="checkbox" value="1"'.$checked.$disable.'/></div>'; 522 return array($label,$input); 523 } 524 525 function update($input) { 526 if ($this->is_protected()) return false; 527 528 $input = ($input) ? 1 : 0; 529 $value = is_null($this->_local) ? $this->_default : $this->_local; 530 if ($value == $input) return false; 531 532 $this->_local = $input; 533 return true; 534 } 535 } 536 } 537 538 if (!class_exists('setting_multichoice')) { 539 class setting_multichoice extends setting_string { 540 var $_choices = array(); 541 542 function html(&$plugin) { 543 $value = ''; 544 $disable = ''; 545 $nochoice = ''; 546 547 if ($this->is_protected()) { 548 $value = $this->_protected; 549 $disable = ' disabled="disabled"'; 550 } else { 551 $value = is_null($this->_local) ? $this->_default : $this->_local; 552 } 553 554 // ensure current value is included 555 if (!in_array($value, $this->_choices)) { 556 $this->_choices[] = $value; 557 } 558 // disable if no other choices 559 if (!$this->is_protected() && count($this->_choices) <= 1) { 560 $disable = ' disabled="disabled"'; 561 $nochoice = $plugin->getLang('nochoice'); 562 } 563 564 $key = htmlspecialchars($this->_key); 565 566 $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 567 568 $input = "<div class=\"input\">\n"; 569 $input .= '<select class="edit" id="config__'.$key.'" name="config['.$key.']"'.$disable.'>'."\n"; 570 foreach ($this->_choices as $choice) { 571 $selected = ($value == $choice) ? ' selected="selected"' : ''; 572 $option = $plugin->getLang($this->_key.'_o_'.$choice); 573 if (!$option) $option = $choice; 574 575 $choice = htmlspecialchars($choice); 576 $option = htmlspecialchars($option); 577 $input .= ' <option value="'.$choice.'"'.$selected.' >'.$option.'</option>'."\n"; 578 } 579 $input .= "</select> $nochoice \n"; 580 $input .= "</div>\n"; 581 582 return array($label,$input); 583 } 584 585 function update($input) { 586 if (is_null($input)) return false; 587 if ($this->is_protected()) return false; 588 589 $value = is_null($this->_local) ? $this->_default : $this->_local; 590 if ($value == $input) return false; 591 592 if (!in_array($input, $this->_choices)) return false; 593 594 $this->_local = $input; 595 return true; 596 } 597 } 598 } 599 600 601 if (!class_exists('setting_dirchoice')) { 602 class setting_dirchoice extends setting_multichoice { 603 604 var $_dir = ''; 605 606 function initialize($default,$local,$protected) { 607 608 // populate $this->_choices with a list of available templates 609 $list = array(); 610 611 if ($dh = @opendir($this->_dir)) { 612 while (false !== ($entry = readdir($dh))) { 613 if ($entry == '.' || $entry == '..') continue; 614 615 $file = (is_link($this->_dir.$entry)) ? readlink($this->_dir.$entry) : $entry; 616 if (is_dir($this->_dir.$file)) $list[] = $entry; 617 } 618 closedir($dh); 619 } 620 sort($list); 621 $this->_choices = $list; 622 623 parent::initialize($default,$local,$protected); 624 } 625 } 626 } 627 628 629 if (!class_exists('setting_hidden')) { 630 class setting_hidden extends setting { 631 // Used to explicitly ignore a setting in the configuration manager. 632 } 633 } 634 635 if (!class_exists('setting_fieldset')) { 636 class setting_fieldset extends setting { 637 // A do-nothing class used to detect the 'fieldset' type. 638 // Used to start a new settings "display-group". 639 } 640 } 641 642 if (!class_exists('setting_undefined')) { 643 class setting_undefined extends setting_hidden { 644 // A do-nothing class used to detect settings with no metadata entry. 645 // Used internaly to hide undefined settings, and generate the undefined settings list. 646 } 647 } 648 649 if (!class_exists('setting_no_class')) { 650 class setting_no_class extends setting_undefined { 651 // A do-nothing class used to detect settings with a missing setting class. 652 // Used internaly to hide undefined settings, and generate the undefined settings list. 653 } 654 } 655 656 if (!class_exists('setting_no_default')) { 657 class setting_no_default extends setting_undefined { 658 // A do-nothing class used to detect settings with no default value. 659 // Used internaly to hide undefined settings, and generate the undefined settings list. 660 } 661 } 662 663 if (!class_exists('setting_multicheckbox')) { 664 class setting_multicheckbox extends setting_string { 665 666 var $_choices = array(); 667 var $_combine = array(); 668 669 function update($input) { 670 if ($this->is_protected()) return false; 671 672 // split any combined values + convert from array to comma separated string 673 $input = ($input) ? $input : array(); 674 $input = $this->_array2str($input); 675 676 $value = is_null($this->_local) ? $this->_default : $this->_local; 677 if ($value == $input) return false; 678 679 if ($this->_pattern && !preg_match($this->_pattern,$input)) { 680 $this->_error = true; 681 $this->_input = $input; 682 return false; 683 } 684 685 $this->_local = $input; 686 return true; 687 } 688 689 function html(&$plugin, $echo=false) { 690 691 $value = ''; 692 $disable = ''; 693 694 if ($this->is_protected()) { 695 $value = $this->_protected; 696 $disable = 'disabled="disabled"'; 697 } else { 698 if ($echo && $this->_error) { 699 $value = $this->_input; 700 } else { 701 $value = is_null($this->_local) ? $this->_default : $this->_local; 702 } 703 } 704 705 $key = htmlspecialchars($this->_key); 706 707 // convert from comma separated list into array + combine complimentary actions 708 $value = $this->_str2array($value); 709 $default = $this->_str2array($this->_default); 710 711 $input = ''; 712 foreach ($this->_choices as $choice) { 713 $idx = array_search($choice, $value); 714 $idx_default = array_search($choice,$default); 715 716 $checked = ($idx !== false) ? 'checked="checked"' : ''; 717 718 // ideally this would be handled using a second class of "default", however IE6 does not 719 // correctly support CSS selectors referencing multiple class names on the same element 720 // (e.g. .default.selection). 721 $class = (($idx !== false) == (false !== $idx_default)) ? " selectiondefault" : ""; 722 723 $prompt = ($plugin->getLang($this->_key.'_'.$choice) ? 724 $plugin->getLang($this->_key.'_'.$choice) : htmlspecialchars($choice)); 725 726 $input .= '<div class="selection'.$class.'">'."\n"; 727 $input .= '<label for="config__'.$key.'_'.$choice.'">'.$prompt."</label>\n"; 728 $input .= '<input id="config__'.$key.'_'.$choice.'" name="config['.$key.'][]" type="checkbox" class="checkbox" value="'.$choice.'" '.$disable.' '.$checked."/>\n"; 729 $input .= "</div>\n"; 730 731 // remove this action from the disabledactions array 732 if ($idx !== false) unset($value[$idx]); 733 if ($idx_default !== false) unset($default[$idx_default]); 734 } 735 736 // handle any remaining values 737 $other = join(',',$value); 738 739 $class = (count($default == count($value)) && (count($value) == count(array_intersect($value,$default)))) ? 740 " selectiondefault" : ""; 741 742 $input .= '<div class="other'.$class.'">'."\n"; 743 $input .= '<label for="config__'.$key.'_other">'.$plugin->getLang($key.'_other')."</label>\n"; 744 $input .= '<input id="config__'.$key.'_other" name="config['.$key.'][other]" type="text" class="edit" value="'.htmlspecialchars($other).'" '.$disable." />\n"; 745 $input .= "</div>\n"; 746 747 // $label = '<label for="config__'.$key.'">'.$this->prompt($plugin).'</label>'; 748 $label = $this->prompt($plugin); 749 return array($label,$input); 750 } 751 752 /** 753 * convert comma separated list to an array and combine any complimentary values 754 */ 755 function _str2array($str) { 756 $array = explode(',',$str); 757 758 if (!empty($this->_combine)) { 759 foreach ($this->_combine as $key => $combinators) { 760 $idx = array(); 761 foreach ($combinators as $val) { 762 if (($idx[] = array_search($val, $array)) === false) break; 763 } 764 765 if (count($idx) && $idx[count($idx)-1] !== false) { 766 foreach ($idx as $i) unset($array[$i]); 767 $array[] = $key; 768 } 769 } 770 } 771 772 return $array; 773 } 774 775 /** 776 * convert array of values + other back to a comma separated list, incl. splitting any combined values 777 */ 778 function _array2str($input) { 779 780 // handle other 781 $other = trim($input['other']); 782 $other = !empty($other) ? explode(',',str_replace(' ','',$input['other'])) : array(); 783 unset($input['other']); 784 785 $array = array_unique(array_merge($input, $other)); 786 787 // deconstruct any combinations 788 if (!empty($this->_combine)) { 789 foreach ($this->_combine as $key => $combinators) { 790 791 $idx = array_search($key,$array); 792 if ($idx !== false) { 793 unset($array[$idx]); 794 $array = array_merge($array, $combinators); 795 } 796 } 797 } 798 799 return join(',',array_unique($array)); 800 } 801 } 802 } 803 804 805 /** 806 * Provide php_strip_whitespace (php5 function) functionality 807 * 808 * @author Chris Smith <chris@jalakai.co.uk> 809 */ 810 if (!function_exists('php_strip_whitespace')) { 811 812 if (function_exists('token_get_all')) { 813 814 if (!defined('T_ML_COMMENT')) { 815 define('T_ML_COMMENT', T_COMMENT); 816 } else { 817 define('T_DOC_COMMENT', T_ML_COMMENT); 818 } 819 820 /** 821 * modified from original 822 * source Google Groups, php.general, by David Otton 823 */ 824 function php_strip_whitespace($file) { 825 if (!@is_readable($file)) return ''; 826 827 $in = join('',@file($file)); 828 $out = ''; 829 830 $tokens = token_get_all($in); 831 832 foreach ($tokens as $token) { 833 if (is_string ($token)) { 834 $out .= $token; 835 } else { 836 list ($id, $text) = $token; 837 switch ($id) { 838 case T_COMMENT : // fall thru 839 case T_ML_COMMENT : // fall thru 840 case T_DOC_COMMENT : // fall thru 841 case T_WHITESPACE : 842 break; 843 default : $out .= $text; break; 844 } 845 } 846 } 847 return ($out); 848 } 849 850 } else { 851 852 function is_whitespace($c) { return (strpos("\t\n\r ",$c) !== false); } 853 function is_quote($c) { return (strpos("\"'",$c) !== false); } 854 function is_escaped($s,$i) { 855 $idx = $i-1; 856 while(($idx>=0) && ($s{$idx} == '\\')) $idx--; 857 return (($i - $idx + 1) % 2); 858 } 859 860 function is_commentopen($str, $i) { 861 if ($str{$i} == '#') return "\n"; 862 if ($str{$i} == '/') { 863 if ($str{$i+1} == '/') return "\n"; 864 if ($str{$i+1} == '*') return "*/"; 865 } 866 867 return false; 868 } 869 870 function php_strip_whitespace($file) { 871 872 if (!@is_readable($file)) return ''; 873 874 $contents = join('',@file($file)); 875 $out = ''; 876 877 $state = 0; 878 for ($i=0; $i<strlen($contents); $i++) { 879 if (!$state && is_whitespace($contents{$i})) continue; 880 881 if (!$state && ($c_close = is_commentopen($contents, $i))) { 882 $c_open_len = ($contents{$i} == '/') ? 2 : 1; 883 $i = strpos($contents, $c_close, $i+$c_open_len)+strlen($c_close)-1; 884 continue; 885 } 886 887 $out .= $contents{$i}; 888 if (is_quote($contents{$i})) { 889 if (($state == $contents{$i}) && !is_escaped($contents, $i)) { $state = 0; continue; } 890 if (!$state) {$state = $contents{$i}; continue; } 891 } 892 } 893 894 return $out; 895 } 896 } 897 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 20:47:31 2007 | par Balluche grâce à PHPXref 0.7 |