]> git.mxchange.org Git - mailer.git/blobdiff - inc/ajax/ajax_installer.php
Heacy rewrite/cleanup:
[mailer.git] / inc / ajax / ajax_installer.php
index 5267a3aee53867c08f79b7748a37d6f5a48a17cb..0031a183cda8e4883a92fb618b56b935082ff5a5 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  ************************************************************************/
 
 // Some security stuff...
-if ((!defined('__SECURITY')) || (!isAjaxOutputMode()) || (!isInstallationPhase())) {
+if (!defined('__SECURITY')) {
        die();
+} elseif ((!isAjaxOutputMode()) || (!isInstaller())) {
+       header('HTTP/1.1 403 Forbidden');
+       die(json_encode(array('reply_content' => 'Access forbidden'), JSON_FORCE_OBJECT));
 } // END - if
 
+//-----------------------------------------------------------------------------
+//         Generic call-back functions, they all rely on session data
+//-----------------------------------------------------------------------------
+
+// Establish a database link
+function establishAjaxInstallerDatabaseLink () {
+       // This requires some session data
+       if (!isSessionDataSet(array('mysql_host', 'mysql_dbase', 'mysql_prefix', 'mysql_login', 'mysql_password1', 'mysql_password2', 'mysql_engine'))) {
+               // Some required session data is not set
+               reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
+       } // END - if
+
+       // Remove any previous flag
+       unsetSqlLinkUp(__FUNCTION__, __LINE__);
+
+       // Establish link
+       $linkResource = sqlConnectToDatabase(getSession('mysql_host'), getSession('mysql_login'), getSession('mysql_password1'), __FUNCTION__, __LINE__);
+
+       // Is this a link resource?
+       if (!is_resource($linkResource)) {
+               // Is not a resource
+               reportBug(__FUNCTION__, __LINE__, 'linkResource[]=' . gettype($linkResource) . ', expected: link resource');
+       } elseif (!isSqlLinkUp()) {
+               // SQL link is not up
+               reportBug(__FUNCTION__, __LINE__, 'Could not bring up SQL link.');
+       }
+
+       // Does selecting the database work?
+       if (!sqlSelectDatabase(getSession('mysql_dbase'), __FUNCTION__, __LINE__)) {
+               // Could not be selected
+               reportBug(__FUNCTION__, __LINE__, 'Could not select database ' . getSession('mysql_dbase'));
+       } elseif (!isInstallerSqlsReadable(getSession('base_path'))) {
+               // Installation area not found
+               reportBug(__FUNCTION__, __LINE__, 'SQL dumps not found. Please extract ALL files from the archive or checkout all files out from SVN.');
+       } elseif (ifFatalErrorsDetected()) {
+               // Some other fatal error occured
+               reportBug(__FUNCTION__, __LINE__, 'Some fatal error detected, please check debug.log for details.');
+       }
+
+       // Set type, prefix from POST data and database name for later queries
+       setConfigEntry('_TABLE_TYPE'  , getSession('mysql_engine'));
+       setConfigEntry('_MYSQL_PREFIX', getSession('mysql_prefix'));
+       setConfigEntry('__DB_NAME'    , getSession('mysql_dbase'));
+}
+
 //-----------------------------------------------------------------------------
 //             Call-back functions for processing AJAX requests
 //-----------------------------------------------------------------------------
@@ -47,7 +95,7 @@ if ((!defined('__SECURITY')) || (!isAjaxOutputMode()) || (!isInstallationPhase()
 // Processes AJAX requests for installer
 function doAjaxProcessInstall () {
        // 'do' must always be set and installation phase must be true
-       if (!isInstallationPhase()) {
+       if (!isInstaller()) {
                // This shall not happen
                reportBug(__FUNCTION__, __LINE__, 'This AJAX request handler was called outside the installer.');
        } elseif (!isPostRequestElementSet('do')) {
@@ -60,6 +108,7 @@ function doAjaxProcessInstall () {
 
        // Again we do a call-back, so generate a function name depending on 'do'
        $callbackName = 'doAjaxInstaller' . capitalizeUnderscoreString(postRequestElement('do'));
+       $GLOBALS['ajax_callback_function'] = $callbackName;
 
        // Is the call-back function there?
        if (!function_exists($callbackName)) {
@@ -100,6 +149,8 @@ function doAjaxInstallerFooterNavigation () {
                case 'database_config':
                case 'smtp_config':
                case 'other_config':
+               case 'extensions':
+               case 'first_admin':
                        array_push($enabledNavigations, 'previous');
                case 'welcome': // Only 'next' works for welcome page
                        array_push($enabledNavigations, 'next');
@@ -122,12 +173,42 @@ function doAjaxInstallerFooterNavigation () {
        } // END - switch
 
        // Output the array for JSON reply
-       setAjaxReplyContent(json_encode($enabledNavigations, JSON_FORCE_OBJECT));
+       setAjaxReplyContent(encodeJson($enabledNavigations));
 
        // All okay if we reach this point
        setHttpStatus('200 OK');
 }
 
+// Processes installer AJAX calls for content-requests
+function doAjaxInstallerDoStep () {
+       // 'step' must be there
+       if (!isPostRequestElementSet('step')) {
+               // This shall not happen
+               reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "step" which is fatal.');
+       } // END - if
+
+       // Construct call-back name
+       $callbackName = 'doAjaxInstallerStep' . capitalizeUnderscoreString(postRequestElement('step'));
+
+       // Is the function there?
+       if (function_exists($callbackName)) {
+               // Call it for setting values in session
+               $status = call_user_func($callbackName);
+       } else {
+               // Log missing functions
+               reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
+       }
+
+       // Did the installation step went fine?
+       if ($status === TRUE) {
+               // All fine
+               setAjaxReplyContent(encodeJson(postRequestElement('step') . '=OK'));
+
+               // All okay if we reach this point
+               setHttpStatus('200 OK');
+       } // END - if
+}
+
 // Processes installer AJAX calls for content-requests
 function doAjaxInstallerRequestContent () {
        // 'tab' must be there
@@ -145,7 +226,7 @@ function doAjaxInstallerRequestContent () {
                call_user_func($callbackName);
        } else {
                // Log missing functions
-               logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
+               reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
        }
 
        // Is the HTTP status still the same? (204 No Content)
@@ -159,7 +240,7 @@ function doAjaxInstallerRequestContent () {
                        setHttpStatus('200 OK');
                } else {
                        // Set 404 error
-                       setHttpStatus('404 NOT FOUND');
+                       setHttpStatus('404 Not Found');
                }
        } // END - if
 }
@@ -175,8 +256,14 @@ function doAjaxInstallerChangeWarning () {
        // "Walk" through all elements
        $OUT = '<ol>';
        foreach (explode(':', postRequestElement('elements')) as $element) {
-               // Add row
-               $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
+               // Is it an extension?
+               if (substr($element, 0, 4) == 'ext_') {
+                       // Add row for extension
+                       $OUT .= '<li>{%message,INSTALLER_CHANGED_ELEMENT_EXTENSION=' . str_replace('_', '-', $element) . '%}</li>';
+               } else {
+                       // Add generic row
+                       $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
+               }
        } // END - foreach
        $OUT .= '</ol>';
 
@@ -211,7 +298,7 @@ function doAjaxInstallerSaveChanges () {
        $currentTab = postRequestElement('tab');
 
        // Remove some elements which should not be saved
-       foreach (array('do', 'level') as $removedElement) {
+       foreach (array('tab', 'do', 'level') as $removedElement) {
                // Remove this element from POST data
                unsetPostRequestElement($removedElement);
        } // END - foreach
@@ -231,6 +318,7 @@ function doAjaxInstallerSaveChanges () {
        // Now set all remaining data in session
        foreach (postRequestArray() as $key => $value) {
                // Set it, if it is valid, else it will be added to $saveStatus (call-by-reference)
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value);
                $saveStatus['is_saved'] = (
                        // Is the data valid?
                        (isInstallerDataValid($saveStatus, $key, $value))
@@ -241,7 +329,7 @@ function doAjaxInstallerSaveChanges () {
 
                // Save the overall status for below final check
                $isAllSaved = (($isAllSaved === TRUE) && ($saveStatus['is_saved'] === TRUE));
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
        } // END - foreach
 
        // 'is_saved' is still true?
@@ -255,12 +343,16 @@ function doAjaxInstallerSaveChanges () {
        } // END - if
 
        // Output the status array for JSON reply
-       setAjaxReplyContent(json_encode($saveStatus, JSON_FORCE_OBJECT));
+       setAjaxReplyContent(encodeJson($saveStatus));
 
        // All okay if we reach this point
        setHttpStatus('200 OK');
 }
 
+// ----------------------------------------------------------------------------
+//          Call-back functions for preparing installer page requests
+// ----------------------------------------------------------------------------
+
 // Prepare AJAX request 'welcome'
 function doAjaxPrepareInstallerWelcome () {
        // Kept empty to prevent logfile entry
@@ -283,13 +375,13 @@ function doAjaxPrepareInstallerBaseData () {
        // Is 'main_title' not set?
        if (!isSessionVariableSet('main_title')) {
                // Then set it from default main title
-               setSession('main_title', '{--DEFAULT_MAIN_TITLE--}');
+               setSession('main_title', compileRawCode(getMessage('DEFAULT_MAIN_TITLE')));
        } // END - if
 
        // Is 'slogan' not set?
        if (!isSessionVariableSet('slogan')) {
                // Then set it from default slogan
-               setSession('slogan', '{--DEFAULT_SLOGAN--}');
+               setSession('slogan', compileRawCode(getMessage('DEFAULT_SLOGAN')));
        } // END - if
 
        // Is 'webmaster' not set?
@@ -326,15 +418,15 @@ function doAjaxPrepareInstallerDatabaseConfig () {
        } // END - if
 
        // Is 'mysql_dbase' not set?
-       if (!isSessionVariableSet('mysql_pass1')) {
+       if (!isSessionVariableSet('mysql_password1')) {
                // Then set it directly
-               setSession('mysql_pass1', '');
+               setSession('mysql_password1', '');
        } // END - if
 
-       // Is 'mysql_pass2' not set?
-       if (!isSessionVariableSet('mysql_pass2')) {
+       // Is 'mysql_password2' not set?
+       if (!isSessionVariableSet('mysql_password2')) {
                // Then set it directly
-               setSession('mysql_pass2', '');
+               setSession('mysql_password2', '');
        } // END - if
 
        // Is 'mysql_engine' not set?
@@ -376,6 +468,48 @@ function doAjaxPrepareInstallerOtherConfig () {
        } // END - if
 }
 
+// Prepare AJAX request 'extensions'
+function doAjaxPrepareInstallerExtensions () {
+       // Is 'extensions' set?
+       if (!isSessionVariableSet('extensions')) {
+               /*
+                * At least ext-admins, ext-sql_patches and ext-task should be installed
+                * (ext-sql_patches is a must!)
+                */
+               setSession('extensions', 'admins:sql_patches:task');
+       } elseif (strpos(getSession('extensions'), 'sql_patches') === FALSE) {
+               // Add missing ext-sql_patches
+               setSession('extensions', getSession('extensions') . ':sql_patches');
+       }
+}
+
+// Prepare AJAX request 'first_admin'
+function doAjaxPrepareInstallerFirstAdmin () {
+       // Is 'admin_login' set?
+       if (!isSessionVariableSet('admin_login')) {
+               // Set it
+               setSession('admin_login', 'admin');
+       } // END - if
+
+       // Is 'admin_email' set?
+       if (!isSessionVariableSet('admin_email')) {
+               // Set it
+               setSession('admin_email', getSession('webmaster'));
+       } // END - if
+
+       // Is 'admin_password1' set?
+       if (!isSessionVariableSet('admin_password1')) {
+               // Set it
+               setSession('admin_password1', '');
+       } // END - if
+
+       // Is 'admin_password2' set?
+       if (!isSessionVariableSet('admin_password2')) {
+               // Set it
+               setSession('admin_password2', '');
+       } // END - if
+}
+
 // Prepare AJAX request 'overview'
 function doAjaxPrepareInstallerOverview () {
        // 'tab' must always be set to create a post-check-callback
@@ -455,5 +589,169 @@ function doAjaxPrepareInstallerOverview () {
        } // END - if
 }
 
+// ----------------------------------------------------------------------------
+//            Call-back functions for doing installation steps
+// ----------------------------------------------------------------------------
+
+// Call-back function to import first tables.sql file
+function doAjaxInstallerStepImportTablesSql () {
+       // Establish database link
+       establishAjaxInstallerDatabaseLink();
+
+       // Init SQL array
+       initSqls();
+
+       // Import tables.sql
+       importInstallSqlDump('tables');
+
+       // Are some SQLs found?
+       if (countSqls() == 0) {
+               // Abort here
+               reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
+       } // END - if
+
+       // Now run all queries through
+       runFilterChain('run_sqls');
+
+       // Close SQL link
+       sqlCloseLink(__FUNCTION__, __LINE__);
+
+       // All fine
+       return TRUE;
+}
+
+// Call-back function to import menu SQL file
+function doAjaxInstallerStepImportMenuSql () {
+       // Establish database link
+       establishAjaxInstallerDatabaseLink();
+
+       // Init SQL array
+       initSqls();
+
+       // Import tables.sql
+       importInstallSqlDump('menu-' . getLanguage());
+
+       // Are some SQLs found?
+       if (countSqls() == 0) {
+               // Abort here
+               reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
+       } // END - if
+
+       // Now run all queries through
+       runFilterChain('run_sqls');
+
+       // Close SQL link
+       sqlCloseLink(__FUNCTION__, __LINE__);
+
+       // All fine
+       return TRUE;
+}
+
+// Call-back function to install some important extensions
+function doAjaxInstallerStepInstallExtensions () {
+       // Only one element is required
+       if (!isSessionVariableSet('extensions')) {
+               // Some required session data is not set
+               reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
+       } // END - if
+
+       // Establish database link
+       establishAjaxInstallerDatabaseLink();
+
+       // Get all extensions
+       $extensions = explode(':', getSession('extensions'));
+
+       // Make sure ext-sql_patches is first
+       array_unshift($extensions, 'sql_patches');
+
+       // "Walk" through all extensions
+       foreach ($extensions as $key => $ext_name) {
+               // Is ext-sql_patches not at key=0?
+               if (($key == 0) && ($ext_name == 'sql_patches')) {
+                       // Then skip this entry
+                       continue;
+               } elseif ((!loadExtension($ext_name, 'test', '0.0.0', TRUE)) || (!registerExtension($ext_name, NULL))) {
+                       // Didn't work
+                       reportBug(__FUNCTION__, __LINE__, 'Cannot load/register extension ' . $ext_name . '.');
+               } // END - if
+       } // END - foreach
+
+       // All fine
+       return TRUE;
+}
+
+// Call-back function to write local configuration file
+function doAjaxInstallerStepWriteLocalConfig () {
+       // Is all set?
+       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'))) {
+               // Some required session data is not set
+               reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
+       } elseif (isInstalled()) {
+               // Is already installed = local config written
+               reportBug(__FUNCTION__, __LINE__, 'Local config file is already written.');
+       } elseif (isAdminRegistered()) {
+               // Admin is already registered
+               reportBug(__FUNCTION__, __LINE__, 'First administrator account is already registered.');
+       }
+
+       // Establish database link
+       establishAjaxInstallerDatabaseLink();
+
+       // Write config file
+       if (!doInstallWriteLocalConfigurationFile(
+               getSession('base_path'),
+               getSession('base_url'),
+               getSession('main_title'),
+               getSession('slogan'),
+               getSession('webmaster'),
+               getSession('warn_no_pass'),
+               getSession('write_footer'),
+               getSession('enable_backlink'),
+               getSession('mysql_host'),
+               getSession('mysql_dbase'),
+               getSession('mysql_login'),
+               getSession('mysql_password1'),
+               getSession('mysql_prefix'),
+               getSession('mysql_engine'),
+               getSession('smtp_host'),
+               getSession('smtp_user'),
+               getSession('smtp_password1')
+       )) {
+               // Something bad went wrong
+               removeFile(getSession('base_path') . getCachePath() . 'config-local.php');
+               reportBug(__FUNCTION__, __LINE__, 'Did not fully write config-local.php .');
+       }
+
+       // Change ADMIN_REGISTERED flag
+       $done = changeDataInLocalConfigurationFile('ADMIN-SETUP', "setConfigEntry('ADMIN_REGISTERED', '", "');", 'Y', 0);
+
+       // All fine
+       return $done;
+}
+
+// Call-back function to register first admin
+function doAjaxInstallerStepRegisterFirstAdmin () {
+       // Is all set?
+       if (!isSessionDataSet(array('admin_login', 'admin_email', 'admin_password1', 'admin_password2'))) {
+               // Some required session data is not set
+               reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
+       } elseif (isAdminRegistered()) {
+               // First admin is already registered
+               reportBug(__FUNCTION__, __LINE__, 'First administrator is already registered.');
+       }
+
+       // Establish database link
+       establishAjaxInstallerDatabaseLink();
+
+       // Load admin include
+       loadIncludeOnce('inc/modules/admin/admin-inc.php');
+
+       // Register first admin
+       $ret = addAdminAccount(getSession('admin_login'), md5(getSession('admin_password1')), getSession('admin_email'), 'allow');
+
+       // Did it work?
+       return ($ret == 'done');
+}
+
 // [EOF]
 ?>