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