- md5
- e21ebe61dc5ff3718b5b45866fea866c
- sha1
- 311ce207a6047b3480760a1270324fac98cafd8d
- sha256
- 0fc438b680b955c22cef472e83992ddd69c5523ce1285046cf9b6145311a061d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
<?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
*/
defined('_JEXEC') or die;
use Joomla\Utilities\ArrayHelper;
/**
* FTP configuration model for the Joomla Core Installer.
*
* @since 3.1
*/
class InstallationModelFtp extends JModelBase
{
/**
* Find the ftp filesystem root for a given user/pass pair.
*
* @param array $options Configuration options.
*
* @return mixed FTP root for given FTP user, or boolean false if not found.
*
* @since 3.1
*/
public function detectFtpRoot($options)
{
// Get the options as an object for easier handling.
$options = ArrayHelper::toObject($options);
// Connect and login to the FTP server.
// Use binary transfer mode to be able to compare files.
@$ftp = JClientFtp::getInstance($options->get('ftp_host'), $options->get('ftp_port'), array('type' => FTP_BINARY));
// Check to make sure FTP is connected and authenticated.
if (!$ftp->isConnected())
{
JFactory::getApplication()->enqueueMessage(
$options->get('ftp_host') . ':' . $options->get('ftp_port') . ' ' . JText::_('INSTL_FTP_NOCONNECT'), 'error'
);
return false;
}
if (!$ftp->login($options->get('ftp_user'), $options->get('ftp_pass_plain')))
{
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOLOGIN'), 'error');
return false;
}
// Get the current working directory from the FTP server.
$cwd = $ftp->pwd();
if ($cwd === false)
{
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOPWD'), 'error');
return false;
}
$cwd = rtrim($cwd, '/');
// Get a list of folders in the current working directory.
$cwdFolders = $ftp->listDetails(null, 'folders');
if ($cwdFolders === false || count($cwdFolders) === 0)
{
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NODIRECTORYLISTING'), 'error');
return false;
}
// Get just the folder names from the list of folder data.
for ($i = 0, $n = count($cwdFolders); $i < $n; $i++)
{
$cwdFolders[$i] = $cwdFolders[$i]['name'];
}
// Check to see if Joomla is installed at the FTP current working directory.
$paths = array();
$known = array('administrator', 'components', 'installation', 'language', 'libraries', 'plugins');
if (count(array_diff($known, $cwdFolders)) === 0)
{
$paths[] = $cwd . '/';
}
// Search through the segments of JPATH_SITE looking for root possibilities.
$parts = explode(DIRECTORY_SEPARATOR, JPATH_SITE);
$tmp = '';
for ($i = count($parts) - 1; $i >= 0; $i--)
{
$tmp = '/' . $parts[$i] . $tmp;
if (in_array($parts[$i], $cwdFolders, true))
{
$paths[] = $cwd . $tmp;
}
}
// Check all possible paths for the real Joomla installation by comparing version files.
$rootPath = false;
$checkValue = file_get_contents(JPATH_LIBRARIES . '/cms/version/version.php');
foreach ($paths as $tmp)
{
$filePath = rtrim($tmp, '/') . '/libraries/cms/version/version.php';
$buffer = null;
@$ftp->read($filePath, $buffer);
if ($buffer === $checkValue)
{
$rootPath = $tmp;
break;
}
}
// Close the FTP connection.
$ftp->quit();
// Return an error if no root path was found.
if ($rootPath === false)
{
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_UNABLE_DETECT_ROOT_FOLDER'), 'error');
return false;
}
return $rootPath;
}
/**
* Verify the FTP settings as being functional and correct.
*
* @param array $options Configuration options.
*
* @return mixed FTP root for given FTP user, or boolean false if not found.
*
* @since 3.1
*/
public function verifyFtpSettings($options)
{
// Get the options as an object for easier handling.
$options = ArrayHelper::toObject($options);
// Connect and login to the FTP server.
@$ftp = JClientFtp::getInstance($options->get('ftp_host'), $options->get('ftp_port'));
// Check to make sure FTP is connected and authenticated.
if (!$ftp->isConnected())
{
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOCONNECT'), 'error');
return false;
}
if (!$ftp->login($options->get('ftp_user'), $options->get('ftp_pass_plain')))
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOLOGIN'), 'error');
return false;
}
// Since the root path will be trimmed when it gets saved to configuration.php,
// we want to test with the same value as well.
$root = rtrim($options->get('ftp_root'), '/');
// Verify PWD function.
if ($ftp->pwd() === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOPWD'), 'error');
return false;
}
// Verify root path exists.
if (!$ftp->chdir($root))
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOROOT'), 'error');
return false;
}
// Verify NLST function.
if (($rootList = $ftp->listNames()) === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NONLST'), 'error');
return false;
}
// Verify LIST function.
if ($ftp->listDetails() === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOLIST'), 'error');
return false;
}
// Verify SYST function.
if ($ftp->syst() === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOSYST'), 'error');
return false;
}
// Verify valid root path, part one.
$checkList = array('robots.txt', 'index.php');
if (count(array_diff($checkList, $rootList)))
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_INVALIDROOT'), 'error');
return false;
}
// Verify RETR function
$buffer = null;
if ($ftp->read($root . '/libraries/cms/version/version.php', $buffer) === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NORETR'), 'error');
return false;
}
// Verify valid root path, part two.
$checkValue = file_get_contents(JPATH_ROOT . '/libraries/cms/version/version.php');
if ($buffer !== $checkValue)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_INVALIDROOT'), 'error');
return false;
}
// Verify STOR function.
if ($ftp->create($root . '/ftp_testfile') === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOSTOR'), 'error');
return false;
}
// Verify DELE function.
if ($ftp->delete($root . '/ftp_testfile') === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NODELE'), 'error');
return false;
}
// Verify MKD function.
if ($ftp->mkdir($root . '/ftp_testdir') === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NOMKD'), 'error');
return false;
}
// Verify RMD function.
if ($ftp->delete($root . '/ftp_testdir') === false)
{
$ftp->quit();
JFactory::getApplication()->enqueueMessage(JText::_('INSTL_FTP_NORMD'), 'error');
return false;
}
$ftp->quit();
return true;
}
}
The same content is in 30 releases, Joomla 3.9.15-rc to 3.10.0-alpha1. See them all, or every version of this file.