6 * Functions needed to display the options pages.
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
15 /**********************************************/
16 /* Define constants used in the options code. */
17 /**********************************************/
19 /* Define constants for the various option types. */
20 define('SMOPT_TYPE_STRING', 0);
21 define('SMOPT_TYPE_STRLIST', 1);
22 define('SMOPT_TYPE_TEXTAREA', 2);
23 define('SMOPT_TYPE_INTEGER', 3);
24 define('SMOPT_TYPE_FLOAT', 4);
25 define('SMOPT_TYPE_BOOLEAN', 5);
26 define('SMOPT_TYPE_HIDDEN', 6);
27 define('SMOPT_TYPE_COMMENT', 7);
28 define('SMOPT_TYPE_FLDRLIST', 8);
29 define('SMOPT_TYPE_FLDRLIST_MULTI', 9);
30 define('SMOPT_TYPE_EDIT_LIST', 10);
31 define('SMOPT_TYPE_STRLIST_MULTI', 11);
32 define('SMOPT_TYPE_BOOLEAN_CHECKBOX', 12);
33 define('SMOPT_TYPE_BOOLEAN_RADIO', 13);
35 /* Define constants for the options refresh levels. */
36 define('SMOPT_REFRESH_NONE', 0);
37 define('SMOPT_REFRESH_FOLDERLIST', 1);
38 define('SMOPT_REFRESH_ALL', 2);
40 /* Define constants for the options size. */
41 define('SMOPT_SIZE_TINY', 0);
42 define('SMOPT_SIZE_SMALL', 1);
43 define('SMOPT_SIZE_MEDIUM', 2);
44 define('SMOPT_SIZE_LARGE', 3);
45 define('SMOPT_SIZE_HUGE', 4);
46 define('SMOPT_SIZE_NORMAL', 5);
48 define('SMOPT_SAVE_DEFAULT', 'save_option');
49 define('SMOPT_SAVE_NOOP', 'save_option_noop');
52 * SquirrelOption: An option for SquirrelMail.
54 * @package squirrelmail
57 class SquirrelOption
{
59 * The name of this setting
64 * The text that prefaces setting on the preferences page
69 * The type of INPUT element
71 * See SMOPT_TYPE_* defines
76 * Indicates if a link should be shown to refresh part
77 * or all of the window
79 * See SMOPT_REFRESH_* defines
84 * Specifies the size of certain input items
86 * See SMOPT_SIZE_* defines
91 * Text that follows a text input or
92 * select list input on the preferences page
94 * useful for indicating units, meanings of special values, etc.
99 * Text that overrides the "Yes" label for boolean
100 * radio option widgets
106 * Text that overrides the "No" label for boolean
107 * radio option widgets
113 * text displayed to the user
115 * Used with SMOPT_TYPE_COMMENT options
120 * additional javascript or other widget attributes added to the
121 * user input; must be an array where keys are attribute names
122 * ("onclick", etc) and values are the attribute values.
127 * script (usually Javascript) that will be placed after (outside of)
134 * The name of the Save Function for this option.
139 /* The various 'values' for this options. */
141 * default/preselected value for this option
151 * associative array, where each key is an actual input value
152 * and the corresponding value is what is displayed to the user
153 * for that list item in the drop-down list
156 var $possible_values;
158 * disables html sanitizing.
160 * WARNING - don't use it, if user input is possible in option
161 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
164 var $htmlencoded=false;
166 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
167 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
168 * See $flag argument in sqimap_mailbox_option_list() function.
172 var $folder_filter='noselect';
175 * Constructor function
176 * @param string $name
177 * @param string $caption
178 * @param integer $type
179 * @param integer $refresh_level
180 * @param mixed $initial_value
181 * @param array $possible_values
182 * @param bool $htmlencoded
184 function SquirrelOption
185 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
186 /* Set the basic stuff. */
188 $this->caption
= $caption;
190 $this->refresh_level
= $refresh_level;
191 $this->possible_values
= $possible_values;
192 $this->htmlencoded
= $htmlencoded;
193 $this->size
= SMOPT_SIZE_MEDIUM
;
194 $this->trailing_text
= '';
195 $this->yes_text
= '';
198 $this->aExtraAttribs
= array();
199 $this->post_script
= '';
201 //Check for a current value.
202 if (isset($GLOBALS[$name])) {
203 $this->value
= $GLOBALS[$name];
204 } else if (!empty($initial_value)) {
205 $this->value
= $initial_value;
210 /* Check for a new value. */
211 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
212 $this->new_value
= '';
215 /* Set the default save function. */
216 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
217 $this->save_function
= SMOPT_SAVE_DEFAULT
;
219 $this->save_function
= SMOPT_SAVE_NOOP
;
223 /** Convenience function that identifies which types of
224 widgets are stored as (serialized) array values. */
225 function is_multiple_valued() {
226 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
227 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
228 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
);
232 * Set the value for this option.
233 * @param mixed $value
235 function setValue($value) {
236 $this->value
= $value;
240 * Set the new value for this option.
241 * @param mixed $new_value
243 function setNewValue($new_value) {
244 $this->new_value
= $new_value;
248 * Set the size for this option.
249 * @param integer $size
251 function setSize($size) {
256 * Set the trailing_text for this option.
257 * @param string $trailing_text
259 function setTrailingText($trailing_text) {
260 $this->trailing_text
= $trailing_text;
264 * Set the yes_text for this option.
265 * @param string $yes_text
267 function setYesText($yes_text) {
268 $this->yes_text
= $yes_text;
272 * Set the no_text for this option.
273 * @param string $no_text
275 function setNoText($no_text) {
276 $this->no_text
= $no_text;
280 * Set the comment for this option.
281 * @param string $comment
283 function setComment($comment) {
284 $this->comment
= $comment;
288 * Set the extra attributes for this option.
289 * @param array $aExtraAttribs
291 function setExtraAttributes($aExtraAttribs) {
292 $this->aExtraAttribs
= $aExtraAttribs;
296 * Set the "post script" for this option.
297 * @param string $post_script
299 function setPostScript($post_script) {
300 $this->post_script
= $post_script;
304 * Set the save function for this option.
305 * @param string $save_function
307 function setSaveFunction($save_function) {
308 $this->save_function
= $save_function;
312 * Set the folder_filter for this option.
313 * @param string $folder_filter
316 function setFolderFilter($folder_filter) {
317 $this->folder_filter
= $folder_filter;
321 * Creates fields on option pages according to option type
323 * This is the function that calls all other createWidget* functions.
325 * @return string The formated option field
328 function createWidget() {
331 // Use new value if available
332 if (!empty($this->new_value
)) {
333 $tempValue = $this->value
;
334 $this->value
= $this->new_value
;
337 /* Get the widget for this option type. */
338 switch ($this->type
) {
339 case SMOPT_TYPE_STRING
:
340 $result = $this->createWidget_String();
342 case SMOPT_TYPE_STRLIST
:
343 $result = $this->createWidget_StrList();
345 case SMOPT_TYPE_TEXTAREA
:
346 $result = $this->createWidget_TextArea();
348 case SMOPT_TYPE_INTEGER
:
349 $result = $this->createWidget_Integer();
351 case SMOPT_TYPE_FLOAT
:
352 $result = $this->createWidget_Float();
354 case SMOPT_TYPE_BOOLEAN
:
355 $result = $this->createWidget_Boolean();
357 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
358 $result = $this->createWidget_Boolean(TRUE);
360 case SMOPT_TYPE_BOOLEAN_RADIO
:
361 $result = $this->createWidget_Boolean(FALSE);
363 case SMOPT_TYPE_HIDDEN
:
364 $result = $this->createWidget_Hidden();
366 case SMOPT_TYPE_COMMENT
:
367 $result = $this->createWidget_Comment();
369 case SMOPT_TYPE_FLDRLIST
:
370 $result = $this->createWidget_FolderList();
372 case SMOPT_TYPE_FLDRLIST_MULTI
:
373 $result = $this->createWidget_FolderList(TRUE);
375 case SMOPT_TYPE_EDIT_LIST
:
376 $result = $this->createWidget_EditList();
378 case SMOPT_TYPE_STRLIST_MULTI
:
379 $result = $this->createWidget_StrList(TRUE);
383 sprintf(_("Option Type '%s' Not Found"), $this->type
)
387 /* Add the "post script" for this option. */
388 $result .= $this->post_script
;
390 // put correct value back if need be
391 if (!empty($this->new_value
)) {
392 $this->value
= $tempValue;
395 /* Now, return the created widget. */
400 * Create string field
401 * @return string html formated option field
403 function createWidget_String() {
404 switch ($this->size
) {
405 case SMOPT_SIZE_TINY
:
408 case SMOPT_SIZE_SMALL
:
411 case SMOPT_SIZE_LARGE
:
414 case SMOPT_SIZE_HUGE
:
417 case SMOPT_SIZE_NORMAL
:
422 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
426 * Create selection box
428 * When $this->htmlencoded is TRUE, the keys and values in
429 * $this->possible_values are assumed to be display-safe.
432 * @param boolean $multiple_select When TRUE, the select widget
433 * will allow multiple selections
434 * (OPTIONAL; default is FALSE
435 * (single select list))
437 * @return string html formated selection box
440 function createWidget_StrList($multiple_select=FALSE) {
441 //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
443 switch ($this->size
) {
444 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
445 case SMOPT_SIZE_TINY
:
448 case SMOPT_SIZE_SMALL
:
451 case SMOPT_SIZE_LARGE
:
454 case SMOPT_SIZE_HUGE
:
457 case SMOPT_SIZE_NORMAL
:
462 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
467 * Create folder selection box
469 * @param boolean $multiple_select When TRUE, the select widget
470 * will allow multiple selections
471 * (OPTIONAL; default is FALSE
472 * (single select list))
474 * @return string html formated selection box
477 function createWidget_FolderList($multiple_select=FALSE) {
479 switch ($this->size
) {
480 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
481 case SMOPT_SIZE_TINY
:
484 case SMOPT_SIZE_SMALL
:
487 case SMOPT_SIZE_LARGE
:
490 case SMOPT_SIZE_HUGE
:
493 case SMOPT_SIZE_NORMAL
:
498 // possible values might include a nested array of
499 // possible values (list of folders)
501 $option_list = array();
502 foreach ($this->possible_values
as $value => $text) {
504 // list of folders (boxes array)
506 if (is_array($text)) {
507 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
509 // just one option here
512 $option_list = array_merge($option_list, array($value => $text));
516 if (empty($option_list))
517 $option_list = array('ignore' => _("unavailable"));
520 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
526 * @return string html formated textarea field
528 function createWidget_TextArea() {
529 switch ($this->size
) {
530 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
531 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
532 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
533 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
534 case SMOPT_SIZE_NORMAL
:
535 default: $rows = 5; $cols = 50;
537 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
541 * Creates field for integer
543 * Difference from createWidget_String is visible only when javascript is enabled
544 * @return string html formated option field
546 function createWidget_Integer() {
548 // add onChange javascript handler to a regular string widget
549 // which will strip out all non-numeric chars
550 if (checkForJavascript())
551 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
552 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
553 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
554 . 'this.value=newVal;';
556 return $this->createWidget_String();
560 * Creates field for floating number
561 * Difference from createWidget_String is visible only when javascript is enabled
562 * @return string html formated option field
564 function createWidget_Float() {
566 // add onChange javascript handler to a regular string widget
567 // which will strip out all non-numeric (period also OK) chars
568 if (checkForJavascript())
569 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
570 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
571 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
572 . 'newVal += origVal.charAt(i); } this.value=newVal;';
574 return $this->createWidget_String();
578 * Create boolean widget
580 * When creating Yes/No radio buttons, the "yes_text"
581 * and "no_text" option attributes are used to override
582 * the typical "Yes" and "No" text.
584 * @param boolean $checkbox When TRUE, the widget will be
585 * constructed as a checkbox,
586 * otherwise it will be a set of
587 * Yes/No radio buttons (OPTIONAL;
588 * default is TRUE (checkbox)).
590 * @return string html formated boolean widget
593 function createWidget_Boolean($checkbox=TRUE) {
595 global $oTemplate, $nbsp;
601 $result = addCheckbox('new_' . $this->name
, ($this->value
!= SMPREF_NO
), SMPREF_YES
, array_merge(array('id' => 'new_' . $this->name
), $this->aExtraAttribs
)) . $nbsp . create_label($this->trailing_text
, 'new_' . $this->name
);
608 /* Build the yes choice. */
609 $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((!empty($this->yes_text
) ?
$this->yes_text
: _("Yes")), 'new_' . $this->name
. '_yes');
611 /* Build the no choice. */
612 $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((!empty($this->no_text
) ?
$this->no_text
: _("No")), 'new_' . $this->name
. '_no');
614 /* Build the combined "boolean widget". */
615 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
623 * Creates hidden field
624 * @return string html formated hidden input field
626 function createWidget_Hidden() {
627 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
632 * @return string comment
634 function createWidget_Comment() {
635 $result = $this->comment
;
640 * Creates an edit list
641 * @return string html formated list of edit fields and
642 * their associated controls
644 function createWidget_EditList() {
648 switch ($this->size
) {
649 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
650 case SMOPT_SIZE_TINY
:
653 case SMOPT_SIZE_SMALL
:
656 case SMOPT_SIZE_LARGE
:
659 case SMOPT_SIZE_HUGE
:
662 case SMOPT_SIZE_NORMAL
:
667 if (empty($this->possible_values
)) $this->possible_values
= array();
668 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
670 //FIXME: $this->aExtraAttribs probably should only be used in one place
671 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
672 $oTemplate->assign('trailing_text', $this->trailing_text
);
673 $oTemplate->assign('select_widget', addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, FALSE, !checkForJavascript() ?
$this->aExtraAttribs
: array_merge(array('onchange' => 'if (typeof(window.addinput) == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name
. '\' ) { pos = j; } j++; } i++; } if( pos >= 0 ) { window.addinput = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput.value = this.options[x].value; break; } }'), $this->aExtraAttribs
), TRUE, $height));
674 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
675 $oTemplate->assign('name', $this->name
);
676 return $oTemplate->fetch('edit_list_widget.tpl');
684 $function = $this->save_function
;
693 // edit lists have a lot going on, so we'll always process them
695 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
697 return ($this->value
!= $this->new_value
);
699 } /* End of SquirrelOption class*/
702 * Saves the option value (this is the default save function
703 * unless overridden by the user)
705 * @param object $option object that holds option name and new_value
707 function save_option($option) {
709 // Can't save the pref if we don't have the username
711 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
717 // edit lists: first add new elements to list, then
718 // remove any selected ones (note that we must add
719 // before deleting because the javascript that populates
720 // the "add" textbox when selecting items in the list
723 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
725 if (empty($option->possible_values
)) $option->possible_values
= array();
726 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
728 // add element if given
730 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
731 $new_element = trim($new_element);
732 if (!empty($new_element)
733 && !in_array($new_element, $option->possible_values
))
734 $option->possible_values
[] = $new_element;
737 // delete selected elements if needed
739 if (is_array($option->new_value
)
740 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
741 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
743 // save full list (stored in "possible_values")
745 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
747 // Certain option types need to be serialized because
748 // they are not scalar
750 } else if ($option->is_multiple_valued())
751 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
754 setPref($data_dir, $username, $option->name
, $option->new_value
);
759 * save function that does not save
760 * @param object $option
762 function save_option_noop($option) {
763 /* Do nothing here... */
767 * Create hidden 'optpage' input field with value set by argument
768 * @param string $optpage identification of option page
769 * @return string html formated hidden input field
771 function create_optpage_element($optpage) {
772 return addHidden('optpage', $optpage);
776 * Create hidden 'optmode' input field with value set by argument
777 * @param string $optmode
778 * @return string html formated hidden input field
780 function create_optmode_element($optmode) {
781 return addHidden('optmode', $optmode);
785 * @param array $optgrps
786 * @param array $optvals
789 function create_option_groups($optgrps, $optvals) {
790 /* Build a simple array with which to start. */
793 /* Create option group for each option group name. */
794 foreach ($optgrps as $grpkey => $grpname) {
795 $result[$grpkey] = array();
796 $result[$grpkey]['name'] = $grpname;
797 $result[$grpkey]['options'] = array();
800 /* Create a new SquirrelOption for each set of option values. */
801 foreach ($optvals as $grpkey => $grpopts) {
802 foreach ($grpopts as $optset) {
803 /* Create a new option with all values given. */
804 $next_option = new SquirrelOption(
808 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
809 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
810 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
811 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
814 /* If provided, set the size for this option. */
815 if (isset($optset['size'])) {
816 $next_option->setSize($optset['size']);
819 /* If provided, set the trailing_text for this option. */
820 if (isset($optset['trailing_text'])) {
821 $next_option->setTrailingText($optset['trailing_text']);
824 /* If provided, set the yes_text for this option. */
825 if (isset($optset['yes_text'])) {
826 $next_option->setYesText($optset['yes_text']);
829 /* If provided, set the no_text for this option. */
830 if (isset($optset['no_text'])) {
831 $next_option->setNoText($optset['no_text']);
834 /* If provided, set the comment for this option. */
835 if (isset($optset['comment'])) {
836 $next_option->setComment($optset['comment']);
839 /* If provided, set the save function for this option. */
840 if (isset($optset['save'])) {
841 $next_option->setSaveFunction($optset['save']);
844 /* If provided, set the extra attributes for this option. */
845 if (isset($optset['extra_attributes'])) {
846 $next_option->setExtraAttributes($optset['extra_attributes']);
849 /* If provided, set the "post script" for this option. */
850 if (isset($optset['post_script'])) {
851 $next_option->setPostScript($optset['post_script']);
854 /* If provided, set the folder_filter for this option. */
855 if (isset($optset['folder_filter'])) {
856 $next_option->setFolderFilter($optset['folder_filter']);
859 /* Add this option to the option array. */
860 $result[$grpkey]['options'][] = $next_option;
864 /* Return our resulting array. */