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