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