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);
33 /* Define constants for the options refresh levels. */
34 define('SMOPT_REFRESH_NONE', 0);
35 define('SMOPT_REFRESH_FOLDERLIST', 1);
36 define('SMOPT_REFRESH_ALL', 2);
38 /* Define constants for the options size. */
39 define('SMOPT_SIZE_TINY', 0);
40 define('SMOPT_SIZE_SMALL', 1);
41 define('SMOPT_SIZE_MEDIUM', 2);
42 define('SMOPT_SIZE_LARGE', 3);
43 define('SMOPT_SIZE_HUGE', 4);
44 define('SMOPT_SIZE_NORMAL', 5);
46 define('SMOPT_SAVE_DEFAULT', 'save_option');
47 define('SMOPT_SAVE_NOOP', 'save_option_noop');
50 * SquirrelOption: An option for SquirrelMail.
52 * @package squirrelmail
55 class SquirrelOption
{
57 * The name of this setting
62 * The text that prefaces setting on the preferences page
67 * The type of INPUT element
69 * See SMOPT_TYPE_* defines
74 * Indicates if a link should be shown to refresh part
75 * or all of the window
77 * See SMOPT_REFRESH_* defines
82 * Specifies the size of certain input items
84 * See SMOPT_SIZE_* defines
89 * Text that follows a text input or
90 * select list input on the preferences page
92 * useful for indicating units, meanings of special values, etc.
97 * text displayed to the user
99 * Used with SMOPT_TYPE_COMMENT options
104 * additional javascript or other widget attributes added to the
105 * user input; must be an array where keys are attribute names
106 * ("onclick", etc) and values are the attribute values.
111 * script (usually Javascript) that will be placed after (outside of)
118 * The name of the Save Function for this option.
123 /* The various 'values' for this options. */
125 * default/preselected value for this option
135 * associative array, where each key is an actual input value
136 * and the corresponding value is what is displayed to the user
137 * for that list item in the drop-down list
140 var $possible_values;
142 * disables html sanitizing.
144 * WARNING - don't use it, if user input is possible in option
145 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
148 var $htmlencoded=false;
150 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
151 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
152 * See $flag argument in sqimap_mailbox_option_list() function.
156 var $folder_filter='noselect';
159 * Constructor function
160 * @param string $name
161 * @param string $caption
162 * @param integer $type
163 * @param integer $refresh_level
164 * @param mixed $initial_value
165 * @param array $possible_values
166 * @param bool $htmlencoded
168 function SquirrelOption
169 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
170 /* Set the basic stuff. */
172 $this->caption
= $caption;
174 $this->refresh_level
= $refresh_level;
175 $this->possible_values
= $possible_values;
176 $this->htmlencoded
= $htmlencoded;
177 $this->size
= SMOPT_SIZE_MEDIUM
;
178 $this->trailing_text
= '';
180 $this->aExtraAttribs
= array();
181 $this->post_script
= '';
183 //Check for a current value.
184 if (isset($GLOBALS[$name])) {
185 $this->value
= $GLOBALS[$name];
186 } else if (!empty($initial_value)) {
187 $this->value
= $initial_value;
192 /* Check for a new value. */
193 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
194 $this->new_value
= '';
197 /* Set the default save function. */
198 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
199 $this->save_function
= SMOPT_SAVE_DEFAULT
;
201 $this->save_function
= SMOPT_SAVE_NOOP
;
205 /** Convenience function that identifies which types of
206 widgets are stored as (serialized) array values. */
207 function is_multiple_valued() {
208 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
209 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
210 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
);
214 * Set the value for this option.
215 * @param mixed $value
217 function setValue($value) {
218 $this->value
= $value;
222 * Set the new value for this option.
223 * @param mixed $new_value
225 function setNewValue($new_value) {
226 $this->new_value
= $new_value;
230 * Set the size for this option.
231 * @param integer $size
233 function setSize($size) {
238 * Set the trailing_text for this option.
239 * @param string $trailing_text
241 function setTrailingText($trailing_text) {
242 $this->trailing_text
= $trailing_text;
246 * Set the comment for this option.
247 * @param string $comment
249 function setComment($comment) {
250 $this->comment
= $comment;
254 * Set the extra attributes for this option.
255 * @param array $aExtraAttribs
257 function setExtraAttributes($aExtraAttribs) {
258 $this->aExtraAttribs
= $aExtraAttribs;
262 * Set the "post script" for this option.
263 * @param string $post_script
265 function setPostScript($post_script) {
266 $this->post_script
= $post_script;
270 * Set the save function for this option.
271 * @param string $save_function
273 function setSaveFunction($save_function) {
274 $this->save_function
= $save_function;
278 * Set the trailing_text for this option.
279 * @param string $folder_filter
282 function setFolderFilter($folder_filter) {
283 $this->folder_filter
= $folder_filter;
287 * Creates fields on option pages according to option type
289 * This is the function that calls all other createWidget* functions.
291 * @return string The formated option field
294 function createWidget() {
297 // Use new value if available
298 if (!empty($this->new_value
)) {
299 $tempValue = $this->value
;
300 $this->value
= $this->new_value
;
303 /* Get the widget for this option type. */
304 switch ($this->type
) {
305 case SMOPT_TYPE_STRING
:
306 $result = $this->createWidget_String();
308 case SMOPT_TYPE_STRLIST
:
309 $result = $this->createWidget_StrList();
311 case SMOPT_TYPE_TEXTAREA
:
312 $result = $this->createWidget_TextArea();
314 case SMOPT_TYPE_INTEGER
:
315 $result = $this->createWidget_Integer();
317 case SMOPT_TYPE_FLOAT
:
318 $result = $this->createWidget_Float();
320 case SMOPT_TYPE_BOOLEAN
:
321 $result = $this->createWidget_Boolean();
323 case SMOPT_TYPE_HIDDEN
:
324 $result = $this->createWidget_Hidden();
326 case SMOPT_TYPE_COMMENT
:
327 $result = $this->createWidget_Comment();
329 case SMOPT_TYPE_FLDRLIST
:
330 $result = $this->createWidget_FolderList();
332 case SMOPT_TYPE_FLDRLIST_MULTI
:
333 $result = $this->createWidget_FolderList(TRUE);
335 case SMOPT_TYPE_EDIT_LIST
:
336 $result = $this->createWidget_EditList();
338 case SMOPT_TYPE_STRLIST_MULTI
:
339 $result = $this->createWidget_StrList(TRUE);
343 sprintf(_("Option Type '%s' Not Found"), $this->type
)
347 /* Add the "post script" for this option. */
348 $result .= $this->post_script
;
350 // put correct value back if need be
351 if (!empty($this->new_value
)) {
352 $this->value
= $tempValue;
355 /* Now, return the created widget. */
360 * Create string field
361 * @return string html formated option field
363 function createWidget_String() {
364 switch ($this->size
) {
365 case SMOPT_SIZE_TINY
:
368 case SMOPT_SIZE_SMALL
:
371 case SMOPT_SIZE_LARGE
:
374 case SMOPT_SIZE_HUGE
:
377 case SMOPT_SIZE_NORMAL
:
382 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
386 * Create selection box
388 * When $this->htmlencoded is TRUE, the keys and values in
389 * $this->possible_values are assumed to be display-safe.
392 * @param boolean $multiple_select When TRUE, the select widget
393 * will allow multiple selections
394 * (OPTIONAL; default is FALSE
395 * (single select list))
397 * @return string html formated selection box
400 function createWidget_StrList($multiple_select=FALSE) {
401 //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
403 switch ($this->size
) {
404 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
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 addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
427 * Create folder selection box
429 * @param boolean $multiple_select When TRUE, the select widget
430 * will allow multiple selections
431 * (OPTIONAL; default is FALSE
432 * (single select list))
434 * @return string html formated selection box
437 function createWidget_FolderList($multiple_select=FALSE) {
439 switch ($this->size
) {
440 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
441 case SMOPT_SIZE_TINY
:
444 case SMOPT_SIZE_SMALL
:
447 case SMOPT_SIZE_LARGE
:
450 case SMOPT_SIZE_HUGE
:
453 case SMOPT_SIZE_NORMAL
:
458 // possible values might include a nested array of
459 // possible values (list of folders)
461 $option_list = array();
462 foreach ($this->possible_values
as $value => $text) {
464 // list of folders (boxes array)
466 if (is_array($text)) {
467 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
469 // just one option here
472 $option_list = array_merge($option_list, array($value => $text));
476 if (empty($option_list))
477 $option_list = array('ignore' => _("unavailable"));
480 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
486 * @return string html formated textarea field
488 function createWidget_TextArea() {
489 switch ($this->size
) {
490 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
491 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
492 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
493 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
494 case SMOPT_SIZE_NORMAL
:
495 default: $rows = 5; $cols = 50;
497 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
501 * Creates field for integer
503 * Difference from createWidget_String is visible only when javascript is enabled
504 * @return string html formated option field
506 function createWidget_Integer() {
508 // add onChange javascript handler to a regular string widget
509 // which will strip out all non-numeric chars
510 if (checkForJavascript())
511 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
512 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
513 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
514 . 'this.value=newVal;';
516 return $this->createWidget_String();
520 * Creates field for floating number
521 * Difference from createWidget_String is visible only when javascript is enabled
522 * @return string html formated option field
524 function createWidget_Float() {
526 // add onChange javascript handler to a regular string widget
527 // which will strip out all non-numeric (period also OK) chars
528 if (checkForJavascript())
529 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
530 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
531 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
532 . 'newVal += origVal.charAt(i); } this.value=newVal;';
534 return $this->createWidget_String();
538 * Creates radio field (yes/no)
539 * @return string html formated radio field
541 function createWidget_Boolean() {
543 global $oTemplate, $nbsp;
545 /* Build the yes choice. */
546 $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');
548 /* Build the no choice. */
549 $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');
551 /* Build and return the combined "boolean widget". */
552 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
557 * Creates hidden field
558 * @return string html formated hidden input field
560 function createWidget_Hidden() {
561 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
566 * @return string comment
568 function createWidget_Comment() {
569 $result = $this->comment
;
574 * Creates an edit list
575 * @return string html formated list of edit fields and
576 * their associated controls
578 function createWidget_EditList() {
582 switch ($this->size
) {
583 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
584 case SMOPT_SIZE_TINY
:
587 case SMOPT_SIZE_SMALL
:
590 case SMOPT_SIZE_LARGE
:
593 case SMOPT_SIZE_HUGE
:
596 case SMOPT_SIZE_NORMAL
:
601 if (empty($this->possible_values
)) $this->possible_values
= array();
602 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
604 //FIXME: $this->aExtraAttribs probably should only be used in one place
605 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
606 $oTemplate->assign('trailing_text', $this->trailing_text
);
607 $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));
608 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
609 $oTemplate->assign('name', $this->name
);
610 return $oTemplate->fetch('edit_list_widget.tpl');
618 $function = $this->save_function
;
627 // edit lists have a lot going on, so we'll always process them
629 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
631 return ($this->value
!= $this->new_value
);
633 } /* End of SquirrelOption class*/
636 * Saves the option value (this is the default save function
637 * unless overridden by the user)
639 * @param object $option object that holds option name and new_value
641 function save_option($option) {
643 // Can't save the pref if we don't have the username
645 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
651 // edit lists: first add new elements to list, then
652 // remove any selected ones (note that we must add
653 // before deleting because the javascript that populates
654 // the "add" textbox when selecting items in the list
657 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
659 if (empty($option->possible_values
)) $option->possible_values
= array();
660 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
662 // add element if given
664 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
665 $new_element = trim($new_element);
666 if (!empty($new_element)
667 && !in_array($new_element, $option->possible_values
))
668 $option->possible_values
[] = $new_element;
671 // delete selected elements if needed
673 if (is_array($option->new_value
)
674 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
675 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
677 // save full list (stored in "possible_values")
679 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
681 // Certain option types need to be serialized because
682 // they are not scalar
684 } else if ($option->is_multiple_valued())
685 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
688 setPref($data_dir, $username, $option->name
, $option->new_value
);
693 * save function that does not save
694 * @param object $option
696 function save_option_noop($option) {
697 /* Do nothing here... */
701 * Create hidden 'optpage' input field with value set by argument
702 * @param string $optpage identification of option page
703 * @return string html formated hidden input field
705 function create_optpage_element($optpage) {
706 return addHidden('optpage', $optpage);
710 * Create hidden 'optmode' input field with value set by argument
711 * @param string $optmode
712 * @return string html formated hidden input field
714 function create_optmode_element($optmode) {
715 return addHidden('optmode', $optmode);
719 * @param array $optgrps
720 * @param array $optvals
723 function create_option_groups($optgrps, $optvals) {
724 /* Build a simple array with which to start. */
727 /* Create option group for each option group name. */
728 foreach ($optgrps as $grpkey => $grpname) {
729 $result[$grpkey] = array();
730 $result[$grpkey]['name'] = $grpname;
731 $result[$grpkey]['options'] = array();
734 /* Create a new SquirrelOption for each set of option values. */
735 foreach ($optvals as $grpkey => $grpopts) {
736 foreach ($grpopts as $optset) {
737 /* Create a new option with all values given. */
738 $next_option = new SquirrelOption(
742 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
743 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
744 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
745 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
748 /* If provided, set the size for this option. */
749 if (isset($optset['size'])) {
750 $next_option->setSize($optset['size']);
753 /* If provided, set the trailing_text for this option. */
754 if (isset($optset['trailing_text'])) {
755 $next_option->setTrailingText($optset['trailing_text']);
758 /* If provided, set the comment for this option. */
759 if (isset($optset['comment'])) {
760 $next_option->setComment($optset['comment']);
763 /* If provided, set the save function for this option. */
764 if (isset($optset['save'])) {
765 $next_option->setSaveFunction($optset['save']);
768 /* If provided, set the extra attributes for this option. */
769 if (isset($optset['extra_attributes'])) {
770 $next_option->setExtraAttributes($optset['extra_attributes']);
773 /* If provided, set the "post script" for this option. */
774 if (isset($optset['post_script'])) {
775 $next_option->setPostScript($optset['post_script']);
778 /* If provided, set the folder_filter for this option. */
779 if (isset($optset['folder_filter'])) {
780 $next_option->setFolderFilter($optset['folder_filter']);
783 /* Add this option to the option array. */
784 $result[$grpkey]['options'][] = $next_option;
788 /* Return our resulting array. */