abfe9fc16cf57af71ff6824652c9086e6fb093fa
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
16 /**********************************************/
17 /* Define constants used in the options code. */
18 /**********************************************/
20 /* Define constants for the various option types. */
21 define('SMOPT_TYPE_STRING', 0);
22 define('SMOPT_TYPE_STRLIST', 1);
23 define('SMOPT_TYPE_TEXTAREA', 2);
24 define('SMOPT_TYPE_INTEGER', 3);
25 define('SMOPT_TYPE_FLOAT', 4);
26 define('SMOPT_TYPE_BOOLEAN', 5);
27 define('SMOPT_TYPE_HIDDEN', 6);
28 define('SMOPT_TYPE_COMMENT', 7);
29 define('SMOPT_TYPE_FLDRLIST', 8);
31 /* Define constants for the options refresh levels. */
32 define('SMOPT_REFRESH_NONE', 0);
33 define('SMOPT_REFRESH_FOLDERLIST', 1);
34 define('SMOPT_REFRESH_ALL', 2);
36 /* Define constants for the options size. */
37 define('SMOPT_SIZE_TINY', 0);
38 define('SMOPT_SIZE_SMALL', 1);
39 define('SMOPT_SIZE_MEDIUM', 2);
40 define('SMOPT_SIZE_LARGE', 3);
41 define('SMOPT_SIZE_HUGE', 4);
42 define('SMOPT_SIZE_NORMAL', 5);
44 define('SMOPT_SAVE_DEFAULT', 'save_option');
45 define('SMOPT_SAVE_NOOP', 'save_option_noop');
48 * SquirrelOption: An option for SquirrelMail.
50 * @package squirrelmail
53 class SquirrelOption
{
55 * The name of this setting
60 * The text that prefaces setting on the preferences page
65 * The type of INPUT element
67 * See SMOPT_TYPE_* defines
72 * Indicates if a link should be shown to refresh part
73 * or all of the window
75 * See SMOPT_REFRESH_* defines
80 * Specifies the size of certain input items
82 * See SMOPT_SIZE_* defines
87 * Text that follows a text input or
88 * select list input on the preferences page
90 * useful for indicating units, meanings of special values, etc.
95 * text displayed to the user
97 * Used with SMOPT_TYPE_COMMENT options
102 * additional javascript or other code added to the user input
107 * script (usually Javascript) that will be placed after (outside of)
114 * The name of the Save Function for this option.
119 /* The various 'values' for this options. */
121 * default/preselected value for this option
131 * associative array, where each key is an actual input value
132 * and the corresponding value is what is displayed to the user
133 * for that list item in the drop-down list
136 var $possible_values;
138 * disables html sanitizing.
140 * WARNING - don't use it, if user input is possible in option
141 * or use own sanitizing functions. Currently works only with
142 * SMOPT_TYPE_STRLIST.
145 var $htmlencoded=false;
148 * Constructor function
149 * @param string $name
150 * @param string $caption
151 * @param integer $type
152 * @param integer $refresh_level
153 * @param mixed $initial_value
154 * @param array $possible_values
155 * @param bool $htmlencoded
157 function SquirrelOption
158 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
159 /* Set the basic stuff. */
161 $this->caption
= $caption;
163 $this->refresh_level
= $refresh_level;
164 $this->possible_values
= $possible_values;
165 $this->htmlencoded
= $htmlencoded;
166 $this->size
= SMOPT_SIZE_MEDIUM
;
167 $this->trailing_text
= '';
170 $this->post_script
= '';
172 /* Check for a current value. */
173 if (!empty($initial_value)) {
174 $this->value
= $initial_value;
175 } else if (isset($GLOBALS[$name])) {
176 $this->value
= $GLOBALS[$name];
181 /* Check for a new value. */
182 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
183 $this->new_value
= '';
186 /* Set the default save function. */
187 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
188 $this->save_function
= SMOPT_SAVE_DEFAULT
;
190 $this->save_function
= SMOPT_SAVE_NOOP
;
195 * Set the value for this option.
196 * @param mixed $value
198 function setValue($value) {
199 $this->value
= $value;
203 * Set the new value for this option.
204 * @param mixed $new_value
206 function setNewValue($new_value) {
207 $this->new_value
= $new_value;
211 * Set the size for this option.
212 * @param integer $size
214 function setSize($size) {
219 * Set the trailing_text for this option.
220 * @param string $trailing_text
222 function setTrailingText($trailing_text) {
223 $this->trailing_text
= $trailing_text;
227 * Set the comment for this option.
228 * @param string $comment
230 function setComment($comment) {
231 $this->comment
= $comment;
235 * Set the script for this option.
236 * @param string $script
238 function setScript($script) {
239 $this->script
= $script;
243 * Set the "post script" for this option.
244 * @param string $post_script
246 function setPostScript($post_script) {
247 $this->post_script
= $post_script;
251 * Set the save function for this option.
252 * @param string $save_function
254 function setSaveFunction($save_function) {
255 $this->save_function
= $save_function;
259 * Creates fields on option pages according to option type
261 * Function that calls other createWidget* functions.
262 * @return string html formated option field
264 function createHTMLWidget() {
267 // Use new value if available
268 if (!empty($this->new_value
)) {
269 $tempValue = $this->value
;
270 $this->value
= $this->new_value
;
273 /* Get the widget for this option type. */
274 switch ($this->type
) {
275 case SMOPT_TYPE_STRING
:
276 $result = $this->createWidget_String();
278 case SMOPT_TYPE_STRLIST
:
279 $result = $this->createWidget_StrList();
281 case SMOPT_TYPE_TEXTAREA
:
282 $result = $this->createWidget_TextArea();
284 case SMOPT_TYPE_INTEGER
:
285 $result = $this->createWidget_Integer();
287 case SMOPT_TYPE_FLOAT
:
288 $result = $this->createWidget_Float();
290 case SMOPT_TYPE_BOOLEAN
:
291 $result = $this->createWidget_Boolean();
293 case SMOPT_TYPE_HIDDEN
:
294 $result = $this->createWidget_Hidden();
296 case SMOPT_TYPE_COMMENT
:
297 $result = $this->createWidget_Comment();
299 case SMOPT_TYPE_FLDRLIST
:
300 $result = $this->createWidget_FolderList();
303 $result = '<font color="' . $color[2] . '">'
304 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
308 /* Add the "post script" for this option. */
309 $result .= $this->post_script
;
311 // put correct value back if need be
312 if (!empty($this->new_value
)) {
313 $this->value
= $tempValue;
316 /* Now, return the created widget. */
321 * Create string field
322 * @return string html formated option field
324 function createWidget_String() {
325 switch ($this->size
) {
326 case SMOPT_SIZE_TINY
:
329 case SMOPT_SIZE_SMALL
:
332 case SMOPT_SIZE_LARGE
:
335 case SMOPT_SIZE_HUGE
:
338 case SMOPT_SIZE_NORMAL
:
343 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
344 htmlspecialchars($this->value
) .
345 "\" size=\"$width\" $this->script />$this->trailing_text\n";
350 * Create selection box
351 * @return string html formated selection box
353 function createWidget_StrList() {
354 /* Begin the select tag. */
355 $result = "<select name=\"new_$this->name\" $this->script>\n";
357 /* Add each possible value to the select list. */
358 foreach ($this->possible_values
as $real_value => $disp_value) {
359 /* Start the next new option string. */
360 $new_option = '<option value="' .
361 ($this->htmlencoded ?
$real_value : htmlspecialchars($real_value)) . '"';
363 /* If this value is the current value, select it. */
364 if ($real_value == $this->value
) {
365 $new_option .= ' selected="selected"';
368 /* Add the display value to our option string. */
369 $new_option .= '>' . ($this->htmlencoded ?
$disp_value : htmlspecialchars($disp_value)) . "</option>\n";
371 /* And add the new option string to our select tag. */
372 $result .= $new_option;
375 /* Close the select tag and return our happy result. */
376 $result .= "</select>$this->trailing_text\n";
381 * Create folder selection box
382 * @return string html formated selection box
384 function createWidget_FolderList() {
385 $selected = array(strtolower($this->value
));
387 /* Begin the select tag. */
388 $result = "<select name=\"new_$this->name\" $this->script>\n";
390 /* Add each possible value to the select list. */
391 foreach ($this->possible_values
as $real_value => $disp_value) {
392 if ( is_array($disp_value) ) {
393 /* For folder list, we passed in the array of boxes.. */
394 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
396 /* Start the next new option string. */
397 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
399 /* If this value is the current value, select it. */
400 if ($real_value == $this->value
) {
401 $new_option .= ' selected="selected"';
404 /* Add the display value to our option string. */
405 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
407 /* And add the new option string to our select tag. */
408 $result .= $new_option;
410 /* Close the select tag and return our happy result. */
411 $result .= "</select>\n";
417 * @return string html formated textarea field
419 function createWidget_TextArea() {
420 switch ($this->size
) {
421 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
422 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
423 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
424 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
425 case SMOPT_SIZE_NORMAL
:
426 default: $rows = 5; $cols = 50;
428 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
429 . "cols=\"$cols\" $this->script>"
430 . htmlspecialchars($this->value
) . "</textarea>\n";
435 * Creates field for integer
437 * Difference from createWidget_String is visible only when javascript is enabled
438 * @return string html formated option field
440 function createWidget_Integer() {
441 global $javascript_on;
443 // add onChange javascript handler to a regular string widget
444 // which will strip out all non-numeric chars
446 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
447 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
448 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
449 . 'this.value=newVal;" />', $this->createWidget_String());
451 return $this->createWidget_String();
455 * Creates field for floating number
456 * Difference from createWidget_String is visible only when javascript is enabled
457 * @return string html formated option field
459 function createWidget_Float() {
460 global $javascript_on;
462 // add onChange javascript handler to a regular string widget
463 // which will strip out all non-numeric (period also OK) chars
465 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
466 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
467 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
468 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
469 , $this->createWidget_String());
471 return $this->createWidget_String();
475 * Creates radio field (yes/no)
476 * @return string html formated radio field
478 function createWidget_Boolean() {
479 /* Do the whole current value thing. */
480 if ($this->value
!= SMPREF_NO
) {
481 $yes_chk = ' checked="checked"';
485 $no_chk = ' checked="checked"';
488 /* Build the yes choice. */
489 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
490 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
491 . $yes_chk . ' ' . $this->script
. ' /> '
492 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
494 /* Build the no choice. */
495 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
496 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
497 . $no_chk . ' ' . $this->script
. ' /> '
498 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
500 /* Build and return the combined "boolean widget". */
501 $result = "$yes_option $no_option";
506 * Creates hidden field
507 * @return string html formated hidden input field
509 function createWidget_Hidden() {
510 $result = '<input type="hidden" name="new_' . $this->name
511 . '" value="' . htmlspecialchars($this->value
)
512 . '" ' . $this->script
. ' />';
518 * @return string comment
520 function createWidget_Comment() {
521 $result = $this->comment
;
529 $function = $this->save_function
;
537 return ($this->value
!= $this->new_value
);
539 } /* End of SquirrelOption class*/
543 * @param object $option object that holds option name and new_value
545 function save_option($option) {
546 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
547 /* Can't save the pref if we don't have the username */
551 setPref($data_dir, $username, $option->name
, $option->new_value
);
555 * save function that does not save
556 * @param object $option
558 function save_option_noop($option) {
559 /* Do nothing here... */
563 * Create hidden 'optpage' input field with value set by argument
564 * @param string $optpage identification of option page
565 * @return string html formated hidden input field
567 function create_optpage_element($optpage) {
568 return create_hidden_element('optpage', $optpage);
572 * Create hidden 'optmode' input field with value set by argument
573 * @param string $optmode
574 * @return string html formated hidden input field
576 function create_optmode_element($optmode) {
577 return create_hidden_element('optmode', $optmode);
581 * Create hidden field.
582 * @param string $name field name
583 * @param string $value field value
584 * @return string html formated hidden input field
586 function create_hidden_element($name, $value) {
587 $result = '<input type="hidden" '
588 . 'name="' . $name . '" '
589 . 'value="' . htmlspecialchars($value) . '" />';
594 * @param array $optgrps
595 * @param array $optvals
598 function create_option_groups($optgrps, $optvals) {
599 /* Build a simple array with which to start. */
602 /* Create option group for each option group name. */
603 foreach ($optgrps as $grpkey => $grpname) {
604 $result[$grpkey] = array();
605 $result[$grpkey]['name'] = $grpname;
606 $result[$grpkey]['options'] = array();
609 /* Create a new SquirrelOption for each set of option values. */
610 foreach ($optvals as $grpkey => $grpopts) {
611 foreach ($grpopts as $optset) {
612 /* Create a new option with all values given. */
613 $next_option = new SquirrelOption(
617 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
618 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
619 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
620 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
623 /* If provided, set the size for this option. */
624 if (isset($optset['size'])) {
625 $next_option->setSize($optset['size']);
628 /* If provided, set the trailing_text for this option. */
629 if (isset($optset['trailing_text'])) {
630 $next_option->setTrailingText($optset['trailing_text']);
633 /* If provided, set the comment for this option. */
634 if (isset($optset['comment'])) {
635 $next_option->setComment($optset['comment']);
638 /* If provided, set the save function for this option. */
639 if (isset($optset['save'])) {
640 $next_option->setSaveFunction($optset['save']);
643 /* If provided, set the script for this option. */
644 if (isset($optset['script'])) {
645 $next_option->setScript($optset['script']);
648 /* If provided, set the "post script" for this option. */
649 if (isset($optset['post_script'])) {
650 $next_option->setPostScript($optset['post_script']);
653 /* Add this option to the option array. */
654 $result[$grpkey]['options'][] = $next_option;
658 /* Return our resulting array. */
663 * @param array $option_groups
665 function print_option_groups($option_groups) {
666 /* Print each option group. */
667 foreach ($option_groups as $next_optgrp) {
668 /* If it is not blank, print the name for this option group. */
669 if ($next_optgrp['name'] != '') {
670 echo html_tag( 'tr', "\n".
672 '<b>' . $next_optgrp['name'] . '</b>' ,
673 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
677 /* Print each option in this option group. */
678 foreach ($next_optgrp['options'] as $option) {
679 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
680 echo html_tag( 'tr', "\n".
681 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
682 html_tag( 'td', $option->createHTMLWidget(), 'left' )
685 echo $option->createHTMLWidget();
689 /* Print an empty row after this option group. */
691 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
697 * Create submit button inside table row.
698 * @param string $name
700 function OptionSubmit( $name ) {
702 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' )