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);
32 /* Define constants for the options refresh levels. */
33 define('SMOPT_REFRESH_NONE', 0);
34 define('SMOPT_REFRESH_FOLDERLIST', 1);
35 define('SMOPT_REFRESH_ALL', 2);
37 /* Define constants for the options size. */
38 define('SMOPT_SIZE_TINY', 0);
39 define('SMOPT_SIZE_SMALL', 1);
40 define('SMOPT_SIZE_MEDIUM', 2);
41 define('SMOPT_SIZE_LARGE', 3);
42 define('SMOPT_SIZE_HUGE', 4);
43 define('SMOPT_SIZE_NORMAL', 5);
45 define('SMOPT_SAVE_DEFAULT', 'save_option');
46 define('SMOPT_SAVE_NOOP', 'save_option_noop');
49 * SquirrelOption: An option for SquirrelMail.
51 * @package squirrelmail
54 class SquirrelOption
{
56 * The name of this setting
61 * The text that prefaces setting on the preferences page
66 * The type of INPUT element
68 * See SMOPT_TYPE_* defines
73 * Indicates if a link should be shown to refresh part
74 * or all of the window
76 * See SMOPT_REFRESH_* defines
81 * Specifies the size of certain input items
83 * See SMOPT_SIZE_* defines
88 * Text that follows a text input or
89 * select list input on the preferences page
91 * useful for indicating units, meanings of special values, etc.
96 * text displayed to the user
98 * Used with SMOPT_TYPE_COMMENT options
103 * additional javascript or other widget attributes added to the
104 * user input; must be an array where keys are attribute names
105 * ("onclick", etc) and values are the attribute values.
110 * script (usually Javascript) that will be placed after (outside of)
117 * The name of the Save Function for this option.
122 /* The various 'values' for this options. */
124 * default/preselected value for this option
134 * associative array, where each key is an actual input value
135 * and the corresponding value is what is displayed to the user
136 * for that list item in the drop-down list
139 var $possible_values;
141 * disables html sanitizing.
143 * WARNING - don't use it, if user input is possible in option
144 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
147 var $htmlencoded=false;
149 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
150 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
151 * See $flag argument in sqimap_mailbox_option_list() function.
155 var $folder_filter='noselect';
158 * Constructor function
159 * @param string $name
160 * @param string $caption
161 * @param integer $type
162 * @param integer $refresh_level
163 * @param mixed $initial_value
164 * @param array $possible_values
165 * @param bool $htmlencoded
167 function SquirrelOption
168 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
169 /* Set the basic stuff. */
171 $this->caption
= $caption;
173 $this->refresh_level
= $refresh_level;
174 $this->possible_values
= $possible_values;
175 $this->htmlencoded
= $htmlencoded;
176 $this->size
= SMOPT_SIZE_MEDIUM
;
177 $this->trailing_text
= '';
179 $this->aExtraAttribs
= array();
180 $this->post_script
= '';
182 //Check for a current value.
183 if (isset($GLOBALS[$name])) {
184 $this->value
= $GLOBALS[$name];
185 } else if (!empty($initial_value)) {
186 $this->value
= $initial_value;
191 /* Check for a new value. */
192 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
193 $this->new_value
= '';
196 /* Set the default save function. */
197 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
198 $this->save_function
= SMOPT_SAVE_DEFAULT
;
200 $this->save_function
= SMOPT_SAVE_NOOP
;
205 * Set the value for this option.
206 * @param mixed $value
208 function setValue($value) {
209 $this->value
= $value;
213 * Set the new value for this option.
214 * @param mixed $new_value
216 function setNewValue($new_value) {
217 $this->new_value
= $new_value;
221 * Set the size for this option.
222 * @param integer $size
224 function setSize($size) {
229 * Set the trailing_text for this option.
230 * @param string $trailing_text
232 function setTrailingText($trailing_text) {
233 $this->trailing_text
= $trailing_text;
237 * Set the comment for this option.
238 * @param string $comment
240 function setComment($comment) {
241 $this->comment
= $comment;
245 * Set the extra attributes for this option.
246 * @param array $aExtraAttribs
248 function setExtraAttributes($aExtraAttribs) {
249 $this->aExtraAttribs
= $aExtraAttribs;
253 * Set the "post script" for this option.
254 * @param string $post_script
256 function setPostScript($post_script) {
257 $this->post_script
= $post_script;
261 * Set the save function for this option.
262 * @param string $save_function
264 function setSaveFunction($save_function) {
265 $this->save_function
= $save_function;
269 * Set the trailing_text for this option.
270 * @param string $folder_filter
273 function setFolderFilter($folder_filter) {
274 $this->folder_filter
= $folder_filter;
278 * Creates fields on option pages according to option type
280 * This is the function that calls all other createWidget* functions.
282 * @return string The formated option field
285 function createWidget() {
288 // Use new value if available
289 if (!empty($this->new_value
)) {
290 $tempValue = $this->value
;
291 $this->value
= $this->new_value
;
294 /* Get the widget for this option type. */
295 switch ($this->type
) {
296 case SMOPT_TYPE_STRING
:
297 $result = $this->createWidget_String();
299 case SMOPT_TYPE_STRLIST
:
300 $result = $this->createWidget_StrList();
302 case SMOPT_TYPE_TEXTAREA
:
303 $result = $this->createWidget_TextArea();
305 case SMOPT_TYPE_INTEGER
:
306 $result = $this->createWidget_Integer();
308 case SMOPT_TYPE_FLOAT
:
309 $result = $this->createWidget_Float();
311 case SMOPT_TYPE_BOOLEAN
:
312 $result = $this->createWidget_Boolean();
314 case SMOPT_TYPE_HIDDEN
:
315 $result = $this->createWidget_Hidden();
317 case SMOPT_TYPE_COMMENT
:
318 $result = $this->createWidget_Comment();
320 case SMOPT_TYPE_FLDRLIST
:
321 $result = $this->createWidget_FolderList();
323 case SMOPT_TYPE_FLDRLIST_MULTI
:
324 $result = $this->createWidget_FolderList(TRUE);
326 case SMOPT_TYPE_EDIT_LIST
:
327 $result = $this->createWidget_EditList();
331 sprintf(_("Option Type '%s' Not Found"), $this->type
)
335 /* Add the "post script" for this option. */
336 $result .= $this->post_script
;
338 // put correct value back if need be
339 if (!empty($this->new_value
)) {
340 $this->value
= $tempValue;
343 /* Now, return the created widget. */
348 * Create string field
349 * @return string html formated option field
351 function createWidget_String() {
352 switch ($this->size
) {
353 case SMOPT_SIZE_TINY
:
356 case SMOPT_SIZE_SMALL
:
359 case SMOPT_SIZE_LARGE
:
362 case SMOPT_SIZE_HUGE
:
365 case SMOPT_SIZE_NORMAL
:
370 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
374 * Create selection box
376 * When $this->htmlencoded is TRUE, the keys and values in
377 * $this->possible_values are assumed to be display-safe.
380 * @return string html formated selection box
382 function createWidget_StrList() {
383 //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
384 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
389 * Create folder selection box
391 * @param boolean $multiple_select When TRUE, the select widget
392 * will allow multiple selections
393 * (OPTIONAL; default is FALSE
394 * (single select list))
396 * @return string html formated selection box
399 function createWidget_FolderList($multiple_select=FALSE) {
401 switch ($this->size
) {
402 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
403 case SMOPT_SIZE_TINY
:
406 case SMOPT_SIZE_SMALL
:
409 case SMOPT_SIZE_LARGE
:
412 case SMOPT_SIZE_HUGE
:
415 case SMOPT_SIZE_NORMAL
:
420 // possible values might include a nested array of
421 // possible values (list of folders)
423 $option_list = array();
424 foreach ($this->possible_values
as $value => $text) {
426 // list of folders (boxes array)
428 if (is_array($text)) {
429 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
431 // just one option here
434 $option_list = array_merge($option_list, array($value => $text));
438 if (empty($option_list))
439 $option_list = array('ignore' => _("unavailable"));
442 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . htmlspecialchars($this->trailing_text
);
448 * @return string html formated textarea field
450 function createWidget_TextArea() {
451 switch ($this->size
) {
452 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
453 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
454 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
455 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
456 case SMOPT_SIZE_NORMAL
:
457 default: $rows = 5; $cols = 50;
459 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
463 * Creates field for integer
465 * Difference from createWidget_String is visible only when javascript is enabled
466 * @return string html formated option field
468 function createWidget_Integer() {
470 // add onChange javascript handler to a regular string widget
471 // which will strip out all non-numeric chars
472 if (checkForJavascript())
473 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
474 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
475 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
476 . 'this.value=newVal;';
478 return $this->createWidget_String();
482 * Creates field for floating number
483 * Difference from createWidget_String is visible only when javascript is enabled
484 * @return string html formated option field
486 function createWidget_Float() {
488 // add onChange javascript handler to a regular string widget
489 // which will strip out all non-numeric (period also OK) chars
490 if (checkForJavascript())
491 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
492 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
493 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
494 . 'newVal += origVal.charAt(i); } this.value=newVal;';
496 return $this->createWidget_String();
500 * Creates radio field (yes/no)
501 * @return string html formated radio field
503 function createWidget_Boolean() {
505 global $oTemplate, $nbsp;
507 /* Build the yes choice. */
508 $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');
510 /* Build the no choice. */
511 $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');
513 /* Build and return the combined "boolean widget". */
514 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
519 * Creates hidden field
520 * @return string html formated hidden input field
522 function createWidget_Hidden() {
523 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
528 * @return string comment
530 function createWidget_Comment() {
531 $result = $this->comment
;
536 * Creates an edit list
537 * @return string html formated list of edit fields and
538 * their associated controls
540 function createWidget_EditList() {
544 switch ($this->size
) {
545 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
546 case SMOPT_SIZE_TINY
:
549 case SMOPT_SIZE_SMALL
:
552 case SMOPT_SIZE_LARGE
:
555 case SMOPT_SIZE_HUGE
:
558 case SMOPT_SIZE_NORMAL
:
563 //FIXME: $this->aExtraAttribs and $this->trailing_text probably should only be used in one place
564 //FIXME: might be nice to have this in a template file instead of creating layout here
565 return create_label(_("Add"), '')
566 . $nbsp . addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
)
567 . $br . 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) . htmlspecialchars($this->trailing_text
)
569 . addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
))
570 . $nbsp . create_label(_("Delete Selected"), 'delete_' . $this->name
);
578 $function = $this->save_function
;
587 // edit lists have a lot going on, so we'll always process them
589 if ($this->type
== SMOPT_TYPE_EDIT_LIST
) return TRUE;
591 return ($this->value
!= $this->new_value
);
593 } /* End of SquirrelOption class*/
596 * Saves the option value (this is the default save function
597 * unless overridden by the user)
599 * @param object $option object that holds option name and new_value
601 function save_option($option) {
603 // Can't save the pref if we don't have the username
605 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
611 // edit lists: first add new elements to list, then
612 // remove any selected ones (note that we must add
613 // before deleting because the javascript that populates
614 // the "add" textbox when selecting items in the list
617 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
619 // add element if given
621 if (sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
622 $new_element = trim($new_element);
623 if (!empty($new_element)
624 && !in_array($new_element, $option->possible_values
))
625 $option->possible_values
[] = $new_element;
628 // delete selected elements if needed
630 if (is_array($option->new_value
)
631 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
632 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
634 // save full list (stored in "possible_values")
636 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
638 // Certain option types need to be serialized because
639 // they are not scalar
641 } else if ($option->type
== SMOPT_TYPE_FLDRLIST_MULTI
)
642 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
645 setPref($data_dir, $username, $option->name
, $option->new_value
);
650 * save function that does not save
651 * @param object $option
653 function save_option_noop($option) {
654 /* Do nothing here... */
658 * Create hidden 'optpage' input field with value set by argument
659 * @param string $optpage identification of option page
660 * @return string html formated hidden input field
662 function create_optpage_element($optpage) {
663 return addHidden('optpage', $optpage);
667 * Create hidden 'optmode' input field with value set by argument
668 * @param string $optmode
669 * @return string html formated hidden input field
671 function create_optmode_element($optmode) {
672 return addHidden('optmode', $optmode);
676 * @param array $optgrps
677 * @param array $optvals
680 function create_option_groups($optgrps, $optvals) {
681 /* Build a simple array with which to start. */
684 /* Create option group for each option group name. */
685 foreach ($optgrps as $grpkey => $grpname) {
686 $result[$grpkey] = array();
687 $result[$grpkey]['name'] = $grpname;
688 $result[$grpkey]['options'] = array();
691 /* Create a new SquirrelOption for each set of option values. */
692 foreach ($optvals as $grpkey => $grpopts) {
693 foreach ($grpopts as $optset) {
694 /* Create a new option with all values given. */
695 $next_option = new SquirrelOption(
699 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
700 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
701 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
702 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
705 /* If provided, set the size for this option. */
706 if (isset($optset['size'])) {
707 $next_option->setSize($optset['size']);
710 /* If provided, set the trailing_text for this option. */
711 if (isset($optset['trailing_text'])) {
712 $next_option->setTrailingText($optset['trailing_text']);
715 /* If provided, set the comment for this option. */
716 if (isset($optset['comment'])) {
717 $next_option->setComment($optset['comment']);
720 /* If provided, set the save function for this option. */
721 if (isset($optset['save'])) {
722 $next_option->setSaveFunction($optset['save']);
725 /* If provided, set the extra attributes for this option. */
726 if (isset($optset['extra_attributes'])) {
727 $next_option->setExtraAttributes($optset['extra_attributes']);
730 /* If provided, set the "post script" for this option. */
731 if (isset($optset['post_script'])) {
732 $next_option->setPostScript($optset['post_script']);
735 /* If provided, set the folder_filter for this option. */
736 if (isset($optset['folder_filter'])) {
737 $next_option->setFolderFilter($optset['folder_filter']);
740 /* Add this option to the option array. */
741 $result[$grpkey]['options'][] = $next_option;
745 /* Return our resulting array. */