Resolve confusing var names for preferences.
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Functions needed to display the options pages.
7 *
8 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage prefs
13 */
14
15 /**********************************************/
16 /* Define constants used in the options code. */
17 /**********************************************/
18
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
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);
34
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);
42
43 define('SMOPT_SAVE_DEFAULT', 'save_option');
44 define('SMOPT_SAVE_NOOP', 'save_option_noop');
45
46 /**
47 * SquirrelOption: An option for SquirrelMail.
48 *
49 * @package squirrelmail
50 * @subpackage prefs
51 */
52 class SquirrelOption {
53 /**
54 * The name of this setting
55 * @var string
56 */
57 var $name;
58 /**
59 * The text that prefaces setting on the preferences page
60 * @var string
61 */
62 var $caption;
63 /**
64 * The type of INPUT element
65 *
66 * See SMOPT_TYPE_* defines
67 * @var integer
68 */
69 var $type;
70 /**
71 * Indicates if a link should be shown to refresh part
72 * or all of the window
73 *
74 * See SMOPT_REFRESH_* defines
75 * @var integer
76 */
77 var $refresh_level;
78 /**
79 * Specifies the size of certain input items
80 *
81 * See SMOPT_SIZE_* defines
82 * @var integer
83 */
84 var $size;
85 /**
86 * Text that follows a text input or
87 * select list input on the preferences page
88 *
89 * useful for indicating units, meanings of special values, etc.
90 * @var string
91 */
92 var $trailing_text;
93 /**
94 * text displayed to the user
95 *
96 * Used with SMOPT_TYPE_COMMENT options
97 * @var string
98 */
99 var $comment;
100 /**
101 * additional javascript or other code added to the user input
102 * @var string
103 */
104 var $script;
105 /**
106 * script (usually Javascript) that will be placed after (outside of)
107 * the INPUT tag
108 * @var string
109 */
110 var $post_script;
111
112 /**
113 * The name of the Save Function for this option.
114 * @var string
115 */
116 var $save_function;
117
118 /* The various 'values' for this options. */
119 /**
120 * default/preselected value for this option
121 * @var mixed
122 */
123 var $value;
124 /**
125 * new option value
126 * @var mixed
127 */
128 var $new_value;
129 /**
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
133 * @var array
134 */
135 var $possible_values;
136 /**
137 * disables html sanitizing.
138 *
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.
142 * @var bool
143 */
144 var $htmlencoded=false;
145 /**
146 * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
147 * See $flag argument in sqimap_mailbox_option_list() function.
148 * @var string
149 * @since 1.5.1
150 */
151 var $folder_filter='noselect';
152
153 /**
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
162 */
163 function SquirrelOption
164 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
165 /* Set the basic stuff. */
166 $this->name = $name;
167 $this->caption = $caption;
168 $this->type = $type;
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 = '';
174 $this->comment = '';
175 $this->script = '';
176 $this->post_script = '';
177
178 /**
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.
182 */
183 $var_type = NULL;
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);
189 }
190
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;
195 } else {
196 $this->value = '';
197 }
198
199 /* Check for a new value. */
200 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
201 $this->new_value = '';
202 }
203
204 /* Set the default save function. */
205 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
206 $this->save_function = SMOPT_SAVE_DEFAULT;
207 } else {
208 $this->save_function = SMOPT_SAVE_NOOP;
209 }
210 }
211
212 /**
213 * Set the value for this option.
214 * @param mixed $value
215 */
216 function setValue($value) {
217 $this->value = $value;
218 }
219
220 /**
221 * Set the new value for this option.
222 * @param mixed $new_value
223 */
224 function setNewValue($new_value) {
225 $this->new_value = $new_value;
226 }
227
228 /**
229 * Set the size for this option.
230 * @param integer $size
231 */
232 function setSize($size) {
233 $this->size = $size;
234 }
235
236 /**
237 * Set the trailing_text for this option.
238 * @param string $trailing_text
239 */
240 function setTrailingText($trailing_text) {
241 $this->trailing_text = $trailing_text;
242 }
243
244 /**
245 * Set the comment for this option.
246 * @param string $comment
247 */
248 function setComment($comment) {
249 $this->comment = $comment;
250 }
251
252 /**
253 * Set the script for this option.
254 * @param string $script
255 */
256 function setScript($script) {
257 $this->script = $script;
258 }
259
260 /**
261 * Set the "post script" for this option.
262 * @param string $post_script
263 */
264 function setPostScript($post_script) {
265 $this->post_script = $post_script;
266 }
267
268 /**
269 * Set the save function for this option.
270 * @param string $save_function
271 */
272 function setSaveFunction($save_function) {
273 $this->save_function = $save_function;
274 }
275
276 /**
277 * Set the trailing_text for this option.
278 * @param string $folder_filter
279 * @since 1.5.1
280 */
281 function setFolderFilter($folder_filter) {
282 $this->folder_filter = $folder_filter;
283 }
284
285 /**
286 * Creates fields on option pages according to option type
287 *
288 * Function that calls other createWidget* functions.
289 * @return string html formated option field
290 */
291 function createHTMLWidget() {
292 global $color;
293
294 // Use new value if available
295 if (!empty($this->new_value)) {
296 $tempValue = $this->value;
297 $this->value = $this->new_value;
298 }
299
300 /* Get the widget for this option type. */
301 switch ($this->type) {
302 case SMOPT_TYPE_STRING:
303 $result = $this->createWidget_String();
304 break;
305 case SMOPT_TYPE_STRLIST:
306 $result = $this->createWidget_StrList();
307 break;
308 case SMOPT_TYPE_TEXTAREA:
309 $result = $this->createWidget_TextArea();
310 break;
311 case SMOPT_TYPE_INTEGER:
312 $result = $this->createWidget_Integer();
313 break;
314 case SMOPT_TYPE_FLOAT:
315 $result = $this->createWidget_Float();
316 break;
317 case SMOPT_TYPE_BOOLEAN:
318 $result = $this->createWidget_Boolean();
319 break;
320 case SMOPT_TYPE_HIDDEN:
321 $result = $this->createWidget_Hidden();
322 break;
323 case SMOPT_TYPE_COMMENT:
324 $result = $this->createWidget_Comment();
325 break;
326 case SMOPT_TYPE_FLDRLIST:
327 $result = $this->createWidget_FolderList();
328 break;
329 default:
330 $result = '<font color="' . $color[2] . '">'
331 . sprintf(_("Option Type '%s' Not Found"), $this->type)
332 . '</font>';
333 }
334
335 /* Add the "post script" for this option. */
336 $result .= $this->post_script;
337
338 // put correct value back if need be
339 if (!empty($this->new_value)) {
340 $this->value = $tempValue;
341 }
342
343 /* Now, return the created widget. */
344 return ($result);
345 }
346
347 /**
348 * Create string field
349 * @return string html formated option field
350 */
351 function createWidget_String() {
352 switch ($this->size) {
353 case SMOPT_SIZE_TINY:
354 $width = 5;
355 break;
356 case SMOPT_SIZE_SMALL:
357 $width = 12;
358 break;
359 case SMOPT_SIZE_LARGE:
360 $width = 38;
361 break;
362 case SMOPT_SIZE_HUGE:
363 $width = 50;
364 break;
365 case SMOPT_SIZE_NORMAL:
366 default:
367 $width = 25;
368 }
369
370 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
371 htmlspecialchars($this->value) .
372 "\" size=\"$width\" $this->script />$this->trailing_text\n";
373 return ($result);
374 }
375
376 /**
377 * Create selection box
378 * @return string html formated selection box
379 */
380 function createWidget_StrList() {
381 /* Begin the select tag. */
382 $result = "<select name=\"new_$this->name\" $this->script>\n";
383
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)) . '"';
389
390 /* If this value is the current value, select it. */
391 if ($real_value == $this->value) {
392 $new_option .= ' selected="selected"';
393 }
394
395 /* Add the display value to our option string. */
396 $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
397
398 /* And add the new option string to our select tag. */
399 $result .= $new_option;
400 }
401
402 /* Close the select tag and return our happy result. */
403 $result .= "</select>$this->trailing_text\n";
404 return ($result);
405 }
406
407 /**
408 * Create folder selection box
409 * @return string html formated selection box
410 */
411 function createWidget_FolderList() {
412 $selected = array(strtolower($this->value));
413
414 /* set initial value */
415 $result = '';
416
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);
422 } else {
423 /* Start the next new option string. */
424 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
425
426 /* If this value is the current value, select it. */
427 if ($real_value == $this->value) {
428 $new_option .= ' selected="selected"';
429 }
430
431 /* Add the display value to our option string. */
432 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
433 }
434 /* And add the new option string to our select tag. */
435 $result .= $new_option;
436 }
437
438
439 if (empty($result)) {
440 // string is displayed when interface can't build folder selection box
441 return _("unavailable");
442 } else {
443 /* Begin the select tag. */
444 $ret = "<select name=\"new_$this->name\" $this->script>\n";
445 $ret.= $result;
446 /* Close the select tag and return our happy result. */
447 $ret.= "</select>\n";
448 return ($ret);
449 }
450 }
451
452 /**
453 * Creates textarea
454 * @return string html formated textarea field
455 */
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;
464 }
465 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
466 . "cols=\"$cols\" $this->script>"
467 . htmlspecialchars($this->value) . "</textarea>\n";
468 return ($result);
469 }
470
471 /**
472 * Creates field for integer
473 *
474 * Difference from createWidget_String is visible only when javascript is enabled
475 * @return string html formated option field
476 */
477 function createWidget_Integer() {
478 global $javascript_on;
479
480 // add onChange javascript handler to a regular string widget
481 // which will strip out all non-numeric chars
482 if ($javascript_on)
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());
487 else
488 return $this->createWidget_String();
489 }
490
491 /**
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
495 */
496 function createWidget_Float() {
497 global $javascript_on;
498
499 // add onChange javascript handler to a regular string widget
500 // which will strip out all non-numeric (period also OK) chars
501 if ($javascript_on)
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());
507 else
508 return $this->createWidget_String();
509 }
510
511 /**
512 * Creates radio field (yes/no)
513 * @return string html formated radio field
514 */
515 function createWidget_Boolean() {
516 /* Do the whole current value thing. */
517 if ($this->value != SMPREF_NO) {
518 $yes_chk = ' checked="checked"';
519 $no_chk = '';
520 } else {
521 $yes_chk = '';
522 $no_chk = ' checked="checked"';
523 }
524
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 . ' />&nbsp;'
529 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
530
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 . ' />&nbsp;'
535 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
536
537 /* Build and return the combined "boolean widget". */
538 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
539 return ($result);
540 }
541
542 /**
543 * Creates hidden field
544 * @return string html formated hidden input field
545 */
546 function createWidget_Hidden() {
547 $result = '<input type="hidden" name="new_' . $this->name
548 . '" value="' . htmlspecialchars($this->value)
549 . '" ' . $this->script . ' />';
550 return ($result);
551 }
552
553 /**
554 * Creates comment
555 * @return string comment
556 */
557 function createWidget_Comment() {
558 $result = $this->comment;
559 return ($result);
560 }
561
562 /**
563 *
564 */
565 function save() {
566 $function = $this->save_function;
567 $function($this);
568 }
569
570 /**
571 *
572 */
573 function changed() {
574 return ($this->value != $this->new_value);
575 }
576 } /* End of SquirrelOption class*/
577
578 /**
579 * Saves option
580 * @param object $option object that holds option name and new_value
581 */
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 */
585 return;
586 }
587 global $data_dir;
588 setPref($data_dir, $username, $option->name, $option->new_value);
589 }
590
591 /**
592 * save function that does not save
593 * @param object $option
594 */
595 function save_option_noop($option) {
596 /* Do nothing here... */
597 }
598
599 /**
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
603 */
604 function create_optpage_element($optpage) {
605 return create_hidden_element('optpage', $optpage);
606 }
607
608 /**
609 * Create hidden 'optmode' input field with value set by argument
610 * @param string $optmode
611 * @return string html formated hidden input field
612 */
613 function create_optmode_element($optmode) {
614 return create_hidden_element('optmode', $optmode);
615 }
616
617 /**
618 * Create hidden field.
619 * @param string $name field name
620 * @param string $value field value
621 * @return string html formated hidden input field
622 */
623 function create_hidden_element($name, $value) {
624 $result = '<input type="hidden" '
625 . 'name="' . $name . '" '
626 . 'value="' . htmlspecialchars($value) . '" />';
627 return ($result);
628 }
629
630 /**
631 * @param array $optgrps
632 * @param array $optvals
633 * @return array
634 */
635 function create_option_groups($optgrps, $optvals) {
636 /* Build a simple array with which to start. */
637 $result = array();
638
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();
644 }
645
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(
651 $optset['name'],
652 $optset['caption'],
653 $optset['type'],
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)
658 );
659
660 /* If provided, set the size for this option. */
661 if (isset($optset['size'])) {
662 $next_option->setSize($optset['size']);
663 }
664
665 /* If provided, set the trailing_text for this option. */
666 if (isset($optset['trailing_text'])) {
667 $next_option->setTrailingText($optset['trailing_text']);
668 }
669
670 /* If provided, set the comment for this option. */
671 if (isset($optset['comment'])) {
672 $next_option->setComment($optset['comment']);
673 }
674
675 /* If provided, set the save function for this option. */
676 if (isset($optset['save'])) {
677 $next_option->setSaveFunction($optset['save']);
678 }
679
680 /* If provided, set the script for this option. */
681 if (isset($optset['script'])) {
682 $next_option->setScript($optset['script']);
683 }
684
685 /* If provided, set the "post script" for this option. */
686 if (isset($optset['post_script'])) {
687 $next_option->setPostScript($optset['post_script']);
688 }
689
690 /* If provided, set the folder_filter for this option. */
691 if (isset($optset['folder_filter'])) {
692 $next_option->setFolderFilter($optset['folder_filter']);
693 }
694
695 /* Add this option to the option array. */
696 $result[$grpkey]['options'][] = $next_option;
697 }
698 }
699
700 /* Return our resulting array. */
701 return ($result);
702 }
703
704 /**
705 * @param array $option_groups
706 */
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".
713 html_tag( 'td',
714 '<b>' . $next_optgrp['name'] . '</b>' ,
715 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
716 ) ."\n";
717 }
718
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' )
725 ) ."\n";
726 } else {
727 echo $option->createHTMLWidget();
728 }
729 }
730
731 /* Print an empty row after this option group. */
732 echo html_tag( 'tr',
733 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
734 ) . "\n";
735 }
736 }
737
738 /**
739 * Create submit button inside table row.
740 * @param string $name
741 */
742 function OptionSubmit( $name ) {
743 echo html_tag( 'tr',
744 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
745 ) . "\n";
746 }
747
748 // vim: et ts=4