]> git.mxchange.org Git - mailer.git/blob - inc/ajax/ajax_installer.php
Installation NG continued (still not fully working)
[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                         if (isInstallationDataCompleted()) {
111                                 // Add 'finish'
112                                 array_push($enabledNavigations, 'finish');
113                         } // END - if
114                         break;
115
116                 default: // Unsupported value
117                         // This shall not happen
118                         reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
119
120                         // This will never be reached
121                         break;
122         } // END - switch
123
124         // Output the array for JSON reply
125         setAjaxReplyContent(json_encode($enabledNavigations, JSON_FORCE_OBJECT));
126
127         // All okay if we reach this point
128         setHttpStatus('200 OK');
129 }
130
131 // Processes installer AJAX calls for content-requests
132 function doAjaxInstallerRequestContent () {
133         // 'tab' must be there
134         if (!isPostRequestElementSet('tab')) {
135                 // This shall not happen
136                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
137         } // END - if
138
139         // Construct call-back name for value-preset
140         $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
141
142         // Is the function there?
143         if (function_exists($callbackName)) {
144                 // Call it for setting values in session
145                 call_user_func($callbackName);
146         } else {
147                 // Log missing functions
148                 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
149         }
150
151         // Is the HTTP status still the same? (204 No Content)
152         if (getHttpStatus() == '204 No Content') {
153                 // We use the current access level 'install' as prefix and construct a template name
154                 setAjaxReplyContent(loadTemplate('install_page_' . trim(postRequestElement('tab')), TRUE));
155
156                 // Has the template been loaded?
157                 if (isset($GLOBALS['template_content']['html']['install_page_' . trim(postRequestElement('tab'))])) {
158                         // All okay if we reach this point
159                         setHttpStatus('200 OK');
160                 } else {
161                         // Set 404 error
162                         setHttpStatus('404 NOT FOUND');
163                 }
164         } // END - if
165 }
166
167 // Process installer AJAX call for change-warning
168 function doAjaxInstallerChangeWarning () {
169         // 'elements' and 'button' must be there
170         if ((!isPostRequestElementSet('elements')) || (!isPostRequestElementSet('button'))) {
171                 // This shall not happen
172                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "elements" and/or "button" which is fatal.');
173         } // END - if
174
175         // "Walk" through all elements
176         $OUT = '<ol>';
177         foreach (explode(':', postRequestElement('elements')) as $element) {
178                 // Add row
179                 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
180         } // END - foreach
181         $OUT .= '</ol>';
182
183         // Prepare content
184         $content = array(
185                 'out'     => $OUT,
186                 'button'  => postRequestElement('button'),
187                 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
188         );
189
190         if (in_array(postRequestElement('button'), array('previous', 'next'))) {
191                 // Load 'prefixed' template
192                 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), TRUE, $content));
193         } else {
194                 // Load 'tab' template
195                 setAjaxReplyContent(loadTemplate('install_warning_tab', TRUE, $content));
196         }
197
198         // All okay if we reach this point
199         setHttpStatus('200 OK');
200 }
201
202 // Process installer AJAC call for saving changes
203 function doAjaxInstallerSaveChanges () {
204         // 'tab' must always be set to create a post-check-callback
205         if (!isPostRequestElementSet('tab')) {
206                 // This shall not happen
207                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
208         } // END - if
209
210         // Save the tab for pre-"filtering"
211         $currentTab = postRequestElement('tab');
212
213         // Remove some elements which should not be saved
214         foreach (array('do', 'level') as $removedElement) {
215                 // Remove this element from POST data
216                 unsetPostRequestElement($removedElement);
217         } // END - foreach
218
219         // Default is failed save attempt (e.g. nothing to save)
220         $saveStatus = array(
221                 'status'        => 'failed',
222                 'message'       => '{--INSTALLER_SAVE_CHANGES_FAILED--}',
223                 // Don't set this to false, or else it will be returned as 'failed' but is saved
224                 'is_saved'      => TRUE,
225                 'failed_fields' => array()
226         );
227
228         // Init overall status
229         $isAllSaved = TRUE;
230
231         // Now set all remaining data in session
232         foreach (postRequestArray() as $key => $value) {
233                 // Set it, if it is valid, else it will be added to $saveStatus (call-by-reference)
234                 $saveStatus['is_saved'] = (
235                         // Is the data valid?
236                         (isInstallerDataValid($saveStatus, $key, $value))
237                 &&
238                         // And can it be stored in session?
239                         (setSession($key, $value))
240                 );
241
242                 // Save the overall status for below final check
243                 $isAllSaved = (($isAllSaved === TRUE) && ($saveStatus['is_saved'] === TRUE));
244                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
245         } // END - foreach
246
247         // 'is_saved' is still true?
248         if ($isAllSaved === TRUE) {
249                 // Set 'done' and message
250                 $saveStatus['status']  = 'done';
251                 $saveStatus['message'] = '{--INSTALLER_SAVE_CHANGES_DONE--}';
252
253                 // Then do the post-check
254                 doInstallerPostCheck($currentTab, $saveStatus);
255         } // END - if
256
257         // Output the status array for JSON reply
258         setAjaxReplyContent(json_encode($saveStatus, JSON_FORCE_OBJECT));
259
260         // All okay if we reach this point
261         setHttpStatus('200 OK');
262 }
263
264 // Prepare AJAX request 'welcome'
265 function doAjaxPrepareInstallerWelcome () {
266         // Kept empty to prevent logfile entry
267 }
268
269 // Prepare AJAX request 'base_data'
270 function doAjaxPrepareInstallerBaseData () {
271         // Is 'base_path' not set?
272         if (!isSessionVariableSet('base_path')) {
273                 // Then set it from PATH
274                 setSession('base_path', getPath());
275         } // END - if
276
277         // Is 'base_url' not set?
278         if (!isSessionVariableSet('base_url')) {
279                 // Then set it from URL
280                 setSession('base_url', getUrl());
281         } // END - if
282
283         // Is 'main_title' not set?
284         if (!isSessionVariableSet('main_title')) {
285                 // Then set it from default main title
286                 setSession('main_title', '{--DEFAULT_MAIN_TITLE--}');
287         } // END - if
288
289         // Is 'slogan' not set?
290         if (!isSessionVariableSet('slogan')) {
291                 // Then set it from default slogan
292                 setSession('slogan', '{--DEFAULT_SLOGAN--}');
293         } // END - if
294
295         // Is 'webmaster' not set?
296         if (!isSessionVariableSet('webmaster')) {
297                 // Then set it from default webmaster email address
298                 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
299         } // END - if
300 }
301
302 // Prepare AJAX request 'database_config'
303 function doAjaxPrepareInstallerDatabaseConfig () {
304         // Is 'mysql_host' not set?
305         if (!isSessionVariableSet('mysql_host')) {
306                 // Then set it directly
307                 setSession('mysql_host', 'localhost');
308         } // END - if
309
310         // Is 'mysql_dbase' not set?
311         if (!isSessionVariableSet('mysql_dbase')) {
312                 // Then set it directly
313                 setSession('mysql_dbase', 'your_database');
314         } // END - if
315
316         // Is 'mysql_prefix' not set?
317         if (!isSessionVariableSet('mysql_prefix')) {
318                 // Then set it directly
319                 setSession('mysql_prefix', 'mailer');
320         } // END - if
321
322         // Is 'mysql_login' not set?
323         if (!isSessionVariableSet('mysql_login')) {
324                 // Then set it directly
325                 setSession('mysql_login', 'your_login');
326         } // END - if
327
328         // Is 'mysql_dbase' not set?
329         if (!isSessionVariableSet('mysql_pass1')) {
330                 // Then set it directly
331                 setSession('mysql_pass1', '');
332         } // END - if
333
334         // Is 'mysql_pass2' not set?
335         if (!isSessionVariableSet('mysql_pass2')) {
336                 // Then set it directly
337                 setSession('mysql_pass2', '');
338         } // END - if
339
340         // Is 'mysql_engine' not set?
341         if (!isSessionVariableSet('mysql_engine')) {
342                 // Then set it directly
343                 setSession('mysql_engine', 'MyISAM');
344         } // END - if
345 }
346
347 // Prepare AJAX request 'smtp_config'
348 function doAjaxPrepareInstallerSmtpConfig () {
349         // Kept empty to prevent logfile entry because SMTP settings are optional
350 }
351
352 // Prepare AJAX request 'other_config'
353 function doAjaxPrepareInstallerOtherConfig () {
354         // Is 'output_mode' not set?
355         if (!isSessionVariableSet('output_mode')) {
356                 // Then set it directly
357                 setSession('output_mode', 'render');
358         } // END - if
359
360         // Is 'warn_no_pass' not set?
361         if (!isSessionVariableSet('warn_no_pass')) {
362                 // Then set it directly
363                 setSession('warn_no_pass', 'Y');
364         } // END - if
365
366         // Is 'write_footer' not set?
367         if (!isSessionVariableSet('write_footer')) {
368                 // Then set it directly
369                 setSession('write_footer', 'Y');
370         } // END - if
371
372         // Is 'enable_backlink' not set?
373         if (!isSessionVariableSet('enable_backlink')) {
374                 // Then set it directly
375                 setSession('enable_backlink', 'Y');
376         } // END - if
377 }
378
379 // Prepare AJAX request 'overview'
380 function doAjaxPrepareInstallerOverview () {
381         // 'tab' must always be set to create a post-check-callback
382         if (!isPostRequestElementSet('tab')) {
383                 // This shall not happen
384                 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
385         } // END - if
386
387         // Save the tab for pre-"filtering"
388         $currentTab = postRequestElement('tab');
389
390         // Default is failed save attempt (e.g. nothing to save)
391         $verificationStatus = array(
392                 // Status code, can be 'failed' or 'done'
393                 'status'        => 'failed',
394                 // Status message (e.g. for output)
395                 'message'       => '{--INSTALLER_OVERVIEW_FINAL_CHECK_FAILED--}',
396                 // Don't set this to false, or else it will be returned as 'failed' but is saved
397                 'is_valid'      => TRUE,
398                 // Failed fields
399                 'failed_fields' => array()
400         );
401
402         // Init overall status and final output
403         $isAllValid = TRUE;
404         $output     = '';
405
406         // Check all data in session
407         foreach (array_keys($GLOBALS['installer_groups']) as $key) {
408                 // Get values from session
409                 $value = getSession($key);
410
411                 // Is the data valid?
412                 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
413
414                 // Is this step okay?
415                 if ($verificationStatus['is_valid'] === TRUE) {
416                         // Add this key/value pair to a overview group
417                         addKeyValueToInstallerOverviewGroup($key, $value);
418                 } // END - if
419
420                 // Save the overall status for below final check
421                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_valid=' . intval($verificationStatus['is_valid']) . ',isAllValid=' . intval($isAllValid));
422                 $isAllValid = (($isAllValid === TRUE) && ($verificationStatus['is_valid'] === TRUE));
423         } // END - foreach
424
425         // Is it still true?
426         if ((isInstallationDataCompleted()) && ($isAllValid === TRUE)) {
427                 // Set 'done' and message
428                 $verificationStatus['status']  = 'done';
429                 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
430
431                 // Then do the post-check
432                 doInstallerPostCheck($currentTab, $verificationStatus);
433         } // END - if
434
435         // Is it still valid?
436         if ($verificationStatus['status'] != 'done') {
437                 // Log message away
438                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
439
440                 // Process failed fields
441                 $verificationStatus['failed_fields'] = handleInstallerFailedFields($verificationStatus['failed_fields']);
442
443                 // Output the array for JSON reply
444                 setAjaxReplyContent(loadTemplate('install_overview_failed', TRUE, $verificationStatus));
445
446                 /*
447                  * Something went wrong, this might happen when e.g. the user has tried
448                  * to save invalid database login data but hit reload button on error
449                  * message.
450                  */
451                 setHttpStatus('200 OK');
452
453                 // Abort here
454                 return;
455         } // END - if
456 }
457
458 // [EOF]
459 ?>