- md5
- 81d38d12ffdd902b2b77152e2a7c63be
- sha1
- 7ac397e1301d692b4187f077c49fb950e28deea9
- sha256
- d19dd7813b87cfe8d238fda5c0623661482e9fed866856e83eecfbdd20c4741c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
<?php
/**
* @package Joomla.Installation
* @subpackage Model
*
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Installation\Model;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Installation\Helper\DatabaseHelper;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Setup model for the Joomla Core Installer.
*
* @since 3.1
*/
class SetupModel extends BaseInstallationModel
{
/**
* Store the current setup options in the session.
*
* @param array $options The installation options.
*
* @return array An array of options from the session.
*
* @since 3.1
*/
public function storeOptions($options)
{
// Get the current setup options from the session.
$old = (array) $this->getOptions();
// Ensure that we have language
if (!isset($options['language']) || empty($options['language'])) {
$options['language'] = Factory::getLanguage()->getTag();
}
// Store passwords as a separate key that is not used in the forms
foreach (['admin_password', 'db_pass'] as $passwordField) {
if (isset($options[$passwordField])) {
$plainTextKey = $passwordField . '_plain';
$options[$plainTextKey] = $options[$passwordField];
unset($options[$passwordField]);
}
}
// Get the session
$session = Factory::getApplication()->getSession();
$options['helpurl'] = $session->get('setup.helpurl', null);
// Merge the new setup options into the current ones and store in the session.
$options = array_merge($old, (array) $options);
$session->set('setup.options', $options);
return $options;
}
/**
* Method to get the form.
*
* @param string|null $view The view being processed.
*
* @return Form|boolean JForm object on success, false on failure.
*
* @since 3.1
*/
public function getForm($view = null)
{
if (!$view) {
$view = Factory::getApplication()->getInput()->getWord('view', 'setup');
}
// Get the form.
Form::addFormPath(JPATH_BASE . '/forms');
try {
$form = Form::getInstance('jform', $view, ['control' => 'jform']);
} catch (\Exception $e) {
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return false;
}
/** @todo make this available in web installer too */
if (!Factory::getApplication()->isClient('cli_installation')) {
$form->removeField('public_folder');
}
// Check the session for previously entered form data.
$data = (array) $this->getOptions();
// Bind the form data if present.
if (!empty($data)) {
$form->bind($data);
}
return $form;
}
/**
* Generate a panel of language choices for the user to select their language.
*
* @return array
*
* @since 3.1
*/
public function getLanguages()
{
// Detect the native language.
$native = LanguageHelper::detectLanguage();
if (empty($native)) {
$native = 'en-GB';
}
// Get a forced language if it exists.
$forced = Factory::getApplication()->getLocalise();
if (!empty($forced['language'])) {
$native = $forced['language'];
}
// Get the list of available languages.
$list = LanguageHelper::createLanguageList($native);
if (!$list || $list instanceof \Exception) {
$list = [];
}
return $list;
}
/**
* Method to validate the form data.
*
* @param array $data The form data.
* @param string|null $view The view.
*
* @return array|boolean Array of filtered data if valid, false otherwise.
*
* @since 3.1
*/
public function validate($data, $view = null)
{
// Get the form.
$form = $this->getForm($view);
// Check for an error.
if ($form === false) {
return false;
}
// Filter and validate the form data.
$data = $form->filter($data);
$return = $form->validate($data);
// Check for an error.
if ($return instanceof \Exception) {
Factory::getApplication()->enqueueMessage($return->getMessage(), 'warning');
return false;
}
// Check the validation results.
if ($return === false) {
// Get the validation messages from the form.
$messages = array_reverse($form->getErrors());
foreach ($messages as $message) {
if ($message instanceof \Exception) {
Factory::getApplication()->enqueueMessage($message->getMessage(), 'warning');
} else {
Factory::getApplication()->enqueueMessage($message, 'warning');
}
}
return false;
}
return $data;
}
/**
* Method to validate the db connection properties.
*
* @param array $options Array with database credentials
*
* @return boolean
*
* @since 4.0.0
* @throws \Exception
*/
public function validateDbConnection(array $options)
{
// Get the options as an object for easier handling.
$options = ArrayHelper::toObject($options);
// Load the backend language files so that the DB error messages work.
$lang = Factory::getLanguage();
$currentLang = $lang->getTag();
// Load the selected language
if (LanguageHelper::exists($currentLang, JPATH_ADMINISTRATOR)) {
$lang->load('joomla', JPATH_ADMINISTRATOR, $currentLang, true);
} else {
// Pre-load en-GB in case the chosen language files do not exist.
$lang->load('joomla', JPATH_ADMINISTRATOR, 'en-GB', true);
}
// Validate and clean up connection parameters
$paramsCheck = DatabaseHelper::validateConnectionParameters($options);
if ($paramsCheck) {
// Validation error: Enqueue the error message
Factory::getApplication()->enqueueMessage($paramsCheck, 'error');
return false;
}
// Security check for remote db hosts
if (!DatabaseHelper::checkRemoteDbHost($options)) {
// Messages have been enqueued in the called function.
return false;
}
// Get a database object.
try {
$db = DatabaseHelper::getDbo(
$options->db_type,
$options->db_host,
$options->db_user,
$options->db_pass_plain,
$options->db_name,
$options->db_prefix,
false,
DatabaseHelper::getEncryptionSettings($options)
);
$db->connect();
} catch (\RuntimeException $e) {
if (
$options->db_type === 'mysql' && strpos($e->getMessage(), '[1049] Unknown database') === 42
|| $options->db_type === 'pgsql' && strpos($e->getMessage(), 'database "' . $options->db_name . '" does not exist')
) {
// Database doesn't exist: Skip the below checks, they will be done later at database creation
return true;
}
Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'error');
return false;
}
// Check database server parameters
$dbServerCheck = DatabaseHelper::checkDbServerParameters($db, $options);
if ($dbServerCheck) {
// Some server parameter is not ok: Enqueue the error message
Factory::getApplication()->enqueueMessage($dbServerCheck, 'error');
return false;
}
return true;
}
}
The same content is in 29 releases, Joomla 6.1.0-alpha3 to 6.2.1-rc1. See them all, or every version of this file.