6 * Functions needed to display the options pages.
8 * @copyright © 1999-2006 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);
30 /* Define constants for the options refresh levels. */
31 define('SMOPT_REFRESH_NONE', 0);
32 define('SMOPT_REFRESH_FOLDERLIST', 1);
33 define('SMOPT_REFRESH_ALL', 2);
35 /* Define constants for the options size. */
36 define('SMOPT_SIZE_TINY', 0);
37 define('SMOPT_SIZE_SMALL', 1);
38 define('SMOPT_SIZE_MEDIUM', 2);
39 define('SMOPT_SIZE_LARGE', 3);
40 define('SMOPT_SIZE_HUGE', 4);
41 define('SMOPT_SIZE_NORMAL', 5);
43 define('SMOPT_SAVE_DEFAULT', 'save_option');
44 define('SMOPT_SAVE_NOOP', 'save_option_noop');
47 * SquirrelOption: An option for SquirrelMail.
49 * @package squirrelmail
52 class SquirrelOption
{
54 * The name of this setting
59 * The text that prefaces setting on the preferences page
64 * The type of INPUT element
66 * See SMOPT_TYPE_* defines
71 * Indicates if a link should be shown to refresh part
72 * or all of the window
74 * See SMOPT_REFRESH_* defines
79 * Specifies the size of certain input items
81 * See SMOPT_SIZE_* defines
86 * Text that follows a text input or
87 * select list input on the preferences page
89 * useful for indicating units, meanings of special values, etc.
94 * text displayed to the user
96 * Used with SMOPT_TYPE_COMMENT options
101 * additional javascript or other code added to the user input
106 * script (usually Javascript) that will be placed after (outside of)
113 * The name of the Save Function for this option.
118 /* The various 'values' for this options. */
120 * default/preselected value for this option
130 * associative array, where each key is an actual input value
131 * and the corresponding value is what is displayed to the user
132 * for that list item in the drop-down list
135 var $possible_values;
137 * disables html sanitizing.
139 * WARNING - don't use it, if user input is possible in option
140 * or use own sanitizing functions. Currently works only with
141 * SMOPT_TYPE_STRLIST.
144 var $htmlencoded=false;
146 * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
147 * See $flag argument in sqimap_mailbox_option_list() function.
151 var $folder_filter='noselect';
154 * Constructor function
155 * @param string $name
156 * @param string $caption
157 * @param integer $type
158 * @param integer $refresh_level
159 * @param mixed $initial_value
160 * @param array $possible_values
161 * @param bool $htmlencoded
163 function SquirrelOption
164 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
165 /* Set the basic stuff. */
167 $this->caption
= $caption;
169 $this->refresh_level
= $refresh_level;
170 $this->possible_values
= $possible_values;
171 $this->htmlencoded
= $htmlencoded;
172 $this->size
= SMOPT_SIZE_MEDIUM
;
173 $this->trailing_text
= '';
176 $this->post_script
= '';
179 * Check for a current value. If the $GLOBALS[] value exists, we also
180 * need to make sure it is the same data type as the initial value to
181 * make sure we are looking at the correct preference.
184 if (!empty($initial_value)) {
185 $var_type = gettype($initial_value);
186 } elseif (is_array($possible_values)) {
187 list($index, $x) = each ($possible_values);
188 $var_type = gettype($index);
191 if (isset($GLOBALS[$name]) && (is_null($var_type) ?
true : $var_type == gettype($GLOBALS[$name]))) {
192 $this->value
= $GLOBALS[$name];
193 } else if (!empty($initial_value)) {
194 $this->value
= $initial_value;
199 /* Check for a new value. */
200 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
201 $this->new_value
= '';
204 /* Set the default save function. */
205 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
206 $this->save_function
= SMOPT_SAVE_DEFAULT
;
208 $this->save_function
= SMOPT_SAVE_NOOP
;
213 * Set the value for this option.
214 * @param mixed $value
216 function setValue($value) {
217 $this->value
= $value;
221 * Set the new value for this option.
222 * @param mixed $new_value
224 function setNewValue($new_value) {
225 $this->new_value
= $new_value;
229 * Set the size for this option.
230 * @param integer $size
232 function setSize($size) {
237 * Set the trailing_text for this option.
238 * @param string $trailing_text
240 function setTrailingText($trailing_text) {
241 $this->trailing_text
= $trailing_text;
245 * Set the comment for this option.
246 * @param string $comment
248 function setComment($comment) {
249 $this->comment
= $comment;
253 * Set the script for this option.
254 * @param string $script
256 function setScript($script) {
257 $this->script
= $script;
261 * Set the "post script" for this option.
262 * @param string $post_script
264 function setPostScript($post_script) {
265 $this->post_script
= $post_script;
269 * Set the save function for this option.
270 * @param string $save_function
272 function setSaveFunction($save_function) {
273 $this->save_function
= $save_function;
277 * Set the trailing_text for this option.
278 * @param string $folder_filter
281 function setFolderFilter($folder_filter) {
282 $this->folder_filter
= $folder_filter;
286 * Creates fields on option pages according to option type
288 * Function that calls other createWidget* functions.
289 * @return string html formated option field
291 function createHTMLWidget() {
294 // Use new value if available
295 if (!empty($this->new_value
)) {
296 $tempValue = $this->value
;
297 $this->value
= $this->new_value
;
300 /* Get the widget for this option type. */
301 switch ($this->type
) {
302 case SMOPT_TYPE_STRING
:
303 $result = $this->createWidget_String();
305 case SMOPT_TYPE_STRLIST
:
306 $result = $this->createWidget_StrList();
308 case SMOPT_TYPE_TEXTAREA
:
309 $result = $this->createWidget_TextArea();
311 case SMOPT_TYPE_INTEGER
:
312 $result = $this->createWidget_Integer();
314 case SMOPT_TYPE_FLOAT
:
315 $result = $this->createWidget_Float();
317 case SMOPT_TYPE_BOOLEAN
:
318 $result = $this->createWidget_Boolean();
320 case SMOPT_TYPE_HIDDEN
:
321 $result = $this->createWidget_Hidden();
323 case SMOPT_TYPE_COMMENT
:
324 $result = $this->createWidget_Comment();
326 case SMOPT_TYPE_FLDRLIST
:
327 $result = $this->createWidget_FolderList();
330 $result = '<font color="' . $color[2] . '">'
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 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
371 htmlspecialchars($this->value
) .
372 "\" size=\"$width\" $this->script />$this->trailing_text\n";
377 * Create selection box
378 * @return string html formated selection box
380 function createWidget_StrList() {
381 /* Begin the select tag. */
382 $result = "<select name=\"new_$this->name\" $this->script>\n";
384 /* Add each possible value to the select list. */
385 foreach ($this->possible_values
as $real_value => $disp_value) {
386 /* Start the next new option string. */
387 $new_option = '<option value="' .
388 ($this->htmlencoded ?
$real_value : htmlspecialchars($real_value)) . '"';
390 /* If this value is the current value, select it. */
391 if ($real_value == $this->value
) {
392 $new_option .= ' selected="selected"';
395 /* Add the display value to our option string. */
396 $new_option .= '>' . ($this->htmlencoded ?
$disp_value : htmlspecialchars($disp_value)) . "</option>\n";
398 /* And add the new option string to our select tag. */
399 $result .= $new_option;
402 /* Close the select tag and return our happy result. */
403 $result .= "</select>$this->trailing_text\n";
408 * Create folder selection box
409 * @return string html formated selection box
411 function createWidget_FolderList() {
412 $selected = array(strtolower($this->value
));
414 /* set initial value */
417 /* Add each possible value to the select list. */
418 foreach ($this->possible_values
as $real_value => $disp_value) {
419 if ( is_array($disp_value) ) {
420 /* For folder list, we passed in the array of boxes.. */
421 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value, $this->folder_filter
);
423 /* Start the next new option string. */
424 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
426 /* If this value is the current value, select it. */
427 if ($real_value == $this->value
) {
428 $new_option .= ' selected="selected"';
431 /* Add the display value to our option string. */
432 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
434 /* And add the new option string to our select tag. */
435 $result .= $new_option;
439 if (empty($result)) {
440 // string is displayed when interface can't build folder selection box
441 return _("unavailable");
443 /* Begin the select tag. */
444 $ret = "<select name=\"new_$this->name\" $this->script>\n";
446 /* Close the select tag and return our happy result. */
447 $ret.= "</select>\n";
454 * @return string html formated textarea field
456 function createWidget_TextArea() {
457 switch ($this->size
) {
458 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
459 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
460 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
461 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
462 case SMOPT_SIZE_NORMAL
:
463 default: $rows = 5; $cols = 50;
465 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
466 . "cols=\"$cols\" $this->script>"
467 . htmlspecialchars($this->value
) . "</textarea>\n";
472 * Creates field for integer
474 * Difference from createWidget_String is visible only when javascript is enabled
475 * @return string html formated option field
477 function createWidget_Integer() {
478 global $javascript_on;
480 // add onChange javascript handler to a regular string widget
481 // which will strip out all non-numeric chars
483 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
484 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
485 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
486 . 'this.value=newVal;" />', $this->createWidget_String());
488 return $this->createWidget_String();
492 * Creates field for floating number
493 * Difference from createWidget_String is visible only when javascript is enabled
494 * @return string html formated option field
496 function createWidget_Float() {
497 global $javascript_on;
499 // add onChange javascript handler to a regular string widget
500 // which will strip out all non-numeric (period also OK) chars
502 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
503 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
504 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
505 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
506 , $this->createWidget_String());
508 return $this->createWidget_String();
512 * Creates radio field (yes/no)
513 * @return string html formated radio field
515 function createWidget_Boolean() {
516 /* Do the whole current value thing. */
517 if ($this->value
!= SMPREF_NO
) {
518 $yes_chk = ' checked="checked"';
522 $no_chk = ' checked="checked"';
525 /* Build the yes choice. */
526 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
527 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
528 . $yes_chk . ' ' . $this->script
. ' /> '
529 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
531 /* Build the no choice. */
532 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
533 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
534 . $no_chk . ' ' . $this->script
. ' /> '
535 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
537 /* Build and return the combined "boolean widget". */
538 $result = "$yes_option $no_option";
543 * Creates hidden field
544 * @return string html formated hidden input field
546 function createWidget_Hidden() {
547 $result = '<input type="hidden" name="new_' . $this->name
548 . '" value="' . htmlspecialchars($this->value
)
549 . '" ' . $this->script
. ' />';
555 * @return string comment
557 function createWidget_Comment() {
558 $result = $this->comment
;
566 $function = $this->save_function
;
574 return ($this->value
!= $this->new_value
);
576 } /* End of SquirrelOption class*/
580 * @param object $option object that holds option name and new_value
582 function save_option($option) {
583 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
584 /* Can't save the pref if we don't have the username */
588 setPref($data_dir, $username, $option->name
, $option->new_value
);
592 * save function that does not save
593 * @param object $option
595 function save_option_noop($option) {
596 /* Do nothing here... */
600 * Create hidden 'optpage' input field with value set by argument
601 * @param string $optpage identification of option page
602 * @return string html formated hidden input field
604 function create_optpage_element($optpage) {
605 return create_hidden_element('optpage', $optpage);
609 * Create hidden 'optmode' input field with value set by argument
610 * @param string $optmode
611 * @return string html formated hidden input field
613 function create_optmode_element($optmode) {
614 return create_hidden_element('optmode', $optmode);
618 * Create hidden field.
619 * @param string $name field name
620 * @param string $value field value
621 * @return string html formated hidden input field
623 function create_hidden_element($name, $value) {
624 $result = '<input type="hidden" '
625 . 'name="' . $name . '" '
626 . 'value="' . htmlspecialchars($value) . '" />';
631 * @param array $optgrps
632 * @param array $optvals
635 function create_option_groups($optgrps, $optvals) {
636 /* Build a simple array with which to start. */
639 /* Create option group for each option group name. */
640 foreach ($optgrps as $grpkey => $grpname) {
641 $result[$grpkey] = array();
642 $result[$grpkey]['name'] = $grpname;
643 $result[$grpkey]['options'] = array();
646 /* Create a new SquirrelOption for each set of option values. */
647 foreach ($optvals as $grpkey => $grpopts) {
648 foreach ($grpopts as $optset) {
649 /* Create a new option with all values given. */
650 $next_option = new SquirrelOption(
654 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
655 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
656 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
657 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
660 /* If provided, set the size for this option. */
661 if (isset($optset['size'])) {
662 $next_option->setSize($optset['size']);
665 /* If provided, set the trailing_text for this option. */
666 if (isset($optset['trailing_text'])) {
667 $next_option->setTrailingText($optset['trailing_text']);
670 /* If provided, set the comment for this option. */
671 if (isset($optset['comment'])) {
672 $next_option->setComment($optset['comment']);
675 /* If provided, set the save function for this option. */
676 if (isset($optset['save'])) {
677 $next_option->setSaveFunction($optset['save']);
680 /* If provided, set the script for this option. */
681 if (isset($optset['script'])) {
682 $next_option->setScript($optset['script']);
685 /* If provided, set the "post script" for this option. */
686 if (isset($optset['post_script'])) {
687 $next_option->setPostScript($optset['post_script']);
690 /* If provided, set the folder_filter for this option. */
691 if (isset($optset['folder_filter'])) {
692 $next_option->setFolderFilter($optset['folder_filter']);
695 /* Add this option to the option array. */
696 $result[$grpkey]['options'][] = $next_option;
700 /* Return our resulting array. */
705 * @param array $option_groups
707 function print_option_groups($option_groups) {
708 /* Print each option group. */
709 foreach ($option_groups as $next_optgrp) {
710 /* If it is not blank, print the name for this option group. */
711 if ($next_optgrp['name'] != '') {
712 echo html_tag( 'tr', "\n".
714 '<b>' . $next_optgrp['name'] . '</b>' ,
715 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
719 /* Print each option in this option group. */
720 foreach ($next_optgrp['options'] as $option) {
721 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
722 echo html_tag( 'tr', "\n".
723 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
724 html_tag( 'td', $option->createHTMLWidget(), 'left' )
727 echo $option->createHTMLWidget();
731 /* Print an empty row after this option group. */
733 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
739 * Create submit button inside table row.
740 * @param string $name
742 function OptionSubmit( $name ) {
744 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' )