PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/config/settings/config.class.php

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