/** * Method to initialise the database. * * @param array $options The options to use for configuration. * * @return JDatabaseDriver|boolean Database object on success, boolean false on failure * * @since 3.1 */ public function initialise($options) { // Get the application. /* @var InstallationApplicationWeb $app */ $app = JFactory::getApplication(); // Get the options as a object for easier handling. $options = JArrayHelper::toObject($options); // Load the back-end language files so that the DB error messages work. $lang = JFactory::getLanguage(); $currentLang = $lang->getTag(); // Load the selected language if (JLanguage::exists($currentLang, JPATH_ADMINISTRATOR)) { $lang->load('joomla', JPATH_ADMINISTRATOR, $currentLang, true); } else { $lang->load('joomla', JPATH_ADMINISTRATOR, 'en-GB', true); } // Ensure a database type was selected. if (empty($options->db_type)) { $app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_TYPE'), 'notice'); return false; } // Ensure that a hostname and user name were input. if (empty($options->db_host) || empty($options->db_user)) { $app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_DB_DETAILS'), 'notice'); return false; } // Ensure that a database name was input. if (empty($options->db_name)) { $app->enqueueMessage(JText::_('INSTL_DATABASE_EMPTY_NAME'), 'notice'); return false; } // Validate database table prefix. if (!preg_match('#^[a-zA-Z]+[a-zA-Z0-9_]*$#', $options->db_prefix)) { $app->enqueueMessage(JText::_('INSTL_DATABASE_PREFIX_MSG'), 'notice'); return false; } // Validate length of database table prefix. if (strlen($options->db_prefix) > 15) { $app->enqueueMessage(JText::_('INSTL_DATABASE_FIX_TOO_LONG'), 'notice'); return false; } // Validate length of database name. if (strlen($options->db_name) > 64) { $app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_TOO_LONG'), 'notice'); return false; } // Workaround for UPPERCASE table prefix for postgresql if ($options->db_type == 'postgresql') { if (strtolower($options->db_prefix) != $options->db_prefix) { $app->enqueueMessage(JText::_('INSTL_DATABASE_FIX_LOWERCASE'), 'notice'); return false; } } // Get a database object. try { return InstallationHelperDatabase::getDbo($options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix, $options->db_select); } catch (RuntimeException $e) { $app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice'); return false; } }
/** * @return boolean * * @since 3.0 */ function _createRootUser($options) { // Get a database object. try { $db = InstallationHelperDatabase::getDBO($options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix); } catch (RuntimeException $e) { $this->setError(JText::sprintf('INSTL_ERROR_CONNECT_DB', $e->getMessage())); } // Create random salt/password for the admin user $salt = JUserHelper::genRandomPassword(32); $crypt = JUserHelper::getCryptedPassword($options->admin_password, $salt); $cryptpass = $crypt . ':' . $salt; // Take the admin user id JLoader::register('InstallationModelDatabase', JPATH_INSTALLATION . '/models/database.php'); $userId = InstallationModelDatabase::getUserId(); // We don't need anymore the randUserId in the session, let's remove it InstallationModelDatabase::resetRandUserId(); // Create the admin user date_default_timezone_set('UTC'); $installdate = date('Y-m-d H:i:s'); $nullDate = $db->getNullDate(); // Sqlsrv change $query = $db->getQuery(true); $query->select('id'); $query->from('#__users'); $query->where('id = ' . $db->quote($userId)); $db->setQuery($query); if ($db->loadResult()) { $query = $db->getQuery(true); $query->update('#__users'); $query->set('name = ' . $db->quote('Super User')); $query->set('username = '******'email = ' . $db->quote($options->admin_email)); $query->set('password = '******'usertype = ' . $db->quote('deprecated')); $query->set('block = 0'); $query->set('sendEmail = 1'); $query->set('registerDate = ' . $db->quote($installdate)); $query->set('lastvisitDate = ' . $db->quote($nullDate)); $query->set('activation = ' . $db->quote('0')); $query->set('params = ' . $db->quote('')); $query->where('id = ' . $db->quote($userId)); } else { $query = $db->getQuery(true); $columns = array($db->quoteName('id'), $db->quoteName('name'), $db->quoteName('username'), $db->quoteName('email'), $db->quoteName('password'), $db->quoteName('usertype'), $db->quoteName('block'), $db->quoteName('sendEmail'), $db->quoteName('registerDate'), $db->quoteName('lastvisitDate'), $db->quoteName('activation'), $db->quoteName('params')); $query->insert('#__users', true); $query->columns($columns); $query->values($db->quote($userId) . ', ' . $db->quote('Super User') . ', ' . $db->quote($options->admin_user) . ', ' . $db->quote($options->admin_email) . ', ' . $db->quote($cryptpass) . ', ' . $db->quote('deprecated') . ', ' . $db->quote('0') . ', ' . $db->quote('1') . ', ' . $db->quote($installdate) . ', ' . $db->quote($nullDate) . ', ' . $db->quote('0') . ', ' . $db->quote('')); } $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $this->setError($e->getMessage()); return false; } // Map the super admin to the Super Admin Group $query = $db->getQuery(true); $query->select('user_id'); $query->from('#__user_usergroup_map'); $query->where('user_id = ' . $db->quote($userId)); $db->setQuery($query); if ($db->loadResult()) { $query = $db->getQuery(true); $query->update('#__user_usergroup_map'); $query->set('user_id = ' . $db->quote($userId)); $query->set('group_id = 8'); } else { $query = $db->getQuery(true); $query->insert('#__user_usergroup_map', false); $query->columns(array($db->quoteName('user_id'), $db->quoteName('group_id'))); $query->values($userId . ', ' . '8'); } $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to create the root user for the site. * * @param object $options The session options. * * @return boolean True on success. * * @since 3.1 */ private function _createRootUser($options) { // Get the application /* @var InstallationApplicationWeb $app */ $app = JFactory::getApplication(); // Get a database object. try { $db = InstallationHelperDatabase::getDbo($options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix); } catch (RuntimeException $e) { $app->enqueueMessage(JText::sprintf('INSTL_ERROR_CONNECT_DB', $e->getMessage()), 'notice'); return false; } $cryptpass = JUserHelper::hashPassword($options->admin_password); // Take the admin user id. $userId = InstallationModelDatabase::getUserId(); // We don't need the randUserId in the session any longer, let's remove it. InstallationModelDatabase::resetRandUserId(); // Create the admin user. date_default_timezone_set('UTC'); $installdate = date('Y-m-d H:i:s'); $nullDate = $db->getNullDate(); // Sqlsrv change. $query = $db->getQuery(true)->select($db->quoteName('id'))->from($db->quoteName('#__users'))->where($db->quoteName('id') . ' = ' . $db->quote($userId)); $db->setQuery($query); if ($db->loadResult()) { $query->clear()->update($db->quoteName('#__users'))->set($db->quoteName('name') . ' = ' . $db->quote('Super User'))->set($db->quoteName('username') . ' = ' . $db->quote(trim($options->admin_user)))->set($db->quoteName('email') . ' = ' . $db->quote($options->admin_email))->set($db->quoteName('password') . ' = ' . $db->quote($cryptpass))->set($db->quoteName('block') . ' = 0')->set($db->quoteName('sendEmail') . ' = 1')->set($db->quoteName('registerDate') . ' = ' . $db->quote($installdate))->set($db->quoteName('lastvisitDate') . ' = ' . $db->quote($nullDate))->set($db->quoteName('activation') . ' = ' . $db->quote('0'))->set($db->quoteName('params') . ' = ' . $db->quote(''))->where($db->quoteName('id') . ' = ' . $db->quote($userId)); } else { $columns = array($db->quoteName('id'), $db->quoteName('name'), $db->quoteName('username'), $db->quoteName('email'), $db->quoteName('password'), $db->quoteName('block'), $db->quoteName('sendEmail'), $db->quoteName('registerDate'), $db->quoteName('lastvisitDate'), $db->quoteName('activation'), $db->quoteName('params')); $query->clear()->insert('#__users', true)->columns($columns)->values($db->quote($userId) . ', ' . $db->quote('Super User') . ', ' . $db->quote(trim($options->admin_user)) . ', ' . $db->quote($options->admin_email) . ', ' . $db->quote($cryptpass) . ', ' . $db->quote('0') . ', ' . $db->quote('1') . ', ' . $db->quote($installdate) . ', ' . $db->quote($nullDate) . ', ' . $db->quote('0') . ', ' . $db->quote('')); } $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $app->enqueueMessage($e->getMessage(), 'notice'); return false; } // Map the super admin to the Super Admin Group $query->clear()->select($db->quoteName('user_id'))->from($db->quoteName('#__user_usergroup_map'))->where($db->quoteName('user_id') . ' = ' . $db->quote($userId)); $db->setQuery($query); if ($db->loadResult()) { $query->clear()->update($db->quoteName('#__user_usergroup_map'))->set($db->quoteName('user_id') . ' = ' . $db->quote($userId))->set($db->quoteName('group_id') . ' = 8'); } else { $query->clear()->insert($db->quoteName('#__user_usergroup_map'), false)->columns(array($db->quoteName('user_id'), $db->quoteName('group_id')))->values($db->quote($userId) . ', 8'); } $db->setQuery($query); try { $db->execute(); } catch (RuntimeException $e) { $app->enqueueMessage($e->getMessage(), 'notice'); return false; } return true; }
/** * @since 3.0 */ function installSampleData($options) { // Get the options as a JObject for easier handling. $options = JArrayHelper::toObject($options, 'JObject'); // Get a database object. try { $db = InstallationHelperDatabase::getDBO($options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix); } catch (RuntimeException $e) { $this->setError(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage())); return false; } // Build the path to the sample data file. $type = $options->db_type; if ($type == 'mysqli') { $type = 'mysql'; } elseif ($type == 'sqlsrv') { $type = 'sqlazure'; } $data = JPATH_INSTALLATION . '/sql/' . $type . '/' . $options->sample_file; // Attempt to import the database schema. if (!file_exists($data)) { $this->setError(JText::sprintf('INSTL_DATABASE_FILE_DOES_NOT_EXIST', $data)); return false; } elseif (!$this->populateDatabase($db, $data)) { $this->setError(JText::sprintf('INSTL_ERROR_DB', $this->getError())); return false; } $this->postInstallSampleData($db); return true; }