Skip to content

Instantly share code, notes, and snippets.

@carlitorweb
Forked from SniperSister/readmedia.php
Created April 25, 2024 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlitorweb/e81ceb34347d0f7a3048668f4039c5c5 to your computer and use it in GitHub Desktop.
Save carlitorweb/e81ceb34347d0f7a3048668f4039c5c5 to your computer and use it in GitHub Desktop.
readmedia.php for Joomla 4 and 5
<?php
/**
* @version 1.0.0
* @package Readmedia
* @copyright Copyright (C) 2018 David Jardin - djumla GmbH
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://www.djumla.de
*/
/* Initialize Joomla framework */
define('_JEXEC', 1);
// Load system defines
if (file_exists(dirname(__FILE__) . '/defines.php'))
{
require_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
/* Create the Application */
$container = \Joomla\CMS\Factory::getContainer();
$container->alias('session.web', 'session.web.site')
->alias('session', 'session.web.site')
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
// Load the extension Namespaces
\JLoader::register('JNamespacePsr4Map', JPATH_LIBRARIES . '/namespacemap.php');
$extensionPsr4Loader = new \JNamespacePsr4Map();
$extensionPsr4Loader->load();
// Set the application as global app
\Joomla\CMS\Factory::$application = $app;
// Actual permission check starts here
if (!\Joomla\CMS\Factory::getUser()->id)
{
header("HTTP/1.0 403 access denied");
echo "Access denied";
exit();
}
$requestedFile = urldecode(str_replace(\Joomla\CMS\Uri\Uri::root(), "", \Joomla\CMS\Uri\Uri::current()));
$location = dirname(__FILE__) . "/" . $requestedFile;
if (!file_exists($location))
{
header("HTTP/1.0 404 file not found");
echo "File not found";
exit();
}
$size = filesize($location);
$time = date('r', filemtime($location));
$fm = @fopen($location, 'rb');
if (!$fm)
{
header("HTTP/1.0 505 Internal server error");
exit();
}
$begin = 0;
$end = $size;
header('HTTP/1.0 200 OK');
header("Content-Type: " . mime_content_type($location));
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header("Content-Disposition: inline; filename=" . basename($location));
header("Content-Transfer-Encoding: binary\n");
header("Last-Modified: $time");
header('Connection: close');
$cur = $begin;
fseek($fm, $begin, 0);
while (!feof($fm) && $cur < $end && (connection_status() == 0))
{
print fread($fm, min(1024 * 16, $end - $cur));
$cur += 1024 * 16;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment