CoreFiles by mySites.guru
DatabaseModel.php779 lines, 20,296 bytes, from Joomla_4.0.0-beta1-Beta-Full_Package.zip
Raw History Compare with 3.9.0-beta4
md5
bc2c23d5cd010a4c0e1db14cf15583a7
sha1
3735014da3385e7363fd80aa6e3d19b91d44f82a
sha256
c1f7547b7baca5c3622907f997b5a59180abd645447caaaba210438c7c2c748d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
<?php
/**
 * @package     Joomla.Installation
 * @subpackage  Model
 *
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Installation\Model;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Installation\Helper\DatabaseHelper;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\User\UserHelper;
use Joomla\CMS\Version;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Utilities\ArrayHelper;

/**
 * Database configuration model for the Joomla Core Installer.
 *
 * @since  3.1
 */
class DatabaseModel extends BaseInstallationModel
{
	/**
	 * Get the current setup options from the session.
	 *
	 * @return  array  An array of options from the session.
	 *
	 * @since   4.0.0
	 */
	public function getOptions()
	{
		return Factory::getSession()->get('setup.options', array());
	}

	/**
	 * Method to initialise the database.
	 *
	 * @param   boolean  $select  Select the database when creating the connections.
	 *
	 * @return  DatabaseInterface|boolean  Database object on success, boolean false on failure
	 *
	 * @since   3.1
	 */
	public function initialise($select = true)
	{
		$options = $this->getOptions();

		// 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);
		}
		// Pre-load en-GB in case the chosen language files do not exist.
		else
		{
			$lang->load('joomla', JPATH_ADMINISTRATOR, 'en-GB', true);
		}

		// Ensure a database type was selected.
		if (empty($options->db_type))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_INVALID_TYPE'), 'warning');

			return false;
		}

		// Ensure that a hostname and user name were input.
		if (empty($options->db_host) || empty($options->db_user))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_INVALID_DB_DETAILS'), 'warning');

			return false;
		}

		// Validate database table prefix.
		if (isset($options->db_prefix) && !preg_match('#^[a-zA-Z]+[a-zA-Z0-9_]*$#', $options->db_prefix))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_PREFIX_MSG'), 'warning');

			return false;
		}

		// Validate length of database table prefix.
		if (isset($options->db_prefix) && strlen($options->db_prefix) > 15)
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_FIX_TOO_LONG'), 'warning');

			return false;
		}

		// Validate length of database name.
		if (strlen($options->db_name) > 64)
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_NAME_TOO_LONG'), 'warning');

			return false;
		}

		// Validate database name.
		if (in_array($options->db_type, ['pgsql', 'postgresql'], true) && !preg_match('#^[a-zA-Z_][0-9a-zA-Z_$]*$#', $options->db_name))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_NAME_MSG_POSTGRESQL'), 'warning');

			return false;
		}

		if (in_array($options->db_type, ['mysql', 'mysqli']) && preg_match('#[\\\\\/\.]#', $options->db_name))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_NAME_MSG_MYSQL'), 'warning');

			return false;
		}

		// Workaround for UPPERCASE table prefix for PostgreSQL
		if (in_array($options->db_type, ['pgsql', 'postgresql']))
		{
			if (isset($options->db_prefix) && strtolower($options->db_prefix) !== $options->db_prefix)
			{
				Factory::getApplication()->enqueueMessage(Text::_('INSTL_DATABASE_FIX_LOWERCASE'), 'warning');

				return false;
			}
		}

		// Security check for remote db hosts: Check env var if disabled
		$shouldCheckLocalhost = getenv('JOOMLA_INSTALLATION_DISABLE_LOCALHOST_CHECK') !== '1';

		// Per Default allowed DB Hosts
		$localhost = array(
			'localhost',
			'127.0.0.1',
			'::1',
		);

		// Check the security file if the db_host is not localhost / 127.0.0.1 / ::1
		if ($shouldCheckLocalhost && !in_array($options->db_host, $localhost))
		{
			$remoteDbFileTestsPassed = Factory::getSession()->get('remoteDbFileTestsPassed', false);

			// When all checks have been passed we don't need to do this here again.
			if ($remoteDbFileTestsPassed === false)
			{
				$generalRemoteDatabaseMessage = Text::sprintf(
					'INSTL_DATABASE_HOST_IS_NOT_LOCALHOST_GENERAL_MESSAGE',
					'https://docs.joomla.org/Special:MyLanguage/J3.x:Secured_procedure_for_installing_Joomla_with_a_remote_database'
				);

				$remoteDbFile = Factory::getSession()->get('remoteDbFile', false);

				if ($remoteDbFile === false)
				{
					// Add the general message
					Factory::getApplication()->enqueueMessage($generalRemoteDatabaseMessage, 'warning');

					// This is the file you need to remove if you want to use a remote database
					$remoteDbFile = '_Joomla' . UserHelper::genRandomPassword(21) . '.txt';
					Factory::getSession()->set('remoteDbFile', $remoteDbFile);

					// Get the path
					$remoteDbPath = JPATH_INSTALLATION . '/' . $remoteDbFile;

					// When the path is not writable the user needs to create the file manually
					if (!File::write($remoteDbPath, ''))
					{
						// Request to create the file manually
						Factory::getApplication()->enqueueMessage(
							Text::sprintf(
								'INSTL_DATABASE_HOST_IS_NOT_LOCALHOST_CREATE_FILE',
								$remoteDbFile,
								'installation',
								Text::_('INSTL_INSTALL_JOOMLA')
							),
							'notice'
						);

						Factory::getSession()->set('remoteDbFileUnwritable', true);

						return false;
					}

					// Save the file name to the session
					Factory::getSession()->set('remoteDbFileWrittenByJoomla', true);

					// Request to delete that file
					Factory::getApplication()->enqueueMessage(
						Text::sprintf(
							'INSTL_DATABASE_HOST_IS_NOT_LOCALHOST_DELETE_FILE',
							$remoteDbFile,
							'installation',
							Text::_('INSTL_INSTALL_JOOMLA')
						),
						'notice'
					);

					return false;
				}

				if (Factory::getSession()->get('remoteDbFileWrittenByJoomla', false) === true
					&& File::exists(JPATH_INSTALLATION . '/' . $remoteDbFile))
				{
					// Add the general message
					Factory::getApplication()->enqueueMessage($generalRemoteDatabaseMessage, 'warning');

					// Request to delete the file
					Factory::getApplication()->enqueueMessage(
						Text::sprintf(
							'INSTL_DATABASE_HOST_IS_NOT_LOCALHOST_DELETE_FILE',
							$remoteDbFile,
							'installation',
							Text::_('INSTL_INSTALL_JOOMLA')
						),
						'notice'
					);

					return false;
				}

				if (Factory::getSession()->get('remoteDbFileUnwritable', false) === true && !File::exists(JPATH_INSTALLATION . '/' . $remoteDbFile))
				{
					// Add the general message
					Factory::getApplication()->enqueueMessage($generalRemoteDatabaseMessage, 'warning');

					// Request to create the file manually
					Factory::getApplication()->enqueueMessage(
						Text::sprintf(
							'INSTL_DATABASE_HOST_IS_NOT_LOCALHOST_CREATE_FILE',
							$remoteDbFile,
							'installation',
							Text::_('INSTL_INSTALL_JOOMLA')
						),
						'notice'
					);

					return false;
				}

				// All tests for this session passed set it to the session
				Factory::getSession()->set('remoteDbFileTestsPassed', true);
			}
		}

		// Get a database object.
		try
		{
			return DatabaseHelper::getDbo(
				$options->db_type,
				$options->db_host,
				$options->db_user,
				$options->db_pass_plain,
				$options->db_name,
				$options->db_prefix,
				$select,
				DatabaseHelper::getEncryptionSettings($options)
			);
		}
		catch (\RuntimeException $e)
		{
			Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'error');

			return false;
		}
	}

	/**
	 * Method to create a new database.
	 *
	 * @return  boolean
	 *
	 * @since   3.1
	 * @throws  \RuntimeException
	 */
	public function createDatabase()
	{
		$options = (object) $this->getOptions();

		$db = $this->initialise(false);

		if ($db === false)
		{
			// Error messages are enqueued by the initialise function, we just need to tell the controller how to redirect
			return false;
		}

		// Check database version.
		$type = $options->db_type;

		try
		{
			$db_version = $db->getVersion();
		}
		catch (\RuntimeException $e)
		{
			/*
			 * We may get here if the database doesn't exist, if so then explain that to users instead of showing the database connector's error
			 * This only supports PDO PostgreSQL and the PDO MySQL drivers presently
			 *
			 * Error Messages:
			 * PDO MySQL: [1049] Unknown database 'database_name'
			 * PDO PostgreSQL: database "database_name" does not exist
			 */
			if ($type === 'mysql' && strpos($e->getMessage(), '[1049] Unknown database') === 42
				|| $type === 'pgsql' && strpos($e->getMessage(), 'database "' . $options->db_name . '" does not exist'))
			{
				/*
				 * Now we're really getting insane here; we're going to try building a new JDatabaseDriver instance
				 * in order to trick the connection into creating the database
				 */
				if ($type === 'mysql')
				{
					// MySQL (PDO): Don't specify database name
					$altDBoptions = array(
						'driver'   => $options->db_type,
						'host'     => $options->db_host,
						'user'     => $options->db_user,
						'password' => $options->db_pass_plain,
						'prefix'   => $options->db_prefix,
						'select'   => $options->db_select,
						DatabaseHelper::getEncryptionSettings($options),
					);
				}
				else
				{
					// PostgreSQL (PDO): Use 'postgres'
					$altDBoptions = array(
						'driver'   => $options->db_type,
						'host'     => $options->db_host,
						'user'     => $options->db_user,
						'password' => $options->db_pass_plain,
						'database' => 'postgres',
						'prefix'   => $options->db_prefix,
						'select'   => $options->db_select,
						DatabaseHelper::getEncryptionSettings($options),
					);
				}

				$altDB = DatabaseDriver::getInstance($altDBoptions);

				// Try to create the database now using the alternate driver
				try
				{
					$this->createDb($altDB, $options, $altDB->hasUTFSupport());
				}
				catch (\RuntimeException $e)
				{
					// We did everything we could
					throw new \RuntimeException(Text::_('INSTL_DATABASE_COULD_NOT_CREATE_DATABASE'), 500, $e);
				}

				// If we got here, the database should have been successfully created, now try one more time to get the version
				try
				{
					$db_version = $db->getVersion();
				}
				catch (\RuntimeException $e)
				{
					// We did everything we could
					throw new \RuntimeException(Text::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 500, $e);
				}
			}
			// Anything getting into this part of the conditional either doesn't support manually creating the database or isn't that type of error
			else
			{
				throw new \RuntimeException(Text::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 500, $e);
			}
		}

		// Get required database version
		$minDbVersionRequired = DatabaseHelper::getMinimumServerVersion($db, $options);

		// Check minimum database version
		if (version_compare($db_version, $minDbVersionRequired) < 0)
		{
			if (in_array($type, ['mysql', 'mysqli']) && $db->isMariaDb())
			{
				throw new \RuntimeException(
					Text::sprintf(
						'INSTL_DATABASE_INVALID_MARIADB_VERSION',
						$minDbVersionRequired,
						$db_version
					)
				);
			}
			else
			{
				throw new \RuntimeException(
					Text::sprintf(
						'INSTL_DATABASE_INVALID_' . strtoupper($type) . '_VERSION',
						$minDbVersionRequired,
						$db_version
					)
				);
			}
		}

		// Check database connection encryption
		if ($options->db_encryption !== 0 && empty($db->getConnectionEncryption()))
		{
			if ($db->isConnectionEncryptionSupported())
			{
				throw new \RuntimeException(Text::_('INSTL_DATABASE_ENCRYPTION_MSG_CONN_NOT_ENCRYPT'));
			}
			else
			{
				throw new \RuntimeException(Text::_('INSTL_DATABASE_ENCRYPTION_MSG_SRV_NOT_SUPPORTS'));
			}
		}

		// @internal Check for spaces in beginning or end of name.
		if (strlen(trim($options->db_name)) <> strlen($options->db_name))
		{
			throw new \RuntimeException(Text::_('INSTL_DATABASE_NAME_INVALID_SPACES'));
		}

		// @internal Check for asc(00) Null in name.
		if (strpos($options->db_name, chr(00)) !== false)
		{
			throw new \RuntimeException(Text::_('INSTL_DATABASE_NAME_INVALID_CHAR'));
		}

		// Get database's UTF support.
		$utfSupport = $db->hasUTFSupport();

		// Try to select the database.
		try
		{
			$db->select($options->db_name);
		}
		catch (\RuntimeException $e)
		{
			// If the database could not be selected, attempt to create it and then select it.
			if (!$this->createDb($db, $options, $utfSupport))
			{
				throw new \RuntimeException(Text::sprintf('INSTL_DATABASE_ERROR_CREATE', $options->db_name), 500, $e);
			}

			$db->select($options->db_name);
		}

		// Set the character set to UTF-8 for pre-existing databases.
		try
		{
			$db->alterDbCharacterSet($options->db_name);
		}
		catch (\RuntimeException $e)
		{
			// Continue Anyhow
		}

		$options = (array) $options;

		// Remove *_errors value.
		foreach ($options as $i => $option)
		{
			if (isset($i['1']) && $i['1'] == '*')
			{
				unset($options[$i]);

				break;
			}
		}

		$options = array_merge(['db_created' => 1], $options);

		Factory::getSession()->set('setup.options', $options);

		return true;
	}

	/**
	 * Method to process the old database.
	 *
	 * @return  boolean  True on success.
	 *
	 * @since   3.1
	 */
	public function handleOldDatabase()
	{
		$options = $this->getOptions();

		if (!isset($options['db_created']) || !$options['db_created'])
		{
			return $this->createDatabase($options);
		}

		// Get the options as an object for easier handling.
		$options = ArrayHelper::toObject($options);

		if (!$db = $this->initialise())
		{
			return false;
		}

		// Set the character set to UTF-8 for pre-existing databases.
		try
		{
			$db->alterDbCharacterSet($options->db_name);
		}
		catch (\RuntimeException $e)
		{
			// Continue Anyhow
		}

		// Backup any old database.
		if (!$this->backupDatabase($db, $options->db_prefix))
		{
			return false;
		}

		return true;
	}

	/**
	 * Method to create the database tables.
	 *
	 * @param   string  $schema  The SQL schema file to apply.
	 *
	 * @return  boolean  True on success.
	 *
	 * @since   3.1
	 */
	public function createTables($schema)
	{
		if (!$db = $this->initialise())
		{
			return false;
		}

		$serverType = $db->getServerType();

		// Set the appropriate schema script based on UTF-8 support.
		$schemaFile = 'sql/' . $serverType . '/' . $schema . '.sql';

		// Check if the schema is a valid file
		if (!is_file($schemaFile))
		{
			Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_ERROR_DB', Text::_('INSTL_DATABASE_NO_SCHEMA')), 'error');

			return false;
		}

		// Attempt to import the database schema.
		if (!$this->populateDatabase($db, $schemaFile))
		{
			return false;
		}

		return true;
	}

	/**
	 * Method to backup all tables in a database with a given prefix.
	 *
	 * @param   DatabaseDriver  $db      JDatabaseDriver object.
	 * @param   string          $prefix  Database table prefix.
	 *
	 * @return  boolean  True on success.
	 *
	 * @since    3.1
	 */
	public function backupDatabase($db, $prefix)
	{
		$return = true;
		$backup = 'bak_' . $prefix;

		// Get the tables in the database.
		$tables = $db->getTableList();

		if ($tables)
		{
			foreach ($tables as $table)
			{
				// If the table uses the given prefix, back it up.
				if (strpos($table, $prefix) === 0)
				{
					// Backup table name.
					$backupTable = str_replace($prefix, $backup, $table);

					// Drop the backup table.
					try
					{
						$db->dropTable($backupTable, true);
					}
					catch (\RuntimeException $e)
					{
						Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_DATABASE_ERROR_BACKINGUP', $e->getMessage()), 'error');

						$return = false;
					}

					// Rename the current table to the backup table.
					try
					{
						$db->renameTable($table, $backupTable, $backup, $prefix);
					}
					catch (\RuntimeException $e)
					{
						Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_DATABASE_ERROR_BACKINGUP', $e->getMessage()), 'error');

						$return = false;
					}
				}
			}
		}

		return $return;
	}

	/**
	 * Method to create a new database.
	 *
	 * @param   DatabaseDriver  $db       Database object.
	 * @param   CMSObject       $options  CMSObject coming from "initialise" function to pass user
	 *                                    and database name to database driver.
	 * @param   boolean         $utf      True if the database supports the UTF-8 character set.
	 *
	 * @return  boolean  True on success.
	 *
	 * @since   3.1
	 */
	public function createDb($db, $options, $utf)
	{
		// Build the create database query.
		try
		{
			// Run the create database query.
			$db->createDatabase($options, $utf);
		}
		catch (\RuntimeException $e)
		{
			// If an error occurred return false.
			return false;
		}

		return true;
	}

	/**
	 * Method to import a database schema from a file.
	 *
	 * @param   \Joomla\Database\DatabaseInterface  $db      JDatabase object.
	 * @param   string                              $schema  Path to the schema file.
	 *
	 * @return  boolean  True on success.
	 *
	 * @since   3.1
	 */
	public function populateDatabase($db, $schema)
	{
		$return = true;

		// Get the contents of the schema file.
		if (!($buffer = file_get_contents($schema)))
		{
			Factory::getApplication()->enqueueMessage(Text::_('INSTL_SAMPLE_DATA_NOT_FOUND'), 'error');

			return false;
		}

		// Get an array of queries from the schema and process them.
		$queries = $this->splitQueries($buffer);

		foreach ($queries as $query)
		{
			// Trim any whitespace.
			$query = trim($query);

			// If the query isn't empty and is not a MySQL or PostgreSQL comment, execute it.
			if (!empty($query) && ($query[0] != '#') && ($query[0] != '-'))
			{
				// Execute the query.
				$db->setQuery($query);

				try
				{
					$db->execute();
				}
				catch (\RuntimeException $e)
				{
					Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');

					$return = false;
				}
			}
		}

		return $return;
	}

	/**
	 * Method to split up queries from a schema file into an array.
	 *
	 * @param   string  $query  SQL schema.
	 *
	 * @return  array  Queries to perform.
	 *
	 * @since   3.1
	 */
	protected function splitQueries($query)
	{
		$buffer    = array();
		$queries   = array();
		$in_string = false;

		// Trim any whitespace.
		$query = trim($query);

		// Remove comment lines.
		$query = preg_replace("/\n\#[^\n]*/", '', "\n" . $query);

		// Remove PostgreSQL comment lines.
		$query = preg_replace("/\n\--[^\n]*/", '', "\n" . $query);

		// Find function.
		$funct = explode('CREATE OR REPLACE FUNCTION', $query);

		// Save sql before function and parse it.
		$query = $funct[0];

		// Parse the schema file to break up queries.
		for ($i = 0; $i < strlen($query) - 1; $i++)
		{
			if ($query[$i] == ';' && !$in_string)
			{
				$queries[] = substr($query, 0, $i);
				$query     = substr($query, $i + 1);
				$i         = 0;
			}

			if ($in_string && ($query[$i] == $in_string) && $buffer[1] != "\\")
			{
				$in_string = false;
			}
			elseif (!$in_string && ($query[$i] == '"' || $query[$i] == "'") && (!isset($buffer[0]) || $buffer[0] != "\\"))
			{
				$in_string = $query[$i];
			}

			if (isset($buffer[1]))
			{
				$buffer[0] = $buffer[1];
			}

			$buffer[1] = $query[$i];
		}

		// If the is anything left over, add it to the queries.
		if (!empty($query))
		{
			$queries[] = $query;
		}

		// Add function part as is.
		for ($f = 1, $fMax = count($funct); $f < $fMax; $f++)
		{
			$queries[] = 'CREATE OR REPLACE FUNCTION ' . $funct[$f];
		}

		return $queries;
	}
}

No other release has this exact content. See every version of this file.