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())) {
40 header('HTTP/1.1 403 Forbidden');
41 die(json_encode(array('reply_content' => 'Access forbidden'), JSON_FORCE_OBJECT));
44 //-----------------------------------------------------------------------------
45 // Generic call-back functions, they all rely on session data
46 //-----------------------------------------------------------------------------
48 // Establish a database link
49 function establishAjaxInstallerDatabaseLink () {
50 // This requires some session data
51 if (!isSessionDataSet(array('mysql_host', 'mysql_dbase', 'mysql_prefix', 'mysql_login', 'mysql_password1', 'mysql_password2', 'mysql_engine'))) {
52 // Some required session data is not set
53 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
56 // Remove any previous flag
57 unsetSqlLinkUp(__FUNCTION__, __LINE__);
60 $linkResource = SQL_CONNECT(getSession('mysql_host'), getSession('mysql_login'), getSession('mysql_password1'), __FUNCTION__, __LINE__);
62 // Is this a link resource?
63 if (!is_resource($linkResource)) {
65 reportBug(__FUNCTION__, __LINE__, 'linkResource[]=' . gettype($linkResource) . ', expected: link resource');
68 // Does selecting the database work?
69 if (!SQL_SELECT_DB(getSession('mysql_dbase'), __FUNCTION__, __LINE__)) {
70 // Could not be selected
71 reportBug(__FUNCTION__, __LINE__, 'Could not select database ' . getSession('mysql_dbase'));
72 } elseif ((!isFileReadable(getSession('base_path') . 'install/tables.sql')) || (!isFileReadable(getSession('base_path') . 'install/menu-'.getLanguage().'.sql'))) {
73 // Installation area not found
74 reportBug(__FUNCTION__, __LINE__, 'SQL dumps not found. Please extract ALL files from the archive or checkout all files out from SVN.');
75 } elseif (ifFatalErrorsDetected()) {
76 // Some other fatal error occured
77 reportBug(__FUNCTION__, __LINE__, 'Some fatal error detected, please check debug.log for details.');
80 // Set type, prefix from POST data and database name for later queries
81 setConfigEntry('_TABLE_TYPE' , getSession('mysql_engine'));
82 setConfigEntry('_MYSQL_PREFIX', getSession('mysql_prefix'));
83 setConfigEntry('__DB_NAME' , getSession('mysql_dbase'));
86 //-----------------------------------------------------------------------------
87 // Call-back functions for processing AJAX requests
88 //-----------------------------------------------------------------------------
90 // Processes AJAX requests for installer
91 function doAjaxProcessInstall () {
92 // 'do' must always be set and installation phase must be true
93 if (!isInstallationPhase()) {
94 // This shall not happen
95 reportBug(__FUNCTION__, __LINE__, 'This AJAX request handler was called outside the installer.');
96 } elseif (!isPostRequestElementSet('do')) {
97 // This shall not happen
98 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "do" which is fatal.');
101 // Notify all modules that we are installing
102 $GLOBALS['__mailer_installing'] = TRUE;
104 // Again we do a call-back, so generate a function name depending on 'do'
105 $callbackName = 'doAjaxInstaller' . capitalizeUnderscoreString(postRequestElement('do'));
107 // Is the call-back function there?
108 if (!function_exists($callbackName)) {
109 // This shall not happen
110 reportBug(__FUNCTION__, __LINE__, 'AJAX call-back ' . $callbackName . ' does not exist.');
114 call_user_func($callbackName);
116 // Is the status fine or template not found (404)?
120 // Processes installer request for testing
121 function doAjaxInstallerTest () {
122 // Load the "test passed" template
123 setAjaxReplyContent(loadTemplate('ajax_test_passed', TRUE));
125 // All okay if we reach this point
126 setHttpStatus('200 OK');
129 // Processes installer requests for footer navigation
130 function doAjaxInstallerFooterNavigation () {
131 // 'tab' must always be set to determine which navigation buttons shall be visible
132 if (!isPostRequestElementSet('tab')) {
133 // This shall not happen
134 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
137 // Init array for footer navigation
138 $enabledNavigations = array();
140 // "Detect" the 'tab' value
141 switch (postRequestElement('tab')) {
142 case 'base_data': // Also 'previous' is valid
143 case 'database_config':
147 array_push($enabledNavigations, 'previous');
148 case 'welcome': // Only 'next' works for welcome page
149 array_push($enabledNavigations, 'next');
152 case 'overview': // Enable only 'previous'
153 array_push($enabledNavigations, 'previous');
154 if (isInstallationDataCompleted()) {
156 array_push($enabledNavigations, 'finish');
160 default: // Unsupported value
161 // This shall not happen
162 reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
164 // This will never be reached
168 // Output the array for JSON reply
169 setAjaxReplyContent(encodeJson($enabledNavigations));
171 // All okay if we reach this point
172 setHttpStatus('200 OK');
175 // Processes installer AJAX calls for content-requests
176 function doAjaxInstallerDoStep () {
177 // 'step' must be there
178 if (!isPostRequestElementSet('step')) {
179 // This shall not happen
180 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "step" which is fatal.');
183 // Construct call-back name
184 $callbackName = 'doAjaxInstallerStep' . capitalizeUnderscoreString(postRequestElement('step'));
186 // Is the function there?
187 if (function_exists($callbackName)) {
188 // Call it for setting values in session
189 call_user_func($callbackName);
191 // Log missing functions
192 reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
196 setAjaxReplyContent(encodeJson(postRequestElement('step').'=OK'));
198 // All okay if we reach this point
199 setHttpStatus('200 OK');
202 // Processes installer AJAX calls for content-requests
203 function doAjaxInstallerRequestContent () {
204 // 'tab' must be there
205 if (!isPostRequestElementSet('tab')) {
206 // This shall not happen
207 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
210 // Construct call-back name for value-preset
211 $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
213 // Is the function there?
214 if (function_exists($callbackName)) {
215 // Call it for setting values in session
216 call_user_func($callbackName);
218 // Log missing functions
219 reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
222 // Is the HTTP status still the same? (204 No Content)
223 if (getHttpStatus() == '204 No Content') {
224 // We use the current access level 'install' as prefix and construct a template name
225 setAjaxReplyContent(loadTemplate('install_page_' . trim(postRequestElement('tab')), TRUE));
227 // Has the template been loaded?
228 if (isset($GLOBALS['template_content']['html']['install_page_' . trim(postRequestElement('tab'))])) {
229 // All okay if we reach this point
230 setHttpStatus('200 OK');
233 setHttpStatus('404 Not Found');
238 // Process installer AJAX call for change-warning
239 function doAjaxInstallerChangeWarning () {
240 // 'elements' and 'button' must be there
241 if ((!isPostRequestElementSet('elements')) || (!isPostRequestElementSet('button'))) {
242 // This shall not happen
243 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "elements" and/or "button" which is fatal.');
246 // "Walk" through all elements
248 foreach (explode(':', postRequestElement('elements')) as $element) {
249 // Is it an extension?
250 if (substr($element, 0, 4) == 'ext_') {
251 // Add row for extension
252 $OUT .= '<li>{%message,INSTALLER_CHANGED_ELEMENT_EXTENSION=' . str_replace('_', '-', $element) . '%}</li>';
255 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
263 'button' => postRequestElement('button'),
264 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
267 if (in_array(postRequestElement('button'), array('previous', 'next'))) {
268 // Load 'prefixed' template
269 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), TRUE, $content));
271 // Load 'tab' template
272 setAjaxReplyContent(loadTemplate('install_warning_tab', TRUE, $content));
275 // All okay if we reach this point
276 setHttpStatus('200 OK');
279 // Process installer AJAC call for saving changes
280 function doAjaxInstallerSaveChanges () {
281 // 'tab' must always be set to create a post-check-callback
282 if (!isPostRequestElementSet('tab')) {
283 // This shall not happen
284 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
287 // Save the tab for pre-"filtering"
288 $currentTab = postRequestElement('tab');
290 // Remove some elements which should not be saved
291 foreach (array('tab', 'do', 'level') as $removedElement) {
292 // Remove this element from POST data
293 unsetPostRequestElement($removedElement);
296 // Default is failed save attempt (e.g. nothing to save)
298 'status' => 'failed',
299 'message' => '{--INSTALLER_SAVE_CHANGES_FAILED--}',
300 // Don't set this to false, or else it will be returned as 'failed' but is saved
302 'failed_fields' => array()
305 // Init overall status
308 // Now set all remaining data in session
309 foreach (postRequestArray() as $key => $value) {
310 // Set it, if it is valid, else it will be added to $saveStatus (call-by-reference)
311 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value);
312 $saveStatus['is_saved'] = (
313 // Is the data valid?
314 (isInstallerDataValid($saveStatus, $key, $value))
316 // And can it be stored in session?
317 (setSession($key, $value))
320 // Save the overall status for below final check
321 $isAllSaved = (($isAllSaved === TRUE) && ($saveStatus['is_saved'] === TRUE));
322 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
325 // 'is_saved' is still true?
326 if ($isAllSaved === TRUE) {
327 // Set 'done' and message
328 $saveStatus['status'] = 'done';
329 $saveStatus['message'] = '{--INSTALLER_SAVE_CHANGES_DONE--}';
331 // Then do the post-check
332 doInstallerPostCheck($currentTab, $saveStatus);
335 // Output the status array for JSON reply
336 setAjaxReplyContent(encodeJson($saveStatus));
338 // All okay if we reach this point
339 setHttpStatus('200 OK');
342 // ----------------------------------------------------------------------------
343 // Call-back functions for preparing installer page requests
344 // ----------------------------------------------------------------------------
346 // Prepare AJAX request 'welcome'
347 function doAjaxPrepareInstallerWelcome () {
348 // Kept empty to prevent logfile entry
351 // Prepare AJAX request 'base_data'
352 function doAjaxPrepareInstallerBaseData () {
353 // Is 'base_path' not set?
354 if (!isSessionVariableSet('base_path')) {
355 // Then set it from PATH
356 setSession('base_path', getPath());
359 // Is 'base_url' not set?
360 if (!isSessionVariableSet('base_url')) {
361 // Then set it from URL
362 setSession('base_url', getUrl());
365 // Is 'main_title' not set?
366 if (!isSessionVariableSet('main_title')) {
367 // Then set it from default main title
368 setSession('main_title', '{--DEFAULT_MAIN_TITLE--}');
371 // Is 'slogan' not set?
372 if (!isSessionVariableSet('slogan')) {
373 // Then set it from default slogan
374 setSession('slogan', '{--DEFAULT_SLOGAN--}');
377 // Is 'webmaster' not set?
378 if (!isSessionVariableSet('webmaster')) {
379 // Then set it from default webmaster email address
380 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
384 // Prepare AJAX request 'database_config'
385 function doAjaxPrepareInstallerDatabaseConfig () {
386 // Is 'mysql_host' not set?
387 if (!isSessionVariableSet('mysql_host')) {
388 // Then set it directly
389 setSession('mysql_host', 'localhost');
392 // Is 'mysql_dbase' not set?
393 if (!isSessionVariableSet('mysql_dbase')) {
394 // Then set it directly
395 setSession('mysql_dbase', 'your_database');
398 // Is 'mysql_prefix' not set?
399 if (!isSessionVariableSet('mysql_prefix')) {
400 // Then set it directly
401 setSession('mysql_prefix', 'mailer');
404 // Is 'mysql_login' not set?
405 if (!isSessionVariableSet('mysql_login')) {
406 // Then set it directly
407 setSession('mysql_login', 'your_login');
410 // Is 'mysql_dbase' not set?
411 if (!isSessionVariableSet('mysql_password1')) {
412 // Then set it directly
413 setSession('mysql_password1', '');
416 // Is 'mysql_password2' not set?
417 if (!isSessionVariableSet('mysql_password2')) {
418 // Then set it directly
419 setSession('mysql_password2', '');
422 // Is 'mysql_engine' not set?
423 if (!isSessionVariableSet('mysql_engine')) {
424 // Then set it directly
425 setSession('mysql_engine', 'MyISAM');
429 // Prepare AJAX request 'smtp_config'
430 function doAjaxPrepareInstallerSmtpConfig () {
431 // Kept empty to prevent logfile entry because SMTP settings are optional
434 // Prepare AJAX request 'other_config'
435 function doAjaxPrepareInstallerOtherConfig () {
436 // Is 'output_mode' not set?
437 if (!isSessionVariableSet('output_mode')) {
438 // Then set it directly
439 setSession('output_mode', 'render');
442 // Is 'warn_no_pass' not set?
443 if (!isSessionVariableSet('warn_no_pass')) {
444 // Then set it directly
445 setSession('warn_no_pass', 'Y');
448 // Is 'write_footer' not set?
449 if (!isSessionVariableSet('write_footer')) {
450 // Then set it directly
451 setSession('write_footer', 'Y');
454 // Is 'enable_backlink' not set?
455 if (!isSessionVariableSet('enable_backlink')) {
456 // Then set it directly
457 setSession('enable_backlink', 'Y');
461 // Prepare AJAX request 'extensions'
462 function doAjaxPrepareInstallerExtensions () {
463 // Is 'extensions' set?
464 if (!isSessionVariableSet('extensions')) {
466 * At least ext-sql_patches and ext-task should be installed
467 *(ext-sql_patches is a must!)
469 setSession('extensions', 'admins:sql_patches:task');
470 } elseif (strpos(getSession('extensions'), 'sql_patches') === FALSE) {
471 // Add missing ext-sql_patches
472 setSession('extensions', getSession('extensions') . ':sql_patches');
476 // Prepare AJAX request 'overview'
477 function doAjaxPrepareInstallerOverview () {
478 // 'tab' must always be set to create a post-check-callback
479 if (!isPostRequestElementSet('tab')) {
480 // This shall not happen
481 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
484 // Save the tab for pre-"filtering"
485 $currentTab = postRequestElement('tab');
487 // Default is failed save attempt (e.g. nothing to save)
488 $verificationStatus = array(
489 // Status code, can be 'failed' or 'done'
490 'status' => 'failed',
491 // Status message (e.g. for output)
492 'message' => '{--INSTALLER_OVERVIEW_FINAL_CHECK_FAILED--}',
493 // Don't set this to false, or else it will be returned as 'failed' but is saved
496 'failed_fields' => array()
499 // Init overall status and final output
503 // Check all data in session
504 foreach (array_keys($GLOBALS['installer_groups']) as $key) {
505 // Get values from session
506 $value = getSession($key);
508 // Is the data valid?
509 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
511 // Is this step okay?
512 if ($verificationStatus['is_valid'] === TRUE) {
513 // Add this key/value pair to a overview group
514 addKeyValueToInstallerOverviewGroup($key, $value);
517 // Save the overall status for below final check
518 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_valid=' . intval($verificationStatus['is_valid']) . ',isAllValid=' . intval($isAllValid));
519 $isAllValid = (($isAllValid === TRUE) && ($verificationStatus['is_valid'] === TRUE));
523 if ((isInstallationDataCompleted()) && ($isAllValid === TRUE)) {
524 // Set 'done' and message
525 $verificationStatus['status'] = 'done';
526 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
528 // Then do the post-check
529 doInstallerPostCheck($currentTab, $verificationStatus);
532 // Is it still valid?
533 if ($verificationStatus['status'] != 'done') {
535 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
537 // Process failed fields
538 $verificationStatus['failed_fields'] = handleInstallerFailedFields($verificationStatus['failed_fields']);
540 // Output the array for JSON reply
541 setAjaxReplyContent(loadTemplate('install_overview_failed', TRUE, $verificationStatus));
544 * Something went wrong, this might happen when e.g. the user has tried
545 * to save invalid database login data but hit reload button on error
548 setHttpStatus('200 OK');
555 // ----------------------------------------------------------------------------
556 // Call-back functions for doing installation steps
557 // ----------------------------------------------------------------------------
559 // Call-back function to import first tables.sql file
560 function doAjaxInstallerStepImportTablesSql () {
561 // Establish database link
562 establishAjaxInstallerDatabaseLink();
568 importInstallSqlDump('tables');
570 // Are some SQLs found?
571 if (countSqls() == 0) {
573 reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
576 // Now run all queries through
577 runFilterChain('run_sqls');
580 SQL_CLOSE(__FUNCTION__, __LINE__);
583 // Call-back function to import menu SQL file
584 function doAjaxInstallerStepImportMenuSql () {
585 // Establish database link
586 establishAjaxInstallerDatabaseLink();
592 importInstallSqlDump('menu-' . getLanguage());
594 // Are some SQLs found?
595 if (countSqls() == 0) {
597 reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
600 // Now run all queries through
601 runFilterChain('run_sqls');
604 SQL_CLOSE(__FUNCTION__, __LINE__);
607 // Call-back function to install some important extensions
608 function doAjaxInstallerStepInstallExtensions () {
609 // Only one element is required
610 if (!isSessionVariableSet('extensions')) {
611 // Some required session data is not set
612 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
615 // Establish database link
616 establishAjaxInstallerDatabaseLink();
618 // Get all extensions
619 $extensions = explode(':', getSession('extensions'));
621 // Make sure ext-sql_patches is first
622 array_unshift($extensions, 'sql_patches');
624 // "Walk" through all extensions
625 foreach ($extensions as $key => $ext_name) {
626 // Is ext-sql_patches not at key=0?
627 if (($key == 0) && ($ext_name == 'sql_patches')) {
628 // Then skip this entry
630 } elseif ((!loadExtension($ext_name, 'test', '0.0.0', TRUE)) || (!registerExtension($ext_name, NULL))) {
632 reportBug(__FUNCTION__, __LINE__, 'Cannot load/register extension ' . $ext_name . '.');
637 // Call-back function to write local configuration file
638 function doAjaxInstallerStepWriteLocalConfig () {
640 if (!isSessionDataSet(array('base_path', 'base_url', 'main_title', 'slogan', 'webmaster', 'mysql_host', 'mysql_dbase', 'mysql_prefix', 'mysql_login', 'mysql_password1', 'mysql_password2', 'mysql_engine', 'output_mode', 'warn_no_pass', 'write_footer', 'enable_backlink'))) {
641 // Some required session data is not set
642 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
646 if (!doInstallWriteLocalConfigurationFile(
647 getSession('base_path'),
648 getSession('base_url'),
649 getSession('main_title'),
650 getSession('slogan'),
651 getSession('webmaster'),
652 getSession('warn_no_pass'),
653 getSession('write_footer'),
654 getSession('enable_backlink'),
655 getSession('mysql_host'),
656 getSession('mysql_dbase'),
657 getSession('mysql_login'),
658 getSession('mysql_password1'),
659 getSession('mysql_prefix'),
660 getSession('mysql_engine'),
661 getSession('smtp_host'),
662 getSession('smtp_user'),
663 getSession('smtp_password1')
665 // Something bad went wrong
666 removeFile(getSession('base_path') . getCachePath() . 'config-local.php');
667 reportBug(__FUNCTION__, __LINE__, 'Did not fully write config-local.php .');