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