CoreFiles by mySites.guru
ApiModel.php569 lines, 15,366 bytes, from Joomla_4.2.0-alpha3-dev-Development-Full_Package.zip
Raw History Compare with 4.2.0-alpha2
md5
e28a6f7ec6d0683671c19b19fc485a99
sha1
ef7be0438331552c022849b199a410fbf2b3603e
sha256
d8b7f1b32fe062cf6faf353f32025e06ce84bdfe86ea2ec6393b9e88121c4ab5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
<?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\Event\FetchMediaItemEvent;
use Joomla\Component\Media\Administrator\Event\FetchMediaItemsEvent;
use Joomla\Component\Media\Administrator\Event\FetchMediaItemUrlEvent;
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\ProviderManagerHelperTrait;

/**
 * Api Model
 *
 * @since  4.0.0
 */
class ApiModel extends BaseDatabaseModel
{
	use ProviderManagerHelperTrait;

	/**
	 * The available extensions.
	 *
	 * @var   string[]
	 * @since  4.0.0
	 */
	private $allowedExtensions = null;

	/**
	 * 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')
		{
			$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;

		$event = new FetchMediaItemEvent('onFetchMediaItem', ['item' => $file]);
		Factory::getApplication()->getDispatcher()->dispatch($event->getName(), $event);

		return $event->getArgument('item');
	}

	/**
	 * 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')
			{
				$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;
		}

		// Make proper indexes
		$files = array_values($files);

		$event = new FetchMediaItemsEvent('onFetchMediaItems', ['items' => $files]);
		Factory::getApplication()->getDispatcher()->dispatch($event->getName(), $event);

		return $event->getArgument('items');
	}

	/**
	 * 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   string   $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   string  $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');

		// Also include the filesystem plugins, perhaps they support batch processing too
 		PluginHelper::importPlugin('media-action');

		$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;
		}

		$url = $this->getAdapter($adapter)->getUrl($path);

		$event = new FetchMediaItemUrlEvent('onFetchMediaFileUrl', ['adapter' => $adapter, 'path' => $path, 'url' => $url]);
		Factory::getApplication()->getDispatcher()->dispatch($event->getName(), $event);

		return $event->getArgument('url');
	}

	/**
	 * 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);
	}

	/**
	 * 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 options from the input or fallback to images only
			$mediaTypes = explode(',', Factory::getApplication()->input->getString('mediatypes', '0'));
			$types      = [];
			$extensions = [];

			// Default to showing all supported formats
			if (count($mediaTypes) === 0) {
				$mediaTypes = ['0', '1', '2', '3'];
			}

			array_map(
				function ($mediaType) use (&$types) {
					switch ($mediaType) {
						case '0':
							$types[] = 'images';
							break;
						case '1':
							$types[] = 'audios';
							break;
						case '2':
							$types[] = 'videos';
							break;
						case '3':
							$types[] = 'documents';
							break;
						default:
							break;
					}
				},
				$mediaTypes
			);

			$images = array_map(
				'trim',
				explode(
					',',
					ComponentHelper::getParams('com_media')->get(
						'image_extensions',
						'bmp,gif,jpg,jpeg,png,webp'
					)
				)
			);
			$audios = array_map(
				'trim',
				explode(
					',',
					ComponentHelper::getParams('com_media')->get(
						'audio_extensions',
						'mp3,m4a,mp4a,ogg'
					)
				)
			);
			$videos = array_map(
				'trim',
				explode(
					',',
					ComponentHelper::getParams('com_media')->get(
						'video_extensions',
						'mp4,mp4v,mpeg,mov,webm'
					)
				)
			);
			$documents = array_map(
				'trim',
				explode(
					',',
					ComponentHelper::getParams('com_media')->get(
						'doc_extensions',
						'doc,odg,odp,ods,odt,pdf,ppt,txt,xcf,xls,csv'
					)
				)
			);

			foreach ($types as $type) {
				if (in_array($type, ['images', 'audios', 'videos', 'documents'])) {
					$extensions = array_merge($extensions, ${$type});
				}
			}

			// Make them an array
			$this->allowedExtensions = $extensions;
		}

		// Extract the extension
		$extension = strtolower(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 20 releases, Joomla 4.1.0-rc2 to 4.2.0-beta2. See them all, or every version of this file.