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. |
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) { |
6b4bd11f |
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) { |
6b4bd11f |
321 | $yes_chk = ' checked'; |
fd87494d |
322 | $no_chk = ''; |
323 | } else { |
324 | $yes_chk = ''; |
6b4bd11f |
325 | $no_chk = ' checked'; |
fd87494d |
326 | } |
327 | |
328 | /* Build the yes choice. */ |
6b4bd11f |
329 | $yes_option = '<input type="radio" name="new_' . $this->name |
37a3ed17 |
330 | . '" value="' . SMPREF_YES . "\"$yes_chk $this->script> " |
fd87494d |
331 | . _("Yes"); |
332 | |
333 | /* Build the no choice. */ |
6b4bd11f |
334 | $no_option = '<input type="radio" name="new_' . $this->name |
37a3ed17 |
335 | . '" value="' . SMPREF_NO . "\"$no_chk $this->script> " |
fd87494d |
336 | . _("No"); |
337 | |
338 | /* Build and return the combined "boolean widget". */ |
339 | $result = "$yes_option $no_option"; |
340 | return ($result); |
a3ec3c91 |
341 | } |
342 | |
37a3ed17 |
343 | function createWidget_Hidden() { |
6b4bd11f |
344 | $result = '<input type="hidden" name="new_' . $this->name |
6ae9e729 |
345 | . '" value="' . $this->value . '" ' . $this->script . '>'; |
a3ec3c91 |
346 | return ($result); |
347 | } |
348 | |
37a3ed17 |
349 | function createWidget_Comment() { |
bbcafebd |
350 | $result = $this->comment; |
351 | return ($result); |
352 | } |
353 | |
cbe5423b |
354 | function save() { |
355 | $function = $this->save_function; |
356 | $function($this); |
44ef0f47 |
357 | } |
cbe5423b |
358 | |
359 | function changed() { |
6206f6c4 |
360 | return ($this->value != $this->new_value); |
cbe5423b |
361 | } |
362 | } |
363 | |
364 | function save_option($option) { |
dac16606 |
365 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
366 | /* Can't save the pref if we don't have the username */ |
367 | return; |
0b97a708 |
368 | } |
369 | global $data_dir; |
0b97a708 |
370 | setPref($data_dir, $username, $option->name, $option->new_value); |
cbe5423b |
371 | } |
372 | |
373 | function save_option_noop($option) { |
374 | /* Do nothing here... */ |
9962527a |
375 | } |
44ef0f47 |
376 | |
cbe5423b |
377 | function create_optpage_element($optpage) { |
378 | return create_hidden_element('optpage', $optpage); |
379 | } |
380 | |
381 | function create_optmode_element($optmode) { |
382 | return create_hidden_element('optmode', $optmode); |
383 | } |
384 | |
385 | function create_hidden_element($name, $value) { |
6b4bd11f |
386 | $result = '<input type="hidden" ' |
387 | . 'name="' . $name . '" ' |
388 | . 'value="' . $value . '">'; |
cbe5423b |
389 | return ($result); |
390 | } |
391 | |
cbe5423b |
392 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
393 | /* Build a simple array with which to start. */ |
394 | $result = array(); |
395 | |
bbcafebd |
396 | /* Create option group for each option group name. */ |
397 | foreach ($optgrps as $grpkey => $grpname) { |
398 | $result[$grpkey] = array(); |
399 | $result[$grpkey]['name'] = $grpname; |
400 | $result[$grpkey]['options'] = array(); |
401 | } |
402 | |
a3ec3c91 |
403 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
404 | foreach ($optvals as $grpkey => $grpopts) { |
405 | foreach ($grpopts as $optset) { |
406 | if (isset($optset['posvals'])) { |
407 | /* Create a new option with all values given. */ |
408 | $next_option = new SquirrelOption( |
409 | $optset['name'], |
410 | $optset['caption'], |
411 | $optset['type'], |
412 | $optset['refresh'], |
6ae9e729 |
413 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
bbcafebd |
414 | $optset['posvals'] |
415 | ); |
416 | } else { |
417 | /* Create a new option with all but possible values given. */ |
418 | $next_option = new SquirrelOption( |
419 | $optset['name'], |
420 | $optset['caption'], |
421 | $optset['type'], |
6ae9e729 |
422 | $optset['refresh'], |
423 | (isset($optset['initial_value']) ? $optset['initial_value'] : '') |
bbcafebd |
424 | ); |
425 | } |
426 | |
427 | /* If provided, set the size for this option. */ |
428 | if (isset($optset['size'])) { |
429 | $next_option->setSize($optset['size']); |
430 | } |
431 | |
432 | /* If provided, set the comment for this option. */ |
433 | if (isset($optset['comment'])) { |
434 | $next_option->setComment($optset['comment']); |
435 | } |
436 | |
cbe5423b |
437 | /* If provided, set the save function for this option. */ |
438 | if (isset($optset['save'])) { |
439 | $next_option->setSaveFunction($optset['save']); |
440 | } |
441 | |
442 | /* If provided, set the script for this option. */ |
443 | if (isset($optset['script'])) { |
444 | $next_option->setScript($optset['script']); |
445 | } |
446 | |
6ae9e729 |
447 | /* If provided, set the "post script" for this option. */ |
448 | if (isset($optset['post_script'])) { |
449 | $next_option->setPostScript($optset['post_script']); |
450 | } |
451 | |
bbcafebd |
452 | /* Add this option to the option array. */ |
453 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
454 | } |
455 | } |
456 | |
457 | /* Return our resulting array. */ |
458 | return ($result); |
459 | } |
460 | |
cbe5423b |
461 | function print_option_groups($option_groups) { |
2fad95fa |
462 | /* Print each option group. */ |
bbcafebd |
463 | foreach ($option_groups as $next_optgrp) { |
2fad95fa |
464 | /* If it is not blank, print the name for this option group. */ |
465 | if ($next_optgrp['name'] != '') { |
6b4bd11f |
466 | echo html_tag( 'tr', "\n". |
467 | html_tag( 'td', |
468 | '<b>' . $next_optgrp['name'] . '</b>' , |
469 | 'center' ,'', 'valign="middle" colspan="2" nowrap' ) |
470 | ) ."\n"; |
7e235a1a |
471 | } |
472 | |
473 | /* Print each option in this option group. */ |
bbcafebd |
474 | foreach ($next_optgrp['options'] as $option) { |
2a50fbd7 |
475 | if ($option->type != SMOPT_TYPE_HIDDEN) { |
6b4bd11f |
476 | echo html_tag( 'tr', "\n". |
477 | html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) . |
478 | html_tag( 'td', $option->createHTMLWidget(), 'left' ) |
479 | ) ."\n"; |
bbcafebd |
480 | } else { |
481 | echo $option->createHTMLWidget(); |
482 | } |
483 | } |
7e235a1a |
484 | |
485 | /* Print an empty row after this option group. */ |
6b4bd11f |
486 | echo html_tag( 'tr', |
487 | html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) |
488 | ) . "\n"; |
bbcafebd |
489 | } |
490 | } |
491 | |
9962527a |
492 | function OptionSubmit( $name ) { |
6b4bd11f |
493 | echo html_tag( 'tr', |
1aefbee0 |
494 | html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '"> ', 'right', '', 'colspan="2"' ) |
6b4bd11f |
495 | ) . "\n"; |
9962527a |
496 | } |
02ddcd00 |
497 | |
23d6bd09 |
498 | ?> |