2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 06/24/2012 *
4 * =================== Last change: 06/24/2012 *
6 * -------------------------------------------------------------------- *
7 * File : ajax_installer.php *
8 * -------------------------------------------------------------------- *
9 * Short description : AJAX-related functions for installer *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : AJAX-bezogene Funktionen fuer Installer *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isAjaxOutputMode()) || (!isInstallationPhase())) {
43 //-----------------------------------------------------------------------------
44 // Call-back functions for processing AJAX requests
45 //-----------------------------------------------------------------------------
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.');
58 // Notify all modules that we are installing
59 $GLOBALS['__mailer_installing'] = TRUE;
61 // Again we do a call-back, so generate a function name depending on 'do'
62 $callbackName = 'doAjaxInstaller' . capitalizeUnderscoreString(postRequestElement('do'));
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.');
71 call_user_func($callbackName);
73 // Is the status fine or template not found (404)?
77 // Processes installer request for testing
78 function doAjaxInstallerTest () {
79 // Load the "test passed" template
80 setAjaxReplyContent(loadTemplate('ajax_test_passed', TRUE));
82 // All okay if we reach this point
83 setHttpStatus('200 OK');
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.');
94 // Init array for footer navigation
95 $enabledNavigations = array();
97 // "Detect" the 'tab' value
98 switch (postRequestElement('tab')) {
99 case 'base_data': // Also 'previous' is valid
100 case 'database_config':
103 array_push($enabledNavigations, 'previous');
104 case 'welcome': // Only 'next' works for welcome page
105 array_push($enabledNavigations, 'next');
108 case 'overview': // Enable only 'previous'
109 array_push($enabledNavigations, 'previous');
110 if (isInstallationDataCompleted()) {
112 array_push($enabledNavigations, 'finish');
116 default: // Unsupported value
117 // This shall not happen
118 reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
120 // This will never be reached
124 // Output the array for JSON reply
125 setAjaxReplyContent(json_encode($enabledNavigations, JSON_FORCE_OBJECT));
127 // All okay if we reach this point
128 setHttpStatus('200 OK');
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.');
139 // Construct call-back name for value-preset
140 $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
142 // Is the function there?
143 if (function_exists($callbackName)) {
144 // Call it for setting values in session
145 call_user_func($callbackName);
147 // Log missing functions
148 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
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));
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');
162 setHttpStatus('404 NOT FOUND');
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.');
175 // "Walk" through all elements
177 foreach (explode(':', postRequestElement('elements')) as $element) {
179 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
186 'button' => postRequestElement('button'),
187 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
190 if (in_array(postRequestElement('button'), array('previous', 'next'))) {
191 // Load 'prefixed' template
192 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), TRUE, $content));
194 // Load 'tab' template
195 setAjaxReplyContent(loadTemplate('install_warning_tab', TRUE, $content));
198 // All okay if we reach this point
199 setHttpStatus('200 OK');
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.');
210 // Save the tab for pre-"filtering"
211 $currentTab = postRequestElement('tab');
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);
219 // Default is failed save attempt (e.g. nothing to save)
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
225 'failed_fields' => array()
228 // Init overall status
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))
238 // And can it be stored in session?
239 (setSession($key, $value))
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));
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--}';
253 // Then do the post-check
254 doInstallerPostCheck($currentTab, $saveStatus);
257 // Output the status array for JSON reply
258 setAjaxReplyContent(json_encode($saveStatus, JSON_FORCE_OBJECT));
260 // All okay if we reach this point
261 setHttpStatus('200 OK');
264 // Prepare AJAX request 'welcome'
265 function doAjaxPrepareInstallerWelcome () {
266 // Kept empty to prevent logfile entry
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());
277 // Is 'base_url' not set?
278 if (!isSessionVariableSet('base_url')) {
279 // Then set it from URL
280 setSession('base_url', getUrl());
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--}');
289 // Is 'slogan' not set?
290 if (!isSessionVariableSet('slogan')) {
291 // Then set it from default slogan
292 setSession('slogan', '{--DEFAULT_SLOGAN--}');
295 // Is 'webmaster' not set?
296 if (!isSessionVariableSet('webmaster')) {
297 // Then set it from default webmaster email address
298 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
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');
310 // Is 'mysql_dbase' not set?
311 if (!isSessionVariableSet('mysql_dbase')) {
312 // Then set it directly
313 setSession('mysql_dbase', 'your_database');
316 // Is 'mysql_prefix' not set?
317 if (!isSessionVariableSet('mysql_prefix')) {
318 // Then set it directly
319 setSession('mysql_prefix', 'mailer');
322 // Is 'mysql_login' not set?
323 if (!isSessionVariableSet('mysql_login')) {
324 // Then set it directly
325 setSession('mysql_login', 'your_login');
328 // Is 'mysql_dbase' not set?
329 if (!isSessionVariableSet('mysql_pass1')) {
330 // Then set it directly
331 setSession('mysql_pass1', '');
334 // Is 'mysql_pass2' not set?
335 if (!isSessionVariableSet('mysql_pass2')) {
336 // Then set it directly
337 setSession('mysql_pass2', '');
340 // Is 'mysql_engine' not set?
341 if (!isSessionVariableSet('mysql_engine')) {
342 // Then set it directly
343 setSession('mysql_engine', 'MyISAM');
347 // Prepare AJAX request 'smtp_config'
348 function doAjaxPrepareInstallerSmtpConfig () {
349 // Kept empty to prevent logfile entry because SMTP settings are optional
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');
360 // Is 'warn_no_pass' not set?
361 if (!isSessionVariableSet('warn_no_pass')) {
362 // Then set it directly
363 setSession('warn_no_pass', 'Y');
366 // Is 'write_footer' not set?
367 if (!isSessionVariableSet('write_footer')) {
368 // Then set it directly
369 setSession('write_footer', 'Y');
372 // Is 'enable_backlink' not set?
373 if (!isSessionVariableSet('enable_backlink')) {
374 // Then set it directly
375 setSession('enable_backlink', 'Y');
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.');
387 // Save the tab for pre-"filtering"
388 $currentTab = postRequestElement('tab');
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
399 'failed_fields' => array()
402 // Init overall status and final output
406 // Check all data in session
407 foreach (array_keys($GLOBALS['installer_groups']) as $key) {
408 // Get values from session
409 $value = getSession($key);
411 // Is the data valid?
412 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
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);
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));
426 if ((isInstallationDataCompleted()) && ($isAllValid === TRUE)) {
427 // Set 'done' and message
428 $verificationStatus['status'] = 'done';
429 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
431 // Then do the post-check
432 doInstallerPostCheck($currentTab, $verificationStatus);
435 // Is it still valid?
436 if ($verificationStatus['status'] != 'done') {
438 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
440 // Process failed fields
441 $verificationStatus['failed_fields'] = handleInstallerFailedFields($verificationStatus['failed_fields']);
443 // Output the array for JSON reply
444 setAjaxReplyContent(loadTemplate('install_overview_failed', TRUE, $verificationStatus));
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
451 setHttpStatus('200 OK');