New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JFile::copy to check realpath #18352

Open
stAn47 opened this Issue Oct 16, 2017 · 1 comment

Comments

Projects
None yet
4 participants
@stAn47
Copy link

stAn47 commented Oct 16, 2017

Steps to reproduce the issue

Hello friends, it would be nice if JFile::copy checked if the src and dest paths are the same. Right now it gives an error and thus installer or other dependencies fail upon such a case.

Usage:
during the development we are using an installer path in an component directory such as /administrator/components/com_test/install/systempluginname

which is symlinked to
/plugins/system/systempluginname

upon changes in the component BE config, it either enables or disables particular plugins that are used per the component's logic, but mainly it makes sure that the plugins is up-to-date by updating it with the Joomla installer.

it would be nice if JFile::copy did not return an error upon same src and dest especially if those directories are symlinked and are seen as the same by "realpath".

i do not really know how to do a PR, but i would suggest:

$src = realpath($src); 
$dest = realpath($dest); 
if ($src === $dest) return true; 

into:
\libraries\joomla\filesystem\file.php

on Joomla 3.8.1

at about line 99

full function would then look like:

/**
	 * Copies a file
	 *
	 * @param   string   $src          The path to the source file
	 * @param   string   $dest         The path to the destination file
	 * @param   string   $path         An optional base path to prefix to the file names
	 * @param   boolean  $use_streams  True to use streams
	 *
	 * @return  boolean  True on success
	 *
	 * @since   11.1
	 */
	public static function copy($src, $dest, $path = null, $use_streams = false)
	{
		
		
		$pathObject = new JFilesystemWrapperPath;

		// Prepend a base path if it exists
		if ($path)
		{
			$src = $pathObject->clean($path . '/' . $src);
			$dest = $pathObject->clean($path . '/' . $dest);
			
		}
		
		$src = realpath($src); 
		$dest = realpath($dest); 
		if ($src === $dest) return true; 
		
		// Check src path
		if (!is_readable($src))
		{
			JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_FIND_COPY', $src), JLog::WARNING, 'jerror');

			return false;
		}

		if ($use_streams)
		{
			$stream = JFactory::getStream();

			if (!$stream->copy($src, $dest))
			{
				JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_STREAMS', $src, $dest, $stream->getError()), JLog::WARNING, 'jerror');

				return false;
			}

			return true;
		}
		else
		{
			$FTPOptions = JClientHelper::getCredentials('ftp');

			if ($FTPOptions['enabled'] == 1)
			{
				// Connect the FTP client
				$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);

				// If the parent folder doesn't exist we must create it
				if (!file_exists(dirname($dest)))
				{
					$folderObject = new JFilesystemWrapperFolder;
					$folderObject->create(dirname($dest));
				}

				// Translate the destination path for the FTP account
				$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');

				if (!$ftp->store($src, $dest))
				{
					// FTP connector throws an error
					return false;
				}

				$ret = true;
			}
			else
			{
				if (!@ copy($src, $dest))
				{
					JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_COPY_FAILED_ERR01', $src, $dest), JLog::WARNING, 'jerror');

					return false;
				}

				$ret = true;
			}

			return $ret;
		}
	}

Expected result

Do not give an error upon same src and dest

Actual result

It now fails...

System information (as much as possible)

Additional comments

@franz-wohlkoenig

This comment has been minimized.

Copy link

franz-wohlkoenig commented Jan 26, 2018

Any Comment on this Feature Request?


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/18352.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment