options.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Functions needed to display the options pages.
  6. *
  7. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage prefs
  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 widget attributes added to the
  95. * user input; must be an array where keys are attribute names
  96. * ("onclick", etc) and values are the attribute values.
  97. * @var array
  98. */
  99. var $aExtraAttribs;
  100. /**
  101. * script (usually Javascript) that will be placed after (outside of)
  102. * the INPUT tag
  103. * @var string
  104. */
  105. var $post_script;
  106. /**
  107. * The name of the Save Function for this option.
  108. * @var string
  109. */
  110. var $save_function;
  111. /* The various 'values' for this options. */
  112. /**
  113. * default/preselected value for this option
  114. * @var mixed
  115. */
  116. var $value;
  117. /**
  118. * new option value
  119. * @var mixed
  120. */
  121. var $new_value;
  122. /**
  123. * associative array, where each key is an actual input value
  124. * and the corresponding value is what is displayed to the user
  125. * for that list item in the drop-down list
  126. * @var array
  127. */
  128. var $possible_values;
  129. /**
  130. * disables html sanitizing.
  131. *
  132. * WARNING - don't use it, if user input is possible in option
  133. * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
  134. * @var bool
  135. */
  136. var $htmlencoded=false;
  137. /**
  138. * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
  139. * See $flag argument in sqimap_mailbox_option_list() function.
  140. * @var string
  141. * @since 1.5.1
  142. */
  143. var $folder_filter='noselect';
  144. /**
  145. * Constructor function
  146. * @param string $name
  147. * @param string $caption
  148. * @param integer $type
  149. * @param integer $refresh_level
  150. * @param mixed $initial_value
  151. * @param array $possible_values
  152. * @param bool $htmlencoded
  153. */
  154. function SquirrelOption
  155. ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  156. /* Set the basic stuff. */
  157. $this->name = $name;
  158. $this->caption = $caption;
  159. $this->type = $type;
  160. $this->refresh_level = $refresh_level;
  161. $this->possible_values = $possible_values;
  162. $this->htmlencoded = $htmlencoded;
  163. $this->size = SMOPT_SIZE_MEDIUM;
  164. $this->trailing_text = '';
  165. $this->comment = '';
  166. $this->aExtraAttribs = array();
  167. $this->post_script = '';
  168. //Check for a current value.
  169. if (isset($GLOBALS[$name])) {
  170. $this->value = $GLOBALS[$name];
  171. } else if (!empty($initial_value)) {
  172. $this->value = $initial_value;
  173. } else {
  174. $this->value = '';
  175. }
  176. /* Check for a new value. */
  177. if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  178. $this->new_value = '';
  179. }
  180. /* Set the default save function. */
  181. if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
  182. $this->save_function = SMOPT_SAVE_DEFAULT;
  183. } else {
  184. $this->save_function = SMOPT_SAVE_NOOP;
  185. }
  186. }
  187. /**
  188. * Set the value for this option.
  189. * @param mixed $value
  190. */
  191. function setValue($value) {
  192. $this->value = $value;
  193. }
  194. /**
  195. * Set the new value for this option.
  196. * @param mixed $new_value
  197. */
  198. function setNewValue($new_value) {
  199. $this->new_value = $new_value;
  200. }
  201. /**
  202. * Set the size for this option.
  203. * @param integer $size
  204. */
  205. function setSize($size) {
  206. $this->size = $size;
  207. }
  208. /**
  209. * Set the trailing_text for this option.
  210. * @param string $trailing_text
  211. */
  212. function setTrailingText($trailing_text) {
  213. $this->trailing_text = $trailing_text;
  214. }
  215. /**
  216. * Set the comment for this option.
  217. * @param string $comment
  218. */
  219. function setComment($comment) {
  220. $this->comment = $comment;
  221. }
  222. /**
  223. * Set the extra attributes for this option.
  224. * @param array $aExtraAttribs
  225. */
  226. function setExtraAttributes($aExtraAttribs) {
  227. $this->aExtraAttribs = $aExtraAttribs;
  228. }
  229. /**
  230. * Set the "post script" for this option.
  231. * @param string $post_script
  232. */
  233. function setPostScript($post_script) {
  234. $this->post_script = $post_script;
  235. }
  236. /**
  237. * Set the save function for this option.
  238. * @param string $save_function
  239. */
  240. function setSaveFunction($save_function) {
  241. $this->save_function = $save_function;
  242. }
  243. /**
  244. * Set the trailing_text for this option.
  245. * @param string $folder_filter
  246. * @since 1.5.1
  247. */
  248. function setFolderFilter($folder_filter) {
  249. $this->folder_filter = $folder_filter;
  250. }
  251. /**
  252. * Creates fields on option pages according to option type
  253. *
  254. * Function that calls other createWidget* functions.
  255. * @return string html formated option field
  256. */
  257. function createHTMLWidget() {
  258. global $color;
  259. // Use new value if available
  260. if (!empty($this->new_value)) {
  261. $tempValue = $this->value;
  262. $this->value = $this->new_value;
  263. }
  264. /* Get the widget for this option type. */
  265. switch ($this->type) {
  266. case SMOPT_TYPE_STRING:
  267. $result = $this->createWidget_String();
  268. break;
  269. case SMOPT_TYPE_STRLIST:
  270. $result = $this->createWidget_StrList();
  271. break;
  272. case SMOPT_TYPE_TEXTAREA:
  273. $result = $this->createWidget_TextArea();
  274. break;
  275. case SMOPT_TYPE_INTEGER:
  276. $result = $this->createWidget_Integer();
  277. break;
  278. case SMOPT_TYPE_FLOAT:
  279. $result = $this->createWidget_Float();
  280. break;
  281. case SMOPT_TYPE_BOOLEAN:
  282. $result = $this->createWidget_Boolean();
  283. break;
  284. case SMOPT_TYPE_HIDDEN:
  285. $result = $this->createWidget_Hidden();
  286. break;
  287. case SMOPT_TYPE_COMMENT:
  288. $result = $this->createWidget_Comment();
  289. break;
  290. case SMOPT_TYPE_FLDRLIST:
  291. $result = $this->createWidget_FolderList();
  292. break;
  293. default:
  294. error_box (
  295. sprintf(_("Option Type '%s' Not Found"), $this->type)
  296. );
  297. }
  298. /* Add the "post script" for this option. */
  299. $result .= $this->post_script;
  300. // put correct value back if need be
  301. if (!empty($this->new_value)) {
  302. $this->value = $tempValue;
  303. }
  304. /* Now, return the created widget. */
  305. return ($result);
  306. }
  307. /**
  308. * Create string field
  309. * @return string html formated option field
  310. */
  311. function createWidget_String() {
  312. switch ($this->size) {
  313. case SMOPT_SIZE_TINY:
  314. $width = 5;
  315. break;
  316. case SMOPT_SIZE_SMALL:
  317. $width = 12;
  318. break;
  319. case SMOPT_SIZE_LARGE:
  320. $width = 38;
  321. break;
  322. case SMOPT_SIZE_HUGE:
  323. $width = 50;
  324. break;
  325. case SMOPT_SIZE_NORMAL:
  326. default:
  327. $width = 25;
  328. }
  329. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  330. }
  331. /**
  332. * Create selection box
  333. *
  334. * When $this->htmlencoded is TRUE, the keys and values in
  335. * $this->possible_values are assumed to be display-safe.
  336. * Use with care!
  337. *
  338. * @return string html formated selection box
  339. */
  340. function createWidget_StrList() {
  341. //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
  342. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  343. }
  344. /**
  345. * Create folder selection box
  346. * @return string html formated selection box
  347. */
  348. function createWidget_FolderList() {
  349. // possible values might include a nested array of
  350. // possible values (list of folders)
  351. //
  352. $option_list = array();
  353. foreach ($this->possible_values as $value => $text) {
  354. // list of folders (boxes array)
  355. //
  356. if (is_array($text)) {
  357. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value)), 0, $text, $this->folder_filter));
  358. // just one option here
  359. //
  360. } else {
  361. $option_list = array_merge($option_list, array($value => $text));
  362. }
  363. }
  364. if (empty($option_list))
  365. $option_list = array('ignore' => _("unavailable"));
  366. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  367. }
  368. /**
  369. * Creates textarea
  370. * @return string html formated textarea field
  371. */
  372. function createWidget_TextArea() {
  373. switch ($this->size) {
  374. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  375. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  376. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  377. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  378. case SMOPT_SIZE_NORMAL:
  379. default: $rows = 5; $cols = 50;
  380. }
  381. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  382. }
  383. /**
  384. * Creates field for integer
  385. *
  386. * Difference from createWidget_String is visible only when javascript is enabled
  387. * @return string html formated option field
  388. */
  389. function createWidget_Integer() {
  390. // add onChange javascript handler to a regular string widget
  391. // which will strip out all non-numeric chars
  392. if (checkForJavascript())
  393. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  394. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  395. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  396. . 'this.value=newVal;';
  397. return $this->createWidget_String();
  398. }
  399. /**
  400. * Creates field for floating number
  401. * Difference from createWidget_String is visible only when javascript is enabled
  402. * @return string html formated option field
  403. */
  404. function createWidget_Float() {
  405. // add onChange javascript handler to a regular string widget
  406. // which will strip out all non-numeric (period also OK) chars
  407. if (checkForJavascript())
  408. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  409. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  410. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  411. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  412. return $this->createWidget_String();
  413. }
  414. /**
  415. * Creates radio field (yes/no)
  416. * @return string html formated radio field
  417. */
  418. function createWidget_Boolean() {
  419. global $oTemplate;
  420. $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
  421. /* Build the yes choice. */
  422. $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label(_("Yes"), 'new_' . $this->name . '_yes');
  423. /* Build the no choice. */
  424. $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label(_("No"), 'new_' . $this->name . '_no');
  425. /* Build and return the combined "boolean widget". */
  426. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  427. return ($result);
  428. }
  429. /**
  430. * Creates hidden field
  431. * @return string html formated hidden input field
  432. */
  433. function createWidget_Hidden() {
  434. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  435. }
  436. /**
  437. * Creates comment
  438. * @return string comment
  439. */
  440. function createWidget_Comment() {
  441. $result = $this->comment;
  442. return ($result);
  443. }
  444. /**
  445. *
  446. */
  447. function save() {
  448. $function = $this->save_function;
  449. $function($this);
  450. }
  451. /**
  452. *
  453. */
  454. function changed() {
  455. return ($this->value != $this->new_value);
  456. }
  457. } /* End of SquirrelOption class*/
  458. /**
  459. * Saves option
  460. * @param object $option object that holds option name and new_value
  461. */
  462. function save_option($option) {
  463. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  464. /* Can't save the pref if we don't have the username */
  465. return;
  466. }
  467. global $data_dir;
  468. setPref($data_dir, $username, $option->name, $option->new_value);
  469. }
  470. /**
  471. * save function that does not save
  472. * @param object $option
  473. */
  474. function save_option_noop($option) {
  475. /* Do nothing here... */
  476. }
  477. /**
  478. * Create hidden 'optpage' input field with value set by argument
  479. * @param string $optpage identification of option page
  480. * @return string html formated hidden input field
  481. */
  482. function create_optpage_element($optpage) {
  483. return addHidden('optpage', $optpage);
  484. }
  485. /**
  486. * Create hidden 'optmode' input field with value set by argument
  487. * @param string $optmode
  488. * @return string html formated hidden input field
  489. */
  490. function create_optmode_element($optmode) {
  491. return addHidden('optmode', $optmode);
  492. }
  493. /**
  494. * @param array $optgrps
  495. * @param array $optvals
  496. * @return array
  497. */
  498. function create_option_groups($optgrps, $optvals) {
  499. /* Build a simple array with which to start. */
  500. $result = array();
  501. /* Create option group for each option group name. */
  502. foreach ($optgrps as $grpkey => $grpname) {
  503. $result[$grpkey] = array();
  504. $result[$grpkey]['name'] = $grpname;
  505. $result[$grpkey]['options'] = array();
  506. }
  507. /* Create a new SquirrelOption for each set of option values. */
  508. foreach ($optvals as $grpkey => $grpopts) {
  509. foreach ($grpopts as $optset) {
  510. /* Create a new option with all values given. */
  511. $next_option = new SquirrelOption(
  512. $optset['name'],
  513. $optset['caption'],
  514. $optset['type'],
  515. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  516. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  517. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  518. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  519. );
  520. /* If provided, set the size for this option. */
  521. if (isset($optset['size'])) {
  522. $next_option->setSize($optset['size']);
  523. }
  524. /* If provided, set the trailing_text for this option. */
  525. if (isset($optset['trailing_text'])) {
  526. $next_option->setTrailingText($optset['trailing_text']);
  527. }
  528. /* If provided, set the comment for this option. */
  529. if (isset($optset['comment'])) {
  530. $next_option->setComment($optset['comment']);
  531. }
  532. /* If provided, set the save function for this option. */
  533. if (isset($optset['save'])) {
  534. $next_option->setSaveFunction($optset['save']);
  535. }
  536. /* If provided, set the extra attributes for this option. */
  537. if (isset($optset['extra_attributes'])) {
  538. $next_option->setExtraAttributes($optset['extra_attributes']);
  539. }
  540. /* If provided, set the "post script" for this option. */
  541. if (isset($optset['post_script'])) {
  542. $next_option->setPostScript($optset['post_script']);
  543. }
  544. /* If provided, set the folder_filter for this option. */
  545. if (isset($optset['folder_filter'])) {
  546. $next_option->setFolderFilter($optset['folder_filter']);
  547. }
  548. /* Add this option to the option array. */
  549. $result[$grpkey]['options'][] = $next_option;
  550. }
  551. }
  552. /* Return our resulting array. */
  553. return ($result);
  554. }