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);
34 define('SMOPT_TYPE_STRLIST_RADIO', 14);
36 /* Define constants for the options refresh levels. */
37 define('SMOPT_REFRESH_NONE', 0);
38 define('SMOPT_REFRESH_FOLDERLIST', 1);
39 define('SMOPT_REFRESH_ALL', 2);
41 /* Define constants for the options size. */
42 define('SMOPT_SIZE_TINY', 0);
43 define('SMOPT_SIZE_SMALL', 1);
44 define('SMOPT_SIZE_MEDIUM', 2);
45 define('SMOPT_SIZE_LARGE', 3);
46 define('SMOPT_SIZE_HUGE', 4);
47 define('SMOPT_SIZE_NORMAL', 5);
49 define('SMOPT_SAVE_DEFAULT', 'save_option');
50 define('SMOPT_SAVE_NOOP', 'save_option_noop');
53 * SquirrelOption: An option for SquirrelMail.
55 * @package squirrelmail
58 class SquirrelOption
{
60 * The name of this setting
65 * The text that prefaces setting on the preferences page
70 * The type of INPUT element
72 * See SMOPT_TYPE_* defines
77 * Indicates if a link should be shown to refresh part
78 * or all of the window
80 * See SMOPT_REFRESH_* defines
85 * Specifies the size of certain input items
87 * See SMOPT_SIZE_* defines
92 * Text that follows a text input or
93 * select list input on the preferences page
95 * useful for indicating units, meanings of special values, etc.
100 * Text that overrides the "Yes" label for boolean
101 * radio option widgets
107 * Text that overrides the "No" label for boolean
108 * radio option widgets
114 * text displayed to the user
116 * Used with SMOPT_TYPE_COMMENT options
121 * additional javascript or other widget attributes added to the
122 * user input; must be an array where keys are attribute names
123 * ("onclick", etc) and values are the attribute values.
128 * script (usually Javascript) that will be placed after (outside of)
135 * The name of the Save Function for this option.
140 /* The various 'values' for this options. */
142 * default/preselected value for this option
152 * associative array, where each key is an actual input value
153 * and the corresponding value is what is displayed to the user
154 * for that list item in the drop-down list
157 var $possible_values;
159 * disables html sanitizing.
161 * WARNING - don't use it, if user input is possible in option
162 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
165 var $htmlencoded=false;
167 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
168 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
169 * See $flag argument in sqimap_mailbox_option_list() function.
173 var $folder_filter='noselect';
176 * Constructor function
177 * @param string $name
178 * @param string $caption
179 * @param integer $type
180 * @param integer $refresh_level
181 * @param mixed $initial_value
182 * @param array $possible_values
183 * @param bool $htmlencoded
185 function SquirrelOption
186 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
187 /* Set the basic stuff. */
189 $this->caption
= $caption;
191 $this->refresh_level
= $refresh_level;
192 $this->possible_values
= $possible_values;
193 $this->htmlencoded
= $htmlencoded;
194 $this->size
= SMOPT_SIZE_MEDIUM
;
195 $this->trailing_text
= '';
196 $this->yes_text
= '';
199 $this->aExtraAttribs
= array();
200 $this->post_script
= '';
202 //Check for a current value.
203 if (isset($GLOBALS[$name])) {
204 $this->value
= $GLOBALS[$name];
205 } else if (!empty($initial_value)) {
206 $this->value
= $initial_value;
211 /* Check for a new value. */
212 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
213 $this->new_value
= '';
216 /* Set the default save function. */
217 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
218 $this->save_function
= SMOPT_SAVE_DEFAULT
;
220 $this->save_function
= SMOPT_SAVE_NOOP
;
224 /** Convenience function that identifies which types of
225 widgets are stored as (serialized) array values. */
226 function is_multiple_valued() {
227 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
228 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
229 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
);
233 * Set the value for this option.
234 * @param mixed $value
236 function setValue($value) {
237 $this->value
= $value;
241 * Set the new value for this option.
242 * @param mixed $new_value
244 function setNewValue($new_value) {
245 $this->new_value
= $new_value;
249 * Set the size for this option.
250 * @param integer $size
252 function setSize($size) {
257 * Set the trailing_text for this option.
258 * @param string $trailing_text
260 function setTrailingText($trailing_text) {
261 $this->trailing_text
= $trailing_text;
265 * Set the yes_text for this option.
266 * @param string $yes_text
268 function setYesText($yes_text) {
269 $this->yes_text
= $yes_text;
273 * Set the no_text for this option.
274 * @param string $no_text
276 function setNoText($no_text) {
277 $this->no_text
= $no_text;
281 * Set the comment for this option.
282 * @param string $comment
284 function setComment($comment) {
285 $this->comment
= $comment;
289 * Set the extra attributes for this option.
290 * @param array $aExtraAttribs
292 function setExtraAttributes($aExtraAttribs) {
293 $this->aExtraAttribs
= $aExtraAttribs;
297 * Set the "post script" for this option.
298 * @param string $post_script
300 function setPostScript($post_script) {
301 $this->post_script
= $post_script;
305 * Set the save function for this option.
306 * @param string $save_function
308 function setSaveFunction($save_function) {
309 $this->save_function
= $save_function;
313 * Set the folder_filter for this option.
314 * @param string $folder_filter
317 function setFolderFilter($folder_filter) {
318 $this->folder_filter
= $folder_filter;
322 * Creates fields on option pages according to option type
324 * This is the function that calls all other createWidget* functions.
326 * @return string The formated option field
329 function createWidget() {
332 // Use new value if available
333 if (!empty($this->new_value
)) {
334 $tempValue = $this->value
;
335 $this->value
= $this->new_value
;
338 /* Get the widget for this option type. */
339 switch ($this->type
) {
340 case SMOPT_TYPE_STRING
:
341 $result = $this->createWidget_String();
343 case SMOPT_TYPE_STRLIST
:
344 $result = $this->createWidget_StrList();
346 case SMOPT_TYPE_TEXTAREA
:
347 $result = $this->createWidget_TextArea();
349 case SMOPT_TYPE_INTEGER
:
350 $result = $this->createWidget_Integer();
352 case SMOPT_TYPE_FLOAT
:
353 $result = $this->createWidget_Float();
355 case SMOPT_TYPE_BOOLEAN
:
356 $result = $this->createWidget_Boolean();
358 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
359 $result = $this->createWidget_Boolean(TRUE);
361 case SMOPT_TYPE_BOOLEAN_RADIO
:
362 $result = $this->createWidget_Boolean(FALSE);
364 case SMOPT_TYPE_HIDDEN
:
365 $result = $this->createWidget_Hidden();
367 case SMOPT_TYPE_COMMENT
:
368 $result = $this->createWidget_Comment();
370 case SMOPT_TYPE_FLDRLIST
:
371 $result = $this->createWidget_FolderList();
373 case SMOPT_TYPE_FLDRLIST_MULTI
:
374 $result = $this->createWidget_FolderList(TRUE);
376 case SMOPT_TYPE_EDIT_LIST
:
377 $result = $this->createWidget_EditList();
379 case SMOPT_TYPE_STRLIST_MULTI
:
380 $result = $this->createWidget_StrList(TRUE);
382 case SMOPT_TYPE_STRLIST_RADIO
:
383 $result = $this->createWidget_StrList(FALSE, TRUE);
387 sprintf(_("Option Type '%s' Not Found"), $this->type
)
391 /* Add the "post script" for this option. */
392 $result .= $this->post_script
;
394 // put correct value back if need be
395 if (!empty($this->new_value
)) {
396 $this->value
= $tempValue;
399 /* Now, return the created widget. */
404 * Create string field
405 * @return string html formated option field
407 function createWidget_String() {
408 switch ($this->size
) {
409 case SMOPT_SIZE_TINY
:
412 case SMOPT_SIZE_SMALL
:
415 case SMOPT_SIZE_LARGE
:
418 case SMOPT_SIZE_HUGE
:
421 case SMOPT_SIZE_NORMAL
:
426 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
430 * Create selection box or radio button group
432 * When $this->htmlencoded is TRUE, the keys and values in
433 * $this->possible_values are assumed to be display-safe.
436 * Note that when building radio buttons instead of a select
437 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
438 * radio buttons will be output one after another without
439 * linebreaks between them. Otherwise, each radio button
440 * goes on a line of its own.
442 * @param boolean $multiple_select When TRUE, the select widget
443 * will allow multiple selections
444 * (OPTIONAL; default is FALSE
445 * (single select list))
446 * @param boolean $radio_buttons When TRUE, the widget will
447 * instead be built as a group
448 * of radio buttons (and
449 * $multiple_select will be
450 * forced to FALSE) (OPTIONAL;
451 * default is FALSE (select widget))
453 * @return string html formated selection box or radio buttons
456 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
457 //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
459 // radio buttons instead of select widget?
461 if ($radio_buttons) {
465 foreach ($this->possible_values
as $real_value => $disp_value) {
466 $result .= addRadioBox('new_' . $this->name
, ($this->value
== $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name
. '_' . $real_value), $this->aExtraAttribs
)) . $nbsp . create_label($disp_value, 'new_' . $this->name
. '_' . $real_value);
467 if ($this->size
!= SMOPT_SIZE_TINY
)
475 // everything below applies to select widgets
477 switch ($this->size
) {
478 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
479 case SMOPT_SIZE_TINY
:
482 case SMOPT_SIZE_SMALL
:
485 case SMOPT_SIZE_LARGE
:
488 case SMOPT_SIZE_HUGE
:
491 case SMOPT_SIZE_NORMAL
:
496 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
501 * Create folder selection box
503 * @param boolean $multiple_select When TRUE, the select widget
504 * will allow multiple selections
505 * (OPTIONAL; default is FALSE
506 * (single select list))
508 * @return string html formated selection box
511 function createWidget_FolderList($multiple_select=FALSE) {
513 switch ($this->size
) {
514 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
515 case SMOPT_SIZE_TINY
:
518 case SMOPT_SIZE_SMALL
:
521 case SMOPT_SIZE_LARGE
:
524 case SMOPT_SIZE_HUGE
:
527 case SMOPT_SIZE_NORMAL
:
532 // possible values might include a nested array of
533 // possible values (list of folders)
535 $option_list = array();
536 foreach ($this->possible_values
as $value => $text) {
538 // list of folders (boxes array)
540 if (is_array($text)) {
541 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
543 // just one option here
546 $option_list = array_merge($option_list, array($value => $text));
550 if (empty($option_list))
551 $option_list = array('ignore' => _("unavailable"));
554 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
560 * @return string html formated textarea field
562 function createWidget_TextArea() {
563 switch ($this->size
) {
564 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
565 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
566 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
567 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
568 case SMOPT_SIZE_NORMAL
:
569 default: $rows = 5; $cols = 50;
571 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
575 * Creates field for integer
577 * Difference from createWidget_String is visible only when javascript is enabled
578 * @return string html formated option field
580 function createWidget_Integer() {
582 // add onChange javascript handler to a regular string widget
583 // which will strip out all non-numeric chars
584 if (checkForJavascript())
585 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
586 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
587 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
588 . 'this.value=newVal;';
590 return $this->createWidget_String();
594 * Creates field for floating number
595 * Difference from createWidget_String is visible only when javascript is enabled
596 * @return string html formated option field
598 function createWidget_Float() {
600 // add onChange javascript handler to a regular string widget
601 // which will strip out all non-numeric (period also OK) chars
602 if (checkForJavascript())
603 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
604 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
605 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
606 . 'newVal += origVal.charAt(i); } this.value=newVal;';
608 return $this->createWidget_String();
612 * Create boolean widget
614 * When creating Yes/No radio buttons, the "yes_text"
615 * and "no_text" option attributes are used to override
616 * the typical "Yes" and "No" text.
618 * @param boolean $checkbox When TRUE, the widget will be
619 * constructed as a checkbox,
620 * otherwise it will be a set of
621 * Yes/No radio buttons (OPTIONAL;
622 * default is TRUE (checkbox)).
624 * @return string html formated boolean widget
627 function createWidget_Boolean($checkbox=TRUE) {
629 global $oTemplate, $nbsp;
635 $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
);
642 /* Build the yes choice. */
643 $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');
645 /* Build the no choice. */
646 $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');
648 /* Build the combined "boolean widget". */
649 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
657 * Creates hidden field
658 * @return string html formated hidden input field
660 function createWidget_Hidden() {
661 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
666 * @return string comment
668 function createWidget_Comment() {
669 $result = $this->comment
;
674 * Creates an edit list
675 * @return string html formated list of edit fields and
676 * their associated controls
678 function createWidget_EditList() {
682 switch ($this->size
) {
683 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
684 case SMOPT_SIZE_TINY
:
687 case SMOPT_SIZE_SMALL
:
690 case SMOPT_SIZE_LARGE
:
693 case SMOPT_SIZE_HUGE
:
696 case SMOPT_SIZE_NORMAL
:
701 if (empty($this->possible_values
)) $this->possible_values
= array();
702 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
704 //FIXME: $this->aExtraAttribs probably should only be used in one place
705 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
706 $oTemplate->assign('trailing_text', $this->trailing_text
);
707 $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));
708 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
709 $oTemplate->assign('name', $this->name
);
710 return $oTemplate->fetch('edit_list_widget.tpl');
718 $function = $this->save_function
;
727 // edit lists have a lot going on, so we'll always process them
729 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
731 return ($this->value
!= $this->new_value
);
733 } /* End of SquirrelOption class*/
736 * Saves the option value (this is the default save function
737 * unless overridden by the user)
739 * @param object $option object that holds option name and new_value
741 function save_option($option) {
743 // Can't save the pref if we don't have the username
745 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
751 // edit lists: first add new elements to list, then
752 // remove any selected ones (note that we must add
753 // before deleting because the javascript that populates
754 // the "add" textbox when selecting items in the list
757 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
759 if (empty($option->possible_values
)) $option->possible_values
= array();
760 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
762 // add element if given
764 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
765 $new_element = trim($new_element);
766 if (!empty($new_element)
767 && !in_array($new_element, $option->possible_values
))
768 $option->possible_values
[] = $new_element;
771 // delete selected elements if needed
773 if (is_array($option->new_value
)
774 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
775 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
777 // save full list (stored in "possible_values")
779 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
781 // Certain option types need to be serialized because
782 // they are not scalar
784 } else if ($option->is_multiple_valued())
785 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
788 setPref($data_dir, $username, $option->name
, $option->new_value
);
793 * save function that does not save
794 * @param object $option
796 function save_option_noop($option) {
797 /* Do nothing here... */
801 * Create hidden 'optpage' input field with value set by argument
802 * @param string $optpage identification of option page
803 * @return string html formated hidden input field
805 function create_optpage_element($optpage) {
806 return addHidden('optpage', $optpage);
810 * Create hidden 'optmode' input field with value set by argument
811 * @param string $optmode
812 * @return string html formated hidden input field
814 function create_optmode_element($optmode) {
815 return addHidden('optmode', $optmode);
819 * @param array $optgrps
820 * @param array $optvals
823 function create_option_groups($optgrps, $optvals) {
824 /* Build a simple array with which to start. */
827 /* Create option group for each option group name. */
828 foreach ($optgrps as $grpkey => $grpname) {
829 $result[$grpkey] = array();
830 $result[$grpkey]['name'] = $grpname;
831 $result[$grpkey]['options'] = array();
834 /* Create a new SquirrelOption for each set of option values. */
835 foreach ($optvals as $grpkey => $grpopts) {
836 foreach ($grpopts as $optset) {
837 /* Create a new option with all values given. */
838 $next_option = new SquirrelOption(
842 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
843 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
844 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
845 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
848 /* If provided, set the size for this option. */
849 if (isset($optset['size'])) {
850 $next_option->setSize($optset['size']);
853 /* If provided, set the trailing_text for this option. */
854 if (isset($optset['trailing_text'])) {
855 $next_option->setTrailingText($optset['trailing_text']);
858 /* If provided, set the yes_text for this option. */
859 if (isset($optset['yes_text'])) {
860 $next_option->setYesText($optset['yes_text']);
863 /* If provided, set the no_text for this option. */
864 if (isset($optset['no_text'])) {
865 $next_option->setNoText($optset['no_text']);
868 /* If provided, set the comment for this option. */
869 if (isset($optset['comment'])) {
870 $next_option->setComment($optset['comment']);
873 /* If provided, set the save function for this option. */
874 if (isset($optset['save'])) {
875 $next_option->setSaveFunction($optset['save']);
878 /* If provided, set the extra attributes for this option. */
879 if (isset($optset['extra_attributes'])) {
880 $next_option->setExtraAttributes($optset['extra_attributes']);
883 /* If provided, set the "post script" for this option. */
884 if (isset($optset['post_script'])) {
885 $next_option->setPostScript($optset['post_script']);
888 /* If provided, set the folder_filter for this option. */
889 if (isset($optset['folder_filter'])) {
890 $next_option->setFolderFilter($optset['folder_filter']);
893 /* Add this option to the option array. */
894 $result[$grpkey]['options'][] = $next_option;
898 /* Return our resulting array. */