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