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