- md5
- bdd04fe344ae23fa2a53cb629e8ee21d
- sha1
- 630ab036fbda18d539098e004f21c20d6c6611e0
- sha256
- d667e56d279dfd85b080935564b7a94a44307389f5fe4976870501990cacb9c5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
<?php
/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Media\Administrator\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
use Joomla\Component\Media\Administrator\Exception\FileExistsException;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
use Joomla\Component\Media\Administrator\Provider\ProviderManager;
/**
* Api Model
*
* @since 4.0.0
*/
class ApiModel extends BaseDatabaseModel
{
/**
* Holds the available media file adapters.
*
* @var ProviderManager
* @since 4.0.0
*/
private $providerManager = null;
/**
* The available extensions.
*
* @var string[]
* @since 4.0.0
*/
private $allowedExtensions = null;
/**
* Return the requested adapter
*
* @param string $name Name of the provider
*
* @since 4.0.0
* @return AdapterInterface
*
* @throws \Exception
*/
private function getAdapter($name)
{
if ($this->providerManager == null)
{
$this->providerManager = new ProviderManager;
// Fire the event to get the results
$eventParameters = ['context' => 'AdapterManager', 'providerManager' => $this->providerManager];
$event = new MediaProviderEvent('onSetupProviders', $eventParameters);
PluginHelper::importPlugin('filesystem');
Factory::getApplication()->triggerEvent('onSetupProviders', $event);
}
return $this->providerManager->getAdapter($name);
}
/**
* Returns the requested file or folder information. More information
* can be found in AdapterInterface::getFile().
*
* @param string $adapter The adapter
* @param string $path The path to the file or folder
* @param array $options The options
*
* @return \stdClass
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::getFile()
*/
public function getFile($adapter, $path = '/', $options = [])
{
// Add adapter prefix to the file returned
$file = $this->getAdapter($adapter)->getFile($path);
// Check if it is a media file
if ($file->type == 'file' && !$this->isMediaFile($file->path))
{
throw new InvalidPathException;
}
if (isset($options['url']) && $options['url'] && $file->type == 'file')
{
if (isset($options['temp']) && $options['temp'])
{
$file->tempUrl = $this->getTemporaryUrl($adapter, $file->path);
}
else
{
$file->url = $this->getUrl($adapter, $file->path);
}
}
if (isset($options['content']) && $options['content'] && $file->type == 'file')
{
$resource = $this->getAdapter($adapter)->getResource($file->path);
if ($resource)
{
$file->content = base64_encode(stream_get_contents($resource));
}
}
$file->path = $adapter . ":" . $file->path;
$file->adapter = $adapter;
return $file;
}
/**
* Returns the folders and files for the given path. More information
* can be found in AdapterInterface::getFiles().
*
* @param string $adapter The adapter
* @param string $path The folder
* @param array $options The options
*
* @return \stdClass[]
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::getFile()
*/
public function getFiles($adapter, $path = '/', $options = [])
{
// Check whether user searching
if ($options['search'] != null)
{
// Do search
$files = $this->search($adapter, $options['search'], $path, $options['recursive']);
}
else
{
// Grab files for the path
$files = $this->getAdapter($adapter)->getFiles($path);
}
// Add adapter prefix to all the files to be returned
foreach ($files as $key => $file)
{
// Check if the file is valid
if ($file->type == 'file' && !$this->isMediaFile($file->path))
{
// Remove the file from the data
unset($files[$key]);
continue;
}
// Check if we need more information
if (isset($options['url']) && $options['url'] && $file->type == 'file')
{
if (isset($options['temp']) && $options['temp'])
{
$file->tempUrl = $this->getTemporaryUrl($adapter, $file->path);
}
else
{
$file->url = $this->getUrl($adapter, $file->path);
}
}
if (isset($options['content']) && $options['content'] && $file->type == 'file')
{
$resource = $this->getAdapter($adapter)->getResource($file->path);
if ($resource)
{
$file->content = base64_encode(stream_get_contents($resource));
}
}
$file->path = $adapter . ":" . $file->path;
$file->adapter = $adapter;
}
// Return array with proper indexes
return array_values($files);
}
/**
* Creates a folder with the given name in the given path. More information
* can be found in AdapterInterface::createFolder().
*
* @param string $adapter The adapter
* @param string $name The name
* @param string $path The folder
* @param boolean $override Should the folder being overridden when it exists
*
* @return string
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::createFolder()
*/
public function createFolder($adapter, $name, $path, $override)
{
try
{
$file = $this->getFile($adapter, $path . '/' . $name);
}
catch (FileNotFoundException $e)
{
// Do nothing
}
// Check if the file exists
if (isset($file) && !$override)
{
throw new FileExistsException;
}
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
PluginHelper::importPlugin('content');
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.folder', $object, true, $object]);
if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}
$object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path);
$app->triggerEvent('onContentAfterSave', ['com_media.folder', $object, true, $object]);
return $object->name;
}
/**
* Creates a file with the given name in the given path with the data. More information
* can be found in AdapterInterface::createFile().
*
* @param string $adapter The adapter
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
* @param boolean $override Should the file being overridden when it exists
*
* @return string
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::createFile()
*/
public function createFile($adapter, $name, $path, $data, $override)
{
try
{
$file = $this->getFile($adapter, $path . '/' . $name);
}
catch (FileNotFoundException $e)
{
// Do nothing
}
// Check if the file exists
if (isset($file) && !$override)
{
throw new FileExistsException;
}
// Check if it is a media file
if (!$this->isMediaFile($path . '/' . $name))
{
throw new InvalidPathException;
}
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
$object->data = $data;
$object->extension = strtolower(File::getExt($name));
PluginHelper::importPlugin('content');
// Also include the filesystem plugins, perhaps they support batch processing too
PluginHelper::importPlugin('media-action');
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, true, $object]);
if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}
$object->name = $this->getAdapter($object->adapter)->createFile($object->name, $object->path, $object->data);
$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, true, $object]);
return $object->name;
}
/**
* Updates the file with the given name in the given path with the data. More information
* can be found in AdapterInterface::updateFile().
*
* @param string $adapter The adapter
* @param string $name The name
* @param string $path The folder
* @param binary $data The data
*
* @return void
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::updateFile()
*/
public function updateFile($adapter, $name, $path, $data)
{
// Check if it is a media file
if (!$this->isMediaFile($path . '/' . $name))
{
throw new InvalidPathException;
}
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
$object->data = $data;
$object->extension = strtolower(File::getExt($name));
PluginHelper::importPlugin('content');
// Also include the filesystem plugins, perhaps they support batch processing too
PluginHelper::importPlugin('media-action');
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, false, $object]);
if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}
$this->getAdapter($object->adapter)->updateFile($object->name, $object->path, $object->data);
$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, false, $object]);
}
/**
* Deletes the folder or file of the given path. More information
* can be found in AdapterInterface::delete().
*
* @param string $adapter The adapter
* @param string $path The path to the file or folder
*
* @return void
*
* @since 4.0.0
* @throws \Exception
* @see AdapterInterface::delete()
*/
public function delete($adapter, $path)
{
$file = $this->getFile($adapter, $path);
// Check if it is a media file
if ($file->type == 'file' && !$this->isMediaFile($file->path))
{
throw new InvalidPathException;
}
$type = $file->type === 'file' ? 'file' : 'folder';
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->path = $path;
PluginHelper::importPlugin('content');
$result = $app->triggerEvent('onContentBeforeDelete', ['com_media.' . $type, $object]);
if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}
$this->getAdapter($object->adapter)->delete($object->path);
$app->triggerEvent('onContentAfterDelete', ['com_media.' . $type, $object]);
}
/**
* Copies file or folder from source path to destination path
* If forced, existing files/folders would be overwritten
*
* @param string $adapter The adapter
* @param string $sourcePath Source path of the file or folder (relative)
* @param string $destinationPath Destination path(relative)
* @param bool $force Force to overwrite
*
* @return string
*
* @since 4.0.0
* @throws \Exception
*/
public function copy($adapter, $sourcePath, $destinationPath, $force = false)
{
return $this->getAdapter($adapter)->copy($sourcePath, $destinationPath, $force);
}
/**
* Moves file or folder from source path to destination path
* If forced, existing files/folders would be overwritten
*
* @param string $adapter The adapter
* @param string $sourcePath Source path of the file or folder (relative)
* @param string $destinationPath Destination path(relative)
* @param bool $force Force to overwrite
*
* @return string
*
* @since 4.0.0
* @throws \Exception
*/
public function move($adapter, $sourcePath, $destinationPath, $force = false)
{
return $this->getAdapter($adapter)->move($sourcePath, $destinationPath, $force);
}
/**
* Returns a url for serve media files from adapter.
* Url must provide a valid image type to be displayed on Joomla! site.
*
* @param string $adapter The adapter
* @param string $path The relative path for the file
*
* @return string Permalink to the relative file
*
* @since 4.0.0
* @throws FileNotFoundException
*/
public function getUrl($adapter, $path)
{
// Check if it is a media file
if (!$this->isMediaFile($path))
{
throw new InvalidPathException;
}
return $this->getAdapter($adapter)->getUrl($path);
}
/**
* Search for a pattern in a given path
*
* @param string $adapter The adapter to work on
* @param string $needle The search therm
* @param string $path The base path for the search
* @param bool $recursive Do a recursive search
*
* @return \stdClass[]
*
* @since 4.0.0
* @throws \Exception
*/
public function search($adapter, $needle, $path = '/', $recursive = true)
{
return $this->getAdapter($adapter)->search($path, $needle, $recursive);
}
/**
* Returns a temporary url for the given path.
* This is used internally in media manager
*
* @param string $adapter The adapter
* @param string $path The path to file
*
* @return string
*
* @since 4.0.0
* @throws \Exception
*/
public function getTemporaryUrl($adapter, $path)
{
// Check if it is a media file
if (!$this->isMediaFile($path))
{
throw new InvalidPathException;
}
return $this->getAdapter($adapter)->getTemporaryUrl($path);
}
/**
* Checks if the given path is an allowed media file.
*
* @param string $path The path to file
*
* @return boolean
*
* @since 4.0.0
*/
private function isMediaFile($path)
{
// Check if there is an extension available
if (!strrpos($path, '.'))
{
return false;
}
// Initialize the allowed extensions
if ($this->allowedExtensions === null)
{
// Get the setting from the params
$this->allowedExtensions = ComponentHelper::getParams('com_media')->get(
'upload_extensions',
'bmp,csv,doc,gif,ico,jpg,jpeg,odg,odp,ods,odt,pdf,png,ppt,txt,xcf,xls,BMP,CSV,DOC,GIF,ICO,JPG,JPEG,ODG,ODP,ODS,ODT,PDF,PNG,PPT,TXT,XCF,XLS'
);
// Make them an array
$this->allowedExtensions = explode(',', $this->allowedExtensions);
}
// Extract the extension
$extension = substr($path, strrpos($path, '.') + 1);
// Check if the extension exists in the allowed extensions
return in_array($extension, $this->allowedExtensions);
}
}
The same content is in 2 releases, Joomla 4.0.0-beta6 to 4.0.0-beta7. See them all, or every version of this file.