Replacing tabs with spaces, trimming white space at EOL and newline at EOF
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions needed to display the options pages.
10 *
11 * @version $Id$
12 * @package squirrelmail
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 * 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.
56 * @package squirrelmail
57 */
58 class SquirrelOption {
59 /* The basic stuff. */
60 var $name;
61 var $caption;
62 var $type;
63 var $refresh_level;
64 var $size;
65 var $trailing_text;
66 var $comment;
67 var $script;
68 var $post_script;
69
70 /* The name of the Save Function for this option. */
71 var $save_function;
72
73 /* The various 'values' for this options. */
74 var $value;
75 var $new_value;
76 var $possible_values;
77 var $htmlencoded=false;
78
79 function SquirrelOption
80 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
81 /* Set the basic stuff. */
82 $this->name = $name;
83 $this->caption = $caption;
84 $this->type = $type;
85 $this->refresh_level = $refresh_level;
86 $this->possible_values = $possible_values;
87 $this->htmlencoded = $htmlencoded;
88 $this->size = SMOPT_SIZE_MEDIUM;
89 $this->trailing_text = '';
90 $this->comment = '';
91 $this->script = '';
92 $this->post_script = '';
93
94 /* Check for a current value. */
95 if (!empty($initial_value)) {
96 $this->value = $initial_value;
97 } else if (isset($GLOBALS[$name])) {
98 $this->value = $GLOBALS[$name];
99 } else {
100 $this->value = '';
101 }
102
103 /* Check for a new value. */
104 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
105 $this->new_value = '';
106 }
107
108 /* Set the default save function. */
109 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
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;
124 }
125
126 /* Set the size for this option. */
127 function setSize($size) {
128 $this->size = $size;
129 }
130
131 /* Set the trailing_text for this option. */
132 function setTrailingText($trailing_text) {
133 $this->trailing_text = $trailing_text;
134 }
135
136 /* Set the comment for this option. */
137 function setComment($comment) {
138 $this->comment = $comment;
139 }
140
141 /* Set the script for this option. */
142 function setScript($script) {
143 $this->script = $script;
144 }
145
146 /* Set the "post script" for this option. */
147 function setPostScript($post_script) {
148 $this->post_script = $post_script;
149 }
150
151 /* Set the save function for this option. */
152 function setSaveFunction($save_function) {
153 $this->save_function = $save_function;
154 }
155
156 function createHTMLWidget() {
157 global $color;
158
159 // Use new value if available
160 if (!empty($this->new_value)) {
161 $tempValue = $this->value;
162 $this->value = $this->new_value;
163 }
164
165 /* Get the widget for this option type. */
166 switch ($this->type) {
167 case SMOPT_TYPE_STRING:
168 $result = $this->createWidget_String();
169 break;
170 case SMOPT_TYPE_STRLIST:
171 $result = $this->createWidget_StrList();
172 break;
173 case SMOPT_TYPE_TEXTAREA:
174 $result = $this->createWidget_TextArea();
175 break;
176 case SMOPT_TYPE_INTEGER:
177 $result = $this->createWidget_Integer();
178 break;
179 case SMOPT_TYPE_FLOAT:
180 $result = $this->createWidget_Float();
181 break;
182 case SMOPT_TYPE_BOOLEAN:
183 $result = $this->createWidget_Boolean();
184 break;
185 case SMOPT_TYPE_HIDDEN:
186 $result = $this->createWidget_Hidden();
187 break;
188 case SMOPT_TYPE_COMMENT:
189 $result = $this->createWidget_Comment();
190 break;
191 case SMOPT_TYPE_FLDRLIST:
192 $result = $this->createWidget_FolderList();
193 break;
194 default:
195 $result = '<font color="' . $color[2] . '">'
196 . sprintf(_("Option Type '%s' Not Found"), $this->type)
197 . '</font>';
198 }
199
200 /* Add the "post script" for this option. */
201 $result .= $this->post_script;
202
203 // put correct value back if need be
204 if (!empty($this->new_value)) {
205 $this->value = $tempValue;
206 }
207
208 /* Now, return the created widget. */
209 return ($result);
210 }
211
212 function createWidget_String() {
213 switch ($this->size) {
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;
226 case SMOPT_SIZE_NORMAL:
227 default:
228 $width = 25;
229 }
230
231 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
232 htmlspecialchars($this->value) .
233 "\" size=\"$width\" $this->script />$this->trailing_text\n";
234 return ($result);
235 }
236
237 function createWidget_StrList() {
238 /* Begin the select tag. */
239 $result = "<select name=\"new_$this->name\" $this->script>\n";
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. */
244 $new_option = '<option value="' .
245 ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"';
246
247 /* If this value is the current value, select it. */
248 if ($real_value == $this->value) {
249 $new_option .= ' selected="selected"';
250 }
251
252 /* Add the display value to our option string. */
253 $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
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. */
260 $result .= "</select>$this->trailing_text\n";
261 return ($result);
262 }
263
264 function createWidget_FolderList() {
265 $selected = array(strtolower($this->value));
266
267 /* Begin the select tag. */
268 $result = "<select name=\"new_$this->name\" $this->script>\n";
269
270 /* Add each possible value to the select list. */
271 foreach ($this->possible_values as $real_value => $disp_value) {
272 if ( is_array($disp_value) ) {
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. */
277 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
278
279 /* If this value is the current value, select it. */
280 if ($real_value == $this->value) {
281 $new_option .= ' selected="selected"';
282 }
283
284 /* Add the display value to our option string. */
285 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
286 }
287 /* And add the new option string to our select tag. */
288 $result .= $new_option;
289 }
290 /* Close the select tag and return our happy result. */
291 $result .= "</select>\n";
292 return ($result);
293 }
294
295
296 function createWidget_TextArea() {
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 }
305 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
306 . "cols=\"$cols\" $this->script>"
307 . htmlspecialchars($this->value) . "</textarea>\n";
308 return ($result);
309 }
310
311 function createWidget_Integer() {
312
313 global $javascript_on;
314
315 // add onChange javascript handler to a regular string widget
316 // which will strip out all non-numeric chars
317 if ($javascript_on)
318 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
319 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
320 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
321 . 'this.value=newVal;" />', $this->createWidget_String());
322 else
323 return $this->createWidget_String();
324 }
325
326 function createWidget_Float() {
327
328 global $javascript_on;
329
330 // add onChange javascript handler to a regular string widget
331 // which will strip out all non-numeric (period also OK) chars
332 if ($javascript_on)
333 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
334 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
335 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
336 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
337 , $this->createWidget_String());
338 else
339 return $this->createWidget_String();
340 }
341
342 function createWidget_Boolean() {
343 /* Do the whole current value thing. */
344 if ($this->value != SMPREF_NO) {
345 $yes_chk = ' checked="checked"';
346 $no_chk = '';
347 } else {
348 $yes_chk = '';
349 $no_chk = ' checked="checked"';
350 }
351
352 /* Build the yes choice. */
353 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
354 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
355 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
356 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
357
358 /* Build the no choice. */
359 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
360 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
361 . $no_chk . ' ' . $this->script . ' />&nbsp;'
362 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
363
364 /* Build and return the combined "boolean widget". */
365 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
366 return ($result);
367 }
368
369 function createWidget_Hidden() {
370 $result = '<input type="hidden" name="new_' . $this->name
371 . '" value="' . htmlspecialchars($this->value)
372 . '" ' . $this->script . ' />';
373 return ($result);
374 }
375
376 function createWidget_Comment() {
377 $result = $this->comment;
378 return ($result);
379 }
380
381 function save() {
382 $function = $this->save_function;
383 $function($this);
384 }
385
386 function changed() {
387 return ($this->value != $this->new_value);
388 }
389 }
390
391 function save_option($option) {
392 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
393 /* Can't save the pref if we don't have the username */
394 return;
395 }
396 global $data_dir;
397 setPref($data_dir, $username, $option->name, $option->new_value);
398 }
399
400 function save_option_noop($option) {
401 /* Do nothing here... */
402 }
403
404 function create_optpage_element($optpage) {
405 return create_hidden_element('optpage', $optpage);
406 }
407
408 function create_optmode_element($optmode) {
409 return create_hidden_element('optmode', $optmode);
410 }
411
412 function create_hidden_element($name, $value) {
413 $result = '<input type="hidden" '
414 . 'name="' . $name . '" '
415 . 'value="' . htmlspecialchars($value) . '" />';
416 return ($result);
417 }
418
419 function create_option_groups($optgrps, $optvals) {
420 /* Build a simple array with which to start. */
421 $result = array();
422
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
430 /* Create a new SquirrelOption for each set of option values. */
431 foreach ($optvals as $grpkey => $grpopts) {
432 foreach ($grpopts as $optset) {
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 );
443
444 /* If provided, set the size for this option. */
445 if (isset($optset['size'])) {
446 $next_option->setSize($optset['size']);
447 }
448
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
454 /* If provided, set the comment for this option. */
455 if (isset($optset['comment'])) {
456 $next_option->setComment($optset['comment']);
457 }
458
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
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
474 /* Add this option to the option array. */
475 $result[$grpkey]['options'][] = $next_option;
476 }
477 }
478
479 /* Return our resulting array. */
480 return ($result);
481 }
482
483 function print_option_groups($option_groups) {
484 /* Print each option group. */
485 foreach ($option_groups as $next_optgrp) {
486 /* If it is not blank, print the name for this option group. */
487 if ($next_optgrp['name'] != '') {
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";
493 }
494
495 /* Print each option in this option group. */
496 foreach ($next_optgrp['options'] as $option) {
497 if ($option->type != SMOPT_TYPE_HIDDEN) {
498 echo html_tag( 'tr', "\n".
499 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
500 html_tag( 'td', $option->createHTMLWidget(), 'left' )
501 ) ."\n";
502 } else {
503 echo $option->createHTMLWidget();
504 }
505 }
506
507 /* Print an empty row after this option group. */
508 echo html_tag( 'tr',
509 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
510 ) . "\n";
511 }
512 }
513
514 function OptionSubmit( $name ) {
515 echo html_tag( 'tr',
516 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
517 ) . "\n";
518 }
519
520 // vim: et ts=4
521 ?>