Add index.html to Development subdir, rename README.russian_apache to
[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 */
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 /**
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
155 */
156 function SquirrelOption
157 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
158 /* Set the basic stuff. */
159 $this->name = $name;
160 $this->caption = $caption;
161 $this->type = $type;
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 = '';
167 $this->comment = '';
168 $this->script = '';
169 $this->post_script = '';
170
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];
176 } else {
177 $this->value = '';
178 }
179
180 /* Check for a new value. */
181 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
182 $this->new_value = '';
183 }
184
185 /* Set the default save function. */
186 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
187 $this->save_function = SMOPT_SAVE_DEFAULT;
188 } else {
189 $this->save_function = SMOPT_SAVE_NOOP;
190 }
191 }
192
193 /**
194 * Set the value for this option.
195 * @param mixed $value
196 */
197 function setValue($value) {
198 $this->value = $value;
199 }
200
201 /**
202 * Set the new value for this option.
203 * @param mixed $new_value
204 */
205 function setNewValue($new_value) {
206 $this->new_value = $new_value;
207 }
208
209 /**
210 * Set the size for this option.
211 * @param integer $size
212 */
213 function setSize($size) {
214 $this->size = $size;
215 }
216
217 /**
218 * Set the trailing_text for this option.
219 * @param string $trailing_text
220 */
221 function setTrailingText($trailing_text) {
222 $this->trailing_text = $trailing_text;
223 }
224
225 /**
226 * Set the comment for this option.
227 * @param string $comment
228 */
229 function setComment($comment) {
230 $this->comment = $comment;
231 }
232
233 /**
234 * Set the script for this option.
235 * @param string $script
236 */
237 function setScript($script) {
238 $this->script = $script;
239 }
240
241 /**
242 * Set the "post script" for this option.
243 * @param string $post_script
244 */
245 function setPostScript($post_script) {
246 $this->post_script = $post_script;
247 }
248
249 /**
250 * Set the save function for this option.
251 * @param string $save_function
252 */
253 function setSaveFunction($save_function) {
254 $this->save_function = $save_function;
255 }
256
257 /**
258 * Creates fields on option pages according to option type
259 *
260 * Function that calls other createWidget* functions.
261 * @return string html formated option field
262 */
263 function createHTMLWidget() {
264 global $color;
265
266 // Use new value if available
267 if (!empty($this->new_value)) {
268 $tempValue = $this->value;
269 $this->value = $this->new_value;
270 }
271
272 /* Get the widget for this option type. */
273 switch ($this->type) {
274 case SMOPT_TYPE_STRING:
275 $result = $this->createWidget_String();
276 break;
277 case SMOPT_TYPE_STRLIST:
278 $result = $this->createWidget_StrList();
279 break;
280 case SMOPT_TYPE_TEXTAREA:
281 $result = $this->createWidget_TextArea();
282 break;
283 case SMOPT_TYPE_INTEGER:
284 $result = $this->createWidget_Integer();
285 break;
286 case SMOPT_TYPE_FLOAT:
287 $result = $this->createWidget_Float();
288 break;
289 case SMOPT_TYPE_BOOLEAN:
290 $result = $this->createWidget_Boolean();
291 break;
292 case SMOPT_TYPE_HIDDEN:
293 $result = $this->createWidget_Hidden();
294 break;
295 case SMOPT_TYPE_COMMENT:
296 $result = $this->createWidget_Comment();
297 break;
298 case SMOPT_TYPE_FLDRLIST:
299 $result = $this->createWidget_FolderList();
300 break;
301 default:
302 $result = '<font color="' . $color[2] . '">'
303 . sprintf(_("Option Type '%s' Not Found"), $this->type)
304 . '</font>';
305 }
306
307 /* Add the "post script" for this option. */
308 $result .= $this->post_script;
309
310 // put correct value back if need be
311 if (!empty($this->new_value)) {
312 $this->value = $tempValue;
313 }
314
315 /* Now, return the created widget. */
316 return ($result);
317 }
318
319 /**
320 * Create string field
321 * @return string html formated option field
322 */
323 function createWidget_String() {
324 switch ($this->size) {
325 case SMOPT_SIZE_TINY:
326 $width = 5;
327 break;
328 case SMOPT_SIZE_SMALL:
329 $width = 12;
330 break;
331 case SMOPT_SIZE_LARGE:
332 $width = 38;
333 break;
334 case SMOPT_SIZE_HUGE:
335 $width = 50;
336 break;
337 case SMOPT_SIZE_NORMAL:
338 default:
339 $width = 25;
340 }
341
342 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
343 htmlspecialchars($this->value) .
344 "\" size=\"$width\" $this->script />$this->trailing_text\n";
345 return ($result);
346 }
347
348 /**
349 * Create selection box
350 * @return string html formated selection box
351 */
352 function createWidget_StrList() {
353 /* Begin the select tag. */
354 $result = "<select name=\"new_$this->name\" $this->script>\n";
355
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)) . '"';
361
362 /* If this value is the current value, select it. */
363 if ($real_value == $this->value) {
364 $new_option .= ' selected="selected"';
365 }
366
367 /* Add the display value to our option string. */
368 $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
369
370 /* And add the new option string to our select tag. */
371 $result .= $new_option;
372 }
373
374 /* Close the select tag and return our happy result. */
375 $result .= "</select>$this->trailing_text\n";
376 return ($result);
377 }
378
379 /**
380 * Create folder selection box
381 * @return string html formated selection box
382 */
383 function createWidget_FolderList() {
384 $selected = array(strtolower($this->value));
385
386 /* Begin the select tag. */
387 $result = "<select name=\"new_$this->name\" $this->script>\n";
388
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);
394 } else {
395 /* Start the next new option string. */
396 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
397
398 /* If this value is the current value, select it. */
399 if ($real_value == $this->value) {
400 $new_option .= ' selected="selected"';
401 }
402
403 /* Add the display value to our option string. */
404 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
405 }
406 /* And add the new option string to our select tag. */
407 $result .= $new_option;
408 }
409 /* Close the select tag and return our happy result. */
410 $result .= "</select>\n";
411 return ($result);
412 }
413
414 /**
415 * Creates textarea
416 * @return string html formated textarea field
417 */
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;
426 }
427 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
428 . "cols=\"$cols\" $this->script>"
429 . htmlspecialchars($this->value) . "</textarea>\n";
430 return ($result);
431 }
432
433 /**
434 * Creates field for integer
435 *
436 * Difference from createWidget_String is visible only when javascript is enabled
437 * @return string html formated option field
438 */
439 function createWidget_Integer() {
440 global $javascript_on;
441
442 // add onChange javascript handler to a regular string widget
443 // which will strip out all non-numeric chars
444 if ($javascript_on)
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());
449 else
450 return $this->createWidget_String();
451 }
452
453 /**
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
457 */
458 function createWidget_Float() {
459 global $javascript_on;
460
461 // add onChange javascript handler to a regular string widget
462 // which will strip out all non-numeric (period also OK) chars
463 if ($javascript_on)
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());
469 else
470 return $this->createWidget_String();
471 }
472
473 /**
474 * Creates radio field (yes/no)
475 * @return string html formated radio field
476 */
477 function createWidget_Boolean() {
478 /* Do the whole current value thing. */
479 if ($this->value != SMPREF_NO) {
480 $yes_chk = ' checked="checked"';
481 $no_chk = '';
482 } else {
483 $yes_chk = '';
484 $no_chk = ' checked="checked"';
485 }
486
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 . ' />&nbsp;'
491 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
492
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 . ' />&nbsp;'
497 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
498
499 /* Build and return the combined "boolean widget". */
500 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
501 return ($result);
502 }
503
504 /**
505 * Creates hidden field
506 * @return string html formated hidden input field
507 */
508 function createWidget_Hidden() {
509 $result = '<input type="hidden" name="new_' . $this->name
510 . '" value="' . htmlspecialchars($this->value)
511 . '" ' . $this->script . ' />';
512 return ($result);
513 }
514
515 /**
516 * Creates comment
517 * @return string comment
518 */
519 function createWidget_Comment() {
520 $result = $this->comment;
521 return ($result);
522 }
523
524 /**
525 *
526 */
527 function save() {
528 $function = $this->save_function;
529 $function($this);
530 }
531
532 /**
533 *
534 */
535 function changed() {
536 return ($this->value != $this->new_value);
537 }
538 } /* End of SquirrelOption class*/
539
540 /**
541 * Saves option
542 * @param object $option object that holds option name and new_value
543 */
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 */
547 return;
548 }
549 global $data_dir;
550 setPref($data_dir, $username, $option->name, $option->new_value);
551 }
552
553 /**
554 * save function that does not save
555 * @param object $option
556 */
557 function save_option_noop($option) {
558 /* Do nothing here... */
559 }
560
561 /**
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
565 */
566 function create_optpage_element($optpage) {
567 return create_hidden_element('optpage', $optpage);
568 }
569
570 /**
571 * Create hidden 'optmode' input field with value set by argument
572 * @param string $optmode
573 * @return string html formated hidden input field
574 */
575 function create_optmode_element($optmode) {
576 return create_hidden_element('optmode', $optmode);
577 }
578
579 /**
580 * Create hidden field.
581 * @param string $name field name
582 * @param string $value field value
583 * @return string html formated hidden input field
584 */
585 function create_hidden_element($name, $value) {
586 $result = '<input type="hidden" '
587 . 'name="' . $name . '" '
588 . 'value="' . htmlspecialchars($value) . '" />';
589 return ($result);
590 }
591
592 /**
593 * @param array $optgrps
594 * @param array $optvals
595 * @return array
596 */
597 function create_option_groups($optgrps, $optvals) {
598 /* Build a simple array with which to start. */
599 $result = array();
600
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();
606 }
607
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(
613 $optset['name'],
614 $optset['caption'],
615 $optset['type'],
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)
620 );
621
622 /* If provided, set the size for this option. */
623 if (isset($optset['size'])) {
624 $next_option->setSize($optset['size']);
625 }
626
627 /* If provided, set the trailing_text for this option. */
628 if (isset($optset['trailing_text'])) {
629 $next_option->setTrailingText($optset['trailing_text']);
630 }
631
632 /* If provided, set the comment for this option. */
633 if (isset($optset['comment'])) {
634 $next_option->setComment($optset['comment']);
635 }
636
637 /* If provided, set the save function for this option. */
638 if (isset($optset['save'])) {
639 $next_option->setSaveFunction($optset['save']);
640 }
641
642 /* If provided, set the script for this option. */
643 if (isset($optset['script'])) {
644 $next_option->setScript($optset['script']);
645 }
646
647 /* If provided, set the "post script" for this option. */
648 if (isset($optset['post_script'])) {
649 $next_option->setPostScript($optset['post_script']);
650 }
651
652 /* Add this option to the option array. */
653 $result[$grpkey]['options'][] = $next_option;
654 }
655 }
656
657 /* Return our resulting array. */
658 return ($result);
659 }
660
661 /**
662 * @param array $option_groups
663 */
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".
670 html_tag( 'td',
671 '<b>' . $next_optgrp['name'] . '</b>' ,
672 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
673 ) ."\n";
674 }
675
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' )
682 ) ."\n";
683 } else {
684 echo $option->createHTMLWidget();
685 }
686 }
687
688 /* Print an empty row after this option group. */
689 echo html_tag( 'tr',
690 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
691 ) . "\n";
692 }
693 }
694
695 /**
696 * Create submit button inside table row.
697 * @param string $name
698 */
699 function OptionSubmit( $name ) {
700 echo html_tag( 'tr',
701 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
702 ) . "\n";
703 }
704
705 // vim: et ts=4
706 ?>