options.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Copyright (c) 1999-2005 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Functions needed to display the options pages.
  9. *
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /**********************************************/
  14. /* Define constants used in the options code. */
  15. /**********************************************/
  16. /* Define constants for the various option types. */
  17. define('SMOPT_TYPE_STRING', 0);
  18. define('SMOPT_TYPE_STRLIST', 1);
  19. define('SMOPT_TYPE_TEXTAREA', 2);
  20. define('SMOPT_TYPE_INTEGER', 3);
  21. define('SMOPT_TYPE_FLOAT', 4);
  22. define('SMOPT_TYPE_BOOLEAN', 5);
  23. define('SMOPT_TYPE_HIDDEN', 6);
  24. define('SMOPT_TYPE_COMMENT', 7);
  25. define('SMOPT_TYPE_FLDRLIST', 8);
  26. /* Define constants for the options refresh levels. */
  27. define('SMOPT_REFRESH_NONE', 0);
  28. define('SMOPT_REFRESH_FOLDERLIST', 1);
  29. define('SMOPT_REFRESH_ALL', 2);
  30. /* Define constants for the options size. */
  31. define('SMOPT_SIZE_TINY', 0);
  32. define('SMOPT_SIZE_SMALL', 1);
  33. define('SMOPT_SIZE_MEDIUM', 2);
  34. define('SMOPT_SIZE_LARGE', 3);
  35. define('SMOPT_SIZE_HUGE', 4);
  36. define('SMOPT_SIZE_NORMAL', 5);
  37. define('SMOPT_SAVE_DEFAULT', 'save_option');
  38. define('SMOPT_SAVE_NOOP', 'save_option_noop');
  39. /**
  40. * SquirrelOption: An option for SquirrelMail.
  41. *
  42. * @package squirrelmail
  43. * @subpackage prefs
  44. */
  45. class SquirrelOption {
  46. /**
  47. * The name of this setting
  48. * @var string
  49. */
  50. var $name;
  51. /**
  52. * The text that prefaces setting on the preferences page
  53. * @var string
  54. */
  55. var $caption;
  56. /**
  57. * The type of INPUT element
  58. *
  59. * See SMOPT_TYPE_* defines
  60. * @var integer
  61. */
  62. var $type;
  63. /**
  64. * Indicates if a link should be shown to refresh part
  65. * or all of the window
  66. *
  67. * See SMOPT_REFRESH_* defines
  68. * @var integer
  69. */
  70. var $refresh_level;
  71. /**
  72. * Specifies the size of certain input items
  73. *
  74. * See SMOPT_SIZE_* defines
  75. * @var integer
  76. */
  77. var $size;
  78. /**
  79. * Text that follows a text input or
  80. * select list input on the preferences page
  81. *
  82. * useful for indicating units, meanings of special values, etc.
  83. * @var string
  84. */
  85. var $trailing_text;
  86. /**
  87. * text displayed to the user
  88. *
  89. * Used with SMOPT_TYPE_COMMENT options
  90. * @var string
  91. */
  92. var $comment;
  93. /**
  94. * additional javascript or other code added to the user input
  95. * @var string
  96. */
  97. var $script;
  98. /**
  99. * script (usually Javascript) that will be placed after (outside of)
  100. * the INPUT tag
  101. * @var string
  102. */
  103. var $post_script;
  104. /**
  105. * The name of the Save Function for this option.
  106. * @var string
  107. */
  108. var $save_function;
  109. /* The various 'values' for this options. */
  110. /**
  111. * default/preselected value for this option
  112. * @var mixed
  113. */
  114. var $value;
  115. /**
  116. * new option value
  117. * @var mixed
  118. */
  119. var $new_value;
  120. /**
  121. * associative array, where each key is an actual input value
  122. * and the corresponding value is what is displayed to the user
  123. * for that list item in the drop-down list
  124. * @var array
  125. */
  126. var $possible_values;
  127. /**
  128. * disables html sanitizing.
  129. *
  130. * WARNING - don't use it, if user input is possible in option
  131. * or use own sanitizing functions. Currently works only with
  132. * SMOPT_TYPE_STRLIST.
  133. * @var bool
  134. */
  135. var $htmlencoded=false;
  136. /**
  137. * Constructor function
  138. * @param string $name
  139. * @param string $caption
  140. * @param integer $type
  141. * @param integer $refresh_level
  142. * @param mixed $initial_value
  143. * @param array $possible_values
  144. * @param bool $htmlencoded
  145. */
  146. function SquirrelOption
  147. ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  148. /* Set the basic stuff. */
  149. $this->name = $name;
  150. $this->caption = $caption;
  151. $this->type = $type;
  152. $this->refresh_level = $refresh_level;
  153. $this->possible_values = $possible_values;
  154. $this->htmlencoded = $htmlencoded;
  155. $this->size = SMOPT_SIZE_MEDIUM;
  156. $this->trailing_text = '';
  157. $this->comment = '';
  158. $this->script = '';
  159. $this->post_script = '';
  160. /* Check for a current value. */
  161. if (!empty($initial_value)) {
  162. $this->value = $initial_value;
  163. } else if (isset($GLOBALS[$name])) {
  164. $this->value = $GLOBALS[$name];
  165. } else {
  166. $this->value = '';
  167. }
  168. /* Check for a new value. */
  169. if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  170. $this->new_value = '';
  171. }
  172. /* Set the default save function. */
  173. if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
  174. $this->save_function = SMOPT_SAVE_DEFAULT;
  175. } else {
  176. $this->save_function = SMOPT_SAVE_NOOP;
  177. }
  178. }
  179. /**
  180. * Set the value for this option.
  181. * @param mixed $value
  182. */
  183. function setValue($value) {
  184. $this->value = $value;
  185. }
  186. /**
  187. * Set the new value for this option.
  188. * @param mixed $new_value
  189. */
  190. function setNewValue($new_value) {
  191. $this->new_value = $new_value;
  192. }
  193. /**
  194. * Set the size for this option.
  195. * @param integer $size
  196. */
  197. function setSize($size) {
  198. $this->size = $size;
  199. }
  200. /**
  201. * Set the trailing_text for this option.
  202. * @param string $trailing_text
  203. */
  204. function setTrailingText($trailing_text) {
  205. $this->trailing_text = $trailing_text;
  206. }
  207. /**
  208. * Set the comment for this option.
  209. * @param string $comment
  210. */
  211. function setComment($comment) {
  212. $this->comment = $comment;
  213. }
  214. /**
  215. * Set the script for this option.
  216. * @param string $script
  217. */
  218. function setScript($script) {
  219. $this->script = $script;
  220. }
  221. /**
  222. * Set the "post script" for this option.
  223. * @param string $post_script
  224. */
  225. function setPostScript($post_script) {
  226. $this->post_script = $post_script;
  227. }
  228. /**
  229. * Set the save function for this option.
  230. * @param string $save_function
  231. */
  232. function setSaveFunction($save_function) {
  233. $this->save_function = $save_function;
  234. }
  235. /**
  236. * Creates fields on option pages according to option type
  237. *
  238. * Function that calls other createWidget* functions.
  239. * @return string html formated option field
  240. */
  241. function createHTMLWidget() {
  242. global $color;
  243. // Use new value if available
  244. if (!empty($this->new_value)) {
  245. $tempValue = $this->value;
  246. $this->value = $this->new_value;
  247. }
  248. /* Get the widget for this option type. */
  249. switch ($this->type) {
  250. case SMOPT_TYPE_STRING:
  251. $result = $this->createWidget_String();
  252. break;
  253. case SMOPT_TYPE_STRLIST:
  254. $result = $this->createWidget_StrList();
  255. break;
  256. case SMOPT_TYPE_TEXTAREA:
  257. $result = $this->createWidget_TextArea();
  258. break;
  259. case SMOPT_TYPE_INTEGER:
  260. $result = $this->createWidget_Integer();
  261. break;
  262. case SMOPT_TYPE_FLOAT:
  263. $result = $this->createWidget_Float();
  264. break;
  265. case SMOPT_TYPE_BOOLEAN:
  266. $result = $this->createWidget_Boolean();
  267. break;
  268. case SMOPT_TYPE_HIDDEN:
  269. $result = $this->createWidget_Hidden();
  270. break;
  271. case SMOPT_TYPE_COMMENT:
  272. $result = $this->createWidget_Comment();
  273. break;
  274. case SMOPT_TYPE_FLDRLIST:
  275. $result = $this->createWidget_FolderList();
  276. break;
  277. default:
  278. $result = '<font color="' . $color[2] . '">'
  279. . sprintf(_("Option Type '%s' Not Found"), $this->type)
  280. . '</font>';
  281. }
  282. /* Add the "post script" for this option. */
  283. $result .= $this->post_script;
  284. // put correct value back if need be
  285. if (!empty($this->new_value)) {
  286. $this->value = $tempValue;
  287. }
  288. /* Now, return the created widget. */
  289. return ($result);
  290. }
  291. /**
  292. * Create string field
  293. * @return string html formated option field
  294. */
  295. function createWidget_String() {
  296. switch ($this->size) {
  297. case SMOPT_SIZE_TINY:
  298. $width = 5;
  299. break;
  300. case SMOPT_SIZE_SMALL:
  301. $width = 12;
  302. break;
  303. case SMOPT_SIZE_LARGE:
  304. $width = 38;
  305. break;
  306. case SMOPT_SIZE_HUGE:
  307. $width = 50;
  308. break;
  309. case SMOPT_SIZE_NORMAL:
  310. default:
  311. $width = 25;
  312. }
  313. $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
  314. htmlspecialchars($this->value) .
  315. "\" size=\"$width\" $this->script />$this->trailing_text\n";
  316. return ($result);
  317. }
  318. /**
  319. * Create selection box
  320. * @return string html formated selection box
  321. */
  322. function createWidget_StrList() {
  323. /* Begin the select tag. */
  324. $result = "<select name=\"new_$this->name\" $this->script>\n";
  325. /* Add each possible value to the select list. */
  326. foreach ($this->possible_values as $real_value => $disp_value) {
  327. /* Start the next new option string. */
  328. $new_option = '<option value="' .
  329. ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"';
  330. /* If this value is the current value, select it. */
  331. if ($real_value == $this->value) {
  332. $new_option .= ' selected="selected"';
  333. }
  334. /* Add the display value to our option string. */
  335. $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
  336. /* And add the new option string to our select tag. */
  337. $result .= $new_option;
  338. }
  339. /* Close the select tag and return our happy result. */
  340. $result .= "</select>$this->trailing_text\n";
  341. return ($result);
  342. }
  343. /**
  344. * Create folder selection box
  345. * @return string html formated selection box
  346. */
  347. function createWidget_FolderList() {
  348. $selected = array(strtolower($this->value));
  349. /* Begin the select tag. */
  350. $result = "<select name=\"new_$this->name\" $this->script>\n";
  351. /* Add each possible value to the select list. */
  352. foreach ($this->possible_values as $real_value => $disp_value) {
  353. if ( is_array($disp_value) ) {
  354. /* For folder list, we passed in the array of boxes.. */
  355. $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
  356. } else {
  357. /* Start the next new option string. */
  358. $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
  359. /* If this value is the current value, select it. */
  360. if ($real_value == $this->value) {
  361. $new_option .= ' selected="selected"';
  362. }
  363. /* Add the display value to our option string. */
  364. $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
  365. }
  366. /* And add the new option string to our select tag. */
  367. $result .= $new_option;
  368. }
  369. /* Close the select tag and return our happy result. */
  370. $result .= "</select>\n";
  371. return ($result);
  372. }
  373. /**
  374. * Creates textarea
  375. * @return string html formated textarea field
  376. */
  377. function createWidget_TextArea() {
  378. switch ($this->size) {
  379. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  380. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  381. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  382. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  383. case SMOPT_SIZE_NORMAL:
  384. default: $rows = 5; $cols = 50;
  385. }
  386. $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
  387. . "cols=\"$cols\" $this->script>"
  388. . htmlspecialchars($this->value) . "</textarea>\n";
  389. return ($result);
  390. }
  391. /**
  392. * Creates field for integer
  393. *
  394. * Difference from createWidget_String is visible only when javascript is enabled
  395. * @return string html formated option field
  396. */
  397. function createWidget_Integer() {
  398. global $javascript_on;
  399. // add onChange javascript handler to a regular string widget
  400. // which will strip out all non-numeric chars
  401. if ($javascript_on)
  402. return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
  403. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  404. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  405. . 'this.value=newVal;" />', $this->createWidget_String());
  406. else
  407. return $this->createWidget_String();
  408. }
  409. /**
  410. * Creates field for floating number
  411. * Difference from createWidget_String is visible only when javascript is enabled
  412. * @return string html formated option field
  413. */
  414. function createWidget_Float() {
  415. global $javascript_on;
  416. // add onChange javascript handler to a regular string widget
  417. // which will strip out all non-numeric (period also OK) chars
  418. if ($javascript_on)
  419. return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
  420. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  421. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  422. . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
  423. , $this->createWidget_String());
  424. else
  425. return $this->createWidget_String();
  426. }
  427. /**
  428. * Creates radio field (yes/no)
  429. * @return string html formated radio field
  430. */
  431. function createWidget_Boolean() {
  432. /* Do the whole current value thing. */
  433. if ($this->value != SMPREF_NO) {
  434. $yes_chk = ' checked="checked"';
  435. $no_chk = '';
  436. } else {
  437. $yes_chk = '';
  438. $no_chk = ' checked="checked"';
  439. }
  440. /* Build the yes choice. */
  441. $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
  442. . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
  443. . $yes_chk . ' ' . $this->script . ' />&nbsp;'
  444. . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
  445. /* Build the no choice. */
  446. $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
  447. . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
  448. . $no_chk . ' ' . $this->script . ' />&nbsp;'
  449. . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
  450. /* Build and return the combined "boolean widget". */
  451. $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
  452. return ($result);
  453. }
  454. /**
  455. * Creates hidden field
  456. * @return string html formated hidden input field
  457. */
  458. function createWidget_Hidden() {
  459. $result = '<input type="hidden" name="new_' . $this->name
  460. . '" value="' . htmlspecialchars($this->value)
  461. . '" ' . $this->script . ' />';
  462. return ($result);
  463. }
  464. /**
  465. * Creates comment
  466. * @return string comment
  467. */
  468. function createWidget_Comment() {
  469. $result = $this->comment;
  470. return ($result);
  471. }
  472. /**
  473. *
  474. */
  475. function save() {
  476. $function = $this->save_function;
  477. $function($this);
  478. }
  479. /**
  480. *
  481. */
  482. function changed() {
  483. return ($this->value != $this->new_value);
  484. }
  485. } /* End of SquirrelOption class*/
  486. /**
  487. * Saves option
  488. * @param object $option object that holds option name and new_value
  489. */
  490. function save_option($option) {
  491. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  492. /* Can't save the pref if we don't have the username */
  493. return;
  494. }
  495. global $data_dir;
  496. setPref($data_dir, $username, $option->name, $option->new_value);
  497. }
  498. /**
  499. * save function that does not save
  500. * @param object $option
  501. */
  502. function save_option_noop($option) {
  503. /* Do nothing here... */
  504. }
  505. /**
  506. * Create hidden 'optpage' input field with value set by argument
  507. * @param string $optpage identification of option page
  508. * @return string html formated hidden input field
  509. */
  510. function create_optpage_element($optpage) {
  511. return create_hidden_element('optpage', $optpage);
  512. }
  513. /**
  514. * Create hidden 'optmode' input field with value set by argument
  515. * @param string $optmode
  516. * @return string html formated hidden input field
  517. */
  518. function create_optmode_element($optmode) {
  519. return create_hidden_element('optmode', $optmode);
  520. }
  521. /**
  522. * Create hidden field.
  523. * @param string $name field name
  524. * @param string $value field value
  525. * @return string html formated hidden input field
  526. */
  527. function create_hidden_element($name, $value) {
  528. $result = '<input type="hidden" '
  529. . 'name="' . $name . '" '
  530. . 'value="' . htmlspecialchars($value) . '" />';
  531. return ($result);
  532. }
  533. /**
  534. * @param array $optgrps
  535. * @param array $optvals
  536. * @return array
  537. */
  538. function create_option_groups($optgrps, $optvals) {
  539. /* Build a simple array with which to start. */
  540. $result = array();
  541. /* Create option group for each option group name. */
  542. foreach ($optgrps as $grpkey => $grpname) {
  543. $result[$grpkey] = array();
  544. $result[$grpkey]['name'] = $grpname;
  545. $result[$grpkey]['options'] = array();
  546. }
  547. /* Create a new SquirrelOption for each set of option values. */
  548. foreach ($optvals as $grpkey => $grpopts) {
  549. foreach ($grpopts as $optset) {
  550. /* Create a new option with all values given. */
  551. $next_option = new SquirrelOption(
  552. $optset['name'],
  553. $optset['caption'],
  554. $optset['type'],
  555. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  556. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  557. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  558. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  559. );
  560. /* If provided, set the size for this option. */
  561. if (isset($optset['size'])) {
  562. $next_option->setSize($optset['size']);
  563. }
  564. /* If provided, set the trailing_text for this option. */
  565. if (isset($optset['trailing_text'])) {
  566. $next_option->setTrailingText($optset['trailing_text']);
  567. }
  568. /* If provided, set the comment for this option. */
  569. if (isset($optset['comment'])) {
  570. $next_option->setComment($optset['comment']);
  571. }
  572. /* If provided, set the save function for this option. */
  573. if (isset($optset['save'])) {
  574. $next_option->setSaveFunction($optset['save']);
  575. }
  576. /* If provided, set the script for this option. */
  577. if (isset($optset['script'])) {
  578. $next_option->setScript($optset['script']);
  579. }
  580. /* If provided, set the "post script" for this option. */
  581. if (isset($optset['post_script'])) {
  582. $next_option->setPostScript($optset['post_script']);
  583. }
  584. /* Add this option to the option array. */
  585. $result[$grpkey]['options'][] = $next_option;
  586. }
  587. }
  588. /* Return our resulting array. */
  589. return ($result);
  590. }
  591. /**
  592. * @param array $option_groups
  593. */
  594. function print_option_groups($option_groups) {
  595. /* Print each option group. */
  596. foreach ($option_groups as $next_optgrp) {
  597. /* If it is not blank, print the name for this option group. */
  598. if ($next_optgrp['name'] != '') {
  599. echo html_tag( 'tr', "\n".
  600. html_tag( 'td',
  601. '<b>' . $next_optgrp['name'] . '</b>' ,
  602. 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
  603. ) ."\n";
  604. }
  605. /* Print each option in this option group. */
  606. foreach ($next_optgrp['options'] as $option) {
  607. if ($option->type != SMOPT_TYPE_HIDDEN) {
  608. echo html_tag( 'tr', "\n".
  609. html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
  610. html_tag( 'td', $option->createHTMLWidget(), 'left' )
  611. ) ."\n";
  612. } else {
  613. echo $option->createHTMLWidget();
  614. }
  615. }
  616. /* Print an empty row after this option group. */
  617. echo html_tag( 'tr',
  618. html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
  619. ) . "\n";
  620. }
  621. }
  622. /**
  623. * Create submit button inside table row.
  624. * @param string $name
  625. */
  626. function OptionSubmit( $name ) {
  627. echo html_tag( 'tr',
  628. html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
  629. ) . "\n";
  630. }
  631. // vim: et ts=4
  632. ?>