options.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. //FIXME: can we throw an error here instead? either way, we don't want HTML here!
  295. $result = '<font color="' . $color[2] . '">'
  296. . sprintf(_("Option Type '%s' Not Found"), $this->type)
  297. . '</font>';
  298. }
  299. /* Add the "post script" for this option. */
  300. $result .= $this->post_script;
  301. // put correct value back if need be
  302. if (!empty($this->new_value)) {
  303. $this->value = $tempValue;
  304. }
  305. /* Now, return the created widget. */
  306. return ($result);
  307. }
  308. /**
  309. * Create string field
  310. * @return string html formated option field
  311. */
  312. function createWidget_String() {
  313. switch ($this->size) {
  314. case SMOPT_SIZE_TINY:
  315. $width = 5;
  316. break;
  317. case SMOPT_SIZE_SMALL:
  318. $width = 12;
  319. break;
  320. case SMOPT_SIZE_LARGE:
  321. $width = 38;
  322. break;
  323. case SMOPT_SIZE_HUGE:
  324. $width = 50;
  325. break;
  326. case SMOPT_SIZE_NORMAL:
  327. default:
  328. $width = 25;
  329. }
  330. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  331. }
  332. /**
  333. * Create selection box
  334. *
  335. * When $this->htmlencoded is TRUE, the keys and values in
  336. * $this->possible_values are assumed to be display-safe.
  337. * Use with care!
  338. *
  339. * @return string html formated selection box
  340. */
  341. function createWidget_StrList() {
  342. //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
  343. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  344. }
  345. /**
  346. * Create folder selection box
  347. * @return string html formated selection box
  348. */
  349. function createWidget_FolderList() {
  350. // possible values might include a nested array of
  351. // possible values (list of folders)
  352. //
  353. $option_list = array();
  354. foreach ($this->possible_values as $value => $text) {
  355. // list of folders (boxes array)
  356. //
  357. if (is_array($text)) {
  358. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value)), 0, $text, $this->folder_filter));
  359. // just one option here
  360. //
  361. } else {
  362. $option_list = array_merge($option_list, array($value => $text));
  363. }
  364. }
  365. if (empty($option_list))
  366. $option_list = array('ignore' => _("unavailable"));
  367. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  368. }
  369. /**
  370. * Creates textarea
  371. * @return string html formated textarea field
  372. */
  373. function createWidget_TextArea() {
  374. switch ($this->size) {
  375. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  376. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  377. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  378. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  379. case SMOPT_SIZE_NORMAL:
  380. default: $rows = 5; $cols = 50;
  381. }
  382. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  383. }
  384. /**
  385. * Creates field for integer
  386. *
  387. * Difference from createWidget_String is visible only when javascript is enabled
  388. * @return string html formated option field
  389. */
  390. function createWidget_Integer() {
  391. // add onChange javascript handler to a regular string widget
  392. // which will strip out all non-numeric chars
  393. if (checkForJavascript())
  394. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  395. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  396. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  397. . 'this.value=newVal;';
  398. return $this->createWidget_String();
  399. }
  400. /**
  401. * Creates field for floating number
  402. * Difference from createWidget_String is visible only when javascript is enabled
  403. * @return string html formated option field
  404. */
  405. function createWidget_Float() {
  406. // add onChange javascript handler to a regular string widget
  407. // which will strip out all non-numeric (period also OK) chars
  408. if (checkForJavascript())
  409. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  410. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  411. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  412. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  413. return $this->createWidget_String();
  414. }
  415. /**
  416. * Creates radio field (yes/no)
  417. * @return string html formated radio field
  418. */
  419. function createWidget_Boolean() {
  420. global $oTemplate;
  421. $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
  422. /* Build the yes choice. */
  423. $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');
  424. /* Build the no choice. */
  425. $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');
  426. /* Build and return the combined "boolean widget". */
  427. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  428. return ($result);
  429. }
  430. /**
  431. * Creates hidden field
  432. * @return string html formated hidden input field
  433. */
  434. function createWidget_Hidden() {
  435. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  436. }
  437. /**
  438. * Creates comment
  439. * @return string comment
  440. */
  441. function createWidget_Comment() {
  442. $result = $this->comment;
  443. return ($result);
  444. }
  445. /**
  446. *
  447. */
  448. function save() {
  449. $function = $this->save_function;
  450. $function($this);
  451. }
  452. /**
  453. *
  454. */
  455. function changed() {
  456. return ($this->value != $this->new_value);
  457. }
  458. } /* End of SquirrelOption class*/
  459. /**
  460. * Saves option
  461. * @param object $option object that holds option name and new_value
  462. */
  463. function save_option($option) {
  464. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  465. /* Can't save the pref if we don't have the username */
  466. return;
  467. }
  468. global $data_dir;
  469. setPref($data_dir, $username, $option->name, $option->new_value);
  470. }
  471. /**
  472. * save function that does not save
  473. * @param object $option
  474. */
  475. function save_option_noop($option) {
  476. /* Do nothing here... */
  477. }
  478. /**
  479. * Create hidden 'optpage' input field with value set by argument
  480. * @param string $optpage identification of option page
  481. * @return string html formated hidden input field
  482. */
  483. function create_optpage_element($optpage) {
  484. return addHidden('optpage', $optpage);
  485. }
  486. /**
  487. * Create hidden 'optmode' input field with value set by argument
  488. * @param string $optmode
  489. * @return string html formated hidden input field
  490. */
  491. function create_optmode_element($optmode) {
  492. return addHidden('optmode', $optmode);
  493. }
  494. /**
  495. * @param array $optgrps
  496. * @param array $optvals
  497. * @return array
  498. */
  499. function create_option_groups($optgrps, $optvals) {
  500. /* Build a simple array with which to start. */
  501. $result = array();
  502. /* Create option group for each option group name. */
  503. foreach ($optgrps as $grpkey => $grpname) {
  504. $result[$grpkey] = array();
  505. $result[$grpkey]['name'] = $grpname;
  506. $result[$grpkey]['options'] = array();
  507. }
  508. /* Create a new SquirrelOption for each set of option values. */
  509. foreach ($optvals as $grpkey => $grpopts) {
  510. foreach ($grpopts as $optset) {
  511. /* Create a new option with all values given. */
  512. $next_option = new SquirrelOption(
  513. $optset['name'],
  514. $optset['caption'],
  515. $optset['type'],
  516. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  517. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  518. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  519. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  520. );
  521. /* If provided, set the size for this option. */
  522. if (isset($optset['size'])) {
  523. $next_option->setSize($optset['size']);
  524. }
  525. /* If provided, set the trailing_text for this option. */
  526. if (isset($optset['trailing_text'])) {
  527. $next_option->setTrailingText($optset['trailing_text']);
  528. }
  529. /* If provided, set the comment for this option. */
  530. if (isset($optset['comment'])) {
  531. $next_option->setComment($optset['comment']);
  532. }
  533. /* If provided, set the save function for this option. */
  534. if (isset($optset['save'])) {
  535. $next_option->setSaveFunction($optset['save']);
  536. }
  537. /* If provided, set the extra attributes for this option. */
  538. if (isset($optset['extra_attributes'])) {
  539. $next_option->setExtraAttributes($optset['extra_attributes']);
  540. }
  541. /* If provided, set the "post script" for this option. */
  542. if (isset($optset['post_script'])) {
  543. $next_option->setPostScript($optset['post_script']);
  544. }
  545. /* If provided, set the folder_filter for this option. */
  546. if (isset($optset['folder_filter'])) {
  547. $next_option->setFolderFilter($optset['folder_filter']);
  548. }
  549. /* Add this option to the option array. */
  550. $result[$grpkey]['options'][] = $next_option;
  551. }
  552. }
  553. /* Return our resulting array. */
  554. return ($result);
  555. }
  556. // vim: et ts=4