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);
34 /* Define constants for the options refresh levels. */
35 define('SMOPT_REFRESH_NONE', 0);
36 define('SMOPT_REFRESH_FOLDERLIST', 1);
37 define('SMOPT_REFRESH_ALL', 2);
39 /* Define constants for the options size. */
40 define('SMOPT_SIZE_TINY', 0);
41 define('SMOPT_SIZE_SMALL', 1);
42 define('SMOPT_SIZE_MEDIUM', 2);
43 define('SMOPT_SIZE_LARGE', 3);
44 define('SMOPT_SIZE_HUGE', 4);
45 define('SMOPT_SIZE_NORMAL', 5);
47 define('SMOPT_SAVE_DEFAULT', 'save_option');
48 define('SMOPT_SAVE_NOOP', 'save_option_noop');
51 * SquirrelOption: An option for SquirrelMail.
53 * @package squirrelmail
56 class SquirrelOption
{
58 * The name of this setting
63 * The text that prefaces setting on the preferences page
68 * The type of INPUT element
70 * See SMOPT_TYPE_* defines
75 * Indicates if a link should be shown to refresh part
76 * or all of the window
78 * See SMOPT_REFRESH_* defines
83 * Specifies the size of certain input items
85 * See SMOPT_SIZE_* defines
90 * Text that follows a text input or
91 * select list input on the preferences page
93 * useful for indicating units, meanings of special values, etc.
98 * text displayed to the user
100 * Used with SMOPT_TYPE_COMMENT options
105 * additional javascript or other widget attributes added to the
106 * user input; must be an array where keys are attribute names
107 * ("onclick", etc) and values are the attribute values.
112 * script (usually Javascript) that will be placed after (outside of)
119 * The name of the Save Function for this option.
124 /* The various 'values' for this options. */
126 * default/preselected value for this option
136 * associative array, where each key is an actual input value
137 * and the corresponding value is what is displayed to the user
138 * for that list item in the drop-down list
141 var $possible_values;
143 * disables html sanitizing.
145 * WARNING - don't use it, if user input is possible in option
146 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
149 var $htmlencoded=false;
151 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
152 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
153 * See $flag argument in sqimap_mailbox_option_list() function.
157 var $folder_filter='noselect';
160 * Constructor function
161 * @param string $name
162 * @param string $caption
163 * @param integer $type
164 * @param integer $refresh_level
165 * @param mixed $initial_value
166 * @param array $possible_values
167 * @param bool $htmlencoded
169 function SquirrelOption
170 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
171 /* Set the basic stuff. */
173 $this->caption
= $caption;
175 $this->refresh_level
= $refresh_level;
176 $this->possible_values
= $possible_values;
177 $this->htmlencoded
= $htmlencoded;
178 $this->size
= SMOPT_SIZE_MEDIUM
;
179 $this->trailing_text
= '';
181 $this->aExtraAttribs
= array();
182 $this->post_script
= '';
184 //Check for a current value.
185 if (isset($GLOBALS[$name])) {
186 $this->value
= $GLOBALS[$name];
187 } else if (!empty($initial_value)) {
188 $this->value
= $initial_value;
193 /* Check for a new value. */
194 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
195 $this->new_value
= '';
198 /* Set the default save function. */
199 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
200 $this->save_function
= SMOPT_SAVE_DEFAULT
;
202 $this->save_function
= SMOPT_SAVE_NOOP
;
206 /** Convenience function that identifies which types of
207 widgets are stored as (serialized) array values. */
208 function is_multiple_valued() {
209 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
210 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
211 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
);
215 * Set the value for this option.
216 * @param mixed $value
218 function setValue($value) {
219 $this->value
= $value;
223 * Set the new value for this option.
224 * @param mixed $new_value
226 function setNewValue($new_value) {
227 $this->new_value
= $new_value;
231 * Set the size for this option.
232 * @param integer $size
234 function setSize($size) {
239 * Set the trailing_text for this option.
240 * @param string $trailing_text
242 function setTrailingText($trailing_text) {
243 $this->trailing_text
= $trailing_text;
247 * Set the comment for this option.
248 * @param string $comment
250 function setComment($comment) {
251 $this->comment
= $comment;
255 * Set the extra attributes for this option.
256 * @param array $aExtraAttribs
258 function setExtraAttributes($aExtraAttribs) {
259 $this->aExtraAttribs
= $aExtraAttribs;
263 * Set the "post script" for this option.
264 * @param string $post_script
266 function setPostScript($post_script) {
267 $this->post_script
= $post_script;
271 * Set the save function for this option.
272 * @param string $save_function
274 function setSaveFunction($save_function) {
275 $this->save_function
= $save_function;
279 * Set the trailing_text for this option.
280 * @param string $folder_filter
283 function setFolderFilter($folder_filter) {
284 $this->folder_filter
= $folder_filter;
288 * Creates fields on option pages according to option type
290 * This is the function that calls all other createWidget* functions.
292 * @return string The formated option field
295 function createWidget() {
298 // Use new value if available
299 if (!empty($this->new_value
)) {
300 $tempValue = $this->value
;
301 $this->value
= $this->new_value
;
304 /* Get the widget for this option type. */
305 switch ($this->type
) {
306 case SMOPT_TYPE_STRING
:
307 $result = $this->createWidget_String();
309 case SMOPT_TYPE_STRLIST
:
310 $result = $this->createWidget_StrList();
312 case SMOPT_TYPE_TEXTAREA
:
313 $result = $this->createWidget_TextArea();
315 case SMOPT_TYPE_INTEGER
:
316 $result = $this->createWidget_Integer();
318 case SMOPT_TYPE_FLOAT
:
319 $result = $this->createWidget_Float();
321 case SMOPT_TYPE_BOOLEAN
:
322 $result = $this->createWidget_Boolean();
324 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
325 $result = $this->createWidget_Boolean(TRUE);
327 case SMOPT_TYPE_HIDDEN
:
328 $result = $this->createWidget_Hidden();
330 case SMOPT_TYPE_COMMENT
:
331 $result = $this->createWidget_Comment();
333 case SMOPT_TYPE_FLDRLIST
:
334 $result = $this->createWidget_FolderList();
336 case SMOPT_TYPE_FLDRLIST_MULTI
:
337 $result = $this->createWidget_FolderList(TRUE);
339 case SMOPT_TYPE_EDIT_LIST
:
340 $result = $this->createWidget_EditList();
342 case SMOPT_TYPE_STRLIST_MULTI
:
343 $result = $this->createWidget_StrList(TRUE);
347 sprintf(_("Option Type '%s' Not Found"), $this->type
)
351 /* Add the "post script" for this option. */
352 $result .= $this->post_script
;
354 // put correct value back if need be
355 if (!empty($this->new_value
)) {
356 $this->value
= $tempValue;
359 /* Now, return the created widget. */
364 * Create string field
365 * @return string html formated option field
367 function createWidget_String() {
368 switch ($this->size
) {
369 case SMOPT_SIZE_TINY
:
372 case SMOPT_SIZE_SMALL
:
375 case SMOPT_SIZE_LARGE
:
378 case SMOPT_SIZE_HUGE
:
381 case SMOPT_SIZE_NORMAL
:
386 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
390 * Create selection box
392 * When $this->htmlencoded is TRUE, the keys and values in
393 * $this->possible_values are assumed to be display-safe.
396 * @param boolean $multiple_select When TRUE, the select widget
397 * will allow multiple selections
398 * (OPTIONAL; default is FALSE
399 * (single select list))
401 * @return string html formated selection box
404 function createWidget_StrList($multiple_select=FALSE) {
405 //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
407 switch ($this->size
) {
408 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
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 addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
431 * Create folder selection box
433 * @param boolean $multiple_select When TRUE, the select widget
434 * will allow multiple selections
435 * (OPTIONAL; default is FALSE
436 * (single select list))
438 * @return string html formated selection box
441 function createWidget_FolderList($multiple_select=FALSE) {
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 // possible values might include a nested array of
463 // possible values (list of folders)
465 $option_list = array();
466 foreach ($this->possible_values
as $value => $text) {
468 // list of folders (boxes array)
470 if (is_array($text)) {
471 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
473 // just one option here
476 $option_list = array_merge($option_list, array($value => $text));
480 if (empty($option_list))
481 $option_list = array('ignore' => _("unavailable"));
484 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
490 * @return string html formated textarea field
492 function createWidget_TextArea() {
493 switch ($this->size
) {
494 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
495 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
496 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
497 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
498 case SMOPT_SIZE_NORMAL
:
499 default: $rows = 5; $cols = 50;
501 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
505 * Creates field for integer
507 * Difference from createWidget_String is visible only when javascript is enabled
508 * @return string html formated option field
510 function createWidget_Integer() {
512 // add onChange javascript handler to a regular string widget
513 // which will strip out all non-numeric chars
514 if (checkForJavascript())
515 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
516 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
517 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
518 . 'this.value=newVal;';
520 return $this->createWidget_String();
524 * Creates field for floating number
525 * Difference from createWidget_String is visible only when javascript is enabled
526 * @return string html formated option field
528 function createWidget_Float() {
530 // add onChange javascript handler to a regular string widget
531 // which will strip out all non-numeric (period also OK) chars
532 if (checkForJavascript())
533 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
534 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
535 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
536 . 'newVal += origVal.charAt(i); } this.value=newVal;';
538 return $this->createWidget_String();
542 * Create boolean widget
544 * @param boolean $checkbox When TRUE, the widget will be
545 * constructed as a checkbox,
546 * otherwise it will be a set of
547 * Yes/No radio buttons (OPTIONAL;
548 * default is FALSE (radio buttons)).
550 * @return string html formated boolean widget
553 function createWidget_Boolean($checkbox=FALSE) {
555 global $oTemplate, $nbsp;
561 $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
);
568 /* Build the yes choice. */
569 $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');
571 /* Build the no choice. */
572 $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');
574 /* Build the combined "boolean widget". */
575 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
583 * Creates hidden field
584 * @return string html formated hidden input field
586 function createWidget_Hidden() {
587 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
592 * @return string comment
594 function createWidget_Comment() {
595 $result = $this->comment
;
600 * Creates an edit list
601 * @return string html formated list of edit fields and
602 * their associated controls
604 function createWidget_EditList() {
608 switch ($this->size
) {
609 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
610 case SMOPT_SIZE_TINY
:
613 case SMOPT_SIZE_SMALL
:
616 case SMOPT_SIZE_LARGE
:
619 case SMOPT_SIZE_HUGE
:
622 case SMOPT_SIZE_NORMAL
:
627 if (empty($this->possible_values
)) $this->possible_values
= array();
628 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
630 //FIXME: $this->aExtraAttribs probably should only be used in one place
631 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
632 $oTemplate->assign('trailing_text', $this->trailing_text
);
633 $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));
634 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
635 $oTemplate->assign('name', $this->name
);
636 return $oTemplate->fetch('edit_list_widget.tpl');
644 $function = $this->save_function
;
653 // edit lists have a lot going on, so we'll always process them
655 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
657 return ($this->value
!= $this->new_value
);
659 } /* End of SquirrelOption class*/
662 * Saves the option value (this is the default save function
663 * unless overridden by the user)
665 * @param object $option object that holds option name and new_value
667 function save_option($option) {
669 // Can't save the pref if we don't have the username
671 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
677 // edit lists: first add new elements to list, then
678 // remove any selected ones (note that we must add
679 // before deleting because the javascript that populates
680 // the "add" textbox when selecting items in the list
683 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
685 if (empty($option->possible_values
)) $option->possible_values
= array();
686 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
688 // add element if given
690 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
691 $new_element = trim($new_element);
692 if (!empty($new_element)
693 && !in_array($new_element, $option->possible_values
))
694 $option->possible_values
[] = $new_element;
697 // delete selected elements if needed
699 if (is_array($option->new_value
)
700 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
701 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
703 // save full list (stored in "possible_values")
705 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
707 // Certain option types need to be serialized because
708 // they are not scalar
710 } else if ($option->is_multiple_valued())
711 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
714 setPref($data_dir, $username, $option->name
, $option->new_value
);
719 * save function that does not save
720 * @param object $option
722 function save_option_noop($option) {
723 /* Do nothing here... */
727 * Create hidden 'optpage' input field with value set by argument
728 * @param string $optpage identification of option page
729 * @return string html formated hidden input field
731 function create_optpage_element($optpage) {
732 return addHidden('optpage', $optpage);
736 * Create hidden 'optmode' input field with value set by argument
737 * @param string $optmode
738 * @return string html formated hidden input field
740 function create_optmode_element($optmode) {
741 return addHidden('optmode', $optmode);
745 * @param array $optgrps
746 * @param array $optvals
749 function create_option_groups($optgrps, $optvals) {
750 /* Build a simple array with which to start. */
753 /* Create option group for each option group name. */
754 foreach ($optgrps as $grpkey => $grpname) {
755 $result[$grpkey] = array();
756 $result[$grpkey]['name'] = $grpname;
757 $result[$grpkey]['options'] = array();
760 /* Create a new SquirrelOption for each set of option values. */
761 foreach ($optvals as $grpkey => $grpopts) {
762 foreach ($grpopts as $optset) {
763 /* Create a new option with all values given. */
764 $next_option = new SquirrelOption(
768 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
769 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
770 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
771 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
774 /* If provided, set the size for this option. */
775 if (isset($optset['size'])) {
776 $next_option->setSize($optset['size']);
779 /* If provided, set the trailing_text for this option. */
780 if (isset($optset['trailing_text'])) {
781 $next_option->setTrailingText($optset['trailing_text']);
784 /* If provided, set the comment for this option. */
785 if (isset($optset['comment'])) {
786 $next_option->setComment($optset['comment']);
789 /* If provided, set the save function for this option. */
790 if (isset($optset['save'])) {
791 $next_option->setSaveFunction($optset['save']);
794 /* If provided, set the extra attributes for this option. */
795 if (isset($optset['extra_attributes'])) {
796 $next_option->setExtraAttributes($optset['extra_attributes']);
799 /* If provided, set the "post script" for this option. */
800 if (isset($optset['post_script'])) {
801 $next_option->setPostScript($optset['post_script']);
804 /* If provided, set the folder_filter for this option. */
805 if (isset($optset['folder_filter'])) {
806 $next_option->setFolderFilter($optset['folder_filter']);
809 /* Add this option to the option array. */
810 $result[$grpkey]['options'][] = $next_option;
814 /* Return our resulting array. */