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