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