ed0fb673f83770e19aabc4a03494bd6938465bd4
[squirrelmail.git] / src / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Displays the options page. Pulls from proper user preference files
7 * and config.php. Displays preferences as selected and other options.
8 *
9 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage prefs
14 */
15
16 /** This is the options page */
17 define('PAGE_NAME', 'options');
18
19 /**
20 * Include the SquirrelMail initialization file.
21 */
22 require('../include/init.php');
23
24 /* SquirrelMail required files. */
25
26 //include(SM_PATH . 'functions/imap_general.php');
27 require_once(SM_PATH . 'functions/options.php');
28 require_once(SM_PATH . 'functions/forms.php');
29
30 /*********************************/
31 /*** Build the resultant page. ***/
32 /*********************************/
33
34 define('SMOPT_MODE_DISPLAY', 'display');
35 define('SMOPT_MODE_SUBMIT', 'submit');
36 define('SMOPT_MODE_LINK', 'link');
37
38 define('SMOPT_PAGE_MAIN', 'main');
39 define('SMOPT_PAGE_PERSONAL', 'personal');
40 define('SMOPT_PAGE_DISPLAY', 'display');
41 define('SMOPT_PAGE_COMPOSE', 'compose');
42 define('SMOPT_PAGE_HIGHLIGHT', 'highlight');
43 define('SMOPT_PAGE_FOLDER', 'folder');
44 define('SMOPT_PAGE_ORDER', 'order');
45
46 /**
47 * Save submitted options and calculate the most
48 * we need to refresh the page
49 *
50 * @param string $optpage The name of the page being submitted
51 * @param array $optpage_data An array of all the submitted options
52 *
53 * @return int The highest level of screen refresh needed per
54 * the options that were changed. This value will
55 * correspond to the SMOPT_REFRESH_* constants found
56 * in functions/options.php.
57 *
58 */
59 function process_optionmode_submit($optpage, $optpage_data) {
60 /* Initialize the maximum option refresh level. */
61 $max_refresh = SMOPT_REFRESH_NONE;
62
63
64
65 /* Save each option in each option group. */
66 foreach ($optpage_data['options'] as $option_grp) {
67 foreach ($option_grp['options'] as $option) {
68
69 /* Special case: need to make sure emailaddress
70 * is saved if we use it as a test for ask_user_info */
71 global $ask_user_info;
72 if ( $optpage = SMOPT_PAGE_PERSONAL && $ask_user_info &&
73 $option->name == 'email_address' ) {
74 $option->setValue('');
75 }
76
77 /* Remove Debug Mode Until Needed
78 echo "name = '$option->name', "
79 . "value = '$option->value', "
80 . "new_value = '$option->new_value'\n";
81 //FIXME: NO HTML IN THE CORE!
82 echo "<br />";
83 */
84 if ($option->changed()) {
85 $option->save();
86 $max_refresh = max($max_refresh, $option->refresh_level);
87 }
88 }
89 }
90
91 /* Return the max refresh level. */
92 return ($max_refresh);
93 }
94
95 function process_optionmode_link($optpage) {
96 /* There will be something here, later. */
97 }
98
99
100
101 /* ---------------------------- main ---------------------------- */
102
103 /* get the globals that we may need */
104 sqgetGlobalVar('optpage', $optpage);
105 sqgetGlobalVar('optmode', $optmode, SQ_FORM);
106 sqgetGlobalVar('optpage_data',$optpage_data, SQ_POST);
107 /* end of getting globals */
108
109 /* Make sure we have an Option Page set. Default to main. */
110 if ( !isset($optpage) || $optpage == '' ) {
111 $optpage = SMOPT_PAGE_MAIN;
112 } else {
113 $optpage = strip_tags( $optpage );
114 }
115
116 /* Make sure we have an Option Mode set. Default to display. */
117 if (!isset($optmode)) {
118 $optmode = SMOPT_MODE_DISPLAY;
119 }
120
121 /*
122 * First, set the load information for each option page.
123 */
124
125 /* Initialize load information variables. */
126 $optpage_name = '';
127 $optpage_file = '';
128 $optpage_loader = '';
129
130 /* Set the load information for each page. */
131 switch ($optpage) {
132 case SMOPT_PAGE_MAIN:
133 break;
134 case SMOPT_PAGE_PERSONAL:
135 $optpage_name = _("Personal Information");
136 $optpage_file = SM_PATH . 'include/options/personal.php';
137 $optpage_loader = 'load_optpage_data_personal';
138 $optpage_loadhook = 'optpage_loadhook_personal';
139 break;
140 case SMOPT_PAGE_DISPLAY:
141 $optpage_name = _("Display Preferences");
142 $optpage_file = SM_PATH . 'include/options/display.php';
143 $optpage_loader = 'load_optpage_data_display';
144 $optpage_loadhook = 'optpage_loadhook_display';
145 break;
146 case SMOPT_PAGE_COMPOSE:
147 $optpage_name = _("Compose Preferences");
148 $optpage_file = SM_PATH . 'include/options/compose.php';
149 $optpage_loader = 'load_optpage_data_compose';
150 $optpage_loadhook = 'optpage_loadhook_compose';
151 break;
152 case SMOPT_PAGE_HIGHLIGHT:
153 $optpage_name = _("Message Highlighting");
154 $optpage_file = SM_PATH . 'include/options/highlight.php';
155 $optpage_loader = 'load_optpage_data_highlight';
156 $optpage_loadhook = 'optpage_loadhook_highlight';
157 break;
158 case SMOPT_PAGE_FOLDER:
159 $optpage_name = _("Folder Preferences");
160 $optpage_file = SM_PATH . 'include/options/folder.php';
161 $optpage_loader = 'load_optpage_data_folder';
162 $optpage_loadhook = 'optpage_loadhook_folder';
163 break;
164 case SMOPT_PAGE_ORDER:
165 $optpage_name = _("Index Order");
166 $optpage_file = SM_PATH . 'include/options/order.php';
167 $optpage_loader = 'load_optpage_data_order';
168 $optpage_loadhook = 'optpage_loadhook_order';
169 break;
170 default: do_hook('optpage_set_loadinfo', $null);
171 }
172
173 /**********************************************************/
174 /*** Second, load the option information for this page. ***/
175 /**********************************************************/
176
177 if ( !@is_file( $optpage_file ) ) {
178 $optpage = SMOPT_PAGE_MAIN;
179 } elseif ($optpage != SMOPT_PAGE_MAIN ) {
180 /* Include the file for this optionpage. */
181
182 require_once($optpage_file);
183
184 /* Assemble the data for this option page. */
185 $optpage_data = array();
186 $optpage_data = $optpage_loader();
187 do_hook($optpage_loadhook, $null);
188 $optpage_data['options'] = create_option_groups($optpage_data['grps'], $optpage_data['vals']);
189 }
190
191 /***********************************************************/
192 /*** Next, process anything that needs to be processed. ***/
193 /***********************************************************/
194
195 $optpage_save_error=array();
196
197 if ( isset( $optpage_data ) ) {
198 switch ($optmode) {
199 case SMOPT_MODE_SUBMIT:
200 $max_refresh = process_optionmode_submit($optpage, $optpage_data);
201 break;
202 case SMOPT_MODE_LINK:
203 $max_refresh = process_optionmode_link($optpage, $optpage_data);
204 break;
205 }
206 }
207
208 $optpage_title = _("Options");
209 if (isset($optpage_name) && ($optpage_name != '')) {
210 $optpage_title .= " - $optpage_name";
211 }
212
213 /*******************************************************************/
214 /* DO OLD SAVING OF SUBMITTED OPTIONS. THIS WILL BE REMOVED LATER. */
215 /*******************************************************************/
216
217 //FIXME: let's remove these finally in 1.5.2..... but first, are there any plugins using them?
218 /* If in submit mode, select a save hook name and run it. */
219 if ($optmode == SMOPT_MODE_SUBMIT) {
220 /* Select a save hook name. */
221 switch ($optpage) {
222 case SMOPT_PAGE_PERSONAL:
223 $save_hook_name = 'options_personal_save';
224 break;
225 case SMOPT_PAGE_DISPLAY:
226 $save_hook_name = 'options_display_save';
227 break;
228 case SMOPT_PAGE_COMPOSE:
229 $save_hook_name = 'options_compose_save';
230 break;
231 case SMOPT_PAGE_FOLDER:
232 $save_hook_name = 'options_folder_save';
233 break;
234 default:
235 $save_hook_name = 'options_save';
236 break;
237 }
238
239 /* Run the options save hook. */
240 do_hook($save_hook_name, $null);
241 }
242
243 /***************************************************************/
244 /* Apply logic to decide what optpage we want to display next. */
245 /***************************************************************/
246
247 /* If this is the result of an option page being submitted, then */
248 /* show the main page. Otherwise, show whatever page was called. */
249
250 if ($optmode == SMOPT_MODE_SUBMIT) {
251 $optpage = SMOPT_PAGE_MAIN;
252 $optpage_title = _("Options");
253 }
254
255 /***************************************************************/
256 /* Finally, display whatever page we are supposed to show now. */
257 /***************************************************************/
258
259 displayPageHeader($color, null, (isset($optpage_data['xtra']) ? $optpage_data['xtra'] : ''));
260
261 /*
262 * The main option page has a different layout then the rest of the option
263 * pages. Therefore, we create it here first, then the others below.
264 */
265 if ($optpage == SMOPT_PAGE_MAIN) {
266 /**********************************************************/
267 /* First, display the results of a submission, if needed. */
268 /**********************************************************/
269 $notice = '';
270 if ($optmode == SMOPT_MODE_SUBMIT) {
271 if (!isset($frame_top)) {
272 $frame_top = '_top';
273 }
274
275 if (isset($optpage_save_error) && $optpage_save_error!=array()) {
276 //FIXME: REMOVE HTML FROM CORE
277 $notice = _("Error(s) occurred while saving your options") . "<br />\n<ul>\n";
278 foreach ($optpage_save_error as $error_message) {
279 $notice.= '<li><small>' . $error_message . "</small></li>\n";
280 }
281 $notice.= "</ul>\n" . _("Some of your preference changes were not applied.") . "\n";
282 } else {
283 /* Display a message indicating a successful save. */
284 $notice = _("Successfully Saved Options") . ": $optpage_name</b><br />\n";
285 }
286
287 /* If $max_refresh != SMOPT_REFRESH_NONE, provide a refresh link. */
288 if ( !isset( $max_refresh ) ) {
289 } else if ($max_refresh == SMOPT_REFRESH_FOLDERLIST) {
290 //FIXME: REMOVE HTML FROM CORE - when migrating, keep in mind that the javascript below assumes the folder list is in a separate sibling frame under the same parent, and it is called "left"
291 if (checkForJavascript()) {
292 $notice .= sprintf(_("Folder list should automatically %srefresh%s."), '<a href="../src/left_main.php" target="left">', '</a>') . '<br /><script type="text/javascript">' . "\n<!--\nparent.left.location = '../src/left_main.php';\n// -->\n</script>\n";
293 } else {
294 $notice .= '<a href="../src/left_main.php" target="left">' . _("Refresh Folder List") . '</a><br />';
295 }
296 } else if ($max_refresh) {
297 if (checkForJavascript()) {
298 //FIXME: REMOVE HTML FROM CORE - when migrating, keep in mind that the javascript below assumes the parent is the top-most SM frame and is what should be refreshed with webmail.php
299 $notice .= sprintf(_("This page should automatically %srefresh%s."), '<a href="../src/webmail.php?right_frame=options.php" target="' . $frame_top . '">', '</a>') . '<br /><script type="text/javascript">' . "\n<!--\nparent.location = '../src/webmail.php?right_frame=options.php';\n// -->\n</script>\n";
300 } else {
301 $notice .= '<a href="../src/webmail.php?right_frame=options.php" target="' . $frame_top . '">' . _("Refresh Page") . '</a><br />';
302 }
303 }
304 }
305
306 if (!empty($notice)) {
307 $oTemplate->assign('note', $notice);
308 $oTemplate->display('note.tpl');
309 }
310
311 /******************************************/
312 /* Build our array of Option Page Blocks. */
313 /******************************************/
314 $optpage_blocks = array();
315
316 /* Build a section for Personal Options. */
317 $optpage_blocks[] = array(
318 'name' => _("Personal Information"),
319 'url' => 'options.php?optpage=' . SMOPT_PAGE_PERSONAL,
320 'desc' => _("This contains personal information about yourself such as your name, your email address, etc."),
321 'js' => false
322 );
323
324 /* Build a section for Display Options. */
325 $optpage_blocks[] = array(
326 'name' => _("Display Preferences"),
327 'url' => 'options.php?optpage=' . SMOPT_PAGE_DISPLAY,
328 'desc' => _("You can change the way that SquirrelMail looks and displays information to you, such as the colors, the language, and other settings."),
329 'js' => false
330 );
331
332 /* Build a section for Message Highlighting Options. */
333 $optpage_blocks[] = array(
334 'name' =>_("Message Highlighting"),
335 'url' => 'options_highlight.php',
336 'desc' =>_("Based upon given criteria, incoming messages can have different background colors in the message list. This helps to easily distinguish who the messages are from, especially for mailing lists."),
337 'js' => false
338 );
339
340 /* Build a section for Folder Options. */
341 $optpage_blocks[] = array(
342 'name' => _("Folder Preferences"),
343 'url' => 'options.php?optpage=' . SMOPT_PAGE_FOLDER,
344 'desc' => _("These settings change the way your folders are displayed and manipulated."),
345 'js' => false
346 );
347
348 /* Build a section for Index Order Options. */
349 $optpage_blocks[] = array(
350 'name' => _("Index Order"),
351 'url' => 'options_order.php',
352 'desc' => _("The order of the message index can be rearranged and changed to contain the headers in any order you want."),
353 'js' => false
354 );
355
356 /* Build a section for Compose Options. */
357 $optpage_blocks[] = array(
358 'name' => _("Compose Preferences"),
359 'url' => 'options.php?optpage=' . SMOPT_PAGE_COMPOSE,
360 'desc' => _("Control the behaviour and layout of writing new mail messages, replying to and forwarding messages."),
361 'js' => false
362 );
363
364 /* Build a section for plugins wanting to register an optionpage. */
365 do_hook('optpage_register_block', $null);
366
367 /*****************************************************/
368 /* Let's sort Javascript Option Pages to the bottom. */
369 /*****************************************************/
370 $js_optpage_blocks = array();
371 $reg_optpage_blocks = array();
372 foreach ($optpage_blocks as $cur_optpage) {
373 if (!isset($cur_optpage['js']) || !$cur_optpage['js']) {
374 $reg_optpage_blocks[] = $cur_optpage;
375 } else if (checkForJavascript()) {
376 $js_optpage_blocks[] = $cur_optpage;
377 }
378 }
379 $optpage_blocks = array_merge($reg_optpage_blocks, $js_optpage_blocks);
380
381 /********************************************/
382 /* Now, print out each option page section. */
383 /********************************************/
384 $oTemplate->assign('page_title', $optpage_title);
385 $oTemplate->assign('options', $optpage_blocks);
386
387 $oTemplate->display('option_groups.tpl');
388
389 do_hook('options_link_and_description', $null);
390
391
392 /*************************************************************************/
393 /* If we are not looking at the main option page, display the page here. */
394 /*************************************************************************/
395 } else {
396 /* Set the bottom_hook_name and submit_name. */
397 switch ($optpage) {
398 case SMOPT_PAGE_PERSONAL:
399 $bottom_hook_name = 'options_personal_bottom';
400 $submit_name = 'submit_personal';
401 break;
402 case SMOPT_PAGE_DISPLAY:
403 $bottom_hook_name = 'options_display_bottom';
404 $submit_name = 'submit_display';
405 break;
406 case SMOPT_PAGE_COMPOSE:
407 $bottom_hook_name = 'options_compose_bottom';
408 $submit_name = 'submit_compose';
409 break;
410 case SMOPT_PAGE_HIGHLIGHT:
411 $bottom_hook_name = 'options_highlight_bottom';
412 $submit_name = 'submit_highlight';
413 break;
414 case SMOPT_PAGE_FOLDER:
415 $bottom_hook_name = 'options_folder_bottom';
416 $submit_name = 'submit_folder';
417 break;
418 case SMOPT_PAGE_ORDER:
419 $bottom_hook_name = 'options_order_bottom';
420 $submit_name = 'submit_order';
421 break;
422 default:
423 $bottom_hook_name = '';
424 $submit_name = 'submit';
425 }
426
427 // Begin output form
428 echo addForm('options.php', 'post', 'option_form')
429 . create_optpage_element($optpage)
430 . create_optmode_element(SMOPT_MODE_SUBMIT);
431
432 // This is the only variable that is needed by *just* the template.
433 $oTemplate->assign('options', $optpage_data['options']);
434
435 global $ask_user_info, $org_name;
436 if ( $optpage == SMOPT_PAGE_PERSONAL && $ask_user_info
437 && getPref($data_dir, $username,'email_address') == "" ) {
438 $oTemplate->assign('topmessage',
439 sprintf(_("Welcome to %s. Please supply your full name and email address."), $org_name) );
440 }
441
442 // These variables are not specifically needed by the template,
443 // but they are relevant to the page being built, so we'll add
444 // them in case some plugin is modifying the page, etc....
445 //
446 $oTemplate->assign('max_refresh', isset($max_refresh) ? $max_refresh : NULL);
447 $oTemplate->assign('page_title', $optpage_title);
448 $oTemplate->assign('optpage', $optpage);
449 $oTemplate->assign('optpage_name', $optpage_name);
450 $oTemplate->assign('optmode', $optmode);
451 $oTemplate->assign('optpage_data', $optpage_data);
452
453 $oTemplate->assign('submit_name', $submit_name);
454 $oTemplate->display('options.tpl');
455
456 $oTemplate->display('form_close.tpl');
457
458 /* If it is not empty, trigger the bottom hook. */
459 if ($bottom_hook_name != '') {
460 do_hook($bottom_hook_name, $null);
461 }
462
463 }
464
465 $oTemplate->display('footer.tpl');