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 displayed to the user
101 * Used with SMOPT_TYPE_COMMENT options
106 * additional javascript or other widget attributes added to the
107 * user input; must be an array where keys are attribute names
108 * ("onclick", etc) and values are the attribute values.
113 * script (usually Javascript) that will be placed after (outside of)
120 * The name of the Save Function for this option.
125 /* The various 'values' for this options. */
127 * default/preselected value for this option
137 * associative array, where each key is an actual input value
138 * and the corresponding value is what is displayed to the user
139 * for that list item in the drop-down list
142 var $possible_values;
144 * disables html sanitizing.
146 * WARNING - don't use it, if user input is possible in option
147 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
150 var $htmlencoded=false;
152 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
153 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
154 * See $flag argument in sqimap_mailbox_option_list() function.
158 var $folder_filter='noselect';
161 * Constructor function
162 * @param string $name
163 * @param string $caption
164 * @param integer $type
165 * @param integer $refresh_level
166 * @param mixed $initial_value
167 * @param array $possible_values
168 * @param bool $htmlencoded
170 function SquirrelOption
171 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
172 /* Set the basic stuff. */
174 $this->caption
= $caption;
176 $this->refresh_level
= $refresh_level;
177 $this->possible_values
= $possible_values;
178 $this->htmlencoded
= $htmlencoded;
179 $this->size
= SMOPT_SIZE_MEDIUM
;
180 $this->trailing_text
= '';
182 $this->aExtraAttribs
= array();
183 $this->post_script
= '';
185 //Check for a current value.
186 if (isset($GLOBALS[$name])) {
187 $this->value
= $GLOBALS[$name];
188 } else if (!empty($initial_value)) {
189 $this->value
= $initial_value;
194 /* Check for a new value. */
195 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
196 $this->new_value
= '';
199 /* Set the default save function. */
200 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
201 $this->save_function
= SMOPT_SAVE_DEFAULT
;
203 $this->save_function
= SMOPT_SAVE_NOOP
;
207 /** Convenience function that identifies which types of
208 widgets are stored as (serialized) array values. */
209 function is_multiple_valued() {
210 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
211 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
212 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
);
216 * Set the value for this option.
217 * @param mixed $value
219 function setValue($value) {
220 $this->value
= $value;
224 * Set the new value for this option.
225 * @param mixed $new_value
227 function setNewValue($new_value) {
228 $this->new_value
= $new_value;
232 * Set the size for this option.
233 * @param integer $size
235 function setSize($size) {
240 * Set the trailing_text for this option.
241 * @param string $trailing_text
243 function setTrailingText($trailing_text) {
244 $this->trailing_text
= $trailing_text;
248 * Set the comment for this option.
249 * @param string $comment
251 function setComment($comment) {
252 $this->comment
= $comment;
256 * Set the extra attributes for this option.
257 * @param array $aExtraAttribs
259 function setExtraAttributes($aExtraAttribs) {
260 $this->aExtraAttribs
= $aExtraAttribs;
264 * Set the "post script" for this option.
265 * @param string $post_script
267 function setPostScript($post_script) {
268 $this->post_script
= $post_script;
272 * Set the save function for this option.
273 * @param string $save_function
275 function setSaveFunction($save_function) {
276 $this->save_function
= $save_function;
280 * Set the trailing_text for this option.
281 * @param string $folder_filter
284 function setFolderFilter($folder_filter) {
285 $this->folder_filter
= $folder_filter;
289 * Creates fields on option pages according to option type
291 * This is the function that calls all other createWidget* functions.
293 * @return string The formated option field
296 function createWidget() {
299 // Use new value if available
300 if (!empty($this->new_value
)) {
301 $tempValue = $this->value
;
302 $this->value
= $this->new_value
;
305 /* Get the widget for this option type. */
306 switch ($this->type
) {
307 case SMOPT_TYPE_STRING
:
308 $result = $this->createWidget_String();
310 case SMOPT_TYPE_STRLIST
:
311 $result = $this->createWidget_StrList();
313 case SMOPT_TYPE_TEXTAREA
:
314 $result = $this->createWidget_TextArea();
316 case SMOPT_TYPE_INTEGER
:
317 $result = $this->createWidget_Integer();
319 case SMOPT_TYPE_FLOAT
:
320 $result = $this->createWidget_Float();
322 case SMOPT_TYPE_BOOLEAN
:
323 $result = $this->createWidget_Boolean();
325 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
326 $result = $this->createWidget_Boolean(TRUE);
328 case SMOPT_TYPE_BOOLEAN_RADIO
:
329 $result = $this->createWidget_Boolean(FALSE);
331 case SMOPT_TYPE_HIDDEN
:
332 $result = $this->createWidget_Hidden();
334 case SMOPT_TYPE_COMMENT
:
335 $result = $this->createWidget_Comment();
337 case SMOPT_TYPE_FLDRLIST
:
338 $result = $this->createWidget_FolderList();
340 case SMOPT_TYPE_FLDRLIST_MULTI
:
341 $result = $this->createWidget_FolderList(TRUE);
343 case SMOPT_TYPE_EDIT_LIST
:
344 $result = $this->createWidget_EditList();
346 case SMOPT_TYPE_STRLIST_MULTI
:
347 $result = $this->createWidget_StrList(TRUE);
351 sprintf(_("Option Type '%s' Not Found"), $this->type
)
355 /* Add the "post script" for this option. */
356 $result .= $this->post_script
;
358 // put correct value back if need be
359 if (!empty($this->new_value
)) {
360 $this->value
= $tempValue;
363 /* Now, return the created widget. */
368 * Create string field
369 * @return string html formated option field
371 function createWidget_String() {
372 switch ($this->size
) {
373 case SMOPT_SIZE_TINY
:
376 case SMOPT_SIZE_SMALL
:
379 case SMOPT_SIZE_LARGE
:
382 case SMOPT_SIZE_HUGE
:
385 case SMOPT_SIZE_NORMAL
:
390 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
394 * Create selection box
396 * When $this->htmlencoded is TRUE, the keys and values in
397 * $this->possible_values are assumed to be display-safe.
400 * @param boolean $multiple_select When TRUE, the select widget
401 * will allow multiple selections
402 * (OPTIONAL; default is FALSE
403 * (single select list))
405 * @return string html formated selection box
408 function createWidget_StrList($multiple_select=FALSE) {
409 //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
411 switch ($this->size
) {
412 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
413 case SMOPT_SIZE_TINY
:
416 case SMOPT_SIZE_SMALL
:
419 case SMOPT_SIZE_LARGE
:
422 case SMOPT_SIZE_HUGE
:
425 case SMOPT_SIZE_NORMAL
:
430 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
435 * Create folder selection box
437 * @param boolean $multiple_select When TRUE, the select widget
438 * will allow multiple selections
439 * (OPTIONAL; default is FALSE
440 * (single select list))
442 * @return string html formated selection box
445 function createWidget_FolderList($multiple_select=FALSE) {
447 switch ($this->size
) {
448 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
449 case SMOPT_SIZE_TINY
:
452 case SMOPT_SIZE_SMALL
:
455 case SMOPT_SIZE_LARGE
:
458 case SMOPT_SIZE_HUGE
:
461 case SMOPT_SIZE_NORMAL
:
466 // possible values might include a nested array of
467 // possible values (list of folders)
469 $option_list = array();
470 foreach ($this->possible_values
as $value => $text) {
472 // list of folders (boxes array)
474 if (is_array($text)) {
475 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
477 // just one option here
480 $option_list = array_merge($option_list, array($value => $text));
484 if (empty($option_list))
485 $option_list = array('ignore' => _("unavailable"));
488 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
494 * @return string html formated textarea field
496 function createWidget_TextArea() {
497 switch ($this->size
) {
498 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
499 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
500 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
501 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
502 case SMOPT_SIZE_NORMAL
:
503 default: $rows = 5; $cols = 50;
505 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
509 * Creates field for integer
511 * Difference from createWidget_String is visible only when javascript is enabled
512 * @return string html formated option field
514 function createWidget_Integer() {
516 // add onChange javascript handler to a regular string widget
517 // which will strip out all non-numeric chars
518 if (checkForJavascript())
519 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
520 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
521 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
522 . 'this.value=newVal;';
524 return $this->createWidget_String();
528 * Creates field for floating number
529 * Difference from createWidget_String is visible only when javascript is enabled
530 * @return string html formated option field
532 function createWidget_Float() {
534 // add onChange javascript handler to a regular string widget
535 // which will strip out all non-numeric (period also OK) chars
536 if (checkForJavascript())
537 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
538 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
539 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
540 . 'newVal += origVal.charAt(i); } this.value=newVal;';
542 return $this->createWidget_String();
546 * Create boolean widget
548 * @param boolean $checkbox When TRUE, the widget will be
549 * constructed as a checkbox,
550 * otherwise it will be a set of
551 * Yes/No radio buttons (OPTIONAL;
552 * default is TRUE (checkbox)).
554 * @return string html formated boolean widget
557 function createWidget_Boolean($checkbox=TRUE) {
559 global $oTemplate, $nbsp;
565 $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
);
572 /* Build the yes choice. */
573 $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');
575 /* Build the no choice. */
576 $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');
578 /* Build the combined "boolean widget". */
579 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
587 * Creates hidden field
588 * @return string html formated hidden input field
590 function createWidget_Hidden() {
591 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
596 * @return string comment
598 function createWidget_Comment() {
599 $result = $this->comment
;
604 * Creates an edit list
605 * @return string html formated list of edit fields and
606 * their associated controls
608 function createWidget_EditList() {
612 switch ($this->size
) {
613 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
614 case SMOPT_SIZE_TINY
:
617 case SMOPT_SIZE_SMALL
:
620 case SMOPT_SIZE_LARGE
:
623 case SMOPT_SIZE_HUGE
:
626 case SMOPT_SIZE_NORMAL
:
631 if (empty($this->possible_values
)) $this->possible_values
= array();
632 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
634 //FIXME: $this->aExtraAttribs probably should only be used in one place
635 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
636 $oTemplate->assign('trailing_text', $this->trailing_text
);
637 $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));
638 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
639 $oTemplate->assign('name', $this->name
);
640 return $oTemplate->fetch('edit_list_widget.tpl');
648 $function = $this->save_function
;
657 // edit lists have a lot going on, so we'll always process them
659 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
661 return ($this->value
!= $this->new_value
);
663 } /* End of SquirrelOption class*/
666 * Saves the option value (this is the default save function
667 * unless overridden by the user)
669 * @param object $option object that holds option name and new_value
671 function save_option($option) {
673 // Can't save the pref if we don't have the username
675 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
681 // edit lists: first add new elements to list, then
682 // remove any selected ones (note that we must add
683 // before deleting because the javascript that populates
684 // the "add" textbox when selecting items in the list
687 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
689 if (empty($option->possible_values
)) $option->possible_values
= array();
690 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
692 // add element if given
694 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
695 $new_element = trim($new_element);
696 if (!empty($new_element)
697 && !in_array($new_element, $option->possible_values
))
698 $option->possible_values
[] = $new_element;
701 // delete selected elements if needed
703 if (is_array($option->new_value
)
704 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
705 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
707 // save full list (stored in "possible_values")
709 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
711 // Certain option types need to be serialized because
712 // they are not scalar
714 } else if ($option->is_multiple_valued())
715 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
718 setPref($data_dir, $username, $option->name
, $option->new_value
);
723 * save function that does not save
724 * @param object $option
726 function save_option_noop($option) {
727 /* Do nothing here... */
731 * Create hidden 'optpage' input field with value set by argument
732 * @param string $optpage identification of option page
733 * @return string html formated hidden input field
735 function create_optpage_element($optpage) {
736 return addHidden('optpage', $optpage);
740 * Create hidden 'optmode' input field with value set by argument
741 * @param string $optmode
742 * @return string html formated hidden input field
744 function create_optmode_element($optmode) {
745 return addHidden('optmode', $optmode);
749 * @param array $optgrps
750 * @param array $optvals
753 function create_option_groups($optgrps, $optvals) {
754 /* Build a simple array with which to start. */
757 /* Create option group for each option group name. */
758 foreach ($optgrps as $grpkey => $grpname) {
759 $result[$grpkey] = array();
760 $result[$grpkey]['name'] = $grpname;
761 $result[$grpkey]['options'] = array();
764 /* Create a new SquirrelOption for each set of option values. */
765 foreach ($optvals as $grpkey => $grpopts) {
766 foreach ($grpopts as $optset) {
767 /* Create a new option with all values given. */
768 $next_option = new SquirrelOption(
772 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
773 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
774 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
775 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
778 /* If provided, set the size for this option. */
779 if (isset($optset['size'])) {
780 $next_option->setSize($optset['size']);
783 /* If provided, set the trailing_text for this option. */
784 if (isset($optset['trailing_text'])) {
785 $next_option->setTrailingText($optset['trailing_text']);
788 /* If provided, set the comment for this option. */
789 if (isset($optset['comment'])) {
790 $next_option->setComment($optset['comment']);
793 /* If provided, set the save function for this option. */
794 if (isset($optset['save'])) {
795 $next_option->setSaveFunction($optset['save']);
798 /* If provided, set the extra attributes for this option. */
799 if (isset($optset['extra_attributes'])) {
800 $next_option->setExtraAttributes($optset['extra_attributes']);
803 /* If provided, set the "post script" for this option. */
804 if (isset($optset['post_script'])) {
805 $next_option->setPostScript($optset['post_script']);
808 /* If provided, set the folder_filter for this option. */
809 if (isset($optset['folder_filter'])) {
810 $next_option->setFolderFilter($optset['folder_filter']);
813 /* Add this option to the option array. */
814 $result[$grpkey]['options'][] = $next_option;
818 /* Return our resulting array. */