abfe9fc16cf57af71ff6824652c9086e6fb093fa
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions needed to display the options pages.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage prefs
14 */
15
16 /**********************************************/
17 /* Define constants used in the options code. */
18 /**********************************************/
19
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);
30
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);
35
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);
43
44 define('SMOPT_SAVE_DEFAULT', 'save_option');
45 define('SMOPT_SAVE_NOOP', 'save_option_noop');
46
47 /**
48 * SquirrelOption: An option for SquirrelMail.
49 *
50 * @package squirrelmail
51 * @subpackage prefs
52 */
53 class SquirrelOption {
54 /**
55 * The name of this setting
56 * @var string
57 */
58 var $name;
59 /**
60 * The text that prefaces setting on the preferences page
61 * @var string
62 */
63 var $caption;
64 /**
65 * The type of INPUT element
66 *
67 * See SMOPT_TYPE_* defines
68 * @var integer
69 */
70 var $type;
71 /**
72 * Indicates if a link should be shown to refresh part
73 * or all of the window
74 *
75 * See SMOPT_REFRESH_* defines
76 * @var integer
77 */
78 var $refresh_level;
79 /**
80 * Specifies the size of certain input items
81 *
82 * See SMOPT_SIZE_* defines
83 * @var integer
84 */
85 var $size;
86 /**
87 * Text that follows a text input or
88 * select list input on the preferences page
89 *
90 * useful for indicating units, meanings of special values, etc.
91 * @var string
92 */
93 var $trailing_text;
94 /**
95 * text displayed to the user
96 *
97 * Used with SMOPT_TYPE_COMMENT options
98 * @var string
99 */
100 var $comment;
101 /**
102 * additional javascript or other code added to the user input
103 * @var string
104 */
105 var $script;
106 /**
107 * script (usually Javascript) that will be placed after (outside of)
108 * the INPUT tag
109 * @var string
110 */
111 var $post_script;
112
113 /**
114 * The name of the Save Function for this option.
115 * @var string
116 */
117 var $save_function;
118
119 /* The various 'values' for this options. */
120 /**
121 * default/preselected value for this option
122 * @var mixed
123 */
124 var $value;
125 /**
126 * new option value
127 * @var mixed
128 */
129 var $new_value;
130 /**
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
134 * @var array
135 */
136 var $possible_values;
137 /**
138 * disables html sanitizing.
139 *
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.
143 * @var bool
144 */
145 var $htmlencoded=false;
146
147 /**
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
156 */
157 function SquirrelOption
158 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
159 /* Set the basic stuff. */
160 $this->name = $name;
161 $this->caption = $caption;
162 $this->type = $type;
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 = '';
168 $this->comment = '';
169 $this->script = '';
170 $this->post_script = '';
171
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];
177 } else {
178 $this->value = '';
179 }
180
181 /* Check for a new value. */
182 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
183 $this->new_value = '';
184 }
185
186 /* Set the default save function. */
187 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
188 $this->save_function = SMOPT_SAVE_DEFAULT;
189 } else {
190 $this->save_function = SMOPT_SAVE_NOOP;
191 }
192 }
193
194 /**
195 * Set the value for this option.
196 * @param mixed $value
197 */
198 function setValue($value) {
199 $this->value = $value;
200 }
201
202 /**
203 * Set the new value for this option.
204 * @param mixed $new_value
205 */
206 function setNewValue($new_value) {
207 $this->new_value = $new_value;
208 }
209
210 /**
211 * Set the size for this option.
212 * @param integer $size
213 */
214 function setSize($size) {
215 $this->size = $size;
216 }
217
218 /**
219 * Set the trailing_text for this option.
220 * @param string $trailing_text
221 */
222 function setTrailingText($trailing_text) {
223 $this->trailing_text = $trailing_text;
224 }
225
226 /**
227 * Set the comment for this option.
228 * @param string $comment
229 */
230 function setComment($comment) {
231 $this->comment = $comment;
232 }
233
234 /**
235 * Set the script for this option.
236 * @param string $script
237 */
238 function setScript($script) {
239 $this->script = $script;
240 }
241
242 /**
243 * Set the "post script" for this option.
244 * @param string $post_script
245 */
246 function setPostScript($post_script) {
247 $this->post_script = $post_script;
248 }
249
250 /**
251 * Set the save function for this option.
252 * @param string $save_function
253 */
254 function setSaveFunction($save_function) {
255 $this->save_function = $save_function;
256 }
257
258 /**
259 * Creates fields on option pages according to option type
260 *
261 * Function that calls other createWidget* functions.
262 * @return string html formated option field
263 */
264 function createHTMLWidget() {
265 global $color;
266
267 // Use new value if available
268 if (!empty($this->new_value)) {
269 $tempValue = $this->value;
270 $this->value = $this->new_value;
271 }
272
273 /* Get the widget for this option type. */
274 switch ($this->type) {
275 case SMOPT_TYPE_STRING:
276 $result = $this->createWidget_String();
277 break;
278 case SMOPT_TYPE_STRLIST:
279 $result = $this->createWidget_StrList();
280 break;
281 case SMOPT_TYPE_TEXTAREA:
282 $result = $this->createWidget_TextArea();
283 break;
284 case SMOPT_TYPE_INTEGER:
285 $result = $this->createWidget_Integer();
286 break;
287 case SMOPT_TYPE_FLOAT:
288 $result = $this->createWidget_Float();
289 break;
290 case SMOPT_TYPE_BOOLEAN:
291 $result = $this->createWidget_Boolean();
292 break;
293 case SMOPT_TYPE_HIDDEN:
294 $result = $this->createWidget_Hidden();
295 break;
296 case SMOPT_TYPE_COMMENT:
297 $result = $this->createWidget_Comment();
298 break;
299 case SMOPT_TYPE_FLDRLIST:
300 $result = $this->createWidget_FolderList();
301 break;
302 default:
303 $result = '<font color="' . $color[2] . '">'
304 . sprintf(_("Option Type '%s' Not Found"), $this->type)
305 . '</font>';
306 }
307
308 /* Add the "post script" for this option. */
309 $result .= $this->post_script;
310
311 // put correct value back if need be
312 if (!empty($this->new_value)) {
313 $this->value = $tempValue;
314 }
315
316 /* Now, return the created widget. */
317 return ($result);
318 }
319
320 /**
321 * Create string field
322 * @return string html formated option field
323 */
324 function createWidget_String() {
325 switch ($this->size) {
326 case SMOPT_SIZE_TINY:
327 $width = 5;
328 break;
329 case SMOPT_SIZE_SMALL:
330 $width = 12;
331 break;
332 case SMOPT_SIZE_LARGE:
333 $width = 38;
334 break;
335 case SMOPT_SIZE_HUGE:
336 $width = 50;
337 break;
338 case SMOPT_SIZE_NORMAL:
339 default:
340 $width = 25;
341 }
342
343 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
344 htmlspecialchars($this->value) .
345 "\" size=\"$width\" $this->script />$this->trailing_text\n";
346 return ($result);
347 }
348
349 /**
350 * Create selection box
351 * @return string html formated selection box
352 */
353 function createWidget_StrList() {
354 /* Begin the select tag. */
355 $result = "<select name=\"new_$this->name\" $this->script>\n";
356
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)) . '"';
362
363 /* If this value is the current value, select it. */
364 if ($real_value == $this->value) {
365 $new_option .= ' selected="selected"';
366 }
367
368 /* Add the display value to our option string. */
369 $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
370
371 /* And add the new option string to our select tag. */
372 $result .= $new_option;
373 }
374
375 /* Close the select tag and return our happy result. */
376 $result .= "</select>$this->trailing_text\n";
377 return ($result);
378 }
379
380 /**
381 * Create folder selection box
382 * @return string html formated selection box
383 */
384 function createWidget_FolderList() {
385 $selected = array(strtolower($this->value));
386
387 /* Begin the select tag. */
388 $result = "<select name=\"new_$this->name\" $this->script>\n";
389
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);
395 } else {
396 /* Start the next new option string. */
397 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
398
399 /* If this value is the current value, select it. */
400 if ($real_value == $this->value) {
401 $new_option .= ' selected="selected"';
402 }
403
404 /* Add the display value to our option string. */
405 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
406 }
407 /* And add the new option string to our select tag. */
408 $result .= $new_option;
409 }
410 /* Close the select tag and return our happy result. */
411 $result .= "</select>\n";
412 return ($result);
413 }
414
415 /**
416 * Creates textarea
417 * @return string html formated textarea field
418 */
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;
427 }
428 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
429 . "cols=\"$cols\" $this->script>"
430 . htmlspecialchars($this->value) . "</textarea>\n";
431 return ($result);
432 }
433
434 /**
435 * Creates field for integer
436 *
437 * Difference from createWidget_String is visible only when javascript is enabled
438 * @return string html formated option field
439 */
440 function createWidget_Integer() {
441 global $javascript_on;
442
443 // add onChange javascript handler to a regular string widget
444 // which will strip out all non-numeric chars
445 if ($javascript_on)
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());
450 else
451 return $this->createWidget_String();
452 }
453
454 /**
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
458 */
459 function createWidget_Float() {
460 global $javascript_on;
461
462 // add onChange javascript handler to a regular string widget
463 // which will strip out all non-numeric (period also OK) chars
464 if ($javascript_on)
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());
470 else
471 return $this->createWidget_String();
472 }
473
474 /**
475 * Creates radio field (yes/no)
476 * @return string html formated radio field
477 */
478 function createWidget_Boolean() {
479 /* Do the whole current value thing. */
480 if ($this->value != SMPREF_NO) {
481 $yes_chk = ' checked="checked"';
482 $no_chk = '';
483 } else {
484 $yes_chk = '';
485 $no_chk = ' checked="checked"';
486 }
487
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 . ' />&nbsp;'
492 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
493
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 . ' />&nbsp;'
498 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
499
500 /* Build and return the combined "boolean widget". */
501 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
502 return ($result);
503 }
504
505 /**
506 * Creates hidden field
507 * @return string html formated hidden input field
508 */
509 function createWidget_Hidden() {
510 $result = '<input type="hidden" name="new_' . $this->name
511 . '" value="' . htmlspecialchars($this->value)
512 . '" ' . $this->script . ' />';
513 return ($result);
514 }
515
516 /**
517 * Creates comment
518 * @return string comment
519 */
520 function createWidget_Comment() {
521 $result = $this->comment;
522 return ($result);
523 }
524
525 /**
526 *
527 */
528 function save() {
529 $function = $this->save_function;
530 $function($this);
531 }
532
533 /**
534 *
535 */
536 function changed() {
537 return ($this->value != $this->new_value);
538 }
539 } /* End of SquirrelOption class*/
540
541 /**
542 * Saves option
543 * @param object $option object that holds option name and new_value
544 */
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 */
548 return;
549 }
550 global $data_dir;
551 setPref($data_dir, $username, $option->name, $option->new_value);
552 }
553
554 /**
555 * save function that does not save
556 * @param object $option
557 */
558 function save_option_noop($option) {
559 /* Do nothing here... */
560 }
561
562 /**
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
566 */
567 function create_optpage_element($optpage) {
568 return create_hidden_element('optpage', $optpage);
569 }
570
571 /**
572 * Create hidden 'optmode' input field with value set by argument
573 * @param string $optmode
574 * @return string html formated hidden input field
575 */
576 function create_optmode_element($optmode) {
577 return create_hidden_element('optmode', $optmode);
578 }
579
580 /**
581 * Create hidden field.
582 * @param string $name field name
583 * @param string $value field value
584 * @return string html formated hidden input field
585 */
586 function create_hidden_element($name, $value) {
587 $result = '<input type="hidden" '
588 . 'name="' . $name . '" '
589 . 'value="' . htmlspecialchars($value) . '" />';
590 return ($result);
591 }
592
593 /**
594 * @param array $optgrps
595 * @param array $optvals
596 * @return array
597 */
598 function create_option_groups($optgrps, $optvals) {
599 /* Build a simple array with which to start. */
600 $result = array();
601
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();
607 }
608
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(
614 $optset['name'],
615 $optset['caption'],
616 $optset['type'],
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)
621 );
622
623 /* If provided, set the size for this option. */
624 if (isset($optset['size'])) {
625 $next_option->setSize($optset['size']);
626 }
627
628 /* If provided, set the trailing_text for this option. */
629 if (isset($optset['trailing_text'])) {
630 $next_option->setTrailingText($optset['trailing_text']);
631 }
632
633 /* If provided, set the comment for this option. */
634 if (isset($optset['comment'])) {
635 $next_option->setComment($optset['comment']);
636 }
637
638 /* If provided, set the save function for this option. */
639 if (isset($optset['save'])) {
640 $next_option->setSaveFunction($optset['save']);
641 }
642
643 /* If provided, set the script for this option. */
644 if (isset($optset['script'])) {
645 $next_option->setScript($optset['script']);
646 }
647
648 /* If provided, set the "post script" for this option. */
649 if (isset($optset['post_script'])) {
650 $next_option->setPostScript($optset['post_script']);
651 }
652
653 /* Add this option to the option array. */
654 $result[$grpkey]['options'][] = $next_option;
655 }
656 }
657
658 /* Return our resulting array. */
659 return ($result);
660 }
661
662 /**
663 * @param array $option_groups
664 */
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".
671 html_tag( 'td',
672 '<b>' . $next_optgrp['name'] . '</b>' ,
673 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
674 ) ."\n";
675 }
676
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' )
683 ) ."\n";
684 } else {
685 echo $option->createHTMLWidget();
686 }
687 }
688
689 /* Print an empty row after this option group. */
690 echo html_tag( 'tr',
691 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
692 ) . "\n";
693 }
694 }
695
696 /**
697 * Create submit button inside table row.
698 * @param string $name
699 */
700 function OptionSubmit( $name ) {
701 echo html_tag( 'tr',
702 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
703 ) . "\n";
704 }
705
706 // vim: et ts=4
707 ?>