- md5
- 5e3ecb8eb2a14f2889bd07abe994868b
- sha1
- 7355623eeb03fe7866edf551b87c9b61940978ac
- sha256
- 3707e4d1e006d9e4d847751ce2d28d8df8c8d4bd240e0f5bfa14505e9861d307
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
<?php
/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Media\Administrator\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Model\BaseModel;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Session\Session;
use Joomla\Component\Media\Administrator\Exception\FileExistsException;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
/**
* Api Media Controller
*
* This is NO public api controller, it is internal for the com_media component only!
*
* @since 4.0.0
*/
class ApiController extends BaseController
{
/**
* Execute a task by triggering a method in the derived class.
*
* @param string $task The task to perform. If no matching task is found, the '__default' task is executed, if defined.
*
* @return mixed The value returned by the called method.
*
* @since 4.0.0
* @throws \Exception
*/
public function execute($task)
{
$method = $this->input->getMethod();
$this->task = $task;
$this->method = $method;
try
{
// Check token for requests which do modify files (all except get requests)
if ($method !== 'GET' && !Session::checkToken('json'))
{
throw new \InvalidArgumentException(Text::_('JINVALID_TOKEN_NOTICE'), 403);
}
$doTask = strtolower($method) . ucfirst($task);
// Record the actual task being fired
$this->doTask = $doTask;
if (!in_array($this->doTask, $this->taskMap))
{
throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task), 405);
}
$data = $this->$doTask();
// Return the data
$this->sendResponse($data);
}
catch (FileNotFoundException $e)
{
$this->sendResponse($e, 404);
}
catch (FileExistsException $e)
{
$this->sendResponse($e, 409);
}
catch (InvalidPathException $e)
{
$this->sendResponse($e, 400);
}
catch (\Exception $e)
{
$errorCode = 500;
if ($e->getCode() > 0)
{
$errorCode = $e->getCode();
}
$this->sendResponse($e, $errorCode);
}
}
/**
* Files Get Method
*
* Examples:
*
* - GET a list of folders below the root:
* index.php?option=com_media&task=api.files
* /api/files
* - GET a list of files and subfolders of a given folder:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
* - GET a list of files and subfolders of a given folder for a given search term:
* use recursive=1 to search recursively in the working directory
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop&search=apple
* /api/files/sampledata/fruitshop?search=apple
* To look up in same working directory set flag recursive=0
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop&search=apple&recursive=0
* /api/files/sampledata/fruitshop?search=apple&recursive=0
* - GET file information for a specific file:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
* - GET a temporary URL to a given file
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg&url=1&temp=1
* /api/files/sampledata/fruitshop/test.jpg&url=1&temp=1
* - GET a temporary URL to a given file
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg&url=1
* /api/files/sampledata/fruitshop/test.jpg&url=1
*
* @return array The data to send with the response
*
* @since 4.0.0
* @throws \Exception
*/
public function getFiles()
{
// Grab options
$options = [];
$options['url'] = $this->input->getBool('url', false);
$options['temp'] = $this->input->getBool('temp', false);
$options['search'] = $this->input->getString('search', '');
$options['recursive'] = $this->input->getBool('recursive', true);
$options['content'] = $this->input->getBool('content', false);
return $this->getModel()->getFiles($this->getAdapter(), $this->getPath(), $options);
}
/**
* Files delete Method
*
* Examples:
*
* - DELETE an existing folder in a specific folder:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test
* /api/files/sampledata/fruitshop/test
* - DELETE an existing file in a specific folder:
* index.php?option=com_media&task=api.files&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
* @return null
*
* @since 4.0.0
* @throws \Exception
*/
public function deleteFiles()
{
$this->getModel()->delete($this->getAdapter(), $this->getPath());
return null;
}
/**
* Files Post Method
*
* Examples:
*
* - POST a new file or folder into a specific folder, the file or folder information is returned:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop
* /api/files/sampledata/fruitshop
*
* New file body:
* {
* "name": "test.jpg",
* "content":"base64 encoded image"
* }
* New folder body:
* {
* "name": "test",
* }
*
* @return array The data to send with the response
*
* @since 4.0.0
* @throws \Exception
*/
public function postFiles()
{
$adapter = $this->getAdapter();
$path = $this->getPath();
$content = $this->input->json;
$name = $content->getString('name');
$mediaContent = base64_decode($content->get('content', '', 'raw'));
$override = $content->get('override', false);
if ($mediaContent)
{
$this->checkContent();
// A file needs to be created
$name = $this->getModel()->createFile($adapter, $name, $path, $mediaContent, $override);
}
else
{
// A file needs to be created
$name = $this->getModel()->createFolder($adapter, $name, $path, $override);
}
return $this->getModel()->getFile($adapter, $path . '/' . $name);
}
/**
* Files Put method
*
* Examples:
*
* - PUT a media file, the file or folder information is returned:
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
* Update file body:
* {
* "content":"base64 encoded image"
* }
*
* - PUT move a file, folder to another one
* path : will be taken as the source
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
* JSON body:
* {
* "newPath" : "/path/to/destination",
* "move" : "1"
* }
*
* - PUT copy a file, folder to another one
* path : will be taken as the source
* index.php?option=com_media&task=api.files&format=json&path=/sampledata/fruitshop/test.jpg
* /api/files/sampledata/fruitshop/test.jpg
*
* JSON body:
* {
* "newPath" : "/path/to/destination",
* "move" : "0"
* }
*
* @return array The data to send with the response
*
* @since 4.0.0
* @throws \Exception
*/
public function putFiles()
{
$adapter = $this->getAdapter();
$path = $this->getPath();
$content = $this->input->json;
$name = basename($path);
$mediaContent = base64_decode($content->get('content', '', 'raw'));
$newPath = $content->getString('newPath', null);
$move = $content->get('move', true);
if ($mediaContent != null)
{
$this->checkContent();
$this->getModel()->updateFile($adapter, $name, str_replace($name, '', $path), $mediaContent);
}
if ($newPath != null && $newPath !== $adapter . ':' . $path)
{
list($destinationAdapter, $destinationPath) = explode(':', $newPath, 2);
if ($move)
{
$destinationPath = $this->getModel()->move($adapter, $path, $destinationPath, false);
}
else
{
$destinationPath = $this->getModel()->copy($adapter, $path, $destinationPath, false);
}
$path = $destinationPath;
}
return $this->getModel()->getFile($adapter, $path);
}
/**
* Send the given data as JSON response in the following format:
*
* {"success":true,"message":"ok","messages":null,"data":[{"type":"dir","name":"banners","path":"//"}]}
*
* @param mixed $data The data to send
* @param integer $responseCode The response code
*
* @return void
*
* @since 4.0.0
*/
private function sendResponse($data = null, int $responseCode = 200)
{
// Set the correct content type
$this->app->setHeader('Content-Type', 'application/json');
// Set the status code for the response
http_response_code($responseCode);
// Send the data
echo new JsonResponse($data);
$this->app->close();
}
/**
* Method to get a model object, loading it if required.
*
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return BaseModel|boolean Model object on success; otherwise false on failure.
*
* @since 4.0.0
*/
public function getModel($name = 'Api', $prefix = 'Administrator', $config = [])
{
return parent::getModel($name, $prefix, $config);
}
/**
* Performs various checks if it is allowed to save the content.
*
* @return void
*
* @since 4.0.0
* @throws \Exception
*/
private function checkContent()
{
if (!Factory::getUser()->authorise('core.create', 'com_media'))
{
throw new \Exception(Text::_('COM_MEDIA_ERROR_CREATE_NOT_PERMITTED'), 403);
}
$params = ComponentHelper::getParams('com_media');
$helper = new MediaHelper;
$serverlength = $this->input->server->getInt('CONTENT_LENGTH');
if ($serverlength > ($params->get('upload_maxsize', 0) * 1024 * 1024)
|| $serverlength > $helper->toBytes(ini_get('upload_max_filesize'))
|| $serverlength > $helper->toBytes(ini_get('post_max_size'))
|| $serverlength > $helper->toBytes(ini_get('memory_limit')))
{
throw new \Exception(Text::_('COM_MEDIA_ERROR_WARNFILETOOLARGE'), 403);
}
}
/**
* Get the Adapter.
*
* @return string
*
* @since 4.0.0
*/
private function getAdapter()
{
$parts = explode(':', $this->input->getString('path', ''), 2);
if (count($parts) < 1)
{
return null;
}
return $parts[0];
}
/**
* Get the Path.
*
* @return string
*
* @since 4.0.0
*/
private function getPath()
{
$parts = explode(':', $this->input->getString('path', ''), 2);
if (count($parts) < 2)
{
return null;
}
return $parts[1];
}
}
The same content is in 3 releases, Joomla 4.0.0-beta to 4.0.0-beta3. See them all, or every version of this file.