]> git.mxchange.org Git - mailer.git/blobdiff - inc/install-functions.php
Updated copyright year.
[mailer.git] / inc / install-functions.php
index c0a939c28668ab9c851a37ece73a20a85ea7d72f..c62b260723d295eb70d3e8bee798b8bdda77696f 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Funktionen fuer die Installationsroutine         *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -36,7 +31,7 @@
  ************************************************************************/
 
 // Some security stuff...
-if ((!defined('__SECURITY')) || (!isInstallationPhase())) {
+if ((!defined('__SECURITY')) || (!isInstaller())) {
        die();
 } // END - if
 
@@ -68,11 +63,21 @@ function initInstaller () {
                'warn_no_pass'    => 'other_config',
                'write_footer'    => 'other_config',
                'enable_backlink' => 'other_config',
+               // first_admin
+               'admin_login'     => 'first_admin',
+               'admin_email'     => 'first_admin',
+               'admin_password1' => 'first_admin',
+               'admin_password2' => 'first_admin',
        );
+
+       // Set mininmum password length/score and other config entries
+       setConfigEntry('min_password_length', 5);
+       setConfigEntry('min_password_score' , 3);
+       setConfigEntry('verbose_sql'        , 'N');
 }
 
 // Write the local config-local.php file from "template"
-function doInstallWriteLocalConfigurationFile ($path, $url, $title, $slogan, $email, $noPassword, $writeFooter, $backLink, $databaseHost, $databaseName, $databaseLogin, $databasePassword, $databasePrefix, $databaseType, $smtpHost, $smtpUser, $smtpPassword) {
+function doInstallWriteLocalConfigurationFile ($path, $url, $title, $slogan, $email, $noPassword, $writeFooter, $backLink, $databaseHost, $databaseName, $databaseLogin, $databasePassword, $databasePrefix, $tableType, $databaseExtension, $smtpHost, $smtpUser, $smtpPassword) {
        // Copy the config template and verify it
        copyFileVerified($path . 'inc/config-local.php.dist', $path . getCachePath() . 'config-local.php', 0644);
 
@@ -100,7 +105,8 @@ function doInstallWriteLocalConfigurationFile ($path, $url, $title, $slogan, $em
        changeDataInLocalConfigurationFile('MYSQL-LOGIN', "     'login'    => '", "',", $databaseLogin, 0);
        changeDataInLocalConfigurationFile('MYSQL-PASSWORD', "  'password' => '", "',", $databasePassword, 0);
        changeDataInLocalConfigurationFile('MYSQL-PREFIX', "setConfigEntry('_MYSQL_PREFIX', '", "');", $databasePrefix, 0);
-       changeDataInLocalConfigurationFile('TABLE-TYPE', "setConfigEntry('_TABLE_TYPE', '", "');", $databaseType, 0);
+       changeDataInLocalConfigurationFile('TABLE-TYPE', "setConfigEntry('_TABLE_TYPE', '", "');", $tableType, 0);
+       changeDataInLocalConfigurationFile('DATABASE-TYPE', "setConfigEntry('_DB_TYPE', '", "');", $databaseExtension, 0);
        changeDataInLocalConfigurationFile('SMTP-HOSTNAME', "setConfigEntry('SMTP_HOSTNAME', '", "');", $smtpHost, 0);
        changeDataInLocalConfigurationFile('SMTP-USER', "setConfigEntry('SMTP_USER', '", "');", $smtpUser, 0);
        changeDataInLocalConfigurationFile('SMTP-PASSWORD', "setConfigEntry('SMTP_PASSWORD', '", "');", $smtpPassword, 0);
@@ -109,7 +115,7 @@ function doInstallWriteLocalConfigurationFile ($path, $url, $title, $slogan, $em
        changeDataInLocalConfigurationFile('SITE-KEY', "setConfigEntry('SITE_KEY', '", "');", generatePassword(50), 0);
 
        // Script is now installed
-       changeDataInLocalConfigurationFile('INSTALLED', "setConfigEntry('MAILER_INSTALLED', '", "');", 'Y', 0);
+       return changeDataInLocalConfigurationFile('INSTALLED', "setConfigEntry('MAILER_INSTALLED', '", "');", 'Y', 0);
 }
 
 // Adds a given template with content to install output stream
@@ -141,7 +147,7 @@ function getInstallerContent () {
                $content = $GLOBALS['install_content'];
        } else {
                // Nothing found, this needs fixing
-               $content = displayMessage('{--INSTALLER_CONTENT_404--}', TRUE);
+               $content = returnMessage('{--INSTALLER_CONTENT_404--}');
        }
 
        // Return content
@@ -154,8 +160,7 @@ function readSqlDump ($FQFN) {
        $content = readFromFile($FQFN);
 
        // Remove some unwanted chars
-       $content = str_replace(chr(13), '', $content);
-       $content = str_replace(PHP_EOL . PHP_EOL, PHP_EOL, $content);
+       $content = str_replace(array(chr(13), PHP_EOL . PHP_EOL), array('', PHP_EOL), $content);
 
        // Return the content
        return $content;
@@ -183,12 +188,58 @@ function generateInstallerFooterNavigation () {
 function generateInstallerDatabaseTypeOptions () {
        return generateOptions(
                '/ARRAY/',
-               array('MyISAM', 'InnoDB'),
-               array('{--INSTALLER_TABLE_TYPE_MYISAM--}', '{--INSTALLER_TABLE_TYPE_INNODB--}'),
+               array(
+                       'MyISAM',
+                       'InnoDB'
+               ),
+               array(
+                       '{--INSTALLER_TABLE_TYPE_MYISAM--}',
+                       '{--INSTALLER_TABLE_TYPE_INNODB--}'
+               ),
                getSession('mysql_engine')
        );
 }
 
+// Generate an option list for database extensions for given default value
+function generateInstallerDatabaseExtensionOptions () {
+       // Init all arrays
+       $keys = array();
+       $values = array();
+       $foundExtensions = array();
+
+       // Scan directory
+       $drivers = getArrayFromDirectory('inc/db/', 'lib-', FALSE, FALSE);
+
+       // Remove prefix + extension
+       foreach ($drivers as $key => $driver) {
+               // Remove driver
+               $drivers[$key] = substr($driver, 4, -4);
+
+               // Is the corresponding extension loaded?
+               if (isPhpExtensionLoaded($drivers[$key])) {
+                       // Then add it
+                       array_push($foundExtensions, $drivers[$key]);
+               } // END - if
+       } // END - foreach
+
+       // Generate list for keys/values ("translations")
+       foreach ($foundExtensions as $extension) {
+               // Use it as direct key
+               array_push($keys, $extension);
+
+               // Add "translation" function around it as value
+               array_push($values, '{%pipe,translatePhpExtension=' . $extension . '%}');
+       } // END - if
+
+       // Get a directory list
+       return generateOptions(
+               '/ARRAY/',
+               $keys,
+               $values,
+               getSession('database_extension')
+       );
+}
+
 // Generate an option list for output mode types for given default value
 function generateInstallerOutputModeOptions ($defaultValue) {
        return generateOptions(
@@ -220,7 +271,7 @@ function isPlainInstaller () {
 // Handle all given failed fields
 function handleInstallerFailedFields ($failedFields) {
        // Don't do anything with no array or no entries
-       if ((!is_array($failedFields)) || (count($failedFields) == 0)) {
+       if (!isFilledArray($failedFields)) {
                // Abort here
                reportBug(__FUNCTION__, __LINE__, 'failedFields[]=' . gettype($failedFields) . ' verification failed');
        } // END - if
@@ -253,7 +304,7 @@ function isInstallerDataValid (&$saveStatus, $key, $value) {
        } // END - if
 
        // Then call it back
-       $isValid = (bool) call_user_func($callbackName, trim($value));
+       $isValid = (bool) call_user_func($callbackName, $value);
 
        // Is it not valid?
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',isValid=' . intval($isValid));
@@ -278,10 +329,10 @@ function doInstallerPostCheck ($currentTab, &$saveStatus) {
        // Is the function there?
        if (!function_exists($callbackName)) {
                // Not found, which is not bad, but it means the post-check won't be run
-               //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' not found. saveStatus[status]=' . $saveStatus['status'] . ', currentTab=' . $currentTab);
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' not found. saveStatus[status]=' . $saveStatus['status'] . ', currentTab=' . $currentTab);
 
                // Abort here
-               return;
+               return FALSE;
        } // END - if
 
        // Init 'tab-specific error message'
@@ -295,13 +346,13 @@ function doInstallerPostCheck ($currentTab, &$saveStatus) {
        $isValid = (bool) call_user_func($callbackName, $currentTab);
 
        // Is it not valid?
-       if (($isValid === FALSE) || (count($GLOBALS['installer_failed_fields'][$currentTab]) > 0)) {
+       if (($isValid === FALSE) || (isFilledArray($GLOBALS['installer_failed_fields'][$currentTab]))) {
                // Then change status and message
                $saveStatus['status']  = 'failed';
                $saveStatus['message'] = '{%message,INSTALLER_POST_CHECK_' . strtoupper($currentTab) . '_FAILED=' . $GLOBALS['installer_post_error'][$currentTab] . '%}';
 
                // Is there failed fields?
-               if (count($GLOBALS['installer_failed_fields'][$currentTab]) > 0) {
+               if (isFilledArray($GLOBALS['installer_failed_fields'][$currentTab])) {
                        // Then merge both
                        $saveStatus['failed_fields'] = merge_array($saveStatus['failed_fields'], $GLOBALS['installer_failed_fields'][$currentTab]);
                } // END - if
@@ -335,6 +386,50 @@ function addKeyValueToInstallerOverviewGroup ($key, $value) {
        $GLOBALS['installer_overview'][$group][$key] = $value;
 }
 
+//-----------------------------------------------------------------------------
+//                        Template call-back functions
+//-----------------------------------------------------------------------------
+
+// ----------------- Extensions -----------------
+
+// Generates (and returns) a table from all extensions
+function generateInstallerExtensionTable () {
+       // Generate extension list
+       $extensions = loadAllExtensionsByTemplate();
+
+       // "Walk" through all
+       $OUT = '';
+       foreach ($extensions as $extension) {
+               // Remove prefix + suffix
+               $ext_name = substr(basename($extension), 4, -4);
+
+               // Is the extension not in development and not admintheme* ?
+               if ((loadExtension($ext_name, 'test', '0.0.0', TRUE)) && (isExtensionProductive($ext_name)) && (substr($ext_name, 0, 10) != 'admintheme')) {
+                       // Default is not disabled
+                       $disabled = '';
+                       if (in_array($ext_name, array('sql_patches', 'timezone'))) {
+                               // Always keep this enabled
+                               $disabled = ' disabled="disabled"';
+                       } // END - if
+
+                       // Initialize content array
+                       $content = array(
+                               'ext_name'    => $ext_name,
+                               'ext_version' => getExtensionVersion($ext_name),
+                               'checked'     => getExtensionSelectedFromSession($ext_name, 'extensions'),
+                               'disabled'    => $disabled,
+                               'description' => loadTemplate('ext_' . $ext_name, TRUE),
+                       );
+
+                       // Load row template
+                       $OUT .= loadTemplate('install_list_extensions_row', TRUE, $content);
+               } // END - if
+       } // END - foreach
+
+       // Load main template
+       return loadTemplate('install_list_extensions', TRUE, $OUT);
+}
+
 //-----------------------------------------------------------------------------
 //                   Call-back functions to check validity
 //-----------------------------------------------------------------------------
@@ -379,7 +474,7 @@ function isInstallerBaseUrlValid ($value) {
                ($value == getUrl())
        || (
                // Starts with http:// or https:// ?
-               ((substr($value, 0, 7) == 'http://') || (substr($value, 0, 8) == 'https://'))
+               (isFullQualifiedUrl($value))
        &&
                // Has no trailing slash?
                (substr($value, -1, 1) != '/')
@@ -424,10 +519,12 @@ function isInstallerWebmasterValid ($value) {
                (isEmailValid($value))
        ||
                // Or is there 'localhost/127.0.0.1' as hostname? Then don't check email address (e.g. you@localhost)
-               (in_array(detectServerName(), array('localhost', '127.0.0.1')))
+               (isDeveloperSystem())
        ) && (
                // Is not default "email address"
                $value != getMessage('DEFAULT_WEBMASTER')
+       ) && (
+               strlen($value) >= 11
        ));
 
        // Return it
@@ -466,6 +563,15 @@ function isInstallerMysqlEngineValid ($value) {
        return $isValid;
 }
 
+// Call-back function to check validity of 'database_extension'
+function isInstallerDatabaseExtensionValid ($value) {
+       // This value must be 'mysql' or 'mysqli'
+       $isValid = in_array($value, array('mysql', 'mysqli'));
+
+       // Return it
+       return $isValid;
+}
+
 // Call-back function to check validity of 'mysql_dbase'
 function isInstallerMysqlDbaseValid ($value) {
        // This value must not be empty
@@ -497,6 +603,70 @@ function isInstallerMysqlPrefixValid ($value) {
 
 // ----------------- Other configuration -----------------
 
+// ----------------- First administator -----------------
+
+// Call-back function to check validity of 'admin_login'
+function isInstallerAdminLoginValid ($value) {
+       // Length should not be shorter than 4 characters
+       return (strlen($value) >= 4);
+}
+
+// Call-back function to check validity of 'admin_email'
+function isInstallerAdminEmailValid ($value) {
+       // Just call webmaster-check function
+       return isInstallerWebmasterValid($value);
+}
+
+// Call-back function to check validity of 'admin_password1'
+function isInstallerAdminPassword1Valid ($value) {
+       // Check if it is strong
+       return isStrongPassword($value);
+}
+
+// Call-back function to check validity of 'admin_password2'
+function isInstallerAdminPassword2Valid ($value) {
+       // Check if it is strong
+       return isStrongPassword($value);
+}
+
+// ----------------- Extensions -----------------
+
+// Call-back function to check 'sel' (array!)
+function isInstallerSelValid ($value) {
+       // $value is not an array, is really bad.
+       if (!is_array($value)) {
+               // Is no array
+               reportBug(__FUNCTION__, __LINE__, 'sel,value[]=' . gettype($value) . '!=array');
+       } // END - if
+
+       // Add always missing ext-sql_patches
+       $value['sql_patches'] = '1';
+
+       // Default is fine
+       $isValid = TRUE;
+
+       // "Walk" through all extensions
+       foreach ($value as $ext_name => $sel) {
+               // Is this extension choosen?
+               if ($sel != '1') {
+                       // Skip this
+                       continue;
+               } // END - if
+
+               // Can it be loaded?
+               $isValid = (($isValid) && (loadExtension($ext_name, 'test', '0.0.0', TRUE)));
+       } // END - foreach
+
+       // Remove 'sel' from POST data as it cannot be saved
+       unsetPostRequestElement('sel');
+
+       // Save it in session (sorry to do that here :( )
+       setSession('extensions', implode(':', array_keys($value)));
+
+       // Return result
+       return $isValid;
+}
+
 //-----------------------------------------------------------------------------
 //                 Call-back functions to post-check validity
 //-----------------------------------------------------------------------------
@@ -569,11 +739,14 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
                return FALSE;
        } // END - if
 
+       // Remove any previous flag
+       unsetSqlLinkUp(__FUNCTION__, __LINE__);
+
        // Try to connect to the database
-       $linkResource = SQL_CONNECT(postRequestElement('mysql_host'), postRequestElement('mysql_login'), postRequestElement('mysql_password1'), __FUNCTION__, __LINE__);
+       sqlConnectToDatabase(postRequestElement('mysql_host'), postRequestElement('mysql_login'), postRequestElement('mysql_password1'), __FUNCTION__, __LINE__);
 
        // Is the link up
-       if (!is_resource($linkResource)) {
+       if (!isSqlLinkUp()) {
                // Cannot connect to database
                $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_DATABASE_CONNECT_ERROR--}';
                array_push($GLOBALS['installer_failed_fields'][$currentTab], 'mysql_login', 'mysql_password1', 'mysql_password2');
@@ -581,13 +754,13 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
        } // END - if
 
        // Then attempt to select the database
-       if (!SQL_SELECT_DB(postRequestElement('mysql_dbase'), __FUNCTION__, __LINE__)) {
+       if (!sqlSelectDatabase(postRequestElement('mysql_dbase'), __FUNCTION__, __LINE__)) {
                // Could not find database
                $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_DATABASE_SELECT_FAILED--}';
                array_push($GLOBALS['installer_failed_fields'][$currentTab], 'mysql_dbase');
 
                // Disconnect here, we don't need idle database connections laying around
-               SQL_CLOSE(__FUNCTION__, __LINE__);
+               sqlCloseLink(__FUNCTION__, __LINE__);
 
                // Abort here
                return FALSE;
@@ -607,17 +780,17 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
                array_push($GLOBALS['installer_failed_fields'][$currentTab], 'mysql_engine');
 
                // Disconnect here, we don't need idle database connections laying around
-               SQL_CLOSE(__FUNCTION__, __LINE__);
+               sqlCloseLink(__FUNCTION__, __LINE__);
 
                // Abort here
                return FALSE;
-       } elseif (count($engines) == 0) {
+       } elseif (!isFilledArray($engines)) {
                // No engine is active
                $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_DATABASE_NO_ENGINES_ACTIVE--}';
                array_push($GLOBALS['installer_failed_fields'][$currentTab], 'mysql_engine');
 
                // Disconnect here, we don't need idle database connections laying around
-               SQL_CLOSE(__FUNCTION__, __LINE__);
+               sqlCloseLink(__FUNCTION__, __LINE__);
 
                // Abort here
                return FALSE;
@@ -643,7 +816,7 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
                array_push($GLOBALS['installer_failed_fields'][$currentTab], 'mysql_engine');
 
                // Disconnect here, we don't need idle database connections laying around
-               SQL_CLOSE(__FUNCTION__, __LINE__);
+               sqlCloseLink(__FUNCTION__, __LINE__);
 
                // Abort here
                return FALSE;
@@ -699,7 +872,7 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
        $isValid = (count($tables) == $missingTables);
 
        // Disconnect here, we don't need idle database connections laying around
-       SQL_CLOSE(__FUNCTION__, __LINE__);
+       sqlCloseLink(__FUNCTION__, __LINE__);
 
        // If the status is true, disconnect the database
        if ($isValid === FALSE) {
@@ -715,6 +888,55 @@ function isInstallerPostDatabaseConfigValid ($currentTab) {
        return $isValid;
 }
 
+// Call-back function to check if first admin data is valid
+function isInstallerPostFirstAdminValid ($currentTab) {
+       // Is all data valid?
+       if (!isPostRequestElementSet('admin_login')) {
+               // Login not set
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_LOGIN_EMPTY--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_login');
+               return FALSE;
+       } elseif (strlen(postRequestElement('admin_login')) < 4) {
+               // Login to short
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_LOGIN_SHORT--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_login');
+               return FALSE;
+       } elseif (!isPostRequestElementSet('admin_email')) {
+               // Email address not set
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_EMAIL_EMPTY--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_email');
+               return FALSE;
+       } elseif (!isInstallerAdminEmailValid(postRequestElement('admin_email'))) {
+               // Invalid email address
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_EMAIL_INVALID--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_email');
+               return FALSE;
+       } elseif ((!isPostRequestElementSet('admin_password1')) && (isPostRequestElementSet('admin_password2'))) {
+               // Password 1 not set
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_PASSWORD1_EMPTY--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_password1');
+               return FALSE;
+       } elseif ((isPostRequestElementSet('admin_password1')) && (!isPostRequestElementSet('admin_password2'))) {
+               // Password 2 not set
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_PASSWORD2_EMPTY--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_password2');
+               return FALSE;
+       } elseif (postRequestElement('admin_password1') != postRequestElement('admin_password2')) {
+               // Passwords mismatch
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_PASSWORDS_MISMATCH--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_password1', 'admin_password2');
+               return FALSE;
+       } elseif (!isStrongPassword(postRequestElement('admin_password1'))) {
+               // Weak passwords entered
+               $GLOBALS['installer_post_error'][$currentTab] = '{--INSTALLER_POST_FIRST_ADMIN_PASSWORDS_WEAK--}';
+               array_push($GLOBALS['installer_failed_fields'][$currentTab], 'admin_password1', 'admin_password2');
+               return FALSE;
+       } // END - if
+
+       // All tests passed
+       return TRUE;
+}
+
 // Store given tab in session
 function installTabOkay ($currentTab) {
        // Is it set?
@@ -771,7 +993,13 @@ function isInstallerOutputModeValid ($currentTab) {
 // Wrapper to import given installation SQL dump
 function importInstallSqlDump ($dumpName) {
        // Import the file
-       importSqlDump('install', $dumpName);
+       importSqlDump('install', $dumpName, 'install');
+}
+
+// Wrapper to check if tables.sql and menu-foo.sql are readable
+function isInstallerSqlsReadable ($path) {
+       // Determine it
+       return ((isFileReadable($path . 'install/tables.sql')) && (isFileReadable($path . 'install/menu-' . getLanguage() . '.sql')));
 }
 
 // [EOF]