Happy 2017
[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 *
22387c8d 8 * @copyright 1999-2017 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
9962527a 15/**
598294a7 16 * SquirrelOption: An option for SquirrelMail.
9962527a 17 *
8f6f9ba5 18 * @package squirrelmail
b4856b14 19 * @subpackage prefs
9962527a 20 */
21class SquirrelOption {
d789daf0 22 /**
23 * The original option configuration array
24 * @var array
25 */
26 var $raw_option_array;
b4856b14 27 /**
28 * The name of this setting
29 * @var string
30 */
9962527a 31 var $name;
b4856b14 32 /**
33 * The text that prefaces setting on the preferences page
34 * @var string
35 */
9962527a 36 var $caption;
e40b0e8e 37 /**
38 * Whether or not the caption text is allowed to wrap
39 * @var boolean
40 */
41 var $caption_wrap;
b4856b14 42 /**
43 * The type of INPUT element
44 *
45 * See SMOPT_TYPE_* defines
46 * @var integer
47 */
9962527a 48 var $type;
b4856b14 49 /**
598294a7 50 * Indicates if a link should be shown to refresh part
b4856b14 51 * or all of the window
52 *
53 * See SMOPT_REFRESH_* defines
54 * @var integer
55 */
a3ec3c91 56 var $refresh_level;
b4856b14 57 /**
58 * Specifies the size of certain input items
59 *
60 * See SMOPT_SIZE_* defines
61 * @var integer
62 */
bbcafebd 63 var $size;
b4856b14 64 /**
598294a7 65 * Text that follows a text input or
b4856b14 66 * select list input on the preferences page
598294a7 67 *
b4856b14 68 * useful for indicating units, meanings of special values, etc.
69 * @var string
70 */
361d6e1b 71 var $trailing_text;
5b277d00 72 /**
73 * Text that overrides the "Yes" label for boolean
74 * radio option widgets
75 *
76 * @var string
77 */
78 var $yes_text;
79 /**
80 * Text that overrides the "No" label for boolean
81 * radio option widgets
82 *
83 * @var string
84 */
85 var $no_text;
52639d23 86 /**
87 * Some widgets support more than one layout type
88 *
89 * @var int
90 */
91 var $layout_type;
b6a08d2d 92 /**
93 * Indicates if the Add widget should be included
94 * with edit lists.
95 *
96 * @var boolean
97 */
98 var $use_add_widget;
de4c101c 99 /**
100 * Indicates if the Delete widget should be included
101 * with edit lists.
102 *
103 * @var boolean
104 */
105 var $use_delete_widget;
282f8e7c 106 /**
107 * associative array, treated the same as $possible_values
108 * (see its documentation below), but usually expected to
109 * have its first value contain a list of IMAP folders, an
110 * array itself in the format as passed back by
111 * sqimap_mailbox_list(). Used to display folder selector
112 * for possible values of an associative edit list option
113 * widget
114 *
115 * @since 1.5.2
116 * @var array
117 */
118 var $poss_value_folders;
b4856b14 119 /**
120 * text displayed to the user
121 *
122 * Used with SMOPT_TYPE_COMMENT options
123 * @var string
124 */
bbcafebd 125 var $comment;
b4856b14 126 /**
0177059f 127 * additional javascript or other widget attributes added to the
128 * user input; must be an array where keys are attribute names
129 * ("onclick", etc) and values are the attribute values.
130 * @var array
b4856b14 131 */
0177059f 132 var $aExtraAttribs;
b4856b14 133 /**
598294a7 134 * script (usually Javascript) that will be placed after (outside of)
b4856b14 135 * the INPUT tag
136 * @var string
137 */
6ae9e729 138 var $post_script;
cbe5423b 139
b4856b14 140 /**
141 * The name of the Save Function for this option.
142 * @var string
143 */
cbe5423b 144 var $save_function;
9962527a 145
146 /* The various 'values' for this options. */
b4856b14 147 /**
148 * default/preselected value for this option
149 * @var mixed
150 */
9962527a 151 var $value;
b4856b14 152 /**
153 * new option value
154 * @var mixed
155 */
9962527a 156 var $new_value;
b4856b14 157 /**
598294a7 158 * associative array, where each key is an actual input value
b4856b14 159 * and the corresponding value is what is displayed to the user
160 * for that list item in the drop-down list
161 * @var array
162 */
a3ec3c91 163 var $possible_values;
b4856b14 164 /**
165 * disables html sanitizing.
598294a7 166 *
167 * WARNING - don't use it, if user input is possible in option
0177059f 168 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
b4856b14 169 * @var bool
170 */
28520c87 171 var $htmlencoded=false;
99ecf044 172 /**
42b7c9d4 173 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
282f8e7c 174 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
175 * embedded folder lists provided for inputting values for
176 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
177 * :idgets.
99ecf044 178 * See $flag argument in sqimap_mailbox_option_list() function.
179 * @var string
180 * @since 1.5.1
181 */
182 var $folder_filter='noselect';
9962527a 183
b4856b14 184 /**
b4d52882 185 * Constructor (PHP5 style, required in some future version of PHP)
d789daf0 186 * @param array $raw_option_array
b4856b14 187 * @param string $name
188 * @param string $caption
189 * @param integer $type
190 * @param integer $refresh_level
191 * @param mixed $initial_value
192 * @param array $possible_values
193 * @param bool $htmlencoded
194 */
b4d52882 195 function __construct
d789daf0 196 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
9962527a 197 /* Set the basic stuff. */
d789daf0 198 $this->raw_option_array = $raw_option_array;
9962527a 199 $this->name = $name;
200 $this->caption = $caption;
e40b0e8e 201 $this->caption_wrap = TRUE;
9962527a 202 $this->type = $type;
a3ec3c91 203 $this->refresh_level = $refresh_level;
204 $this->possible_values = $possible_values;
28520c87 205 $this->htmlencoded = $htmlencoded;
b1dcab7e 206 $this->size = SMOPT_SIZE_NORMAL;
361d6e1b 207 $this->trailing_text = '';
5b277d00 208 $this->yes_text = '';
209 $this->no_text = '';
bbcafebd 210 $this->comment = '';
52639d23 211 $this->layout_type = 0;
b6a08d2d 212 $this->use_add_widget = TRUE;
de4c101c 213 $this->use_delete_widget = TRUE;
282f8e7c 214 $this->poss_value_folders = '';
0177059f 215 $this->aExtraAttribs = array();
6ae9e729 216 $this->post_script = '';
a3ec3c91 217
991c88e7 218 //Check for a current value.
219 if (isset($GLOBALS[$name])) {
a3ec3c91 220 $this->value = $GLOBALS[$name];
17f3d242 221 } else if (!empty($initial_value)) {
222 $this->value = $initial_value;
a3ec3c91 223 } else {
224 $this->value = '';
225 }
9962527a 226
a3ec3c91 227 /* Check for a new value. */
b4856b14 228 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
59fc0b63 229 $this->new_value = NULL;
44ef0f47 230 }
cbe5423b 231
232 /* Set the default save function. */
6c06cf54 233 if ($type != SMOPT_TYPE_HIDDEN
234 && $type != SMOPT_TYPE_INFO
235 && $type != SMOPT_TYPE_COMMENT) {
cbe5423b 236 $this->save_function = SMOPT_SAVE_DEFAULT;
237 } else {
238 $this->save_function = SMOPT_SAVE_NOOP;
239 }
240 }
241
b4d52882 242 /**
243 * Constructor (PHP4 style, kept for compatibility reasons)
244 * @param array $raw_option_array
245 * @param string $name
246 * @param string $caption
247 * @param integer $type
248 * @param integer $refresh_level
249 * @param mixed $initial_value
250 * @param array $possible_values
251 * @param bool $htmlencoded
252 */
253 function SquirrelOption
254 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
255 self::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
256 }
257
5a42c101 258 /** Convenience function that identifies which types of
259 widgets are stored as (serialized) array values. */
260 function is_multiple_valued() {
261 return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
262 || $this->type == SMOPT_TYPE_STRLIST_MULTI
282f8e7c 263 || $this->type == SMOPT_TYPE_EDIT_LIST
264 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE);
5a42c101 265 }
266
b4856b14 267 /**
268 * Set the value for this option.
269 * @param mixed $value
270 */
cbe5423b 271 function setValue($value) {
272 $this->value = $value;
273 }
274
b4856b14 275 /**
276 * Set the new value for this option.
277 * @param mixed $new_value
278 */
cbe5423b 279 function setNewValue($new_value) {
280 $this->new_value = $new_value;
9962527a 281 }
44ef0f47 282
e40b0e8e 283 /**
284 * Set whether the caption is allowed to wrap for this option.
285 * @param boolean $caption_wrap
286 */
287 function setCaptionWrap($caption_wrap) {
288 $this->caption_wrap = $caption_wrap;
289 }
290
b4856b14 291 /**
292 * Set the size for this option.
293 * @param integer $size
294 */
bbcafebd 295 function setSize($size) {
296 $this->size = $size;
297 }
298
b4856b14 299 /**
300 * Set the trailing_text for this option.
301 * @param string $trailing_text
302 */
361d6e1b 303 function setTrailingText($trailing_text) {
304 $this->trailing_text = $trailing_text;
305 }
306
5b277d00 307 /**
308 * Set the yes_text for this option.
309 * @param string $yes_text
310 */
311 function setYesText($yes_text) {
312 $this->yes_text = $yes_text;
313 }
314
315 /**
316 * Set the no_text for this option.
317 * @param string $no_text
318 */
319 function setNoText($no_text) {
320 $this->no_text = $no_text;
321 }
322
b6a08d2d 323 /* Set the "use add widget" value for this option. */
324 function setUseAddWidget($use_add_widget) {
325 $this->use_add_widget = $use_add_widget;
326 }
327
de4c101c 328 /* Set the "use delete widget" value for this option. */
329 function setUseDeleteWidget($use_delete_widget) {
330 $this->use_delete_widget = $use_delete_widget;
331 }
332
282f8e7c 333 /* Set the "poss value folders" value for this option.
334 See the associative edit list widget, which uses this
335 to offer folder list selection for the values */
336 function setPossValueFolders($poss_value_folders) {
337 $this->poss_value_folders = $poss_value_folders;
338 }
339
52639d23 340 /**
341 * Set the layout type for this option.
342 * @param int $layout_type
343 */
344 function setLayoutType($layout_type) {
345 $this->layout_type = $layout_type;
346 }
347
b4856b14 348 /**
349 * Set the comment for this option.
350 * @param string $comment
351 */
bbcafebd 352 function setComment($comment) {
353 $this->comment = $comment;
354 }
355
b4856b14 356 /**
0177059f 357 * Set the extra attributes for this option.
358 * @param array $aExtraAttribs
b4856b14 359 */
0177059f 360 function setExtraAttributes($aExtraAttribs) {
361 $this->aExtraAttribs = $aExtraAttribs;
cbe5423b 362 }
363
b4856b14 364 /**
365 * Set the "post script" for this option.
366 * @param string $post_script
367 */
6ae9e729 368 function setPostScript($post_script) {
369 $this->post_script = $post_script;
370 }
371
b4856b14 372 /**
373 * Set the save function for this option.
374 * @param string $save_function
375 */
cbe5423b 376 function setSaveFunction($save_function) {
377 $this->save_function = $save_function;
378 }
379
99ecf044 380 /**
5b277d00 381 * Set the folder_filter for this option.
99ecf044 382 * @param string $folder_filter
383 * @since 1.5.1
384 */
385 function setFolderFilter($folder_filter) {
386 $this->folder_filter = $folder_filter;
387 }
388
b4856b14 389 /**
390 * Creates fields on option pages according to option type
391 *
9786ea94 392 * This is the function that calls all other createWidget* functions.
393 *
394 * @return string The formated option field
395 *
b4856b14 396 */
9786ea94 397 function createWidget() {
ce68b76b 398 global $color;
cbe5423b 399
62f7daa5 400 // Use new value if available
59fc0b63 401 if (!is_null($this->new_value)) {
74e44765 402 $tempValue = $this->value;
403 $this->value = $this->new_value;
404 }
405
cbe5423b 406 /* Get the widget for this option type. */
a3ec3c91 407 switch ($this->type) {
3fa09710 408 case SMOPT_TYPE_PASSWORD:
409 $result = $this->createWidget_String(TRUE);
410 break;
a3ec3c91 411 case SMOPT_TYPE_STRING:
37a3ed17 412 $result = $this->createWidget_String();
a3ec3c91 413 break;
414 case SMOPT_TYPE_STRLIST:
37a3ed17 415 $result = $this->createWidget_StrList();
a3ec3c91 416 break;
7e6d5ea3 417 case SMOPT_TYPE_TEXTAREA:
37a3ed17 418 $result = $this->createWidget_TextArea();
a3ec3c91 419 break;
420 case SMOPT_TYPE_INTEGER:
37a3ed17 421 $result = $this->createWidget_Integer();
a3ec3c91 422 break;
423 case SMOPT_TYPE_FLOAT:
37a3ed17 424 $result = $this->createWidget_Float();
a3ec3c91 425 break;
426 case SMOPT_TYPE_BOOLEAN:
37a3ed17 427 $result = $this->createWidget_Boolean();
a3ec3c91 428 break;
1b7db98b 429 case SMOPT_TYPE_BOOLEAN_CHECKBOX:
430 $result = $this->createWidget_Boolean(TRUE);
431 break;
8b2726c5 432 case SMOPT_TYPE_BOOLEAN_RADIO:
433 $result = $this->createWidget_Boolean(FALSE);
434 break;
2a50fbd7 435 case SMOPT_TYPE_HIDDEN:
37a3ed17 436 $result = $this->createWidget_Hidden();
a3ec3c91 437 break;
bbcafebd 438 case SMOPT_TYPE_COMMENT:
37a3ed17 439 $result = $this->createWidget_Comment();
bbcafebd 440 break;
be2d5495 441 case SMOPT_TYPE_FLDRLIST:
37a3ed17 442 $result = $this->createWidget_FolderList();
be2d5495 443 break;
42b7c9d4 444 case SMOPT_TYPE_FLDRLIST_MULTI:
445 $result = $this->createWidget_FolderList(TRUE);
446 break;
38d93650 447 case SMOPT_TYPE_EDIT_LIST:
448 $result = $this->createWidget_EditList();
449 break;
282f8e7c 450 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE:
451 $result = $this->createWidget_EditListAssociative();
452 break;
40268626 453 case SMOPT_TYPE_STRLIST_MULTI:
454 $result = $this->createWidget_StrList(TRUE);
455 break;
daf77710 456 case SMOPT_TYPE_STRLIST_RADIO:
457 $result = $this->createWidget_StrList(FALSE, TRUE);
458 break;
b6a08d2d 459 case SMOPT_TYPE_SUBMIT:
460 $result = $this->createWidget_Submit();
461 break;
6c06cf54 462 case SMOPT_TYPE_INFO:
463 $result = $this->createWidget_Info();
464 break;
a3ec3c91 465 default:
fb8c4296 466 error_box (
467 sprintf(_("Option Type '%s' Not Found"), $this->type)
468 );
a3ec3c91 469 }
470
6ae9e729 471 /* Add the "post script" for this option. */
472 $result .= $this->post_script;
62f7daa5 473
74e44765 474 // put correct value back if need be
59fc0b63 475 if (!is_null($this->new_value)) {
74e44765 476 $this->value = $tempValue;
477 }
478
a3ec3c91 479 /* Now, return the created widget. */
9786ea94 480 return $result;
a3ec3c91 481 }
482
6c06cf54 483 /**
484 * Creates info block
485 * @return string html formated output
486 */
487 function createWidget_Info() {
488 return sq_htmlspecialchars($this->value);
489 }
490
b4856b14 491 /**
492 * Create string field
3fa09710 493 *
494 * @param boolean $password When TRUE, the text in the input
495 * widget will be obscured (OPTIONAL;
496 * default = FALSE).
497 *
b4856b14 498 * @return string html formated option field
3fa09710 499 *
b4856b14 500 */
3fa09710 501 function createWidget_String($password=FALSE) {
bbcafebd 502 switch ($this->size) {
88cb1b4d 503 case SMOPT_SIZE_TINY:
504 $width = 5;
505 break;
506 case SMOPT_SIZE_SMALL:
507 $width = 12;
508 break;
509 case SMOPT_SIZE_LARGE:
510 $width = 38;
511 break;
512 case SMOPT_SIZE_HUGE:
513 $width = 50;
514 break;
bbcafebd 515 case SMOPT_SIZE_NORMAL:
88cb1b4d 516 default:
517 $width = 25;
bbcafebd 518 }
519
f08c3bcc 520//TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here
3fa09710 521 if ($password)
3047e291 522 return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . sm_encode_html_special_chars($this->trailing_text);
3fa09710 523 else
3047e291 524 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . sm_encode_html_special_chars($this->trailing_text);
a3ec3c91 525 }
526
b4856b14 527 /**
daf77710 528 * Create selection box or radio button group
0177059f 529 *
530 * When $this->htmlencoded is TRUE, the keys and values in
531 * $this->possible_values are assumed to be display-safe.
532 * Use with care!
533 *
daf77710 534 * Note that when building radio buttons instead of a select
535 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
536 * radio buttons will be output one after another without
537 * linebreaks between them. Otherwise, each radio button
538 * goes on a line of its own.
539 *
540 * @param boolean $multiple_select When TRUE, the select widget
40268626 541 * will allow multiple selections
daf77710 542 * (OPTIONAL; default is FALSE
40268626 543 * (single select list))
daf77710 544 * @param boolean $radio_buttons When TRUE, the widget will
545 * instead be built as a group
546 * of radio buttons (and
547 * $multiple_select will be
548 * forced to FALSE) (OPTIONAL;
549 * default is FALSE (select widget))
40268626 550 *
daf77710 551 * @return string html formated selection box or radio buttons
40268626 552 *
b4856b14 553 */
daf77710 554 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
98e88751 555//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
40268626 556
daf77710 557 // radio buttons instead of select widget?
558 //
559 if ($radio_buttons) {
560
561 global $br, $nbsp;
562 $result = '';
563 foreach ($this->possible_values as $real_value => $disp_value) {
564 $result .= addRadioBox('new_' . $this->name, ($this->value == $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name . '_' . $real_value), $this->aExtraAttribs)) . $nbsp . create_label($disp_value, 'new_' . $this->name . '_' . $real_value);
565 if ($this->size != SMOPT_SIZE_TINY)
566 $result .= $br;
567 }
568
569 return $result;
570 }
571
572
573 // everything below applies to select widgets
574 //
40268626 575 switch ($this->size) {
576//FIXME: not sure about these sizes... seems like we could add another on the "large" side...
577 case SMOPT_SIZE_TINY:
578 $height = 3;
579 break;
580 case SMOPT_SIZE_SMALL:
581 $height = 8;
582 break;
583 case SMOPT_SIZE_LARGE:
584 $height = 15;
585 break;
586 case SMOPT_SIZE_HUGE:
587 $height = 25;
588 break;
589 case SMOPT_SIZE_NORMAL:
590 default:
591 $height = 5;
592 }
593
3047e291 594 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . sm_encode_html_special_chars($this->trailing_text);
a3ec3c91 595
a3ec3c91 596 }
597
b4856b14 598 /**
599 * Create folder selection box
42b7c9d4 600 *
601 * @param boolean $multiple_select When TRUE, the select widget
602 * will allow multiple selections
603 * (OPTIONAL; default is FALSE
604 * (single select list))
605 *
b4856b14 606 * @return string html formated selection box
42b7c9d4 607 *
b4856b14 608 */
42b7c9d4 609 function createWidget_FolderList($multiple_select=FALSE) {
be2d5495 610
38d93650 611 switch ($this->size) {
612//FIXME: not sure about these sizes... seems like we could add another on the "large" side...
613 case SMOPT_SIZE_TINY:
614 $height = 3;
615 break;
616 case SMOPT_SIZE_SMALL:
617 $height = 8;
618 break;
619 case SMOPT_SIZE_LARGE:
620 $height = 15;
621 break;
622 case SMOPT_SIZE_HUGE:
623 $height = 25;
624 break;
625 case SMOPT_SIZE_NORMAL:
626 default:
627 $height = 5;
628 }
629
0177059f 630 // possible values might include a nested array of
631 // possible values (list of folders)
632 //
633 $option_list = array();
634 foreach ($this->possible_values as $value => $text) {
62f7daa5 635
0177059f 636 // list of folders (boxes array)
637 //
638 if (is_array($text)) {
42b7c9d4 639 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
62f7daa5 640
0177059f 641 // just one option here
642 //
643 } else {
644 $option_list = array_merge($option_list, array($value => $text));
be2d5495 645 }
0177059f 646
62f7daa5 647 }
0177059f 648 if (empty($option_list))
649 $option_list = array('ignore' => _("unavailable"));
99ecf044 650
651
3047e291 652 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . sm_encode_html_special_chars($this->trailing_text);
0177059f 653
be2d5495 654 }
655
b4856b14 656 /**
657 * Creates textarea
658 * @return string html formated textarea field
659 */
37a3ed17 660 function createWidget_TextArea() {
bbcafebd 661 switch ($this->size) {
662 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
663 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
664 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
665 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
666 case SMOPT_SIZE_NORMAL:
667 default: $rows = 5; $cols = 50;
668 }
ba556ce5 669 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
a3ec3c91 670 }
671
b4856b14 672 /**
673 * Creates field for integer
674 *
675 * Difference from createWidget_String is visible only when javascript is enabled
676 * @return string html formated option field
677 */
37a3ed17 678 function createWidget_Integer() {
0d08ea5a 679
b65d1a08 680 // add onChange javascript handler to a regular string widget
681 // which will strip out all non-numeric chars
83aff890 682 if (checkForJavascript())
0177059f 683 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 684 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
685 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
0177059f 686 . 'this.value=newVal;';
687
688 return $this->createWidget_String();
a3ec3c91 689 }
690
b4856b14 691 /**
692 * Creates field for floating number
693 * Difference from createWidget_String is visible only when javascript is enabled
694 * @return string html formated option field
695 */
37a3ed17 696 function createWidget_Float() {
37a3ed17 697
b65d1a08 698 // add onChange javascript handler to a regular string widget
62f7daa5 699 // which will strip out all non-numeric (period also OK) chars
83aff890 700 if (checkForJavascript())
0177059f 701 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 702 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
703 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
0177059f 704 . 'newVal += origVal.charAt(i); } this.value=newVal;';
705
706 return $this->createWidget_String();
a3ec3c91 707 }
708
b4856b14 709 /**
1b7db98b 710 * Create boolean widget
711 *
5b277d00 712 * When creating Yes/No radio buttons, the "yes_text"
713 * and "no_text" option attributes are used to override
714 * the typical "Yes" and "No" text.
715 *
1b7db98b 716 * @param boolean $checkbox When TRUE, the widget will be
717 * constructed as a checkbox,
718 * otherwise it will be a set of
719 * Yes/No radio buttons (OPTIONAL;
3b6a455c 720 * default is TRUE (checkbox)).
1b7db98b 721 *
722 * @return string html formated boolean widget
723 *
b4856b14 724 */
3b6a455c 725 function createWidget_Boolean($checkbox=TRUE) {
0177059f 726
5f88daeb 727 global $oTemplate, $nbsp;
fd87494d 728
fd87494d 729
1b7db98b 730 // checkbox...
731 //
732 if ($checkbox) {
733 $result = addCheckbox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name), $this->aExtraAttribs)) . $nbsp . create_label($this->trailing_text, 'new_' . $this->name);
734 }
735
736 // radio buttons...
737 //
738 else {
739
740 /* Build the yes choice. */
5b277d00 741 $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((!empty($this->yes_text) ? $this->yes_text : _("Yes")), 'new_' . $this->name . '_yes');
1b7db98b 742
743 /* Build the no choice. */
5b277d00 744 $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((!empty($this->no_text) ? $this->no_text : _("No")), 'new_' . $this->name . '_no');
1b7db98b 745
746 /* Build the combined "boolean widget". */
747 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
748
749 }
fd87494d 750
fd87494d 751 return ($result);
a3ec3c91 752 }
753
b4856b14 754 /**
755 * Creates hidden field
756 * @return string html formated hidden input field
757 */
37a3ed17 758 function createWidget_Hidden() {
0177059f 759 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
a3ec3c91 760 }
761
b4856b14 762 /**
763 * Creates comment
764 * @return string comment
765 */
37a3ed17 766 function createWidget_Comment() {
bbcafebd 767 $result = $this->comment;
768 return ($result);
769 }
770
38d93650 771 /**
282f8e7c 772 * Creates a (non-associative) edit list
52639d23 773 *
774 * Note that multiple layout types are supported for this widget.
775 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
776 * constants.
777 *
38d93650 778 * @return string html formated list of edit fields and
779 * their associated controls
780 */
781 function createWidget_EditList() {
782
cb606dfd 783 global $oTemplate;
38d93650 784
785 switch ($this->size) {
38d93650 786 case SMOPT_SIZE_TINY:
787 $height = 3;
788 break;
789 case SMOPT_SIZE_SMALL:
790 $height = 8;
791 break;
b1dcab7e 792 case SMOPT_SIZE_MEDIUM:
38d93650 793 $height = 15;
794 break;
b1dcab7e 795 case SMOPT_SIZE_LARGE:
38d93650 796 $height = 25;
797 break;
b1dcab7e 798 case SMOPT_SIZE_HUGE:
799 $height = 40;
800 break;
38d93650 801 case SMOPT_SIZE_NORMAL:
802 default:
803 $height = 5;
804 }
805
dd6f9627 806 if (empty($this->possible_values)) $this->possible_values = array();
2ebfc729 807 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
dd6f9627 808
cb606dfd 809//FIXME: $this->aExtraAttribs probably should only be used in one place
810 $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
b6a08d2d 811 $oTemplate->assign('use_input_widget', $this->use_add_widget);
de4c101c 812 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
b6a08d2d 813
cb606dfd 814 $oTemplate->assign('trailing_text', $this->trailing_text);
52639d23 815 $oTemplate->assign('possible_values', $this->possible_values);
282f8e7c 816 $oTemplate->assign('current_value', $this->value);
817 $oTemplate->assign('select_widget', addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput_' . $this->name . ') == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name . '\' ) { pos = j; i=f-1; j=e-1; } j++; } i++; } if( pos >= 0 ) { window.addinput_' . $this->name . ' = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput_' . $this->name . '.value = this.options[x].text; break; } }'), $this->aExtraAttribs), TRUE, $height));
818// NOTE: i=f-1; j=e-1 is in lieu of break 2
cb606dfd 819 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
820 $oTemplate->assign('name', $this->name);
52639d23 821
822 switch ($this->layout_type) {
823 case SMOPT_EDIT_LIST_LAYOUT_SELECT:
824 return $oTemplate->fetch('edit_list_widget.tpl');
825 case SMOPT_EDIT_LIST_LAYOUT_LIST:
826 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
827 default:
282f8e7c 828 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type));
829 }
830
831 }
832
833 /**
834 * Creates an associative edit list
835 *
836 * Note that multiple layout types are supported for this widget.
837 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
838 * constants.
839 *
840 * @return string html formated list of edit fields and
841 * their associated controls
842 */
843 function createWidget_EditListAssociative() {
844
845 global $oTemplate;
846
847 switch ($this->size) {
848 case SMOPT_SIZE_TINY:
849 $height = 3;
850 break;
851 case SMOPT_SIZE_SMALL:
852 $height = 8;
853 break;
854 case SMOPT_SIZE_MEDIUM:
855 $height = 15;
856 break;
857 case SMOPT_SIZE_LARGE:
858 $height = 25;
859 break;
860 case SMOPT_SIZE_HUGE:
861 $height = 40;
862 break;
863 case SMOPT_SIZE_NORMAL:
864 default:
865 $height = 5;
866 }
867
868
869 // ensure correct format of current value(s)
870 //
871 if (empty($this->possible_values)) $this->possible_values = array();
872 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
873
874
875 $oTemplate->assign('name', $this->name);
876 $oTemplate->assign('current_value', $this->value);
877 $oTemplate->assign('possible_values', $this->possible_values);
878 $oTemplate->assign('poss_value_folders', $this->poss_value_folders);
879 $oTemplate->assign('folder_filter', $this->folder_filter);
880
881 $oTemplate->assign('use_input_widget', $this->use_add_widget);
882 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
883
884 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
885
886//FIXME: $this->aExtraAttribs probably should only be used in one place
887 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name . '_key', '', 22, 0, $this->aExtraAttribs));
888 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name . '_value', '', 12, 0, $this->aExtraAttribs));
889
890 $oTemplate->assign('select_height', $height);
891
892 $oTemplate->assign('aAttribs', $this->aExtraAttribs);
893
894 $oTemplate->assign('trailing_text', $this->trailing_text);
895
896 switch ($this->layout_type) {
897 case SMOPT_EDIT_LIST_LAYOUT_SELECT:
898 return $oTemplate->fetch('edit_list_associative_widget.tpl');
899 case SMOPT_EDIT_LIST_LAYOUT_LIST:
900 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
901 default:
902 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type));
52639d23 903 }
38d93650 904
905 }
906
b6a08d2d 907 /**
908 * Creates a submit button
909 *
910 * @return string html formated submit button widget
911 *
912 */
913 function createWidget_Submit() {
914
3047e291 915 return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . sm_encode_html_special_chars($this->trailing_text);
b6a08d2d 916
917 }
918
b4856b14 919 /**
920 *
921 */
cbe5423b 922 function save() {
923 $function = $this->save_function;
924 $function($this);
44ef0f47 925 }
cbe5423b 926
b4856b14 927 /**
928 *
929 */
cbe5423b 930 function changed() {
38d93650 931
932 // edit lists have a lot going on, so we'll always process them
933 //
282f8e7c 934 if ($this->type == SMOPT_TYPE_EDIT_LIST
935 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE)
936 return TRUE;
38d93650 937
6206f6c4 938 return ($this->value != $this->new_value);
cbe5423b 939 }
b4856b14 940} /* End of SquirrelOption class*/
cbe5423b 941
b4856b14 942/**
f2aba536 943 * Saves the option value (this is the default save function
944 * unless overridden by the user)
945 *
b4856b14 946 * @param object $option object that holds option name and new_value
947 */
cbe5423b 948function save_option($option) {
f2aba536 949
950 // Can't save the pref if we don't have the username
951 //
dac16606 952 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
dac16606 953 return;
0b97a708 954 }
f2aba536 955
ce102fcc 956 // if the widget is a selection list, make sure the new
957 // value is actually in the selection list and is not an
958 // injection attack
959 //
960 if ($option->type == SMOPT_TYPE_STRLIST
961 && !array_key_exists($option->new_value, $option->possible_values))
962 return;
963
964
965 // all other widgets except TEXTAREAs should never be allowed to have newlines
966 //
967 else if ($option->type != SMOPT_TYPE_TEXTAREA)
968 $option->new_value = str_replace(array("\r", "\n"), '', $option->new_value);
969
970
0b97a708 971 global $data_dir;
f2aba536 972
38d93650 973 // edit lists: first add new elements to list, then
974 // remove any selected ones (note that we must add
975 // before deleting because the javascript that populates
976 // the "add" textbox when selecting items in the list
977 // (for deletion))
978 //
979 if ($option->type == SMOPT_TYPE_EDIT_LIST) {
980
8eb63bb6 981 if (empty($option->possible_values)) $option->possible_values = array();
95dbbd91 982 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
8eb63bb6 983
38d93650 984 // add element if given
985 //
b6a08d2d 986 if ((isset($option->use_add_widget) && $option->use_add_widget)
987 && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
38d93650 988 $new_element = trim($new_element);
989 if (!empty($new_element)
990 && !in_array($new_element, $option->possible_values))
991 $option->possible_values[] = $new_element;
992 }
993
994 // delete selected elements if needed
995 //
de4c101c 996 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
997 && is_array($option->new_value)
38d93650 998 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
999 $option->possible_values = array_diff($option->possible_values, $option->new_value);
1000
1001 // save full list (stored in "possible_values")
1002 //
1003 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
1004
282f8e7c 1005 // associative edit lists are handled similar to
1006 // non-associative ones
1007 //
1008 } else if ($option->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) {
1009
1010 if (empty($option->possible_values)) $option->possible_values = array();
1011 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
1012
1013 // add element if given
1014 //
1015 $new_element_key = '';
1016 $new_element_value = '';
1017 $retrieve_key = sqGetGlobalVar('add_' . $option->name . '_key', $new_element_key, SQ_POST);
1018 $retrieve_value = sqGetGlobalVar('add_' . $option->name . '_value', $new_element_value, SQ_POST);
1019
1020 if ((isset($option->use_add_widget) && $option->use_add_widget)
1021 && ($retrieve_key || $retrieve_value)) {
1022 $new_element_key = trim($new_element_key);
1023 $new_element_value = trim($new_element_value);
1024 if ($option->poss_value_folders && empty($new_element_key))
1025 $new_element_value = '';
1026 if (!empty($new_element_key) || !empty($new_element_value)) {
1027 if (empty($new_element_key)) $new_element_key = '0';
1028 $option->possible_values[$new_element_key] = $new_element_value;
1029 }
1030 }
1031
1032 // delete selected elements if needed
1033 //
1034 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
1035 && is_array($option->new_value)
1036 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) {
1037
1038 if ($option->layout_type == SMOPT_EDIT_LIST_LAYOUT_SELECT) {
1039 foreach ($option->new_value as $key)
1040 unset($option->possible_values[urldecode($key)]);
1041 }
1042 else
1043 $option->possible_values = array_diff($option->possible_values, $option->new_value);
1044 }
1045
1046 // save full list (stored in "possible_values")
1047 //
1048 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
1049
f2aba536 1050 // Certain option types need to be serialized because
1051 // they are not scalar
1052 //
5a42c101 1053 } else if ($option->is_multiple_valued())
f2aba536 1054 setPref($data_dir, $username, $option->name, serialize($option->new_value));
38d93650 1055
74b80a51 1056 // Checkboxes, when unchecked, don't submit anything in
1057 // the POST, so set to SMPREF_OFF if not found
1058 //
1059 else if (($option->type == SMOPT_TYPE_BOOLEAN
1060 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
1061 && empty($option->new_value))
1062 setPref($data_dir, $username, $option->name, SMPREF_OFF);
1063
2f8c79ee 1064 // For integer fields, make sure we only have digits...
1065 // We'll be nice and instead of just converting to an integer,
1066 // we'll physically remove each non-digit in the string.
1067 //
1068 else if ($option->type == SMOPT_TYPE_INTEGER) {
1069 $option->new_value = preg_replace('/[^0-9]/', '', $option->new_value);
1070 setPref($data_dir, $username, $option->name, $option->new_value);
1071 }
1072
f2aba536 1073 else
1074 setPref($data_dir, $username, $option->name, $option->new_value);
1075
b4f1a9ee 1076
1077 // if a checkbox or multi select is zeroed/cleared out, it
1078 // needs to have an empty value pushed into its "new_value" slot
1079 //
1080 if (($option->type == SMOPT_TYPE_STRLIST_MULTI
1081 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
1082 && is_null($option->new_value))
1083 $option->new_value = '';
1084
cbe5423b 1085}
1086
b4856b14 1087/**
1088 * save function that does not save
1089 * @param object $option
1090 */
cbe5423b 1091function save_option_noop($option) {
1092 /* Do nothing here... */
9962527a 1093}
44ef0f47 1094
b4856b14 1095/**
1096 * Create hidden 'optpage' input field with value set by argument
1097 * @param string $optpage identification of option page
1098 * @return string html formated hidden input field
1099 */
cbe5423b 1100function create_optpage_element($optpage) {
0177059f 1101 return addHidden('optpage', $optpage);
cbe5423b 1102}
1103
b4856b14 1104/**
1105 * Create hidden 'optmode' input field with value set by argument
1106 * @param string $optmode
1107 * @return string html formated hidden input field
1108 */
cbe5423b 1109function create_optmode_element($optmode) {
0177059f 1110 return addHidden('optmode', $optmode);
cbe5423b 1111}
1112
b4856b14 1113/**
1114 * @param array $optgrps
1115 * @param array $optvals
1116 * @return array
1117 */
cbe5423b 1118function create_option_groups($optgrps, $optvals) {
a3ec3c91 1119 /* Build a simple array with which to start. */
1120 $result = array();
1121
bbcafebd 1122 /* Create option group for each option group name. */
1123 foreach ($optgrps as $grpkey => $grpname) {
1124 $result[$grpkey] = array();
1125 $result[$grpkey]['name'] = $grpname;
1126 $result[$grpkey]['options'] = array();
1127 }
1128
a3ec3c91 1129 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 1130 foreach ($optvals as $grpkey => $grpopts) {
1131 foreach ($grpopts as $optset) {
28520c87 1132 /* Create a new option with all values given. */
1133 $next_option = new SquirrelOption(
d789daf0 1134 $optset,
7390e240 1135 $optset['name'],
1136 $optset['caption'],
1137 $optset['type'],
1138 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
1139 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
1140 (isset($optset['posvals']) ? $optset['posvals'] : ''),
1141 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
1142 );
bbcafebd 1143
e40b0e8e 1144 /* If provided, set if the caption is allowed to wrap for this option. */
1145 if (isset($optset['caption_wrap'])) {
1146 $next_option->setCaptionWrap($optset['caption_wrap']);
1147 }
1148
bbcafebd 1149 /* If provided, set the size for this option. */
1150 if (isset($optset['size'])) {
1151 $next_option->setSize($optset['size']);
1152 }
1153
361d6e1b 1154 /* If provided, set the trailing_text for this option. */
1155 if (isset($optset['trailing_text'])) {
1156 $next_option->setTrailingText($optset['trailing_text']);
1157 }
1158
5b277d00 1159 /* If provided, set the yes_text for this option. */
1160 if (isset($optset['yes_text'])) {
1161 $next_option->setYesText($optset['yes_text']);
1162 }
1163
1164 /* If provided, set the no_text for this option. */
1165 if (isset($optset['no_text'])) {
1166 $next_option->setNoText($optset['no_text']);
1167 }
1168
282f8e7c 1169 /* If provided, set the poss_value_folders value for this option. */
1170 if (isset($optset['poss_value_folders'])) {
1171 $next_option->setPossValueFolders($optset['poss_value_folders']);
1172 }
1173
52639d23 1174 /* If provided, set the layout type for this option. */
1175 if (isset($optset['layout_type'])) {
1176 $next_option->setLayoutType($optset['layout_type']);
1177 }
1178
b6a08d2d 1179 /* If provided, set the use_add_widget value for this option. */
1180 if (isset($optset['use_add_widget'])) {
1181 $next_option->setUseAddWidget($optset['use_add_widget']);
1182 }
1183
de4c101c 1184 /* If provided, set the use_delete_widget value for this option. */
1185 if (isset($optset['use_delete_widget'])) {
1186 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1187 }
1188
bbcafebd 1189 /* If provided, set the comment for this option. */
1190 if (isset($optset['comment'])) {
1191 $next_option->setComment($optset['comment']);
1192 }
1193
cbe5423b 1194 /* If provided, set the save function for this option. */
1195 if (isset($optset['save'])) {
1196 $next_option->setSaveFunction($optset['save']);
1197 }
1198
0177059f 1199 /* If provided, set the extra attributes for this option. */
1200 if (isset($optset['extra_attributes'])) {
1201 $next_option->setExtraAttributes($optset['extra_attributes']);
cbe5423b 1202 }
1203
6ae9e729 1204 /* If provided, set the "post script" for this option. */
1205 if (isset($optset['post_script'])) {
1206 $next_option->setPostScript($optset['post_script']);
1207 }
1208
99ecf044 1209 /* If provided, set the folder_filter for this option. */
1210 if (isset($optset['folder_filter'])) {
1211 $next_option->setFolderFilter($optset['folder_filter']);
1212 }
1213
bbcafebd 1214 /* Add this option to the option array. */
1215 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 1216 }
1217 }
1218
1219 /* Return our resulting array. */
1220 return ($result);
1221}
1222