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
;
206 * Set the value for this option.
207 * @param mixed $value
209 function setValue($value) {
210 $this->value
= $value;
214 * Set the new value for this option.
215 * @param mixed $new_value
217 function setNewValue($new_value) {
218 $this->new_value
= $new_value;
222 * Set the size for this option.
223 * @param integer $size
225 function setSize($size) {
230 * Set the trailing_text for this option.
231 * @param string $trailing_text
233 function setTrailingText($trailing_text) {
234 $this->trailing_text
= $trailing_text;
238 * Set the comment for this option.
239 * @param string $comment
241 function setComment($comment) {
242 $this->comment
= $comment;
246 * Set the extra attributes for this option.
247 * @param array $aExtraAttribs
249 function setExtraAttributes($aExtraAttribs) {
250 $this->aExtraAttribs
= $aExtraAttribs;
254 * Set the "post script" for this option.
255 * @param string $post_script
257 function setPostScript($post_script) {
258 $this->post_script
= $post_script;
262 * Set the save function for this option.
263 * @param string $save_function
265 function setSaveFunction($save_function) {
266 $this->save_function
= $save_function;
270 * Set the trailing_text for this option.
271 * @param string $folder_filter
274 function setFolderFilter($folder_filter) {
275 $this->folder_filter
= $folder_filter;
279 * Creates fields on option pages according to option type
281 * This is the function that calls all other createWidget* functions.
283 * @return string The formated option field
286 function createWidget() {
289 // Use new value if available
290 if (!empty($this->new_value
)) {
291 $tempValue = $this->value
;
292 $this->value
= $this->new_value
;
295 /* Get the widget for this option type. */
296 switch ($this->type
) {
297 case SMOPT_TYPE_STRING
:
298 $result = $this->createWidget_String();
300 case SMOPT_TYPE_STRLIST
:
301 $result = $this->createWidget_StrList();
303 case SMOPT_TYPE_TEXTAREA
:
304 $result = $this->createWidget_TextArea();
306 case SMOPT_TYPE_INTEGER
:
307 $result = $this->createWidget_Integer();
309 case SMOPT_TYPE_FLOAT
:
310 $result = $this->createWidget_Float();
312 case SMOPT_TYPE_BOOLEAN
:
313 $result = $this->createWidget_Boolean();
315 case SMOPT_TYPE_HIDDEN
:
316 $result = $this->createWidget_Hidden();
318 case SMOPT_TYPE_COMMENT
:
319 $result = $this->createWidget_Comment();
321 case SMOPT_TYPE_FLDRLIST
:
322 $result = $this->createWidget_FolderList();
324 case SMOPT_TYPE_FLDRLIST_MULTI
:
325 $result = $this->createWidget_FolderList(TRUE);
327 case SMOPT_TYPE_EDIT_LIST
:
328 $result = $this->createWidget_EditList();
330 case SMOPT_TYPE_STRLIST_MULTI
:
331 $result = $this->createWidget_StrList(TRUE);
335 sprintf(_("Option Type '%s' Not Found"), $this->type
)
339 /* Add the "post script" for this option. */
340 $result .= $this->post_script
;
342 // put correct value back if need be
343 if (!empty($this->new_value
)) {
344 $this->value
= $tempValue;
347 /* Now, return the created widget. */
352 * Create string field
353 * @return string html formated option field
355 function createWidget_String() {
356 switch ($this->size
) {
357 case SMOPT_SIZE_TINY
:
360 case SMOPT_SIZE_SMALL
:
363 case SMOPT_SIZE_LARGE
:
366 case SMOPT_SIZE_HUGE
:
369 case SMOPT_SIZE_NORMAL
:
374 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
378 * Create selection box
380 * When $this->htmlencoded is TRUE, the keys and values in
381 * $this->possible_values are assumed to be display-safe.
384 * @param boolean $multiple_select When TRUE, the select widget
385 * will allow multiple selections
386 * (OPTIONAL; default is FALSE
387 * (single select list))
389 * @return string html formated selection box
392 function createWidget_StrList($multiple_select=FALSE) {
393 //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
395 switch ($this->size
) {
396 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
397 case SMOPT_SIZE_TINY
:
400 case SMOPT_SIZE_SMALL
:
403 case SMOPT_SIZE_LARGE
:
406 case SMOPT_SIZE_HUGE
:
409 case SMOPT_SIZE_NORMAL
:
414 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . htmlspecialchars($this->trailing_text
);
419 * Create folder selection box
421 * @param boolean $multiple_select When TRUE, the select widget
422 * will allow multiple selections
423 * (OPTIONAL; default is FALSE
424 * (single select list))
426 * @return string html formated selection box
429 function createWidget_FolderList($multiple_select=FALSE) {
431 switch ($this->size
) {
432 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
433 case SMOPT_SIZE_TINY
:
436 case SMOPT_SIZE_SMALL
:
439 case SMOPT_SIZE_LARGE
:
442 case SMOPT_SIZE_HUGE
:
445 case SMOPT_SIZE_NORMAL
:
450 // possible values might include a nested array of
451 // possible values (list of folders)
453 $option_list = array();
454 foreach ($this->possible_values
as $value => $text) {
456 // list of folders (boxes array)
458 if (is_array($text)) {
459 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
461 // just one option here
464 $option_list = array_merge($option_list, array($value => $text));
468 if (empty($option_list))
469 $option_list = array('ignore' => _("unavailable"));
472 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
478 * @return string html formated textarea field
480 function createWidget_TextArea() {
481 switch ($this->size
) {
482 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
483 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
484 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
485 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
486 case SMOPT_SIZE_NORMAL
:
487 default: $rows = 5; $cols = 50;
489 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
493 * Creates field for integer
495 * Difference from createWidget_String is visible only when javascript is enabled
496 * @return string html formated option field
498 function createWidget_Integer() {
500 // add onChange javascript handler to a regular string widget
501 // which will strip out all non-numeric chars
502 if (checkForJavascript())
503 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
504 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
505 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
506 . 'this.value=newVal;';
508 return $this->createWidget_String();
512 * Creates field for floating number
513 * Difference from createWidget_String is visible only when javascript is enabled
514 * @return string html formated option field
516 function createWidget_Float() {
518 // add onChange javascript handler to a regular string widget
519 // which will strip out all non-numeric (period also OK) chars
520 if (checkForJavascript())
521 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
522 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
523 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
524 . 'newVal += origVal.charAt(i); } this.value=newVal;';
526 return $this->createWidget_String();
530 * Creates radio field (yes/no)
531 * @return string html formated radio field
533 function createWidget_Boolean() {
535 global $oTemplate, $nbsp;
537 /* Build the yes choice. */
538 $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');
540 /* Build the no choice. */
541 $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');
543 /* Build and return the combined "boolean widget". */
544 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
549 * Creates hidden field
550 * @return string html formated hidden input field
552 function createWidget_Hidden() {
553 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
558 * @return string comment
560 function createWidget_Comment() {
561 $result = $this->comment
;
566 * Creates an edit list
567 * @return string html formated list of edit fields and
568 * their associated controls
570 function createWidget_EditList() {
574 switch ($this->size
) {
575 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
576 case SMOPT_SIZE_TINY
:
579 case SMOPT_SIZE_SMALL
:
582 case SMOPT_SIZE_LARGE
:
585 case SMOPT_SIZE_HUGE
:
588 case SMOPT_SIZE_NORMAL
:
593 if (empty($this->possible_values
)) $this->possible_values
= array();
595 //FIXME: $this->aExtraAttribs probably should only be used in one place
596 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
597 $oTemplate->assign('trailing_text', $this->trailing_text
);
598 $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));
599 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
600 $oTemplate->assign('name', $this->name
);
601 return $oTemplate->fetch('edit_list_widget.tpl');
609 $function = $this->save_function
;
618 // edit lists have a lot going on, so we'll always process them
620 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
622 return ($this->value
!= $this->new_value
);
624 } /* End of SquirrelOption class*/
627 * Saves the option value (this is the default save function
628 * unless overridden by the user)
630 * @param object $option object that holds option name and new_value
632 function save_option($option) {
634 // Can't save the pref if we don't have the username
636 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
642 // edit lists: first add new elements to list, then
643 // remove any selected ones (note that we must add
644 // before deleting because the javascript that populates
645 // the "add" textbox when selecting items in the list
648 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
650 if (empty($option->possible_values
)) $option->possible_values
= array();
651 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
653 // add element if given
655 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
656 $new_element = trim($new_element);
657 if (!empty($new_element)
658 && !in_array($new_element, $option->possible_values
))
659 $option->possible_values
[] = $new_element;
662 // delete selected elements if needed
664 if (is_array($option->new_value
)
665 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
666 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
668 // save full list (stored in "possible_values")
670 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
672 // Certain option types need to be serialized because
673 // they are not scalar
675 } else if ($option->type
== SMOPT_TYPE_FLDRLIST_MULTI
676 ||
$option->type
== SMOPT_TYPE_STRLIST_MULTI
)
677 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
680 setPref($data_dir, $username, $option->name
, $option->new_value
);
685 * save function that does not save
686 * @param object $option
688 function save_option_noop($option) {
689 /* Do nothing here... */
693 * Create hidden 'optpage' input field with value set by argument
694 * @param string $optpage identification of option page
695 * @return string html formated hidden input field
697 function create_optpage_element($optpage) {
698 return addHidden('optpage', $optpage);
702 * Create hidden 'optmode' input field with value set by argument
703 * @param string $optmode
704 * @return string html formated hidden input field
706 function create_optmode_element($optmode) {
707 return addHidden('optmode', $optmode);
711 * @param array $optgrps
712 * @param array $optvals
715 function create_option_groups($optgrps, $optvals) {
716 /* Build a simple array with which to start. */
719 /* Create option group for each option group name. */
720 foreach ($optgrps as $grpkey => $grpname) {
721 $result[$grpkey] = array();
722 $result[$grpkey]['name'] = $grpname;
723 $result[$grpkey]['options'] = array();
726 /* Create a new SquirrelOption for each set of option values. */
727 foreach ($optvals as $grpkey => $grpopts) {
728 foreach ($grpopts as $optset) {
729 /* Create a new option with all values given. */
730 $next_option = new SquirrelOption(
734 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
735 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
736 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
737 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
740 /* If provided, set the size for this option. */
741 if (isset($optset['size'])) {
742 $next_option->setSize($optset['size']);
745 /* If provided, set the trailing_text for this option. */
746 if (isset($optset['trailing_text'])) {
747 $next_option->setTrailingText($optset['trailing_text']);
750 /* If provided, set the comment for this option. */
751 if (isset($optset['comment'])) {
752 $next_option->setComment($optset['comment']);
755 /* If provided, set the save function for this option. */
756 if (isset($optset['save'])) {
757 $next_option->setSaveFunction($optset['save']);
760 /* If provided, set the extra attributes for this option. */
761 if (isset($optset['extra_attributes'])) {
762 $next_option->setExtraAttributes($optset['extra_attributes']);
765 /* If provided, set the "post script" for this option. */
766 if (isset($optset['post_script'])) {
767 $next_option->setPostScript($optset['post_script']);
770 /* If provided, set the folder_filter for this option. */
771 if (isset($optset['folder_filter'])) {
772 $next_option->setFolderFilter($optset['folder_filter']);
775 /* Add this option to the option array. */
776 $result[$grpkey]['options'][] = $next_option;
780 /* Return our resulting array. */