Code style changed, ext-user continued:
[mailer.git] / inc / ajax / ajax_installer.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/24/2012 *
4  * ===================                          Last change: 06/24/2012 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : ajax_installer.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : AJAX-related functions for installer             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : AJAX-bezogene Funktionen fuer Installer          *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 //-----------------------------------------------------------------------------
44 //             Call-back functions for processing AJAX requests
45 //-----------------------------------------------------------------------------
46
47 // Processes AJAX requests for installer
48 function doAjaxProcessInstall () {
49         // 'do' must always be set and installation phase must be true
50         if (!isInstallationPhase()) {
51                 // This shall not happen
52                 reportBug(__FUNCTION__, __LINE__, 'This AJAX request handler was called outside the installer.');
53         } elseif (!isPostRequestElementSet('do')) {
54                 // This shall not happen
55                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "do" which is fatal.');
56         } // END - if
57
58         // Notify all modules that we are installing
59         $GLOBALS['__mailer_installing'] =  true;
60
61         // Again we do a call-back, so generate a function name depending on 'do'
62         $callbackName = 'doAjaxInstaller' . capitalizeUnderscoreString(postRequestElement('do'));
63
64         // Is the call-back function there?
65         if (!function_exists($callbackName)) {
66                 // This shall not happen
67                 reportBug(__FUNCTION__, __LINE__, 'AJAX call-back ' . $callbackName . ' does not exist.');
68         } // END - if
69
70         // Call the function
71         call_user_func($callbackName);
72
73         // Is the status fine or template not found (404)?
74         sendAjaxContent();
75 }
76
77 // Processes installer request for testing
78 function doAjaxInstallerTest () {
79         // Load the "test passed" template
80         setAjaxReplyContent(loadTemplate('ajax_test_passed', TRUE));
81
82         // All okay if we reach this point
83         setHttpStatus('200 OK');
84 }
85
86 // Processes installer requests for footer navigation
87 function doAjaxInstallerFooterNavigation () {
88         // 'tab' must always be set to determine which navigation buttons shall be visible
89         if (!isPostRequestElementSet('tab')) {
90                 // This shall not happen
91                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
92         } // END - if
93
94         // Init array for footer navigation
95         $enabledNavigations = array();
96
97         // "Detect" the 'tab' value
98         switch (postRequestElement('tab')) {
99                 case 'base_data': // Also 'previous' is valid
100                 case 'database_config':
101                 case 'smtp_config':
102                 case 'other_config':
103                         array_push($enabledNavigations, 'previous');
104                 case 'welcome': // Only 'next' works for welcome page
105                         array_push($enabledNavigations, 'next');
106                         break;
107
108                 case 'overview': // Enable only 'previous'
109                         array_push($enabledNavigations, 'previous');
110                         break;
111
112                 default: // Unsupported value
113                         // This shall not happen
114                         reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
115
116                         // This will never be reached
117                         break;
118         } // END - switch
119
120         // Output the array for JSON reply
121         setAjaxReplyContent(json_encode($enabledNavigations, JSON_FORCE_OBJECT));
122
123         // All okay if we reach this point
124         setHttpStatus('200 OK');
125 }
126
127 // Processes installer AJAX calls for content-requests
128 function doAjaxInstallerRequestContent () {
129         // 'tab' must be there
130         if (!isPostRequestElementSet('tab')) {
131                 // This shall not happen
132                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
133         } // END - if
134
135         // Construct call-back name for value-preset
136         $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
137
138         // Is the function there?
139         if (function_exists($callbackName)) {
140                 // Call it for setting values in session
141                 call_user_func($callbackName);
142         } else {
143                 // Log missing functions
144                 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
145         }
146
147         // Is the HTTP status still the same? (204 No Content)
148         if (getHttpStatus() == '204 No Content') {
149                 // We use the current access level 'install' as prefix and construct a template name
150                 setAjaxReplyContent(loadTemplate('install_page_' . trim(postRequestElement('tab')), TRUE));
151
152                 // Has the template been loaded?
153                 if (isset($GLOBALS['template_content']['html']['install_page_' . trim(postRequestElement('tab'))])) {
154                         // All okay if we reach this point
155                         setHttpStatus('200 OK');
156                 } else {
157                         // Set 404 error
158                         setHttpStatus('404 NOT FOUND');
159                 }
160         } // END - if
161 }
162
163 // Process installer AJAX call for change-warning
164 function doAjaxInstallerChangeWarning () {
165         // 'elements' and 'button' must be there
166         if ((!isPostRequestElementSet('elements')) || (!isPostRequestElementSet('button'))) {
167                 // This shall not happen
168                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "elements" and/or "button" which is fatal.');
169         } // END - if
170
171         // "Walk" through all elements
172         $OUT = '<ol>';
173         foreach (explode(':', postRequestElement('elements')) as $element) {
174                 // Add row
175                 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
176         } // END - foreach
177         $OUT .= '</ol>';
178
179         // Prepare content
180         $content = array(
181                 'out'     => $OUT,
182                 'button'  => postRequestElement('button'),
183                 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
184         );
185
186         if (in_array(postRequestElement('button'), array('previous', 'next'))) {
187                 // Load 'prefixed' template
188                 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), TRUE, $content));
189         } else {
190                 // Load 'tab' template
191                 setAjaxReplyContent(loadTemplate('install_warning_tab', TRUE, $content));
192         }
193
194         // All okay if we reach this point
195         setHttpStatus('200 OK');
196 }
197
198 // Process installer AJAC call for saving changes
199 function doAjaxInstallerSaveChanges () {
200         // 'tab' must always be set to create a post-check-callback
201         if (!isPostRequestElementSet('tab')) {
202                 // This shall not happen
203                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
204         } // END - if
205
206         // Save the tab for pre-"filtering"
207         $currentTab = postRequestElement('tab');
208
209         // Remove some elements which should not be saved
210         foreach (array('do', 'level') as $removedElement) {
211                 // Remove this element from POST data
212                 unsetPostRequestElement($removedElement);
213         } // END - foreach
214
215         // Default is failed save attempt (e.g. nothing to save)
216         $saveStatus = array(
217                 'status'        => 'failed',
218                 'message'       => '{--INSTALLER_SAVE_CHANGES_FAILED--}',
219                 // Don't set this to false, or else it will be returned as 'failed' but is saved
220                 'is_saved'      => TRUE,
221                 'failed_fields' => array()
222         );
223
224         // Init overall status
225         $isAllSaved = TRUE;
226
227         // Now set all remaining data in session
228         foreach (postRequestArray() as $key => $value) {
229                 // Set it, if it is valid, else it will be added to $saveStatus (call-by-reference)
230                 $saveStatus['is_saved'] = (
231                         // Is the data valid?
232                         (isInstallerDataValid($saveStatus, $key, $value))
233                 &&
234                         // And can it be stored in session?
235                         (setSession($key, $value))
236                 );
237
238                 // Save the overall status for below final check
239                 $isAllSaved = (($isAllSaved === TRUE) && ($saveStatus['is_saved'] === TRUE));
240                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
241         } // END - foreach
242
243         // 'is_saved' is still true?
244         if ($isAllSaved === TRUE) {
245                 // Set 'done' and message
246                 $saveStatus['status']  = 'done';
247                 $saveStatus['message'] = '{--INSTALLER_SAVE_CHANGES_DONE--}';
248
249                 // Then do the post-check
250                 doInstallerPostCheck($currentTab, $saveStatus);
251         } // END - if
252
253         // Output the status array for JSON reply
254         setAjaxReplyContent(json_encode($saveStatus, JSON_FORCE_OBJECT));
255
256         // All okay if we reach this point
257         setHttpStatus('200 OK');
258 }
259
260 // Prepare AJAX request 'welcome'
261 function doAjaxPrepareInstallerWelcome () {
262         // Kept empty to prevent logfile entry
263 }
264
265 // Prepare AJAX request 'base_data'
266 function doAjaxPrepareInstallerBaseData () {
267         // Is 'base_path' not set?
268         if (!isSessionVariableSet('base_path')) {
269                 // Then set it from PATH
270                 setSession('base_path', getPath());
271         } // END - if
272
273         // Is 'base_url' not set?
274         if (!isSessionVariableSet('base_url')) {
275                 // Then set it from URL
276                 setSession('base_url', getUrl());
277         } // END - if
278
279         // Is 'main_title' not set?
280         if (!isSessionVariableSet('main_title')) {
281                 // Then set it from default main title
282                 setSession('main_title', '{--DEFAULT_MAIN_TITLE--}');
283         } // END - if
284
285         // Is 'slogan' not set?
286         if (!isSessionVariableSet('slogan')) {
287                 // Then set it from default slogan
288                 setSession('slogan', '{--DEFAULT_SLOGAN--}');
289         } // END - if
290
291         // Is 'webmaster' not set?
292         if (!isSessionVariableSet('webmaster')) {
293                 // Then set it from default webmaster email address
294                 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
295         } // END - if
296 }
297
298 // Prepare AJAX request 'database_config'
299 function doAjaxPrepareInstallerDatabaseConfig () {
300         // Is 'mysql_host' not set?
301         if (!isSessionVariableSet('mysql_host')) {
302                 // Then set it directly
303                 setSession('mysql_host', 'localhost');
304         } // END - if
305
306         // Is 'mysql_dbase' not set?
307         if (!isSessionVariableSet('mysql_dbase')) {
308                 // Then set it directly
309                 setSession('mysql_dbase', 'your_database');
310         } // END - if
311
312         // Is 'mysql_prefix' not set?
313         if (!isSessionVariableSet('mysql_prefix')) {
314                 // Then set it directly
315                 setSession('mysql_prefix', 'mailer');
316         } // END - if
317
318         // Is 'mysql_login' not set?
319         if (!isSessionVariableSet('mysql_login')) {
320                 // Then set it directly
321                 setSession('mysql_login', 'your_login');
322         } // END - if
323
324         // Is 'mysql_dbase' not set?
325         if (!isSessionVariableSet('mysql_pass1')) {
326                 // Then set it directly
327                 setSession('mysql_pass1', '');
328         } // END - if
329
330         // Is 'mysql_pass2' not set?
331         if (!isSessionVariableSet('mysql_pass2')) {
332                 // Then set it directly
333                 setSession('mysql_pass2', '');
334         } // END - if
335
336         // Is 'mysql_type' not set?
337         if (!isSessionVariableSet('mysql_type')) {
338                 // Then set it directly
339                 setSession('mysql_type', 'MyISAM');
340         } // END - if
341 }
342
343 // Prepare AJAX request 'smtp_config'
344 function doAjaxPrepareInstallerSmtpConfig () {
345         // Kept empty to prevent logfile entry because SMTP settings are optional
346 }
347
348 // Prepare AJAX request 'other_config'
349 function doAjaxPrepareInstallerOtherConfig () {
350         // Is 'output_mode' not set?
351         if (!isSessionVariableSet('output_mode')) {
352                 // Then set it directly
353                 setSession('output_mode', 'render');
354         } // END - if
355
356         // Is 'warn_no_pass' not set?
357         if (!isSessionVariableSet('warn_no_pass')) {
358                 // Then set it directly
359                 setSession('warn_no_pass', 'Y');
360         } // END - if
361
362         // Is 'write_footer' not set?
363         if (!isSessionVariableSet('write_footer')) {
364                 // Then set it directly
365                 setSession('write_footer', 'Y');
366         } // END - if
367
368         // Is 'enable_backlink' not set?
369         if (!isSessionVariableSet('enable_backlink')) {
370                 // Then set it directly
371                 setSession('enable_backlink', 'Y');
372         } // END - if
373 }
374
375 // Prepare AJAX request 'overview'
376 function doAjaxPrepareInstallerOverview () {
377         // 'tab' must always be set to create a post-check-callback
378         if (!isPostRequestElementSet('tab')) {
379                 // This shall not happen
380                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
381         } // END - if
382
383         // Save the tab for pre-"filtering"
384         $currentTab = postRequestElement('tab');
385
386         // Default is failed save attempt (e.g. nothing to save)
387         $verificationStatus = array(
388                 // Status code, can be 'failed' or 'done'
389                 'status'        => 'failed',
390                 // Status message (e.g. for output)
391                 'message'       => '{--INSTALLER_OVERVIEW_FINAL_CHECK_FAILED--}',
392                 // Don't set this to false, or else it will be returned as 'failed' but is saved
393                 'is_valid'      => TRUE,
394                 // Failed fields
395                 'failed_fields' => array()
396         );
397
398         // Init overall status and final output
399         $isAllValid = TRUE;
400         $output     = '';
401
402         // Check all data in session
403         foreach (getSessionArray() as $key => $value) {
404                 // Skip 'mailer_theme', 'tab' and 'installer'
405                 if (in_array($key, array('mailer_theme', 'tab', 'installer'))) {
406                         // Skip this
407                         continue;
408                 } // END - if
409
410                 // Is the data valid?
411                 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
412
413                 // Is this step okay?
414                 if ($verificationStatus['is_valid'] === TRUE) {
415                         // Add this key/value pair to a overview group
416                         addKeyValueToInstallerOverviewGroup($key, $value);
417                 } // END - if
418
419                 // Save the overall status for below final check
420                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_valid=' . intval($verificationStatus['is_valid']) . ',isAllValid=' . intval($isAllValid));
421                 $isAllValid = (($isAllValid === TRUE) && ($verificationStatus['is_valid'] === TRUE));
422         } // END - foreach
423
424         // Is it still true?
425         if ($isAllValid === TRUE) {
426                 // Set 'done' and message
427                 $verificationStatus['status']  = 'done';
428                 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
429
430                 // Then do the post-check
431                 doInstallerPostCheck($currentTab, $verificationStatus);
432         } // END - if
433
434         // Is it still valid?
435         if ($verificationStatus['status'] != 'done') {
436                 // Log message away
437                 logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
438
439                 // Output the array for JSON reply
440                 setAjaxReplyContent(json_encode($verificationStatus, JSON_FORCE_OBJECT));
441
442                 /*
443                  * Something went wrong, this might happen when e.g. the user has tried
444                  * to save invalid database login data but hit reload button on error
445                  * message.
446                  */
447                 setHttpStatus('500 Internal Server Error');
448
449                 // Abort here
450                 return;
451         } // END - if
452
453         // Output final rendered content
454         setAjaxReplyContent($output);
455
456         // All okay if we reach this point
457         setHttpStatus('200 OK');
458 }
459
460 // [EOF]
461 ?>