Shifting the location of bind_textdomain_codeset() in the SM wrapper functions
[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
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 error_box (
320 sprintf(_("Option Type '%s' Not Found"), $this->type)
321 );
322 }
323
324 /* Add the "post script" for this option. */
325 $result .= $this->post_script;
326
327 // put correct value back if need be
328 if (!empty($this->new_value)) {
329 $this->value = $tempValue;
330 }
331
332 /* Now, return the created widget. */
333 return ($result);
334 }
335
336 /**
337 * Create string field
338 * @return string html formated option field
339 */
340 function createWidget_String() {
341 switch ($this->size) {
342 case SMOPT_SIZE_TINY:
343 $width = 5;
344 break;
345 case SMOPT_SIZE_SMALL:
346 $width = 12;
347 break;
348 case SMOPT_SIZE_LARGE:
349 $width = 38;
350 break;
351 case SMOPT_SIZE_HUGE:
352 $width = 50;
353 break;
354 case SMOPT_SIZE_NORMAL:
355 default:
356 $width = 25;
357 }
358
359 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
360 }
361
362 /**
363 * Create selection box
364 *
365 * When $this->htmlencoded is TRUE, the keys and values in
366 * $this->possible_values are assumed to be display-safe.
367 * Use with care!
368 *
369 * @return string html formated selection box
370 */
371 function createWidget_StrList() {
372 //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
373 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
374
375 }
376
377 /**
378 * Create folder selection box
379 * @return string html formated selection box
380 */
381 function createWidget_FolderList() {
382
383 // possible values might include a nested array of
384 // possible values (list of folders)
385 //
386 $option_list = array();
387 foreach ($this->possible_values as $value => $text) {
388
389 // list of folders (boxes array)
390 //
391 if (is_array($text)) {
392 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value)), 0, $text, $this->folder_filter));
393
394 // just one option here
395 //
396 } else {
397 $option_list = array_merge($option_list, array($value => $text));
398 }
399
400 }
401 if (empty($option_list))
402 $option_list = array('ignore' => _("unavailable"));
403
404
405 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
406
407 }
408
409 /**
410 * Creates textarea
411 * @return string html formated textarea field
412 */
413 function createWidget_TextArea() {
414 switch ($this->size) {
415 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
416 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
417 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
418 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
419 case SMOPT_SIZE_NORMAL:
420 default: $rows = 5; $cols = 50;
421 }
422 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
423 }
424
425 /**
426 * Creates field for integer
427 *
428 * Difference from createWidget_String is visible only when javascript is enabled
429 * @return string html formated option field
430 */
431 function createWidget_Integer() {
432
433 // add onChange javascript handler to a regular string widget
434 // which will strip out all non-numeric chars
435 if (checkForJavascript())
436 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
437 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
438 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
439 . 'this.value=newVal;';
440
441 return $this->createWidget_String();
442 }
443
444 /**
445 * Creates field for floating number
446 * Difference from createWidget_String is visible only when javascript is enabled
447 * @return string html formated option field
448 */
449 function createWidget_Float() {
450
451 // add onChange javascript handler to a regular string widget
452 // which will strip out all non-numeric (period also OK) chars
453 if (checkForJavascript())
454 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
455 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
456 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
457 . 'newVal += origVal.charAt(i); } this.value=newVal;';
458
459 return $this->createWidget_String();
460 }
461
462 /**
463 * Creates radio field (yes/no)
464 * @return string html formated radio field
465 */
466 function createWidget_Boolean() {
467
468 global $oTemplate;
469 $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
470
471 /* Build the yes choice. */
472 $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');
473
474 /* Build the no choice. */
475 $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');
476
477 /* Build and return the combined "boolean widget". */
478 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
479 return ($result);
480 }
481
482 /**
483 * Creates hidden field
484 * @return string html formated hidden input field
485 */
486 function createWidget_Hidden() {
487 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
488 }
489
490 /**
491 * Creates comment
492 * @return string comment
493 */
494 function createWidget_Comment() {
495 $result = $this->comment;
496 return ($result);
497 }
498
499 /**
500 *
501 */
502 function save() {
503 $function = $this->save_function;
504 $function($this);
505 }
506
507 /**
508 *
509 */
510 function changed() {
511 return ($this->value != $this->new_value);
512 }
513 } /* End of SquirrelOption class*/
514
515 /**
516 * Saves option
517 * @param object $option object that holds option name and new_value
518 */
519 function save_option($option) {
520 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
521 /* Can't save the pref if we don't have the username */
522 return;
523 }
524 global $data_dir;
525 setPref($data_dir, $username, $option->name, $option->new_value);
526 }
527
528 /**
529 * save function that does not save
530 * @param object $option
531 */
532 function save_option_noop($option) {
533 /* Do nothing here... */
534 }
535
536 /**
537 * Create hidden 'optpage' input field with value set by argument
538 * @param string $optpage identification of option page
539 * @return string html formated hidden input field
540 */
541 function create_optpage_element($optpage) {
542 return addHidden('optpage', $optpage);
543 }
544
545 /**
546 * Create hidden 'optmode' input field with value set by argument
547 * @param string $optmode
548 * @return string html formated hidden input field
549 */
550 function create_optmode_element($optmode) {
551 return addHidden('optmode', $optmode);
552 }
553
554 /**
555 * @param array $optgrps
556 * @param array $optvals
557 * @return array
558 */
559 function create_option_groups($optgrps, $optvals) {
560 /* Build a simple array with which to start. */
561 $result = array();
562
563 /* Create option group for each option group name. */
564 foreach ($optgrps as $grpkey => $grpname) {
565 $result[$grpkey] = array();
566 $result[$grpkey]['name'] = $grpname;
567 $result[$grpkey]['options'] = array();
568 }
569
570 /* Create a new SquirrelOption for each set of option values. */
571 foreach ($optvals as $grpkey => $grpopts) {
572 foreach ($grpopts as $optset) {
573 /* Create a new option with all values given. */
574 $next_option = new SquirrelOption(
575 $optset['name'],
576 $optset['caption'],
577 $optset['type'],
578 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
579 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
580 (isset($optset['posvals']) ? $optset['posvals'] : ''),
581 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
582 );
583
584 /* If provided, set the size for this option. */
585 if (isset($optset['size'])) {
586 $next_option->setSize($optset['size']);
587 }
588
589 /* If provided, set the trailing_text for this option. */
590 if (isset($optset['trailing_text'])) {
591 $next_option->setTrailingText($optset['trailing_text']);
592 }
593
594 /* If provided, set the comment for this option. */
595 if (isset($optset['comment'])) {
596 $next_option->setComment($optset['comment']);
597 }
598
599 /* If provided, set the save function for this option. */
600 if (isset($optset['save'])) {
601 $next_option->setSaveFunction($optset['save']);
602 }
603
604 /* If provided, set the extra attributes for this option. */
605 if (isset($optset['extra_attributes'])) {
606 $next_option->setExtraAttributes($optset['extra_attributes']);
607 }
608
609 /* If provided, set the "post script" for this option. */
610 if (isset($optset['post_script'])) {
611 $next_option->setPostScript($optset['post_script']);
612 }
613
614 /* If provided, set the folder_filter for this option. */
615 if (isset($optset['folder_filter'])) {
616 $next_option->setFolderFilter($optset['folder_filter']);
617 }
618
619 /* Add this option to the option array. */
620 $result[$grpkey]['options'][] = $next_option;
621 }
622 }
623
624 /* Return our resulting array. */
625 return ($result);
626 }
627