Fix URL to send read receipts from read_body (#1637572),
[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:
0177059f 319//FIXME: can we throw an error here instead? either way, we don't want HTML here!
6b4bd11f 320 $result = '<font color="' . $color[2] . '">'
a3ec3c91 321 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 322 . '</font>';
a3ec3c91 323 }
324
6ae9e729 325 /* Add the "post script" for this option. */
326 $result .= $this->post_script;
62f7daa5 327
74e44765 328 // put correct value back if need be
329 if (!empty($this->new_value)) {
330 $this->value = $tempValue;
331 }
332
a3ec3c91 333 /* Now, return the created widget. */
334 return ($result);
335 }
336
b4856b14 337 /**
338 * Create string field
339 * @return string html formated option field
340 */
37a3ed17 341 function createWidget_String() {
bbcafebd 342 switch ($this->size) {
88cb1b4d 343 case SMOPT_SIZE_TINY:
344 $width = 5;
345 break;
346 case SMOPT_SIZE_SMALL:
347 $width = 12;
348 break;
349 case SMOPT_SIZE_LARGE:
350 $width = 38;
351 break;
352 case SMOPT_SIZE_HUGE:
353 $width = 50;
354 break;
bbcafebd 355 case SMOPT_SIZE_NORMAL:
88cb1b4d 356 default:
357 $width = 25;
bbcafebd 358 }
359
0177059f 360 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 361 }
362
b4856b14 363 /**
364 * Create selection box
0177059f 365 *
366 * When $this->htmlencoded is TRUE, the keys and values in
367 * $this->possible_values are assumed to be display-safe.
368 * Use with care!
369 *
b4856b14 370 * @return string html formated selection box
371 */
37a3ed17 372 function createWidget_StrList() {
98e88751 373//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
374 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 375
a3ec3c91 376 }
377
b4856b14 378 /**
379 * Create folder selection box
380 * @return string html formated selection box
381 */
37a3ed17 382 function createWidget_FolderList() {
be2d5495 383
0177059f 384 // possible values might include a nested array of
385 // possible values (list of folders)
386 //
387 $option_list = array();
388 foreach ($this->possible_values as $value => $text) {
62f7daa5 389
0177059f 390 // list of folders (boxes array)
391 //
392 if (is_array($text)) {
393 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value)), 0, $text, $this->folder_filter));
62f7daa5 394
0177059f 395 // just one option here
396 //
397 } else {
398 $option_list = array_merge($option_list, array($value => $text));
be2d5495 399 }
0177059f 400
62f7daa5 401 }
0177059f 402 if (empty($option_list))
403 $option_list = array('ignore' => _("unavailable"));
99ecf044 404
405
0177059f 406 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
407
be2d5495 408 }
409
b4856b14 410 /**
411 * Creates textarea
412 * @return string html formated textarea field
413 */
37a3ed17 414 function createWidget_TextArea() {
bbcafebd 415 switch ($this->size) {
416 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
417 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
418 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
419 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
420 case SMOPT_SIZE_NORMAL:
421 default: $rows = 5; $cols = 50;
422 }
ba556ce5 423 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
a3ec3c91 424 }
425
b4856b14 426 /**
427 * Creates field for integer
428 *
429 * Difference from createWidget_String is visible only when javascript is enabled
430 * @return string html formated option field
431 */
37a3ed17 432 function createWidget_Integer() {
0d08ea5a 433
b65d1a08 434 // add onChange javascript handler to a regular string widget
435 // which will strip out all non-numeric chars
83aff890 436 if (checkForJavascript())
0177059f 437 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 438 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
439 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
0177059f 440 . 'this.value=newVal;';
441
442 return $this->createWidget_String();
a3ec3c91 443 }
444
b4856b14 445 /**
446 * Creates field for floating number
447 * Difference from createWidget_String is visible only when javascript is enabled
448 * @return string html formated option field
449 */
37a3ed17 450 function createWidget_Float() {
37a3ed17 451
b65d1a08 452 // add onChange javascript handler to a regular string widget
62f7daa5 453 // which will strip out all non-numeric (period also OK) chars
83aff890 454 if (checkForJavascript())
0177059f 455 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 456 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
457 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
0177059f 458 . 'newVal += origVal.charAt(i); } this.value=newVal;';
459
460 return $this->createWidget_String();
a3ec3c91 461 }
462
b4856b14 463 /**
464 * Creates radio field (yes/no)
465 * @return string html formated radio field
466 */
37a3ed17 467 function createWidget_Boolean() {
0177059f 468
469 global $oTemplate;
470 $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
fd87494d 471
472 /* Build the yes choice. */
0177059f 473 $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 474
475 /* Build the no choice. */
0177059f 476 $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 477
478 /* Build and return the combined "boolean widget". */
0177059f 479 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
fd87494d 480 return ($result);
a3ec3c91 481 }
482
b4856b14 483 /**
484 * Creates hidden field
485 * @return string html formated hidden input field
486 */
37a3ed17 487 function createWidget_Hidden() {
0177059f 488 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
a3ec3c91 489 }
490
b4856b14 491 /**
492 * Creates comment
493 * @return string comment
494 */
37a3ed17 495 function createWidget_Comment() {
bbcafebd 496 $result = $this->comment;
497 return ($result);
498 }
499
b4856b14 500 /**
501 *
502 */
cbe5423b 503 function save() {
504 $function = $this->save_function;
505 $function($this);
44ef0f47 506 }
cbe5423b 507
b4856b14 508 /**
509 *
510 */
cbe5423b 511 function changed() {
6206f6c4 512 return ($this->value != $this->new_value);
cbe5423b 513 }
b4856b14 514} /* End of SquirrelOption class*/
cbe5423b 515
b4856b14 516/**
517 * Saves option
518 * @param object $option object that holds option name and new_value
519 */
cbe5423b 520function save_option($option) {
dac16606 521 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
522 /* Can't save the pref if we don't have the username */
523 return;
0b97a708 524 }
525 global $data_dir;
0b97a708 526 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 527}
528
b4856b14 529/**
530 * save function that does not save
531 * @param object $option
532 */
cbe5423b 533function save_option_noop($option) {
534 /* Do nothing here... */
9962527a 535}
44ef0f47 536
b4856b14 537/**
538 * Create hidden 'optpage' input field with value set by argument
539 * @param string $optpage identification of option page
540 * @return string html formated hidden input field
541 */
cbe5423b 542function create_optpage_element($optpage) {
0177059f 543 return addHidden('optpage', $optpage);
cbe5423b 544}
545
b4856b14 546/**
547 * Create hidden 'optmode' input field with value set by argument
548 * @param string $optmode
549 * @return string html formated hidden input field
550 */
cbe5423b 551function create_optmode_element($optmode) {
0177059f 552 return addHidden('optmode', $optmode);
cbe5423b 553}
554
b4856b14 555/**
556 * @param array $optgrps
557 * @param array $optvals
558 * @return array
559 */
cbe5423b 560function create_option_groups($optgrps, $optvals) {
a3ec3c91 561 /* Build a simple array with which to start. */
562 $result = array();
563
bbcafebd 564 /* Create option group for each option group name. */
565 foreach ($optgrps as $grpkey => $grpname) {
566 $result[$grpkey] = array();
567 $result[$grpkey]['name'] = $grpname;
568 $result[$grpkey]['options'] = array();
569 }
570
a3ec3c91 571 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 572 foreach ($optvals as $grpkey => $grpopts) {
573 foreach ($grpopts as $optset) {
28520c87 574 /* Create a new option with all values given. */
575 $next_option = new SquirrelOption(
7390e240 576 $optset['name'],
577 $optset['caption'],
578 $optset['type'],
579 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
580 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
581 (isset($optset['posvals']) ? $optset['posvals'] : ''),
582 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
583 );
bbcafebd 584
585 /* If provided, set the size for this option. */
586 if (isset($optset['size'])) {
587 $next_option->setSize($optset['size']);
588 }
589
361d6e1b 590 /* If provided, set the trailing_text for this option. */
591 if (isset($optset['trailing_text'])) {
592 $next_option->setTrailingText($optset['trailing_text']);
593 }
594
bbcafebd 595 /* If provided, set the comment for this option. */
596 if (isset($optset['comment'])) {
597 $next_option->setComment($optset['comment']);
598 }
599
cbe5423b 600 /* If provided, set the save function for this option. */
601 if (isset($optset['save'])) {
602 $next_option->setSaveFunction($optset['save']);
603 }
604
0177059f 605 /* If provided, set the extra attributes for this option. */
606 if (isset($optset['extra_attributes'])) {
607 $next_option->setExtraAttributes($optset['extra_attributes']);
cbe5423b 608 }
609
6ae9e729 610 /* If provided, set the "post script" for this option. */
611 if (isset($optset['post_script'])) {
612 $next_option->setPostScript($optset['post_script']);
613 }
614
99ecf044 615 /* If provided, set the folder_filter for this option. */
616 if (isset($optset['folder_filter'])) {
617 $next_option->setFolderFilter($optset['folder_filter']);
618 }
619
bbcafebd 620 /* Add this option to the option array. */
621 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 622 }
623 }
624
625 /* Return our resulting array. */
626 return ($result);
627}
628
d6150d69 629// vim: et ts=4