- md5
- e1ed17c018147c7d734ffe9346a9901d
- sha1
- aa3846aa39d25e3bc875b862eefbc3169f7266da
- sha256
- b4776ecedca74d95f0b325c6b9b96d4bc8dbd4f2b7858543584efddc5f5f3ee2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
<?php
/**
* @package Joomla.Plugin
* @subpackage Filesystem.Local
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\Filesystem\Local\Adapter;
defined('_JEXEC') or die;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Image\Image;
use Joomla\CMS\Uri\Uri;
\JLoader::import('joomla.filesystem.file');
\JLoader::import('joomla.filesystem.folder');
/**
* Local file adapter.
*
* @since __DEPLOY_VERSION__
*/
class LocalAdapter implements AdapterInterface
{
/**
* The root path to gather file information from.
*
* @var string
*
* @since __DEPLOY_VERSION__
*/
private $rootPath = null;
/**
* The file_path of media directory related to site
*
* @var string
*
* @since __DEPLOY_VERSION__
*/
private $filePath = null;
/**
* The absolute root path in the local file system.
*
* @param string $rootPath The root path
* @param string $filePath The file path of media folder
*
* @since __DEPLOY_VERSION__
*/
public function __construct($rootPath, $filePath)
{
if (!file_exists($rootPath))
{
throw new \InvalidArgumentException;
}
$this->rootPath = \JPath::clean($rootPath, '/');
$this->filePath = $filePath;
}
/**
* Returns the requested file or folder. The returned object
* has the following properties available:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* If the path doesn't exist a FileNotFoundException is thrown.
*
* @param string $path The path to the file or folder
*
* @return \stdClass
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function getFile($path = '/')
{
// Set up the path correctly
$basePath = \JPath::clean($this->rootPath . '/' . $path);
// Check if file exists
if (!file_exists($basePath))
{
throw new FileNotFoundException;
}
return $this->getPathInformation($basePath);
}
/**
* Returns the folders and files for the given path. The returned objects
* have the following properties available:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
*
* If the path doesn't exist a FileNotFoundException is thrown.
*
* @param string $path The folder
*
* @return \stdClass[]
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function getFiles($path = '/')
{
// Set up the path correctly
$basePath = \JPath::clean($this->rootPath . '/' . $path);
// Check if file exists
if (!file_exists($basePath))
{
throw new FileNotFoundException;
}
// Check if the path points to a file
if (is_file($basePath))
{
return array($this->getPathInformation($basePath));
}
// The data to return
$data = array();
// Read the folders
foreach (\JFolder::folders($basePath) as $folder)
{
$data[] = $this->getPathInformation(\JPath::clean($basePath . '/' . $folder));
}
// Read the files
foreach (\JFolder::files($basePath) as $file)
{
$data[] = $this->getPathInformation(\JPath::clean($basePath . '/' . $file));
}
// Return the data
return $data;
}
/**
* Creates a folder with the given name in the given path.
*
* @param string $name The name
* @param string $path The folder
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function createFolder($name, $path)
{
\JFolder::create($this->rootPath . $path . '/' . $name);
}
/**
* Creates a file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param string $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function createFile($name, $path, $data)
{
\JFile::write($this->rootPath . $path . '/' . $name, $data);
}
/**
* Updates the file with the given name in the given path with the data.
*
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function updateFile($name, $path, $data)
{
if (!\JFile::exists($this->rootPath . $path . '/' . $name))
{
throw new FileNotFoundException;
}
\JFile::write($this->rootPath . $path . '/' . $name, $data);
}
/**
* Deletes the folder or file of the given path.
*
* @param string $path The path to the file or folder
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
public function delete($path)
{
if (is_file($this->rootPath . $path))
{
if (!\JFile::exists($this->rootPath . $path))
{
throw new FileNotFoundException;
}
$success = \JFile::delete($this->rootPath . $path);
}
else
{
if (!\JFolder::exists($this->rootPath . $path))
{
throw new FileNotFoundException;
}
$success = \JFolder::delete($this->rootPath . $path);
}
if (!$success)
{
throw new \Exception('Delete not possible!');
}
}
/**
* Returns the folder or file information for the given path. The returned object
* has the following properties:
* - type: The type can be file or dir
* - name: The name of the file
* - path: The relative path to the root
* - extension: The file extension
* - size: The size of the file
* - create_date: The date created
* - modified_date: The date modified
* - mime_type: The mime type
* - width: The width, when available
* - height: The height, when available
* - thumb_path The thumbnail path of file, when available
*
* @param string $path The folder
*
* @return \stdClass
*
* @since __DEPLOY_VERSION__
*/
private function getPathInformation($path)
{
// Prepare the path
$path = \JPath::clean($path, '/');
// The boolean if it is a dir
$isDir = is_dir($path);
$createDate = $this->getDate(filectime($path));
$modifiedDate = $this->getDate(filemtime($path));
// Set the values
$obj = new \stdClass;
$obj->type = $isDir ? 'dir' : 'file';
$obj->name = basename($path);
$obj->path = str_replace($this->rootPath, '/', $path);
$obj->extension = !$isDir ? \JFile::getExt($obj->name) : '';
$obj->size = !$isDir ? filesize($path) : '';
$obj->mime_type = MediaHelper::getMimeType($path, MediaHelper::isImage($obj->name));
$obj->width = 0;
$obj->height = 0;
// Dates
$obj->create_date = $createDate->format('c', true);
$obj->create_date_formatted = $createDate->format(\JText::_('DATE_FORMAT_LC5'), true);
$obj->modified_date = $modifiedDate->format('c', true);
$obj->modified_date_formatted = $modifiedDate->format(\JText::_('DATE_FORMAT_LC5'), true);
if (MediaHelper::isImage($obj->name))
{
// Get the image properties
$props = Image::getImageFileProperties($path);
$obj->width = $props->width;
$obj->height = $props->height;
// Todo : Change this path to an actual thumbnail path
$obj->thumb_path = $this->getUrl($obj->path);
}
return $obj;
}
/**
* Returns a JDate with the correct Joomla timezone for the given date.
*
* @param string $date The date to create a JDate from
*
* @return Date[]
*
* @since __DEPLOY_VERSION__
*/
private function getDate($date = null)
{
$dateObj = \JFactory::getDate($date);
$timezone = \JFactory::getApplication()->get('offset');
$user = \JFactory::getUser();
if ($user->id)
{
$userTimezone = $user->getParam('timezone');
if (!empty($userTimezone))
{
$timezone = $userTimezone;
}
}
if ($timezone)
{
$dateObj->setTimezone(new \DateTimeZone($timezone));
}
return $dateObj;
}
/**
* Copies a file or folder to a destination
* If the destination folder or file already exists, it will not overwrite them without
* force.
*
* @param string $sourcePath Source path of the file or directory
* @param string $destinationPath Destination path of the file or directory
* @param bool $force Set true to overwrite files or directories
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws FileNotFoundException
*/
public function copy($sourcePath, $destinationPath, $force = false)
{
// Get absolute paths from relative paths
$sourcePath = \JPath::clean($this->rootPath . $sourcePath, '/');
$destinationPath = \JPath::clean($this->rootPath . $destinationPath, '/');
if (!file_exists($sourcePath))
{
throw new FileNotFoundException;
}
// Check for existence of the file in destination
// if it does not exists simply copy source to destination
if (is_dir($sourcePath))
{
$this->copyFolder($sourcePath, $destinationPath, $force);
}
else
{
$this->copyFile($sourcePath, $destinationPath, $force);
}
}
/**
* Copies a file
*
* @param string $sourcePath Source path of the file or directory
* @param string $destinationPath Destination path of the file or directory
* @param bool $force Set true to overwrite files or directories
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
private function copyFile($sourcePath, $destinationPath, $force = false)
{
if (is_dir($destinationPath))
{
// If the destination is a folder we create a file with the same name as the source
$destinationPath = $destinationPath . '/' . basename($sourcePath);
}
if (file_exists($destinationPath) && !$force)
{
throw new \Exception('Copy file is not possible as destination file already exists');
}
if (!\JFile::copy($sourcePath, $destinationPath))
{
throw new \Exception('Copy file is not possible');
}
}
/**
* Copies a folder
*
* @param string $sourcePath Source path of the file or directory
* @param string $destinationPath Destination path of the file or directory
* @param bool $force Set true to overwrite files or directories
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
private function copyFolder($sourcePath, $destinationPath, $force = false)
{
if (file_exists($destinationPath) && !$force)
{
throw new \Exception('Copy folder is not possible as destination folder already exists');
}
if (is_file($destinationPath) && !\JFile::delete($destinationPath))
{
throw new \Exception('Copy folder is not possible as destination folder is a file and can not be deleted');
}
if (!\JFolder::copy($sourcePath, $destinationPath, '', $force))
{
throw new \Exception('Copy folder is not possible');
}
}
/**
* Moves a file or folder to a destination
* If the destination folder or file already exists, it will not overwrite them without
* force.
*
* @param string $sourcePath Source path of the file or directory
* @param string $destinationPath Destination path of the file or directory
* @param bool $force Set true to overwrite files or directories
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws FileNotFoundException
*/
public function move($sourcePath, $destinationPath, $force = false)
{
// Get absolute paths from relative paths
$sourcePath = \JPath::clean($this->rootPath . $sourcePath, '/');
$destinationPath = \JPath::clean($this->rootPath . $destinationPath, '/');
if (!file_exists($sourcePath))
{
throw new FileNotFoundException;
}
if (is_dir($sourcePath))
{
$this->moveFolder($sourcePath, $destinationPath, $force);
}
else
{
$this->moveFile($sourcePath, $destinationPath, $force);
}
}
/**
* Moves a file
*
* @param string $sourcePath Absolute path of source
* @param string $destinationPath Absolute path of destination
* @param bool $force Set true to overwrite file if exists
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
private function moveFile($sourcePath, $destinationPath, $force = false)
{
if (is_dir($destinationPath))
{
// If the destination is a folder we create a file with the same name as the source
$destinationPath = $destinationPath . '/' . basename($sourcePath);
}
if (file_exists($destinationPath) && !$force)
{
throw new \Exception('Move file is not possible as destination file already exists');
}
if (!\JFile::move($sourcePath, $destinationPath))
{
throw new \Exception('Move file is not possible');
}
}
/**
* Moves a folder from source to destination
*
* @param string $sourcePath Source path of the file or directory
* @param string $destinationPath Destination path of the file or directory
* @param bool $force Set true to overwrite files or directories
*
* @return void
*
* @since __DEPLOY_VERSION__
* @throws \Exception
*/
private function moveFolder($sourcePath, $destinationPath, $force = false)
{
if (file_exists($destinationPath) && !$force)
{
throw new \Exception('Move folder is not possible as destination folder already exists');
}
if (is_file($destinationPath) && !\JFile::delete($destinationPath))
{
throw new \Exception('Move folder is not possible as destination folder is a file and can not be deleted');
}
if (is_dir($destinationPath))
{
// We need to bypass exception thrown in JFolder when destination exists
// So we only copy it in forced condition, then delete the source to simulate a move
if (!\JFolder::copy($sourcePath, $destinationPath, '', true))
{
throw new \Exception('Move folder to an existing destination failed');
}
// Delete the source
\JFolder::delete($sourcePath);
return;
}
// Perform usual moves
$value = \JFolder::move($sourcePath, $destinationPath);
if ($value !== true)
{
throw new \Exception($value);
}
}
/**
* Returns an url which can be used to display an image from within the "images" directory.
*
* @param string $path Path of the file relative to adapter
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getUrl($path)
{
return Uri::root() . $this->getEncodedPath($this->filePath . $path);
}
/**
* Returns the name of this adapter.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getAdapterName()
{
return $this->filePath;
}
/**
* Search for a pattern in a given path
*
* @param string $path The base path for the search
* @param string $needle The path to file
* @param bool $recursive Do a recursive search
*
* @return \stdClass[]
*
* @since __DEPLOY_VERSION__
*/
public function search($path, $needle, $recursive)
{
$pattern = \JPath::clean($this->rootPath . '/' . $path . '/*' . $needle . '*');
if ($recursive)
{
$results = $this->rglob($pattern);
}
else
{
$results = glob($pattern);
}
$searchResults = [];
foreach ($results as $result)
{
$searchResults[] = $this->getPathInformation($result);
}
return $searchResults;
}
/**
* Do a recursive search on a given path
*
* @param string $pattern The pattern for search
* @param int $flags Flags for search
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
private function rglob($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
{
$files = array_merge($files, $this->rglob($dir . '/' . basename($pattern), $flags));
}
return $files;
}
/**
* Returns a temporary url for the given path.
* This is used internally in media manager
*
* @param string $path The path to file
*
* @return string
*
* @since __DEPLOY_VERSION__
* @throws \FileNotFoundException
*/
public function getTemporaryUrl($path)
{
return $this->getUrl($path);
}
/**
* Replace spaces on a path with %20
*
* @param string $path The Path to be encoded
*
* @return string
*
* @since __DEPLOY_VERSION__
* @throws \FileNotFoundException
*/
private function getEncodedPath($path)
{
return str_replace(" ", "%20", $path);
}
}
No other release has this exact content. See every version of this file.