options.php 21 KB

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