adding address book block
[squirrelmail.git] / functions / options.php
CommitLineData
44ef0f47 1<?php
2ba13803 2
35586184 3/**
4 * options.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions needed to display the options pages.
10 *
31841a9e 11 * @version $Id$
d6c32258 12 * @package squirrelmail
35586184 13 */
a3ec3c91 14
15/**********************************************/
16/* Define constants used in the options code. */
17/**********************************************/
18
19/* Define constants for the various option types. */
20define('SMOPT_TYPE_STRING', 0);
21define('SMOPT_TYPE_STRLIST', 1);
7e6d5ea3 22define('SMOPT_TYPE_TEXTAREA', 2);
a3ec3c91 23define('SMOPT_TYPE_INTEGER', 3);
24define('SMOPT_TYPE_FLOAT', 4);
25define('SMOPT_TYPE_BOOLEAN', 5);
2a50fbd7 26define('SMOPT_TYPE_HIDDEN', 6);
bbcafebd 27define('SMOPT_TYPE_COMMENT', 7);
be2d5495 28define('SMOPT_TYPE_FLDRLIST', 8);
a3ec3c91 29
30/* Define constants for the options refresh levels. */
31define('SMOPT_REFRESH_NONE', 0);
32define('SMOPT_REFRESH_FOLDERLIST', 1);
33define('SMOPT_REFRESH_ALL', 2);
34
bbcafebd 35/* Define constants for the options size. */
36define('SMOPT_SIZE_TINY', 0);
37define('SMOPT_SIZE_SMALL', 1);
38define('SMOPT_SIZE_MEDIUM', 2);
39define('SMOPT_SIZE_LARGE', 3);
40define('SMOPT_SIZE_HUGE', 4);
88cb1b4d 41define('SMOPT_SIZE_NORMAL', 5);
bbcafebd 42
cbe5423b 43define('SMOPT_SAVE_DEFAULT', 'save_option');
44define('SMOPT_SAVE_NOOP', 'save_option_noop');
45
9962527a 46/**
47 * SquirrelOption: An option for Squirrelmail.
48 *
49 * This class is a work in progress. When complete, it will handle
50 * presentation and saving of Squirrelmail user options in a simple,
51 * streamline manner. Stay tuned for more stuff.
52 *
53 * Also, I'd like to ask that people leave this alone (mostly :) until
54 * I get it a little further along. That should only be a day or two or
55 * three. I will remove this message when it is ready for primetime usage.
8f6f9ba5 56 * @package squirrelmail
9962527a 57 */
58class SquirrelOption {
59 /* The basic stuff. */
60 var $name;
61 var $caption;
9962527a 62 var $type;
a3ec3c91 63 var $refresh_level;
bbcafebd 64 var $size;
361d6e1b 65 var $trailing_text;
bbcafebd 66 var $comment;
cbe5423b 67 var $script;
6ae9e729 68 var $post_script;
cbe5423b 69
70 /* The name of the Save Function for this option. */
71 var $save_function;
9962527a 72
73 /* The various 'values' for this options. */
74 var $value;
75 var $new_value;
a3ec3c91 76 var $possible_values;
28520c87 77 var $htmlencoded=false;
9962527a 78
9962527a 79 function SquirrelOption
28520c87 80 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
9962527a 81 /* Set the basic stuff. */
82 $this->name = $name;
83 $this->caption = $caption;
9962527a 84 $this->type = $type;
a3ec3c91 85 $this->refresh_level = $refresh_level;
86 $this->possible_values = $possible_values;
28520c87 87 $this->htmlencoded = $htmlencoded;
bbcafebd 88 $this->size = SMOPT_SIZE_MEDIUM;
361d6e1b 89 $this->trailing_text = '';
bbcafebd 90 $this->comment = '';
cbe5423b 91 $this->script = '';
6ae9e729 92 $this->post_script = '';
a3ec3c91 93
94 /* Check for a current value. */
6ae9e729 95 if (!empty($initial_value)) {
96 $this->value = $initial_value;
97 } else if (isset($GLOBALS[$name])) {
a3ec3c91 98 $this->value = $GLOBALS[$name];
99 } else {
100 $this->value = '';
101 }
9962527a 102
a3ec3c91 103 /* Check for a new value. */
d6150d69 104 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
a3ec3c91 105 $this->new_value = '';
44ef0f47 106 }
cbe5423b 107
108 /* Set the default save function. */
2a50fbd7 109 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 110 $this->save_function = SMOPT_SAVE_DEFAULT;
111 } else {
112 $this->save_function = SMOPT_SAVE_NOOP;
113 }
114 }
115
116 /* Set the value for this option. */
117 function setValue($value) {
118 $this->value = $value;
119 }
120
121 /* Set the new value for this option. */
122 function setNewValue($new_value) {
123 $this->new_value = $new_value;
9962527a 124 }
44ef0f47 125
bbcafebd 126 /* Set the size for this option. */
127 function setSize($size) {
128 $this->size = $size;
129 }
130
361d6e1b 131 /* Set the trailing_text for this option. */
132 function setTrailingText($trailing_text) {
133 $this->trailing_text = $trailing_text;
134 }
135
bbcafebd 136 /* Set the comment for this option. */
137 function setComment($comment) {
138 $this->comment = $comment;
139 }
140
cbe5423b 141 /* Set the script for this option. */
142 function setScript($script) {
143 $this->script = $script;
144 }
145
6ae9e729 146 /* Set the "post script" for this option. */
147 function setPostScript($post_script) {
148 $this->post_script = $post_script;
149 }
150
cbe5423b 151 /* Set the save function for this option. */
152 function setSaveFunction($save_function) {
153 $this->save_function = $save_function;
154 }
155
a3ec3c91 156 function createHTMLWidget() {
ce68b76b 157 global $color;
cbe5423b 158
62f7daa5 159 // Use new value if available
74e44765 160 if (!empty($this->new_value)) {
161 $tempValue = $this->value;
162 $this->value = $this->new_value;
163 }
164
cbe5423b 165 /* Get the widget for this option type. */
a3ec3c91 166 switch ($this->type) {
167 case SMOPT_TYPE_STRING:
37a3ed17 168 $result = $this->createWidget_String();
a3ec3c91 169 break;
170 case SMOPT_TYPE_STRLIST:
37a3ed17 171 $result = $this->createWidget_StrList();
a3ec3c91 172 break;
7e6d5ea3 173 case SMOPT_TYPE_TEXTAREA:
37a3ed17 174 $result = $this->createWidget_TextArea();
a3ec3c91 175 break;
176 case SMOPT_TYPE_INTEGER:
37a3ed17 177 $result = $this->createWidget_Integer();
a3ec3c91 178 break;
179 case SMOPT_TYPE_FLOAT:
37a3ed17 180 $result = $this->createWidget_Float();
a3ec3c91 181 break;
182 case SMOPT_TYPE_BOOLEAN:
37a3ed17 183 $result = $this->createWidget_Boolean();
a3ec3c91 184 break;
2a50fbd7 185 case SMOPT_TYPE_HIDDEN:
37a3ed17 186 $result = $this->createWidget_Hidden();
a3ec3c91 187 break;
bbcafebd 188 case SMOPT_TYPE_COMMENT:
37a3ed17 189 $result = $this->createWidget_Comment();
bbcafebd 190 break;
be2d5495 191 case SMOPT_TYPE_FLDRLIST:
37a3ed17 192 $result = $this->createWidget_FolderList();
be2d5495 193 break;
a3ec3c91 194 default:
6b4bd11f 195 $result = '<font color="' . $color[2] . '">'
a3ec3c91 196 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 197 . '</font>';
a3ec3c91 198 }
199
6ae9e729 200 /* Add the "post script" for this option. */
201 $result .= $this->post_script;
62f7daa5 202
74e44765 203 // put correct value back if need be
204 if (!empty($this->new_value)) {
205 $this->value = $tempValue;
206 }
207
a3ec3c91 208 /* Now, return the created widget. */
209 return ($result);
210 }
211
37a3ed17 212 function createWidget_String() {
bbcafebd 213 switch ($this->size) {
88cb1b4d 214 case SMOPT_SIZE_TINY:
215 $width = 5;
216 break;
217 case SMOPT_SIZE_SMALL:
218 $width = 12;
219 break;
220 case SMOPT_SIZE_LARGE:
221 $width = 38;
222 break;
223 case SMOPT_SIZE_HUGE:
224 $width = 50;
225 break;
bbcafebd 226 case SMOPT_SIZE_NORMAL:
88cb1b4d 227 default:
228 $width = 25;
bbcafebd 229 }
230
88440172 231 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
62f7daa5 232 htmlspecialchars($this->value) .
361d6e1b 233 "\" size=\"$width\" $this->script />$this->trailing_text\n";
a3ec3c91 234 return ($result);
235 }
236
37a3ed17 237 function createWidget_StrList() {
a3ec3c91 238 /* Begin the select tag. */
d6150d69 239 $result = "<select name=\"new_$this->name\" $this->script>\n";
a3ec3c91 240
241 /* Add each possible value to the select list. */
242 foreach ($this->possible_values as $real_value => $disp_value) {
243 /* Start the next new option string. */
62f7daa5 244 $new_option = '<option value="' .
28520c87 245 ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"';
a3ec3c91 246
247 /* If this value is the current value, select it. */
248 if ($real_value == $this->value) {
d6150d69 249 $new_option .= ' selected="selected"';
a3ec3c91 250 }
251
252 /* Add the display value to our option string. */
28520c87 253 $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
a3ec3c91 254
255 /* And add the new option string to our select tag. */
256 $result .= $new_option;
257 }
258
259 /* Close the select tag and return our happy result. */
361d6e1b 260 $result .= "</select>$this->trailing_text\n";
a3ec3c91 261 return ($result);
262 }
263
37a3ed17 264 function createWidget_FolderList() {
be2d5495 265 $selected = array(strtolower($this->value));
266
267 /* Begin the select tag. */
d6150d69 268 $result = "<select name=\"new_$this->name\" $this->script>\n";
be2d5495 269
270 /* Add each possible value to the select list. */
271 foreach ($this->possible_values as $real_value => $disp_value) {
62f7daa5 272 if ( is_array($disp_value) ) {
be2d5495 273 /* For folder list, we passed in the array of boxes.. */
274 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
275 } else {
276 /* Start the next new option string. */
d6150d69 277 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
62f7daa5 278
be2d5495 279 /* If this value is the current value, select it. */
280 if ($real_value == $this->value) {
d6150d69 281 $new_option .= ' selected="selected"';
be2d5495 282 }
62f7daa5 283
be2d5495 284 /* Add the display value to our option string. */
d6150d69 285 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
be2d5495 286 }
287 /* And add the new option string to our select tag. */
288 $result .= $new_option;
62f7daa5 289 }
be2d5495 290 /* Close the select tag and return our happy result. */
d6150d69 291 $result .= "</select>\n";
be2d5495 292 return ($result);
293 }
294
295
37a3ed17 296 function createWidget_TextArea() {
bbcafebd 297 switch ($this->size) {
298 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
299 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
300 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
301 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
302 case SMOPT_SIZE_NORMAL:
303 default: $rows = 5; $cols = 50;
304 }
6b4bd11f 305 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
d6150d69 306 . "cols=\"$cols\" $this->script>"
307 . htmlspecialchars($this->value) . "</textarea>\n";
bbcafebd 308 return ($result);
a3ec3c91 309 }
310
37a3ed17 311 function createWidget_Integer() {
8bde22ae 312
b65d1a08 313 global $javascript_on;
0d08ea5a 314
b65d1a08 315 // add onChange javascript handler to a regular string widget
316 // which will strip out all non-numeric chars
317 if ($javascript_on)
d6150d69 318 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 319 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
320 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
d6150d69 321 . 'this.value=newVal;" />', $this->createWidget_String());
b65d1a08 322 else
37a3ed17 323 return $this->createWidget_String();
a3ec3c91 324 }
325
37a3ed17 326 function createWidget_Float() {
62f7daa5 327
37a3ed17 328 global $javascript_on;
329
b65d1a08 330 // add onChange javascript handler to a regular string widget
62f7daa5 331 // which will strip out all non-numeric (period also OK) chars
b65d1a08 332 if ($javascript_on)
d6150d69 333 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 334 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
335 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
d6150d69 336 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
37a3ed17 337 , $this->createWidget_String());
b65d1a08 338 else
37a3ed17 339 return $this->createWidget_String();
a3ec3c91 340 }
341
37a3ed17 342 function createWidget_Boolean() {
fd87494d 343 /* Do the whole current value thing. */
344 if ($this->value != SMPREF_NO) {
62f7daa5 345 $yes_chk = ' checked="checked"';
fd87494d 346 $no_chk = '';
347 } else {
348 $yes_chk = '';
62f7daa5 349 $no_chk = ' checked="checked"';
fd87494d 350 }
351
352 /* Build the yes choice. */
f4c37e3c 353 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
354 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
d6150d69 355 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 356 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
fd87494d 357
358 /* Build the no choice. */
f4c37e3c 359 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
360 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
d6150d69 361 . $no_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 362 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
fd87494d 363
364 /* Build and return the combined "boolean widget". */
365 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
366 return ($result);
a3ec3c91 367 }
368
37a3ed17 369 function createWidget_Hidden() {
6b4bd11f 370 $result = '<input type="hidden" name="new_' . $this->name
d6150d69 371 . '" value="' . htmlspecialchars($this->value)
372 . '" ' . $this->script . ' />';
a3ec3c91 373 return ($result);
374 }
375
37a3ed17 376 function createWidget_Comment() {
bbcafebd 377 $result = $this->comment;
378 return ($result);
379 }
380
cbe5423b 381 function save() {
382 $function = $this->save_function;
383 $function($this);
44ef0f47 384 }
cbe5423b 385
386 function changed() {
6206f6c4 387 return ($this->value != $this->new_value);
cbe5423b 388 }
389}
390
391function save_option($option) {
dac16606 392 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
393 /* Can't save the pref if we don't have the username */
394 return;
0b97a708 395 }
396 global $data_dir;
0b97a708 397 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 398}
399
400function save_option_noop($option) {
401 /* Do nothing here... */
9962527a 402}
44ef0f47 403
cbe5423b 404function create_optpage_element($optpage) {
405 return create_hidden_element('optpage', $optpage);
406}
407
408function create_optmode_element($optmode) {
409 return create_hidden_element('optmode', $optmode);
410}
411
412function create_hidden_element($name, $value) {
6b4bd11f 413 $result = '<input type="hidden" '
414 . 'name="' . $name . '" '
d6150d69 415 . 'value="' . htmlspecialchars($value) . '" />';
cbe5423b 416 return ($result);
417}
418
cbe5423b 419function create_option_groups($optgrps, $optvals) {
a3ec3c91 420 /* Build a simple array with which to start. */
421 $result = array();
422
bbcafebd 423 /* Create option group for each option group name. */
424 foreach ($optgrps as $grpkey => $grpname) {
425 $result[$grpkey] = array();
426 $result[$grpkey]['name'] = $grpname;
427 $result[$grpkey]['options'] = array();
428 }
429
a3ec3c91 430 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 431 foreach ($optvals as $grpkey => $grpopts) {
432 foreach ($grpopts as $optset) {
28520c87 433 /* Create a new option with all values given. */
434 $next_option = new SquirrelOption(
435 $optset['name'],
436 $optset['caption'],
437 $optset['type'],
438 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
439 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
440 (isset($optset['posvals']) ? $optset['posvals'] : ''),
441 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
442 );
bbcafebd 443
444 /* If provided, set the size for this option. */
445 if (isset($optset['size'])) {
446 $next_option->setSize($optset['size']);
447 }
448
361d6e1b 449 /* If provided, set the trailing_text for this option. */
450 if (isset($optset['trailing_text'])) {
451 $next_option->setTrailingText($optset['trailing_text']);
452 }
453
bbcafebd 454 /* If provided, set the comment for this option. */
455 if (isset($optset['comment'])) {
456 $next_option->setComment($optset['comment']);
457 }
458
cbe5423b 459 /* If provided, set the save function for this option. */
460 if (isset($optset['save'])) {
461 $next_option->setSaveFunction($optset['save']);
462 }
463
464 /* If provided, set the script for this option. */
465 if (isset($optset['script'])) {
466 $next_option->setScript($optset['script']);
467 }
468
6ae9e729 469 /* If provided, set the "post script" for this option. */
470 if (isset($optset['post_script'])) {
471 $next_option->setPostScript($optset['post_script']);
472 }
473
bbcafebd 474 /* Add this option to the option array. */
475 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 476 }
477 }
478
479 /* Return our resulting array. */
480 return ($result);
481}
482
cbe5423b 483function print_option_groups($option_groups) {
2fad95fa 484 /* Print each option group. */
bbcafebd 485 foreach ($option_groups as $next_optgrp) {
2fad95fa 486 /* If it is not blank, print the name for this option group. */
487 if ($next_optgrp['name'] != '') {
6b4bd11f 488 echo html_tag( 'tr', "\n".
489 html_tag( 'td',
490 '<b>' . $next_optgrp['name'] . '</b>' ,
491 'center' ,'', 'valign="middle" colspan="2" nowrap' )
492 ) ."\n";
7e235a1a 493 }
494
495 /* Print each option in this option group. */
bbcafebd 496 foreach ($next_optgrp['options'] as $option) {
2a50fbd7 497 if ($option->type != SMOPT_TYPE_HIDDEN) {
6b4bd11f 498 echo html_tag( 'tr', "\n".
499 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
500 html_tag( 'td', $option->createHTMLWidget(), 'left' )
501 ) ."\n";
bbcafebd 502 } else {
503 echo $option->createHTMLWidget();
504 }
505 }
7e235a1a 506
507 /* Print an empty row after this option group. */
6b4bd11f 508 echo html_tag( 'tr',
509 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
510 ) . "\n";
bbcafebd 511 }
512}
513
9962527a 514function OptionSubmit( $name ) {
6b4bd11f 515 echo html_tag( 'tr',
6fd95361 516 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
6b4bd11f 517 ) . "\n";
9962527a 518}
02ddcd00 519
d6150d69 520// vim: et ts=4
6fd95361 521?>