Removed "Include CCs when Forwarding Messages", which had no functionality whatsoever
[squirrelmail.git] / functions / options.php
CommitLineData
44ef0f47 1<?php
2ba13803 2
35586184 3/**
4 * options.php
5 *
35586184 6 * Functions needed to display the options pages.
7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
ca479ad1 12 * @subpackage prefs
35586184 13 */
a3ec3c91 14
15/**********************************************/
16/* Define constants used in the options code. */
17/**********************************************/
18
19/* Define constants for the various option types. */
20define('SMOPT_TYPE_STRING', 0);
21define('SMOPT_TYPE_STRLIST', 1);
7e6d5ea3 22define('SMOPT_TYPE_TEXTAREA', 2);
a3ec3c91 23define('SMOPT_TYPE_INTEGER', 3);
24define('SMOPT_TYPE_FLOAT', 4);
25define('SMOPT_TYPE_BOOLEAN', 5);
2a50fbd7 26define('SMOPT_TYPE_HIDDEN', 6);
bbcafebd 27define('SMOPT_TYPE_COMMENT', 7);
be2d5495 28define('SMOPT_TYPE_FLDRLIST', 8);
a3ec3c91 29
30/* Define constants for the options refresh levels. */
31define('SMOPT_REFRESH_NONE', 0);
32define('SMOPT_REFRESH_FOLDERLIST', 1);
33define('SMOPT_REFRESH_ALL', 2);
34
bbcafebd 35/* Define constants for the options size. */
36define('SMOPT_SIZE_TINY', 0);
37define('SMOPT_SIZE_SMALL', 1);
38define('SMOPT_SIZE_MEDIUM', 2);
39define('SMOPT_SIZE_LARGE', 3);
40define('SMOPT_SIZE_HUGE', 4);
88cb1b4d 41define('SMOPT_SIZE_NORMAL', 5);
bbcafebd 42
cbe5423b 43define('SMOPT_SAVE_DEFAULT', 'save_option');
44define('SMOPT_SAVE_NOOP', 'save_option_noop');
45
9962527a 46/**
598294a7 47 * SquirrelOption: An option for SquirrelMail.
9962527a 48 *
8f6f9ba5 49 * @package squirrelmail
b4856b14 50 * @subpackage prefs
9962527a 51 */
52class SquirrelOption {
b4856b14 53 /**
54 * The name of this setting
55 * @var string
56 */
9962527a 57 var $name;
b4856b14 58 /**
59 * The text that prefaces setting on the preferences page
60 * @var string
61 */
9962527a 62 var $caption;
b4856b14 63 /**
64 * The type of INPUT element
65 *
66 * See SMOPT_TYPE_* defines
67 * @var integer
68 */
9962527a 69 var $type;
b4856b14 70 /**
598294a7 71 * Indicates if a link should be shown to refresh part
b4856b14 72 * or all of the window
73 *
74 * See SMOPT_REFRESH_* defines
75 * @var integer
76 */
a3ec3c91 77 var $refresh_level;
b4856b14 78 /**
79 * Specifies the size of certain input items
80 *
81 * See SMOPT_SIZE_* defines
82 * @var integer
83 */
bbcafebd 84 var $size;
b4856b14 85 /**
598294a7 86 * Text that follows a text input or
b4856b14 87 * select list input on the preferences page
598294a7 88 *
b4856b14 89 * useful for indicating units, meanings of special values, etc.
90 * @var string
91 */
361d6e1b 92 var $trailing_text;
b4856b14 93 /**
94 * text displayed to the user
95 *
96 * Used with SMOPT_TYPE_COMMENT options
97 * @var string
98 */
bbcafebd 99 var $comment;
b4856b14 100 /**
0177059f 101 * additional javascript or other widget attributes added to the
102 * user input; must be an array where keys are attribute names
103 * ("onclick", etc) and values are the attribute values.
104 * @var array
b4856b14 105 */
0177059f 106 var $aExtraAttribs;
b4856b14 107 /**
598294a7 108 * script (usually Javascript) that will be placed after (outside of)
b4856b14 109 * the INPUT tag
110 * @var string
111 */
6ae9e729 112 var $post_script;
cbe5423b 113
b4856b14 114 /**
115 * The name of the Save Function for this option.
116 * @var string
117 */
cbe5423b 118 var $save_function;
9962527a 119
120 /* The various 'values' for this options. */
b4856b14 121 /**
122 * default/preselected value for this option
123 * @var mixed
124 */
9962527a 125 var $value;
b4856b14 126 /**
127 * new option value
128 * @var mixed
129 */
9962527a 130 var $new_value;
b4856b14 131 /**
598294a7 132 * associative array, where each key is an actual input value
b4856b14 133 * and the corresponding value is what is displayed to the user
134 * for that list item in the drop-down list
135 * @var array
136 */
a3ec3c91 137 var $possible_values;
b4856b14 138 /**
139 * disables html sanitizing.
598294a7 140 *
141 * WARNING - don't use it, if user input is possible in option
0177059f 142 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
b4856b14 143 * @var bool
144 */
28520c87 145 var $htmlencoded=false;
99ecf044 146 /**
147 * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
148 * See $flag argument in sqimap_mailbox_option_list() function.
149 * @var string
150 * @since 1.5.1
151 */
152 var $folder_filter='noselect';
9962527a 153
b4856b14 154 /**
155 * Constructor function
156 * @param string $name
157 * @param string $caption
158 * @param integer $type
159 * @param integer $refresh_level
160 * @param mixed $initial_value
161 * @param array $possible_values
162 * @param bool $htmlencoded
163 */
9962527a 164 function SquirrelOption
28520c87 165 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
9962527a 166 /* Set the basic stuff. */
167 $this->name = $name;
168 $this->caption = $caption;
9962527a 169 $this->type = $type;
a3ec3c91 170 $this->refresh_level = $refresh_level;
171 $this->possible_values = $possible_values;
28520c87 172 $this->htmlencoded = $htmlencoded;
bbcafebd 173 $this->size = SMOPT_SIZE_MEDIUM;
361d6e1b 174 $this->trailing_text = '';
bbcafebd 175 $this->comment = '';
0177059f 176 $this->aExtraAttribs = array();
6ae9e729 177 $this->post_script = '';
a3ec3c91 178
991c88e7 179 //Check for a current value.
180 if (isset($GLOBALS[$name])) {
a3ec3c91 181 $this->value = $GLOBALS[$name];
17f3d242 182 } else if (!empty($initial_value)) {
183 $this->value = $initial_value;
a3ec3c91 184 } else {
185 $this->value = '';
186 }
9962527a 187
a3ec3c91 188 /* Check for a new value. */
b4856b14 189 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
a3ec3c91 190 $this->new_value = '';
44ef0f47 191 }
cbe5423b 192
193 /* Set the default save function. */
2a50fbd7 194 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 195 $this->save_function = SMOPT_SAVE_DEFAULT;
196 } else {
197 $this->save_function = SMOPT_SAVE_NOOP;
198 }
199 }
200
b4856b14 201 /**
202 * Set the value for this option.
203 * @param mixed $value
204 */
cbe5423b 205 function setValue($value) {
206 $this->value = $value;
207 }
208
b4856b14 209 /**
210 * Set the new value for this option.
211 * @param mixed $new_value
212 */
cbe5423b 213 function setNewValue($new_value) {
214 $this->new_value = $new_value;
9962527a 215 }
44ef0f47 216
b4856b14 217 /**
218 * Set the size for this option.
219 * @param integer $size
220 */
bbcafebd 221 function setSize($size) {
222 $this->size = $size;
223 }
224
b4856b14 225 /**
226 * Set the trailing_text for this option.
227 * @param string $trailing_text
228 */
361d6e1b 229 function setTrailingText($trailing_text) {
230 $this->trailing_text = $trailing_text;
231 }
232
b4856b14 233 /**
234 * Set the comment for this option.
235 * @param string $comment
236 */
bbcafebd 237 function setComment($comment) {
238 $this->comment = $comment;
239 }
240
b4856b14 241 /**
0177059f 242 * Set the extra attributes for this option.
243 * @param array $aExtraAttribs
b4856b14 244 */
0177059f 245 function setExtraAttributes($aExtraAttribs) {
246 $this->aExtraAttribs = $aExtraAttribs;
cbe5423b 247 }
248
b4856b14 249 /**
250 * Set the "post script" for this option.
251 * @param string $post_script
252 */
6ae9e729 253 function setPostScript($post_script) {
254 $this->post_script = $post_script;
255 }
256
b4856b14 257 /**
258 * Set the save function for this option.
259 * @param string $save_function
260 */
cbe5423b 261 function setSaveFunction($save_function) {
262 $this->save_function = $save_function;
263 }
264
99ecf044 265 /**
266 * Set the trailing_text for this option.
267 * @param string $folder_filter
268 * @since 1.5.1
269 */
270 function setFolderFilter($folder_filter) {
271 $this->folder_filter = $folder_filter;
272 }
273
b4856b14 274 /**
275 * Creates fields on option pages according to option type
276 *
277 * Function that calls other createWidget* functions.
598294a7 278 * @return string html formated option field
b4856b14 279 */
a3ec3c91 280 function createHTMLWidget() {
ce68b76b 281 global $color;
cbe5423b 282
62f7daa5 283 // Use new value if available
74e44765 284 if (!empty($this->new_value)) {
285 $tempValue = $this->value;
286 $this->value = $this->new_value;
287 }
288
cbe5423b 289 /* Get the widget for this option type. */
a3ec3c91 290 switch ($this->type) {
291 case SMOPT_TYPE_STRING:
37a3ed17 292 $result = $this->createWidget_String();
a3ec3c91 293 break;
294 case SMOPT_TYPE_STRLIST:
37a3ed17 295 $result = $this->createWidget_StrList();
a3ec3c91 296 break;
7e6d5ea3 297 case SMOPT_TYPE_TEXTAREA:
37a3ed17 298 $result = $this->createWidget_TextArea();
a3ec3c91 299 break;
300 case SMOPT_TYPE_INTEGER:
37a3ed17 301 $result = $this->createWidget_Integer();
a3ec3c91 302 break;
303 case SMOPT_TYPE_FLOAT:
37a3ed17 304 $result = $this->createWidget_Float();
a3ec3c91 305 break;
306 case SMOPT_TYPE_BOOLEAN:
37a3ed17 307 $result = $this->createWidget_Boolean();
a3ec3c91 308 break;
2a50fbd7 309 case SMOPT_TYPE_HIDDEN:
37a3ed17 310 $result = $this->createWidget_Hidden();
a3ec3c91 311 break;
bbcafebd 312 case SMOPT_TYPE_COMMENT:
37a3ed17 313 $result = $this->createWidget_Comment();
bbcafebd 314 break;
be2d5495 315 case SMOPT_TYPE_FLDRLIST:
37a3ed17 316 $result = $this->createWidget_FolderList();
be2d5495 317 break;
a3ec3c91 318 default:
fb8c4296 319 error_box (
320 sprintf(_("Option Type '%s' Not Found"), $this->type)
321 );
a3ec3c91 322 }
323
6ae9e729 324 /* Add the "post script" for this option. */
325 $result .= $this->post_script;
62f7daa5 326
74e44765 327 // put correct value back if need be
328 if (!empty($this->new_value)) {
329 $this->value = $tempValue;
330 }
331
a3ec3c91 332 /* Now, return the created widget. */
333 return ($result);
334 }
335
b4856b14 336 /**
337 * Create string field
338 * @return string html formated option field
339 */
37a3ed17 340 function createWidget_String() {
bbcafebd 341 switch ($this->size) {
88cb1b4d 342 case SMOPT_SIZE_TINY:
343 $width = 5;
344 break;
345 case SMOPT_SIZE_SMALL:
346 $width = 12;
347 break;
348 case SMOPT_SIZE_LARGE:
349 $width = 38;
350 break;
351 case SMOPT_SIZE_HUGE:
352 $width = 50;
353 break;
bbcafebd 354 case SMOPT_SIZE_NORMAL:
88cb1b4d 355 default:
356 $width = 25;
bbcafebd 357 }
358
0177059f 359 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 360 }
361
b4856b14 362 /**
363 * Create selection box
0177059f 364 *
365 * When $this->htmlencoded is TRUE, the keys and values in
366 * $this->possible_values are assumed to be display-safe.
367 * Use with care!
368 *
b4856b14 369 * @return string html formated selection box
370 */
37a3ed17 371 function createWidget_StrList() {
98e88751 372//FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
373 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 374
a3ec3c91 375 }
376
b4856b14 377 /**
378 * Create folder selection box
379 * @return string html formated selection box
380 */
37a3ed17 381 function createWidget_FolderList() {
be2d5495 382
0177059f 383 // possible values might include a nested array of
384 // possible values (list of folders)
385 //
386 $option_list = array();
387 foreach ($this->possible_values as $value => $text) {
62f7daa5 388
0177059f 389 // list of folders (boxes array)
390 //
391 if (is_array($text)) {
392 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value)), 0, $text, $this->folder_filter));
62f7daa5 393
0177059f 394 // just one option here
395 //
396 } else {
397 $option_list = array_merge($option_list, array($value => $text));
be2d5495 398 }
0177059f 399
62f7daa5 400 }
0177059f 401 if (empty($option_list))
402 $option_list = array('ignore' => _("unavailable"));
99ecf044 403
404
0177059f 405 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
406
be2d5495 407 }
408
b4856b14 409 /**
410 * Creates textarea
411 * @return string html formated textarea field
412 */
37a3ed17 413 function createWidget_TextArea() {
bbcafebd 414 switch ($this->size) {
415 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
416 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
417 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
418 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
419 case SMOPT_SIZE_NORMAL:
420 default: $rows = 5; $cols = 50;
421 }
ba556ce5 422 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
a3ec3c91 423 }
424
b4856b14 425 /**
426 * Creates field for integer
427 *
428 * Difference from createWidget_String is visible only when javascript is enabled
429 * @return string html formated option field
430 */
37a3ed17 431 function createWidget_Integer() {
0d08ea5a 432
b65d1a08 433 // add onChange javascript handler to a regular string widget
434 // which will strip out all non-numeric chars
83aff890 435 if (checkForJavascript())
0177059f 436 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 437 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
438 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
0177059f 439 . 'this.value=newVal;';
440
441 return $this->createWidget_String();
a3ec3c91 442 }
443
b4856b14 444 /**
445 * Creates field for floating number
446 * Difference from createWidget_String is visible only when javascript is enabled
447 * @return string html formated option field
448 */
37a3ed17 449 function createWidget_Float() {
37a3ed17 450
b65d1a08 451 // add onChange javascript handler to a regular string widget
62f7daa5 452 // which will strip out all non-numeric (period also OK) chars
83aff890 453 if (checkForJavascript())
0177059f 454 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 455 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
456 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
0177059f 457 . 'newVal += origVal.charAt(i); } this.value=newVal;';
458
459 return $this->createWidget_String();
a3ec3c91 460 }
461
b4856b14 462 /**
463 * Creates radio field (yes/no)
464 * @return string html formated radio field
465 */
37a3ed17 466 function createWidget_Boolean() {
0177059f 467
468 global $oTemplate;
469 $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
fd87494d 470
471 /* Build the yes choice. */
0177059f 472 $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label(_("Yes"), 'new_' . $this->name . '_yes');
fd87494d 473
474 /* Build the no choice. */
0177059f 475 $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label(_("No"), 'new_' . $this->name . '_no');
fd87494d 476
477 /* Build and return the combined "boolean widget". */
0177059f 478 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
fd87494d 479 return ($result);
a3ec3c91 480 }
481
b4856b14 482 /**
483 * Creates hidden field
484 * @return string html formated hidden input field
485 */
37a3ed17 486 function createWidget_Hidden() {
0177059f 487 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
a3ec3c91 488 }
489
b4856b14 490 /**
491 * Creates comment
492 * @return string comment
493 */
37a3ed17 494 function createWidget_Comment() {
bbcafebd 495 $result = $this->comment;
496 return ($result);
497 }
498
b4856b14 499 /**
500 *
501 */
cbe5423b 502 function save() {
503 $function = $this->save_function;
504 $function($this);
44ef0f47 505 }
cbe5423b 506
b4856b14 507 /**
508 *
509 */
cbe5423b 510 function changed() {
6206f6c4 511 return ($this->value != $this->new_value);
cbe5423b 512 }
b4856b14 513} /* End of SquirrelOption class*/
cbe5423b 514
b4856b14 515/**
516 * Saves option
517 * @param object $option object that holds option name and new_value
518 */
cbe5423b 519function save_option($option) {
dac16606 520 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
521 /* Can't save the pref if we don't have the username */
522 return;
0b97a708 523 }
524 global $data_dir;
0b97a708 525 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 526}
527
b4856b14 528/**
529 * save function that does not save
530 * @param object $option
531 */
cbe5423b 532function save_option_noop($option) {
533 /* Do nothing here... */
9962527a 534}
44ef0f47 535
b4856b14 536/**
537 * Create hidden 'optpage' input field with value set by argument
538 * @param string $optpage identification of option page
539 * @return string html formated hidden input field
540 */
cbe5423b 541function create_optpage_element($optpage) {
0177059f 542 return addHidden('optpage', $optpage);
cbe5423b 543}
544
b4856b14 545/**
546 * Create hidden 'optmode' input field with value set by argument
547 * @param string $optmode
548 * @return string html formated hidden input field
549 */
cbe5423b 550function create_optmode_element($optmode) {
0177059f 551 return addHidden('optmode', $optmode);
cbe5423b 552}
553
b4856b14 554/**
555 * @param array $optgrps
556 * @param array $optvals
557 * @return array
558 */
cbe5423b 559function create_option_groups($optgrps, $optvals) {
a3ec3c91 560 /* Build a simple array with which to start. */
561 $result = array();
562
bbcafebd 563 /* Create option group for each option group name. */
564 foreach ($optgrps as $grpkey => $grpname) {
565 $result[$grpkey] = array();
566 $result[$grpkey]['name'] = $grpname;
567 $result[$grpkey]['options'] = array();
568 }
569
a3ec3c91 570 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 571 foreach ($optvals as $grpkey => $grpopts) {
572 foreach ($grpopts as $optset) {
28520c87 573 /* Create a new option with all values given. */
574 $next_option = new SquirrelOption(
7390e240 575 $optset['name'],
576 $optset['caption'],
577 $optset['type'],
578 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
579 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
580 (isset($optset['posvals']) ? $optset['posvals'] : ''),
581 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
582 );
bbcafebd 583
584 /* If provided, set the size for this option. */
585 if (isset($optset['size'])) {
586 $next_option->setSize($optset['size']);
587 }
588
361d6e1b 589 /* If provided, set the trailing_text for this option. */
590 if (isset($optset['trailing_text'])) {
591 $next_option->setTrailingText($optset['trailing_text']);
592 }
593
bbcafebd 594 /* If provided, set the comment for this option. */
595 if (isset($optset['comment'])) {
596 $next_option->setComment($optset['comment']);
597 }
598
cbe5423b 599 /* If provided, set the save function for this option. */
600 if (isset($optset['save'])) {
601 $next_option->setSaveFunction($optset['save']);
602 }
603
0177059f 604 /* If provided, set the extra attributes for this option. */
605 if (isset($optset['extra_attributes'])) {
606 $next_option->setExtraAttributes($optset['extra_attributes']);
cbe5423b 607 }
608
6ae9e729 609 /* If provided, set the "post script" for this option. */
610 if (isset($optset['post_script'])) {
611 $next_option->setPostScript($optset['post_script']);
612 }
613
99ecf044 614 /* If provided, set the folder_filter for this option. */
615 if (isset($optset['folder_filter'])) {
616 $next_option->setFolderFilter($optset['folder_filter']);
617 }
618
bbcafebd 619 /* Add this option to the option array. */
620 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 621 }
622 }
623
624 /* Return our resulting array. */
625 return ($result);
626}
627