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