CoreFiles by mySites.guru
filesystem.php565 lines, 13,205 bytes, from Joomla_3.0.0-Stable-Full_Package.zip
Raw History Compare with 2.5.28
md5
845e88e87d6d6fba0cdb3678381a279d
sha1
9444c8cfbad1155a5ec51c261759999ec1095dc5
sha256
5a0ecb1b198bb8f973917ec3d7c4d9d3f31621edba77c89bd18d6880476135cd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
<?php
/**
 * @package    Joomla.Installation
 *
 * @copyright  Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.path');

/**
 * FTP configuration model for the Joomla Core Installer.
 *
 * @package  Joomla.Installation
 * @since    3.0
 */
class InstallationModelFtp extends JModelLegacy
{
	/**
	 * 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.0
	 */
	public function detectFtpRoot($options)
	{
		// Get the options as a object for easier handling.
		$options = JArrayHelper::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())
		{
			$this->setError($options->get('ftp_host') . ':' . $options->get('ftp_port') . ' ' . JText::_('INSTL_FTP_NOCONNECT'));
			return false;
		}
		if (!$ftp->login($options->get('ftp_user'), $options->get('ftp_pass')))
		{
			$this->setError(JText::_('INSTL_FTP_NOLOGIN'));
			return false;
		}

		// Get the current working directory from the FTP server.
		$cwd = $ftp->pwd();
		if ($cwd === false)
		{
			$this->setError(JText::_('INSTL_FTP_NOPWD'));
			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)
		{
			$this->setError(JText::_('INSTL_FTP_NODIRECTORYLISTING'));
			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))
			{
				$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)
		{
			$this->setError(JText::_('INSTL_FTP_UNABLE_DETECT_ROOT_FOLDER'));
			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.0
	 */
	public function verifyFtpSettings($options)
	{
		// Get the options as a object for easier handling.
		$options = JArrayHelper::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())
		{
			$this->setError(JText::_('INSTL_FTP_NOCONNECT'));
			return false;
		}
		if (!$ftp->login($options->get('ftp_user'), $options->get('ftp_pass')))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOLOGIN'));
			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();
			$this->setError(JText::_('INSTL_FTP_NOPWD'));
			return false;
		}

		// Verify root path exists
		if (!$ftp->chdir($root))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOROOT'));
			return false;
		}

		// Verify NLST function
		if (($rootList = $ftp->listNames()) === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NONLST'));
			return false;
		}

		// Verify LIST function
		if ($ftp->listDetails() === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOLIST'));
			return false;
		}

		// Verify SYST function
		if ($ftp->syst() === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOSYST'));
			return false;
		}

		// Verify valid root path, part one
		$checkList = array('robots.txt', 'index.php');
		if (count(array_diff($checkList, $rootList)))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_INVALIDROOT'));
			return false;
		}

		// Verify RETR function
		$buffer = null;
		if ($ftp->read($root . '/libraries/cms/version/version.php', $buffer) === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NORETR'));
			return false;
		}

		// Verify valid root path, part two
		$checkValue = file_get_contents(JPATH_ROOT . '/libraries/cms/version/version.php');
		if ($buffer !== $checkValue)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_INVALIDROOT'));
			return false;
		}

		// Verify STOR function
		if ($ftp->create($root . '/ftp_testfile') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOSTOR'));
			return false;
		}

		// Verify DELE function
		if ($ftp->delete($root . '/ftp_testfile') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NODELE'));
			return false;
		}

		// Verify MKD function
		if ($ftp->mkdir($root . '/ftp_testdir') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOMKD'));
			return false;
		}

		// Verify RMD function
		if ($ftp->delete($root . '/ftp_testdir') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NORMD'));
			return false;
		}

		$ftp->quit();
		return true;
	}

	/**
	 * Check the webserver user permissions for writing files/folders
	 *
	 * @return  boolean  True if correct permissions exist
	 *
	 * @since   3.0
	 */
	public static function checkPermissions()
	{
		if (!is_writable(JPATH_ROOT . '/tmp'))
		{
			return false;
		}
		if (!mkdir(JPATH_ROOT . '/tmp/test', 0755))
		{
			return false;
		}
		if (!copy(JPATH_ROOT . '/tmp/index.html', JPATH_ROOT . 'tmp/test/index.html'))
		{
			return false;
		}
		if (!chmod(JPATH_ROOT . '/tmp/test/index.html', 0777))
		{
			return false;
		}
		if (!unlink(JPATH_ROOT . '/tmp/test/index.html'))
		{
			return false;
		}
		if (!rmdir(JPATH_ROOT . '/tmp/test'))
		{
			return false;
		}

		return true;
	}

	/**
	 * Verify the FTP configuration values are valid
	 *
	 * @param   string  $user  Username of the ftp user to determine root for
	 * @param   string  $pass  Password of the ftp user to determine root for
	 * @param   string  $root
	 * @param   string  $host
	 * @param   string  $port
	 *
	 * @return  mixed   Boolean true on success or JError object on fail
	 *
	 * @since   3.0
	 */
	public function checkSettings($user, $pass, $root, $host = '127.0.0.1', $port = '21')
	{
		$ftp = JClientFtp::getInstance($host, $port);

		// 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($root, '/');

		// Verify connection
		if (!$ftp->isConnected())
		{
			$this->setError('kiki' . JText::_('INSTL_FTP_NOCONNECT'));
			return false;
		}

		// Verify username and password
		if (!$ftp->login($user, $pass))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOLOGIN'));
			return false;
		}

		// Verify PWD function
		if ($ftp->pwd() === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOPWD'));
			return false;
		}

		// Verify root path exists
		if (!$ftp->chdir($root))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOROOT'));
			return false;
		}

		// Verify NLST function
		if (($rootList = $ftp->listNames()) === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NONLST'));
			return false;
		}

		// Verify LIST function
		if ($ftp->listDetails() === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOLIST'));
			return false;
		}

		// Verify SYST function
		if ($ftp->syst() === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOSYST'));
			return false;
		}

		// Verify valid root path, part one
		$checkList = array('CHANGELOG.php', 'COPYRIGHT.php', 'index.php', 'INSTALL.php', 'LICENSE.php');
		if (count(array_diff($checkList, $rootList)))
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_INVALIDROOT'));
			return false;
		}

		// Verify RETR function
		$buffer = null;
		if ($ftp->read($root . '/libraries/cms/version/version.php', $buffer) === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NORETR'));
			return false;
		}

		// Verify valid root path, part two
		$checkValue = file_get_contents(JPATH_ROOT . '/libraries/cms/version/version.php');
		if ($buffer !== $checkValue)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_INVALIDROOT'));
			return false;
		}

		// Verify STOR function
		if ($ftp->create($root . '/ftp_testfile') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOSTOR'));
			return false;
		}

		// Verify DELE function
		if ($ftp->delete($root . '/ftp_testfile') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NODELE'));
			return false;
		}

		// Verify MKD function
		if ($ftp->mkdir($root . '/ftp_testdir') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NOMKD'));
			return false;
		}

		// Verify RMD function
		if ($ftp->delete($root . '/ftp_testdir') === false)
		{
			$ftp->quit();
			$this->setError(JText::_('INSTL_FTP_NORMD'));
			return false;
		}

		return true;
	}

	/**
	 * Set default folder permissions
	 *
	 * @param   string  $folder
	 * @param   string  $options
	 *
	 * @return  boolean  True on success
	 *
	 * @since   3.0
	 */
	public function setFolderPermissions($folder, $options)
	{
		// Get the options as a object for easier handling.
		$options = JArrayHelper::toObject($options);

		$ftpFlag = false;
		$ftpRoot = $options->ftpRoot;

		// Determine if the path is "chmodable".
		if (!JPath::canChmod(JPath::clean(JPATH_SITE . '/' . $folder)))
		{
			$ftpFlag = true;
		}

		// Do NOT use ftp if it is not enabled
		if (empty($options->ftp_enable))
		{
			$ftpFlag = false;
		}

		if ($ftpFlag == true)
		{
			// Connect the FTP client
			$client = JClientFtp::getInstance($options['ftp_host'], $options['ftp_port']);
			$client->login($options['ftp_user'], $options['ftp_pass']);

			// Translate path for the FTP account
			$path = JPath::clean($ftpRoot . "/" . $folder);

			// Chmod using ftp
			if (!$client->chmod($path, '0755'))
			{
				$ret = false;
			}

			$client->quit();
			$ret = true;
		}
		else
		{
			$path = JPath::clean(JPATH_SITE . '/' . $folder);

			if (!@chmod($path, octdec('0755')))
			{
				$ret = false;
			}
			else
			{
				$ret = true;
			}
		}

		return $ret;
	}

	/**
	 * Inserts ftp variables to mainframe registry
	 * Needed to activate ftp layer for file operations in safe mode
	 *
	 * @param   array  $vars  The post values
	 *
	 * @return  void
	 *
	 * @since   3.0
	 */
	public function setFTPCfg($vars)
	{
		$app = JFactory::getApplication();
		$arr = array();
		$arr['ftp_enable'] = $vars['ftp_enable'];
		$arr['ftp_user'] = $vars['ftp_user'];
		$arr['ftp_pass'] = $vars['ftp_pass'];
		$arr['ftp_root'] = $vars['ftp_root'];
		$arr['ftp_host'] = $vars['ftp_host'];
		$arr['ftp_port'] = $vars['ftp_port'];

		$app->setCfg($arr, 'config');
	}

	/**
	 * @since   3.0
	 */
	function _chmod($path, $mode)
	{
		$app = JFactory::getApplication();
		$ret = false;

		$ftpFlag = true;
		$ftpRoot = $app->getCfg('ftp_root');

		// Do NOT use ftp if it is not enabled
		if ($app->getCfg('ftp_enable') != 1)
		{
			$ftpFlag = false;
		}

		if ($ftpFlag == true)
		{
			// Connect the FTP client
			$ftp = JClientFtp::getInstance($app->getCfg('ftp_host'), $app->getCfg('ftp_port'));
			$ftp->login($app->getCfg('ftp_user'), $app->getCfg('ftp_pass'));

			// Translate the destination path for the FTP account
			$path = JPath::clean(str_replace(JPATH_SITE, $ftpRoot, $path), '/');

			// Do the ftp chmod
			if (!$ftp->chmod($path, $mode))
			{
				// FTP connector throws an error
				return false;
			}
			$ftp->quit();
			$ret = true;
		}
		else
		{
			$ret = @chmod($path, $mode);
		}

		return $ret;
	}
}

The same content is in 3 releases, Joomla 3.0.0 to 3.0.2. See them all, or every version of this file.