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')) {
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');
112 default: // Unsupported value
113 // This shall not happen
114 reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
116 // This will never be reached
120 // Output the array for JSON reply
121 setAjaxReplyContent(json_encode($enabledNavigations, JSON_FORCE_OBJECT));
123 // All okay if we reach this point
124 setHttpStatus('200 OK');
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.');
135 // Construct call-back name for value-preset
136 $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
138 // Is the function there?
139 if (function_exists($callbackName)) {
140 // Call it for setting values in session
141 call_user_func($callbackName);
143 // Log missing functions
144 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
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));
152 // Has the template been loaded?
153 if (isset($GLOBALS['tpl_content']['install_page_' . trim(postRequestElement('tab'))])) {
154 // All okay if we reach this point
155 setHttpStatus('200 OK');
158 setHttpStatus('404 NOT FOUND');
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.');
171 // "Walk" through all elements
173 foreach (explode(':', postRequestElement('elements')) as $element) {
175 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
182 'button' => postRequestElement('button'),
183 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
186 if (in_array(postRequestElement('button'), array('previous', 'next'))) {
187 // Load 'prefixed' template
188 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), true, $content));
190 // Load 'tab' template
191 setAjaxReplyContent(loadTemplate('install_warning_tab', true, $content));
194 // All okay if we reach this point
195 setHttpStatus('200 OK');
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.');
206 // Save the tab for pre-"filtering"
207 $currentTab = postRequestElement('tab');
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);
215 // Default is failed save attempt (e.g. nothing to save)
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
221 'failed_fields' => array()
224 // Init overall status
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))
234 // And can it be stored in session?
235 (setSession($key, $value))
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));
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--}';
249 // Then do the post-check
250 doInstallerPostCheck($currentTab, $saveStatus);
253 // Output the status array for JSON reply
254 setAjaxReplyContent(json_encode($saveStatus, JSON_FORCE_OBJECT));
256 // All okay if we reach this point
257 setHttpStatus('200 OK');
260 // Prepare AJAX request 'welcome'
261 function doAjaxPrepareInstallerWelcome () {
262 // Kept empty to prevent logfile entry
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());
273 // Is 'base_url' not set?
274 if (!isSessionVariableSet('base_url')) {
275 // Then set it from URL
276 setSession('base_url', getUrl());
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--}');
285 // Is 'slogan' not set?
286 if (!isSessionVariableSet('slogan')) {
287 // Then set it from default slogan
288 setSession('slogan', '{--DEFAULT_SLOGAN--}');
291 // Is 'webmaster' not set?
292 if (!isSessionVariableSet('webmaster')) {
293 // Then set it from default webmaster email address
294 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
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');
306 // Is 'mysql_dbase' not set?
307 if (!isSessionVariableSet('mysql_dbase')) {
308 // Then set it directly
309 setSession('mysql_dbase', 'your_database');
312 // Is 'mysql_prefix' not set?
313 if (!isSessionVariableSet('mysql_prefix')) {
314 // Then set it directly
315 setSession('mysql_prefix', 'mailer');
318 // Is 'mysql_login' not set?
319 if (!isSessionVariableSet('mysql_login')) {
320 // Then set it directly
321 setSession('mysql_login', 'your_login');
324 // Is 'mysql_dbase' not set?
325 if (!isSessionVariableSet('mysql_pass1')) {
326 // Then set it directly
327 setSession('mysql_pass1', '');
330 // Is 'mysql_pass2' not set?
331 if (!isSessionVariableSet('mysql_pass2')) {
332 // Then set it directly
333 setSession('mysql_pass2', '');
336 // Is 'mysql_type' not set?
337 if (!isSessionVariableSet('mysql_type')) {
338 // Then set it directly
339 setSession('mysql_type', 'MyISAM');
343 // Prepare AJAX request 'smtp_config'
344 function doAjaxPrepareInstallerSmtpConfig () {
345 // Kept empty to prevent logfile entry because SMTP settings are optional
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');
356 // Is 'warn_no_pass' not set?
357 if (!isSessionVariableSet('warn_no_pass')) {
358 // Then set it directly
359 setSession('warn_no_pass', 'Y');
362 // Is 'write_footer' not set?
363 if (!isSessionVariableSet('write_footer')) {
364 // Then set it directly
365 setSession('write_footer', 'Y');
368 // Is 'enable_backlink' not set?
369 if (!isSessionVariableSet('enable_backlink')) {
370 // Then set it directly
371 setSession('enable_backlink', 'Y');
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.');
383 // Save the tab for pre-"filtering"
384 $currentTab = postRequestElement('tab');
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
395 'failed_fields' => array()
398 // Init overall status and final output
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'))) {
410 // Is the data valid?
411 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
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);
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));
425 if ($isAllValid === true) {
426 // Set 'done' and message
427 $verificationStatus['status'] = 'done';
428 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
430 // Then do the post-check
431 doInstallerPostCheck($currentTab, $verificationStatus);
434 // Is it still valid?
435 if ($verificationStatus['status'] != 'done') {
437 logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
439 // Output the array for JSON reply
440 setAjaxReplyContent(json_encode($verificationStatus, JSON_FORCE_OBJECT));
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
447 setHttpStatus('500 Internal Server Error');
453 // Output final rendered content
454 setAjaxReplyContent($output);
456 // All okay if we reach this point
457 setHttpStatus('200 OK');