6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Functions needed to display the options pages.
12 * @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;
147 * Constructor function
148 * @param string $name
149 * @param string $caption
150 * @param integer $type
151 * @param integer $refresh_level
152 * @param mixed $initial_value
153 * @param array $possible_values
154 * @param bool $htmlencoded
156 function SquirrelOption
157 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
158 /* Set the basic stuff. */
160 $this->caption
= $caption;
162 $this->refresh_level
= $refresh_level;
163 $this->possible_values
= $possible_values;
164 $this->htmlencoded
= $htmlencoded;
165 $this->size
= SMOPT_SIZE_MEDIUM
;
166 $this->trailing_text
= '';
169 $this->post_script
= '';
171 /* Check for a current value. */
172 if (!empty($initial_value)) {
173 $this->value
= $initial_value;
174 } else if (isset($GLOBALS[$name])) {
175 $this->value
= $GLOBALS[$name];
180 /* Check for a new value. */
181 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
182 $this->new_value
= '';
185 /* Set the default save function. */
186 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
187 $this->save_function
= SMOPT_SAVE_DEFAULT
;
189 $this->save_function
= SMOPT_SAVE_NOOP
;
194 * Set the value for this option.
195 * @param mixed $value
197 function setValue($value) {
198 $this->value
= $value;
202 * Set the new value for this option.
203 * @param mixed $new_value
205 function setNewValue($new_value) {
206 $this->new_value
= $new_value;
210 * Set the size for this option.
211 * @param integer $size
213 function setSize($size) {
218 * Set the trailing_text for this option.
219 * @param string $trailing_text
221 function setTrailingText($trailing_text) {
222 $this->trailing_text
= $trailing_text;
226 * Set the comment for this option.
227 * @param string $comment
229 function setComment($comment) {
230 $this->comment
= $comment;
234 * Set the script for this option.
235 * @param string $script
237 function setScript($script) {
238 $this->script
= $script;
242 * Set the "post script" for this option.
243 * @param string $post_script
245 function setPostScript($post_script) {
246 $this->post_script
= $post_script;
250 * Set the save function for this option.
251 * @param string $save_function
253 function setSaveFunction($save_function) {
254 $this->save_function
= $save_function;
258 * Creates fields on option pages according to option type
260 * Function that calls other createWidget* functions.
261 * @return string html formated option field
263 function createHTMLWidget() {
266 // Use new value if available
267 if (!empty($this->new_value
)) {
268 $tempValue = $this->value
;
269 $this->value
= $this->new_value
;
272 /* Get the widget for this option type. */
273 switch ($this->type
) {
274 case SMOPT_TYPE_STRING
:
275 $result = $this->createWidget_String();
277 case SMOPT_TYPE_STRLIST
:
278 $result = $this->createWidget_StrList();
280 case SMOPT_TYPE_TEXTAREA
:
281 $result = $this->createWidget_TextArea();
283 case SMOPT_TYPE_INTEGER
:
284 $result = $this->createWidget_Integer();
286 case SMOPT_TYPE_FLOAT
:
287 $result = $this->createWidget_Float();
289 case SMOPT_TYPE_BOOLEAN
:
290 $result = $this->createWidget_Boolean();
292 case SMOPT_TYPE_HIDDEN
:
293 $result = $this->createWidget_Hidden();
295 case SMOPT_TYPE_COMMENT
:
296 $result = $this->createWidget_Comment();
298 case SMOPT_TYPE_FLDRLIST
:
299 $result = $this->createWidget_FolderList();
302 $result = '<font color="' . $color[2] . '">'
303 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
307 /* Add the "post script" for this option. */
308 $result .= $this->post_script
;
310 // put correct value back if need be
311 if (!empty($this->new_value
)) {
312 $this->value
= $tempValue;
315 /* Now, return the created widget. */
320 * Create string field
321 * @return string html formated option field
323 function createWidget_String() {
324 switch ($this->size
) {
325 case SMOPT_SIZE_TINY
:
328 case SMOPT_SIZE_SMALL
:
331 case SMOPT_SIZE_LARGE
:
334 case SMOPT_SIZE_HUGE
:
337 case SMOPT_SIZE_NORMAL
:
342 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
343 htmlspecialchars($this->value
) .
344 "\" size=\"$width\" $this->script />$this->trailing_text\n";
349 * Create selection box
350 * @return string html formated selection box
352 function createWidget_StrList() {
353 /* Begin the select tag. */
354 $result = "<select name=\"new_$this->name\" $this->script>\n";
356 /* Add each possible value to the select list. */
357 foreach ($this->possible_values
as $real_value => $disp_value) {
358 /* Start the next new option string. */
359 $new_option = '<option value="' .
360 ($this->htmlencoded ?
$real_value : htmlspecialchars($real_value)) . '"';
362 /* If this value is the current value, select it. */
363 if ($real_value == $this->value
) {
364 $new_option .= ' selected="selected"';
367 /* Add the display value to our option string. */
368 $new_option .= '>' . ($this->htmlencoded ?
$disp_value : htmlspecialchars($disp_value)) . "</option>\n";
370 /* And add the new option string to our select tag. */
371 $result .= $new_option;
374 /* Close the select tag and return our happy result. */
375 $result .= "</select>$this->trailing_text\n";
380 * Create folder selection box
381 * @return string html formated selection box
383 function createWidget_FolderList() {
384 $selected = array(strtolower($this->value
));
386 /* Begin the select tag. */
387 $result = "<select name=\"new_$this->name\" $this->script>\n";
389 /* Add each possible value to the select list. */
390 foreach ($this->possible_values
as $real_value => $disp_value) {
391 if ( is_array($disp_value) ) {
392 /* For folder list, we passed in the array of boxes.. */
393 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
395 /* Start the next new option string. */
396 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
398 /* If this value is the current value, select it. */
399 if ($real_value == $this->value
) {
400 $new_option .= ' selected="selected"';
403 /* Add the display value to our option string. */
404 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
406 /* And add the new option string to our select tag. */
407 $result .= $new_option;
409 /* Close the select tag and return our happy result. */
410 $result .= "</select>\n";
416 * @return string html formated textarea field
418 function createWidget_TextArea() {
419 switch ($this->size
) {
420 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
421 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
422 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
423 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
424 case SMOPT_SIZE_NORMAL
:
425 default: $rows = 5; $cols = 50;
427 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
428 . "cols=\"$cols\" $this->script>"
429 . htmlspecialchars($this->value
) . "</textarea>\n";
434 * Creates field for integer
436 * Difference from createWidget_String is visible only when javascript is enabled
437 * @return string html formated option field
439 function createWidget_Integer() {
440 global $javascript_on;
442 // add onChange javascript handler to a regular string widget
443 // which will strip out all non-numeric chars
445 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
446 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
447 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
448 . 'this.value=newVal;" />', $this->createWidget_String());
450 return $this->createWidget_String();
454 * Creates field for floating number
455 * Difference from createWidget_String is visible only when javascript is enabled
456 * @return string html formated option field
458 function createWidget_Float() {
459 global $javascript_on;
461 // add onChange javascript handler to a regular string widget
462 // which will strip out all non-numeric (period also OK) chars
464 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
465 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
466 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
467 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
468 , $this->createWidget_String());
470 return $this->createWidget_String();
474 * Creates radio field (yes/no)
475 * @return string html formated radio field
477 function createWidget_Boolean() {
478 /* Do the whole current value thing. */
479 if ($this->value
!= SMPREF_NO
) {
480 $yes_chk = ' checked="checked"';
484 $no_chk = ' checked="checked"';
487 /* Build the yes choice. */
488 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
489 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
490 . $yes_chk . ' ' . $this->script
. ' /> '
491 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
493 /* Build the no choice. */
494 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
495 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
496 . $no_chk . ' ' . $this->script
. ' /> '
497 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
499 /* Build and return the combined "boolean widget". */
500 $result = "$yes_option $no_option";
505 * Creates hidden field
506 * @return string html formated hidden input field
508 function createWidget_Hidden() {
509 $result = '<input type="hidden" name="new_' . $this->name
510 . '" value="' . htmlspecialchars($this->value
)
511 . '" ' . $this->script
. ' />';
517 * @return string comment
519 function createWidget_Comment() {
520 $result = $this->comment
;
528 $function = $this->save_function
;
536 return ($this->value
!= $this->new_value
);
538 } /* End of SquirrelOption class*/
542 * @param object $option object that holds option name and new_value
544 function save_option($option) {
545 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
546 /* Can't save the pref if we don't have the username */
550 setPref($data_dir, $username, $option->name
, $option->new_value
);
554 * save function that does not save
555 * @param object $option
557 function save_option_noop($option) {
558 /* Do nothing here... */
562 * Create hidden 'optpage' input field with value set by argument
563 * @param string $optpage identification of option page
564 * @return string html formated hidden input field
566 function create_optpage_element($optpage) {
567 return create_hidden_element('optpage', $optpage);
571 * Create hidden 'optmode' input field with value set by argument
572 * @param string $optmode
573 * @return string html formated hidden input field
575 function create_optmode_element($optmode) {
576 return create_hidden_element('optmode', $optmode);
580 * Create hidden field.
581 * @param string $name field name
582 * @param string $value field value
583 * @return string html formated hidden input field
585 function create_hidden_element($name, $value) {
586 $result = '<input type="hidden" '
587 . 'name="' . $name . '" '
588 . 'value="' . htmlspecialchars($value) . '" />';
593 * @param array $optgrps
594 * @param array $optvals
597 function create_option_groups($optgrps, $optvals) {
598 /* Build a simple array with which to start. */
601 /* Create option group for each option group name. */
602 foreach ($optgrps as $grpkey => $grpname) {
603 $result[$grpkey] = array();
604 $result[$grpkey]['name'] = $grpname;
605 $result[$grpkey]['options'] = array();
608 /* Create a new SquirrelOption for each set of option values. */
609 foreach ($optvals as $grpkey => $grpopts) {
610 foreach ($grpopts as $optset) {
611 /* Create a new option with all values given. */
612 $next_option = new SquirrelOption(
616 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
617 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
618 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
619 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
622 /* If provided, set the size for this option. */
623 if (isset($optset['size'])) {
624 $next_option->setSize($optset['size']);
627 /* If provided, set the trailing_text for this option. */
628 if (isset($optset['trailing_text'])) {
629 $next_option->setTrailingText($optset['trailing_text']);
632 /* If provided, set the comment for this option. */
633 if (isset($optset['comment'])) {
634 $next_option->setComment($optset['comment']);
637 /* If provided, set the save function for this option. */
638 if (isset($optset['save'])) {
639 $next_option->setSaveFunction($optset['save']);
642 /* If provided, set the script for this option. */
643 if (isset($optset['script'])) {
644 $next_option->setScript($optset['script']);
647 /* If provided, set the "post script" for this option. */
648 if (isset($optset['post_script'])) {
649 $next_option->setPostScript($optset['post_script']);
652 /* Add this option to the option array. */
653 $result[$grpkey]['options'][] = $next_option;
657 /* Return our resulting array. */
662 * @param array $option_groups
664 function print_option_groups($option_groups) {
665 /* Print each option group. */
666 foreach ($option_groups as $next_optgrp) {
667 /* If it is not blank, print the name for this option group. */
668 if ($next_optgrp['name'] != '') {
669 echo html_tag( 'tr', "\n".
671 '<b>' . $next_optgrp['name'] . '</b>' ,
672 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
676 /* Print each option in this option group. */
677 foreach ($next_optgrp['options'] as $option) {
678 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
679 echo html_tag( 'tr', "\n".
680 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
681 html_tag( 'td', $option->createHTMLWidget(), 'left' )
684 echo $option->createHTMLWidget();
688 /* Print an empty row after this option group. */
690 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
696 * Create submit button inside table row.
697 * @param string $name
699 function OptionSubmit( $name ) {
701 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' )