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
= '';
178 /* Check for a current value. */
179 if (!empty($initial_value)) {
180 $this->value
= $initial_value;
181 } else if (isset($GLOBALS[$name])) {
182 $this->value
= $GLOBALS[$name];
187 /* Check for a new value. */
188 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
189 $this->new_value
= '';
192 /* Set the default save function. */
193 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
194 $this->save_function
= SMOPT_SAVE_DEFAULT
;
196 $this->save_function
= SMOPT_SAVE_NOOP
;
201 * Set the value for this option.
202 * @param mixed $value
204 function setValue($value) {
205 $this->value
= $value;
209 * Set the new value for this option.
210 * @param mixed $new_value
212 function setNewValue($new_value) {
213 $this->new_value
= $new_value;
217 * Set the size for this option.
218 * @param integer $size
220 function setSize($size) {
225 * Set the trailing_text for this option.
226 * @param string $trailing_text
228 function setTrailingText($trailing_text) {
229 $this->trailing_text
= $trailing_text;
233 * Set the comment for this option.
234 * @param string $comment
236 function setComment($comment) {
237 $this->comment
= $comment;
241 * Set the script for this option.
242 * @param string $script
244 function setScript($script) {
245 $this->script
= $script;
249 * Set the "post script" for this option.
250 * @param string $post_script
252 function setPostScript($post_script) {
253 $this->post_script
= $post_script;
257 * Set the save function for this option.
258 * @param string $save_function
260 function setSaveFunction($save_function) {
261 $this->save_function
= $save_function;
265 * Set the trailing_text for this option.
266 * @param string $folder_filter
269 function setFolderFilter($folder_filter) {
270 $this->folder_filter
= $folder_filter;
274 * Creates fields on option pages according to option type
276 * Function that calls other createWidget* functions.
277 * @return string html formated option field
279 function createHTMLWidget() {
282 // Use new value if available
283 if (!empty($this->new_value
)) {
284 $tempValue = $this->value
;
285 $this->value
= $this->new_value
;
288 /* Get the widget for this option type. */
289 switch ($this->type
) {
290 case SMOPT_TYPE_STRING
:
291 $result = $this->createWidget_String();
293 case SMOPT_TYPE_STRLIST
:
294 $result = $this->createWidget_StrList();
296 case SMOPT_TYPE_TEXTAREA
:
297 $result = $this->createWidget_TextArea();
299 case SMOPT_TYPE_INTEGER
:
300 $result = $this->createWidget_Integer();
302 case SMOPT_TYPE_FLOAT
:
303 $result = $this->createWidget_Float();
305 case SMOPT_TYPE_BOOLEAN
:
306 $result = $this->createWidget_Boolean();
308 case SMOPT_TYPE_HIDDEN
:
309 $result = $this->createWidget_Hidden();
311 case SMOPT_TYPE_COMMENT
:
312 $result = $this->createWidget_Comment();
314 case SMOPT_TYPE_FLDRLIST
:
315 $result = $this->createWidget_FolderList();
318 $result = '<font color="' . $color[2] . '">'
319 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
323 /* Add the "post script" for this option. */
324 $result .= $this->post_script
;
326 // put correct value back if need be
327 if (!empty($this->new_value
)) {
328 $this->value
= $tempValue;
331 /* Now, return the created widget. */
336 * Create string field
337 * @return string html formated option field
339 function createWidget_String() {
340 switch ($this->size
) {
341 case SMOPT_SIZE_TINY
:
344 case SMOPT_SIZE_SMALL
:
347 case SMOPT_SIZE_LARGE
:
350 case SMOPT_SIZE_HUGE
:
353 case SMOPT_SIZE_NORMAL
:
358 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
359 htmlspecialchars($this->value
) .
360 "\" size=\"$width\" $this->script />$this->trailing_text\n";
365 * Create selection box
366 * @return string html formated selection box
368 function createWidget_StrList() {
369 /* Begin the select tag. */
370 $result = "<select name=\"new_$this->name\" $this->script>\n";
372 /* Add each possible value to the select list. */
373 foreach ($this->possible_values
as $real_value => $disp_value) {
374 /* Start the next new option string. */
375 $new_option = '<option value="' .
376 ($this->htmlencoded ?
$real_value : htmlspecialchars($real_value)) . '"';
378 /* If this value is the current value, select it. */
379 if ($real_value == $this->value
) {
380 $new_option .= ' selected="selected"';
383 /* Add the display value to our option string. */
384 $new_option .= '>' . ($this->htmlencoded ?
$disp_value : htmlspecialchars($disp_value)) . "</option>\n";
386 /* And add the new option string to our select tag. */
387 $result .= $new_option;
390 /* Close the select tag and return our happy result. */
391 $result .= "</select>$this->trailing_text\n";
396 * Create folder selection box
397 * @return string html formated selection box
399 function createWidget_FolderList() {
400 $selected = array(strtolower($this->value
));
402 /* set initial value */
405 /* Add each possible value to the select list. */
406 foreach ($this->possible_values
as $real_value => $disp_value) {
407 if ( is_array($disp_value) ) {
408 /* For folder list, we passed in the array of boxes.. */
409 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value, $this->folder_filter
);
411 /* Start the next new option string. */
412 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
414 /* If this value is the current value, select it. */
415 if ($real_value == $this->value
) {
416 $new_option .= ' selected="selected"';
419 /* Add the display value to our option string. */
420 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
422 /* And add the new option string to our select tag. */
423 $result .= $new_option;
427 if (empty($result)) {
428 // string is displayed when interface can't build folder selection box
429 return _("unavailable");
431 /* Begin the select tag. */
432 $ret = "<select name=\"new_$this->name\" $this->script>\n";
434 /* Close the select tag and return our happy result. */
435 $ret.= "</select>\n";
442 * @return string html formated textarea field
444 function createWidget_TextArea() {
445 switch ($this->size
) {
446 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
447 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
448 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
449 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
450 case SMOPT_SIZE_NORMAL
:
451 default: $rows = 5; $cols = 50;
453 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
454 . "cols=\"$cols\" $this->script>"
455 . htmlspecialchars($this->value
) . "</textarea>\n";
460 * Creates field for integer
462 * Difference from createWidget_String is visible only when javascript is enabled
463 * @return string html formated option field
465 function createWidget_Integer() {
466 global $javascript_on;
468 // add onChange javascript handler to a regular string widget
469 // which will strip out all non-numeric chars
471 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
472 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
473 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
474 . 'this.value=newVal;" />', $this->createWidget_String());
476 return $this->createWidget_String();
480 * Creates field for floating number
481 * Difference from createWidget_String is visible only when javascript is enabled
482 * @return string html formated option field
484 function createWidget_Float() {
485 global $javascript_on;
487 // add onChange javascript handler to a regular string widget
488 // which will strip out all non-numeric (period also OK) chars
490 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
491 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
492 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
493 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
494 , $this->createWidget_String());
496 return $this->createWidget_String();
500 * Creates radio field (yes/no)
501 * @return string html formated radio field
503 function createWidget_Boolean() {
504 /* Do the whole current value thing. */
505 if ($this->value
!= SMPREF_NO
) {
506 $yes_chk = ' checked="checked"';
510 $no_chk = ' checked="checked"';
513 /* Build the yes choice. */
514 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
515 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
516 . $yes_chk . ' ' . $this->script
. ' /> '
517 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
519 /* Build the no choice. */
520 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
521 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
522 . $no_chk . ' ' . $this->script
. ' /> '
523 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
525 /* Build and return the combined "boolean widget". */
526 $result = "$yes_option $no_option";
531 * Creates hidden field
532 * @return string html formated hidden input field
534 function createWidget_Hidden() {
535 $result = '<input type="hidden" name="new_' . $this->name
536 . '" value="' . htmlspecialchars($this->value
)
537 . '" ' . $this->script
. ' />';
543 * @return string comment
545 function createWidget_Comment() {
546 $result = $this->comment
;
554 $function = $this->save_function
;
562 return ($this->value
!= $this->new_value
);
564 } /* End of SquirrelOption class*/
568 * @param object $option object that holds option name and new_value
570 function save_option($option) {
571 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
572 /* Can't save the pref if we don't have the username */
576 setPref($data_dir, $username, $option->name
, $option->new_value
);
580 * save function that does not save
581 * @param object $option
583 function save_option_noop($option) {
584 /* Do nothing here... */
588 * Create hidden 'optpage' input field with value set by argument
589 * @param string $optpage identification of option page
590 * @return string html formated hidden input field
592 function create_optpage_element($optpage) {
593 return create_hidden_element('optpage', $optpage);
597 * Create hidden 'optmode' input field with value set by argument
598 * @param string $optmode
599 * @return string html formated hidden input field
601 function create_optmode_element($optmode) {
602 return create_hidden_element('optmode', $optmode);
606 * Create hidden field.
607 * @param string $name field name
608 * @param string $value field value
609 * @return string html formated hidden input field
611 function create_hidden_element($name, $value) {
612 $result = '<input type="hidden" '
613 . 'name="' . $name . '" '
614 . 'value="' . htmlspecialchars($value) . '" />';
619 * @param array $optgrps
620 * @param array $optvals
623 function create_option_groups($optgrps, $optvals) {
624 /* Build a simple array with which to start. */
627 /* Create option group for each option group name. */
628 foreach ($optgrps as $grpkey => $grpname) {
629 $result[$grpkey] = array();
630 $result[$grpkey]['name'] = $grpname;
631 $result[$grpkey]['options'] = array();
634 /* Create a new SquirrelOption for each set of option values. */
635 foreach ($optvals as $grpkey => $grpopts) {
636 foreach ($grpopts as $optset) {
637 /* Create a new option with all values given. */
638 $next_option = new SquirrelOption(
642 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
643 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
644 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
645 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
648 /* If provided, set the size for this option. */
649 if (isset($optset['size'])) {
650 $next_option->setSize($optset['size']);
653 /* If provided, set the trailing_text for this option. */
654 if (isset($optset['trailing_text'])) {
655 $next_option->setTrailingText($optset['trailing_text']);
658 /* If provided, set the comment for this option. */
659 if (isset($optset['comment'])) {
660 $next_option->setComment($optset['comment']);
663 /* If provided, set the save function for this option. */
664 if (isset($optset['save'])) {
665 $next_option->setSaveFunction($optset['save']);
668 /* If provided, set the script for this option. */
669 if (isset($optset['script'])) {
670 $next_option->setScript($optset['script']);
673 /* If provided, set the "post script" for this option. */
674 if (isset($optset['post_script'])) {
675 $next_option->setPostScript($optset['post_script']);
678 /* If provided, set the folder_filter for this option. */
679 if (isset($optset['folder_filter'])) {
680 $next_option->setFolderFilter($optset['folder_filter']);
683 /* Add this option to the option array. */
684 $result[$grpkey]['options'][] = $next_option;
688 /* Return our resulting array. */
693 * @param array $option_groups
695 function print_option_groups($option_groups) {
696 /* Print each option group. */
697 foreach ($option_groups as $next_optgrp) {
698 /* If it is not blank, print the name for this option group. */
699 if ($next_optgrp['name'] != '') {
700 echo html_tag( 'tr', "\n".
702 '<b>' . $next_optgrp['name'] . '</b>' ,
703 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
707 /* Print each option in this option group. */
708 foreach ($next_optgrp['options'] as $option) {
709 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
710 echo html_tag( 'tr', "\n".
711 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
712 html_tag( 'td', $option->createHTMLWidget(), 'left' )
715 echo $option->createHTMLWidget();
719 /* Print an empty row after this option group. */
721 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
727 * Create submit button inside table row.
728 * @param string $name
730 function OptionSubmit( $name ) {
732 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' )