Happy 2017
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Functions needed to display the options pages.
7 *
8 * @copyright 1999-2017 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage prefs
13 */
14
15 /**
16 * SquirrelOption: An option for SquirrelMail.
17 *
18 * @package squirrelmail
19 * @subpackage prefs
20 */
21 class SquirrelOption {
22 /**
23 * The original option configuration array
24 * @var array
25 */
26 var $raw_option_array;
27 /**
28 * The name of this setting
29 * @var string
30 */
31 var $name;
32 /**
33 * The text that prefaces setting on the preferences page
34 * @var string
35 */
36 var $caption;
37 /**
38 * Whether or not the caption text is allowed to wrap
39 * @var boolean
40 */
41 var $caption_wrap;
42 /**
43 * The type of INPUT element
44 *
45 * See SMOPT_TYPE_* defines
46 * @var integer
47 */
48 var $type;
49 /**
50 * Indicates if a link should be shown to refresh part
51 * or all of the window
52 *
53 * See SMOPT_REFRESH_* defines
54 * @var integer
55 */
56 var $refresh_level;
57 /**
58 * Specifies the size of certain input items
59 *
60 * See SMOPT_SIZE_* defines
61 * @var integer
62 */
63 var $size;
64 /**
65 * Text that follows a text input or
66 * select list input on the preferences page
67 *
68 * useful for indicating units, meanings of special values, etc.
69 * @var string
70 */
71 var $trailing_text;
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;
86 /**
87 * Some widgets support more than one layout type
88 *
89 * @var int
90 */
91 var $layout_type;
92 /**
93 * Indicates if the Add widget should be included
94 * with edit lists.
95 *
96 * @var boolean
97 */
98 var $use_add_widget;
99 /**
100 * Indicates if the Delete widget should be included
101 * with edit lists.
102 *
103 * @var boolean
104 */
105 var $use_delete_widget;
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;
119 /**
120 * text displayed to the user
121 *
122 * Used with SMOPT_TYPE_COMMENT options
123 * @var string
124 */
125 var $comment;
126 /**
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
131 */
132 var $aExtraAttribs;
133 /**
134 * script (usually Javascript) that will be placed after (outside of)
135 * the INPUT tag
136 * @var string
137 */
138 var $post_script;
139
140 /**
141 * The name of the Save Function for this option.
142 * @var string
143 */
144 var $save_function;
145
146 /* The various 'values' for this options. */
147 /**
148 * default/preselected value for this option
149 * @var mixed
150 */
151 var $value;
152 /**
153 * new option value
154 * @var mixed
155 */
156 var $new_value;
157 /**
158 * associative array, where each key is an actual input value
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 */
163 var $possible_values;
164 /**
165 * disables html sanitizing.
166 *
167 * WARNING - don't use it, if user input is possible in option
168 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
169 * @var bool
170 */
171 var $htmlencoded=false;
172 /**
173 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
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.
178 * See $flag argument in sqimap_mailbox_option_list() function.
179 * @var string
180 * @since 1.5.1
181 */
182 var $folder_filter='noselect';
183
184 /**
185 * Constructor (PHP5 style, required in some future version of PHP)
186 * @param array $raw_option_array
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 */
195 function __construct
196 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
197 /* Set the basic stuff. */
198 $this->raw_option_array = $raw_option_array;
199 $this->name = $name;
200 $this->caption = $caption;
201 $this->caption_wrap = TRUE;
202 $this->type = $type;
203 $this->refresh_level = $refresh_level;
204 $this->possible_values = $possible_values;
205 $this->htmlencoded = $htmlencoded;
206 $this->size = SMOPT_SIZE_NORMAL;
207 $this->trailing_text = '';
208 $this->yes_text = '';
209 $this->no_text = '';
210 $this->comment = '';
211 $this->layout_type = 0;
212 $this->use_add_widget = TRUE;
213 $this->use_delete_widget = TRUE;
214 $this->poss_value_folders = '';
215 $this->aExtraAttribs = array();
216 $this->post_script = '';
217
218 //Check for a current value.
219 if (isset($GLOBALS[$name])) {
220 $this->value = $GLOBALS[$name];
221 } else if (!empty($initial_value)) {
222 $this->value = $initial_value;
223 } else {
224 $this->value = '';
225 }
226
227 /* Check for a new value. */
228 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
229 $this->new_value = NULL;
230 }
231
232 /* Set the default save function. */
233 if ($type != SMOPT_TYPE_HIDDEN
234 && $type != SMOPT_TYPE_INFO
235 && $type != SMOPT_TYPE_COMMENT) {
236 $this->save_function = SMOPT_SAVE_DEFAULT;
237 } else {
238 $this->save_function = SMOPT_SAVE_NOOP;
239 }
240 }
241
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
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
263 || $this->type == SMOPT_TYPE_EDIT_LIST
264 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE);
265 }
266
267 /**
268 * Set the value for this option.
269 * @param mixed $value
270 */
271 function setValue($value) {
272 $this->value = $value;
273 }
274
275 /**
276 * Set the new value for this option.
277 * @param mixed $new_value
278 */
279 function setNewValue($new_value) {
280 $this->new_value = $new_value;
281 }
282
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
291 /**
292 * Set the size for this option.
293 * @param integer $size
294 */
295 function setSize($size) {
296 $this->size = $size;
297 }
298
299 /**
300 * Set the trailing_text for this option.
301 * @param string $trailing_text
302 */
303 function setTrailingText($trailing_text) {
304 $this->trailing_text = $trailing_text;
305 }
306
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
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
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
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
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
348 /**
349 * Set the comment for this option.
350 * @param string $comment
351 */
352 function setComment($comment) {
353 $this->comment = $comment;
354 }
355
356 /**
357 * Set the extra attributes for this option.
358 * @param array $aExtraAttribs
359 */
360 function setExtraAttributes($aExtraAttribs) {
361 $this->aExtraAttribs = $aExtraAttribs;
362 }
363
364 /**
365 * Set the "post script" for this option.
366 * @param string $post_script
367 */
368 function setPostScript($post_script) {
369 $this->post_script = $post_script;
370 }
371
372 /**
373 * Set the save function for this option.
374 * @param string $save_function
375 */
376 function setSaveFunction($save_function) {
377 $this->save_function = $save_function;
378 }
379
380 /**
381 * Set the folder_filter for this option.
382 * @param string $folder_filter
383 * @since 1.5.1
384 */
385 function setFolderFilter($folder_filter) {
386 $this->folder_filter = $folder_filter;
387 }
388
389 /**
390 * Creates fields on option pages according to option type
391 *
392 * This is the function that calls all other createWidget* functions.
393 *
394 * @return string The formated option field
395 *
396 */
397 function createWidget() {
398 global $color;
399
400 // Use new value if available
401 if (!is_null($this->new_value)) {
402 $tempValue = $this->value;
403 $this->value = $this->new_value;
404 }
405
406 /* Get the widget for this option type. */
407 switch ($this->type) {
408 case SMOPT_TYPE_PASSWORD:
409 $result = $this->createWidget_String(TRUE);
410 break;
411 case SMOPT_TYPE_STRING:
412 $result = $this->createWidget_String();
413 break;
414 case SMOPT_TYPE_STRLIST:
415 $result = $this->createWidget_StrList();
416 break;
417 case SMOPT_TYPE_TEXTAREA:
418 $result = $this->createWidget_TextArea();
419 break;
420 case SMOPT_TYPE_INTEGER:
421 $result = $this->createWidget_Integer();
422 break;
423 case SMOPT_TYPE_FLOAT:
424 $result = $this->createWidget_Float();
425 break;
426 case SMOPT_TYPE_BOOLEAN:
427 $result = $this->createWidget_Boolean();
428 break;
429 case SMOPT_TYPE_BOOLEAN_CHECKBOX:
430 $result = $this->createWidget_Boolean(TRUE);
431 break;
432 case SMOPT_TYPE_BOOLEAN_RADIO:
433 $result = $this->createWidget_Boolean(FALSE);
434 break;
435 case SMOPT_TYPE_HIDDEN:
436 $result = $this->createWidget_Hidden();
437 break;
438 case SMOPT_TYPE_COMMENT:
439 $result = $this->createWidget_Comment();
440 break;
441 case SMOPT_TYPE_FLDRLIST:
442 $result = $this->createWidget_FolderList();
443 break;
444 case SMOPT_TYPE_FLDRLIST_MULTI:
445 $result = $this->createWidget_FolderList(TRUE);
446 break;
447 case SMOPT_TYPE_EDIT_LIST:
448 $result = $this->createWidget_EditList();
449 break;
450 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE:
451 $result = $this->createWidget_EditListAssociative();
452 break;
453 case SMOPT_TYPE_STRLIST_MULTI:
454 $result = $this->createWidget_StrList(TRUE);
455 break;
456 case SMOPT_TYPE_STRLIST_RADIO:
457 $result = $this->createWidget_StrList(FALSE, TRUE);
458 break;
459 case SMOPT_TYPE_SUBMIT:
460 $result = $this->createWidget_Submit();
461 break;
462 case SMOPT_TYPE_INFO:
463 $result = $this->createWidget_Info();
464 break;
465 default:
466 error_box (
467 sprintf(_("Option Type '%s' Not Found"), $this->type)
468 );
469 }
470
471 /* Add the "post script" for this option. */
472 $result .= $this->post_script;
473
474 // put correct value back if need be
475 if (!is_null($this->new_value)) {
476 $this->value = $tempValue;
477 }
478
479 /* Now, return the created widget. */
480 return $result;
481 }
482
483 /**
484 * Creates info block
485 * @return string html formated output
486 */
487 function createWidget_Info() {
488 return sq_htmlspecialchars($this->value);
489 }
490
491 /**
492 * Create string field
493 *
494 * @param boolean $password When TRUE, the text in the input
495 * widget will be obscured (OPTIONAL;
496 * default = FALSE).
497 *
498 * @return string html formated option field
499 *
500 */
501 function createWidget_String($password=FALSE) {
502 switch ($this->size) {
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;
515 case SMOPT_SIZE_NORMAL:
516 default:
517 $width = 25;
518 }
519
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
521 if ($password)
522 return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . sm_encode_html_special_chars($this->trailing_text);
523 else
524 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . sm_encode_html_special_chars($this->trailing_text);
525 }
526
527 /**
528 * Create selection box or radio button group
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 *
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
541 * will allow multiple selections
542 * (OPTIONAL; default is FALSE
543 * (single select list))
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))
550 *
551 * @return string html formated selection box or radio buttons
552 *
553 */
554 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
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
556
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 //
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
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);
595
596 }
597
598 /**
599 * Create folder selection box
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 *
606 * @return string html formated selection box
607 *
608 */
609 function createWidget_FolderList($multiple_select=FALSE) {
610
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
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) {
635
636 // list of folders (boxes array)
637 //
638 if (is_array($text)) {
639 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
640
641 // just one option here
642 //
643 } else {
644 $option_list = array_merge($option_list, array($value => $text));
645 }
646
647 }
648 if (empty($option_list))
649 $option_list = array('ignore' => _("unavailable"));
650
651
652 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . sm_encode_html_special_chars($this->trailing_text);
653
654 }
655
656 /**
657 * Creates textarea
658 * @return string html formated textarea field
659 */
660 function createWidget_TextArea() {
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 }
669 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
670 }
671
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 */
678 function createWidget_Integer() {
679
680 // add onChange javascript handler to a regular string widget
681 // which will strip out all non-numeric chars
682 if (checkForJavascript())
683 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
684 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
685 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
686 . 'this.value=newVal;';
687
688 return $this->createWidget_String();
689 }
690
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 */
696 function createWidget_Float() {
697
698 // add onChange javascript handler to a regular string widget
699 // which will strip out all non-numeric (period also OK) chars
700 if (checkForJavascript())
701 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
702 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
703 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
704 . 'newVal += origVal.charAt(i); } this.value=newVal;';
705
706 return $this->createWidget_String();
707 }
708
709 /**
710 * Create boolean widget
711 *
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 *
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;
720 * default is TRUE (checkbox)).
721 *
722 * @return string html formated boolean widget
723 *
724 */
725 function createWidget_Boolean($checkbox=TRUE) {
726
727 global $oTemplate, $nbsp;
728
729
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. */
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');
742
743 /* Build the no choice. */
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');
745
746 /* Build the combined "boolean widget". */
747 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
748
749 }
750
751 return ($result);
752 }
753
754 /**
755 * Creates hidden field
756 * @return string html formated hidden input field
757 */
758 function createWidget_Hidden() {
759 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
760 }
761
762 /**
763 * Creates comment
764 * @return string comment
765 */
766 function createWidget_Comment() {
767 $result = $this->comment;
768 return ($result);
769 }
770
771 /**
772 * Creates a (non-associative) edit list
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 *
778 * @return string html formated list of edit fields and
779 * their associated controls
780 */
781 function createWidget_EditList() {
782
783 global $oTemplate;
784
785 switch ($this->size) {
786 case SMOPT_SIZE_TINY:
787 $height = 3;
788 break;
789 case SMOPT_SIZE_SMALL:
790 $height = 8;
791 break;
792 case SMOPT_SIZE_MEDIUM:
793 $height = 15;
794 break;
795 case SMOPT_SIZE_LARGE:
796 $height = 25;
797 break;
798 case SMOPT_SIZE_HUGE:
799 $height = 40;
800 break;
801 case SMOPT_SIZE_NORMAL:
802 default:
803 $height = 5;
804 }
805
806 if (empty($this->possible_values)) $this->possible_values = array();
807 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
808
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));
811 $oTemplate->assign('use_input_widget', $this->use_add_widget);
812 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
813
814 $oTemplate->assign('trailing_text', $this->trailing_text);
815 $oTemplate->assign('possible_values', $this->possible_values);
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
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);
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:
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));
903 }
904
905 }
906
907 /**
908 * Creates a submit button
909 *
910 * @return string html formated submit button widget
911 *
912 */
913 function createWidget_Submit() {
914
915 return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . sm_encode_html_special_chars($this->trailing_text);
916
917 }
918
919 /**
920 *
921 */
922 function save() {
923 $function = $this->save_function;
924 $function($this);
925 }
926
927 /**
928 *
929 */
930 function changed() {
931
932 // edit lists have a lot going on, so we'll always process them
933 //
934 if ($this->type == SMOPT_TYPE_EDIT_LIST
935 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE)
936 return TRUE;
937
938 return ($this->value != $this->new_value);
939 }
940 } /* End of SquirrelOption class*/
941
942 /**
943 * Saves the option value (this is the default save function
944 * unless overridden by the user)
945 *
946 * @param object $option object that holds option name and new_value
947 */
948 function save_option($option) {
949
950 // Can't save the pref if we don't have the username
951 //
952 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
953 return;
954 }
955
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
971 global $data_dir;
972
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
981 if (empty($option->possible_values)) $option->possible_values = array();
982 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
983
984 // add element if given
985 //
986 if ((isset($option->use_add_widget) && $option->use_add_widget)
987 && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
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 //
996 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
997 && is_array($option->new_value)
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
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
1050 // Certain option types need to be serialized because
1051 // they are not scalar
1052 //
1053 } else if ($option->is_multiple_valued())
1054 setPref($data_dir, $username, $option->name, serialize($option->new_value));
1055
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
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
1073 else
1074 setPref($data_dir, $username, $option->name, $option->new_value);
1075
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
1085 }
1086
1087 /**
1088 * save function that does not save
1089 * @param object $option
1090 */
1091 function save_option_noop($option) {
1092 /* Do nothing here... */
1093 }
1094
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 */
1100 function create_optpage_element($optpage) {
1101 return addHidden('optpage', $optpage);
1102 }
1103
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 */
1109 function create_optmode_element($optmode) {
1110 return addHidden('optmode', $optmode);
1111 }
1112
1113 /**
1114 * @param array $optgrps
1115 * @param array $optvals
1116 * @return array
1117 */
1118 function create_option_groups($optgrps, $optvals) {
1119 /* Build a simple array with which to start. */
1120 $result = array();
1121
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
1129 /* Create a new SquirrelOption for each set of option values. */
1130 foreach ($optvals as $grpkey => $grpopts) {
1131 foreach ($grpopts as $optset) {
1132 /* Create a new option with all values given. */
1133 $next_option = new SquirrelOption(
1134 $optset,
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 );
1143
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
1149 /* If provided, set the size for this option. */
1150 if (isset($optset['size'])) {
1151 $next_option->setSize($optset['size']);
1152 }
1153
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
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
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
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
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
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
1189 /* If provided, set the comment for this option. */
1190 if (isset($optset['comment'])) {
1191 $next_option->setComment($optset['comment']);
1192 }
1193
1194 /* If provided, set the save function for this option. */
1195 if (isset($optset['save'])) {
1196 $next_option->setSaveFunction($optset['save']);
1197 }
1198
1199 /* If provided, set the extra attributes for this option. */
1200 if (isset($optset['extra_attributes'])) {
1201 $next_option->setExtraAttributes($optset['extra_attributes']);
1202 }
1203
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
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
1214 /* Add this option to the option array. */
1215 $result[$grpkey]['options'][] = $next_option;
1216 }
1217 }
1218
1219 /* Return our resulting array. */
1220 return ($result);
1221 }
1222