Skip to content

Instantly share code, notes, and snippets.

@SniperSister
Last active February 10, 2022 10:04
Show Gist options
  • Select an option

  • Save SniperSister/3cbc2055282c08ed43d4682e4fd36498 to your computer and use it in GitHub Desktop.

Select an option

Save SniperSister/3cbc2055282c08ed43d4682e4fd36498 to your computer and use it in GitHub Desktop.

Revisions

  1. SniperSister revised this gist Feb 10, 2022. 1 changed file with 3 additions and 26 deletions.
    29 changes: 3 additions & 26 deletions readmedia.php
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,7 @@

    /* Create the Application */
    $mainframe = JFactory::getApplication('site');

    /**************************************************/
    // Your code starts here...
    /**************************************************/
    @@ -70,34 +71,10 @@
    $begin = 0;
    $end = $size;

    if (isset($_SERVER['HTTP_RANGE']))
    {
    if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
    {
    $begin = intval($matches[0]);

    if (!empty($matches[1]))
    {
    $end = intval($matches[1]);
    }
    }
    }

    if ($begin > 0 || $end < $size)
    {
    header('HTTP/1.0 206 Partial Content');
    }
    else
    {
    header('HTTP/1.0 200 OK');
    }

    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('Accept-Ranges: bytes');
    header('Content-Length:' . ($end - $begin));
    header("Content-Range: bytes $begin-$end/$size");
    header("Content-Disposition: inline; filename=" . basename($location));
    header("Content-Transfer-Encoding: binary\n");
    header("Last-Modified: $time");
    @@ -110,4 +87,4 @@
    {
    print fread($fm, min(1024 * 16, $end - $cur));
    $cur += 1024 * 16;
    }
    }
  2. SniperSister created this gist Aug 16, 2018.
    113 changes: 113 additions & 0 deletions readmedia.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    <?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_LIBRARIES . '/import.legacy.php';
    require_once JPATH_LIBRARIES . '/cms.php';

    // Load the configuration
    require_once JPATH_CONFIGURATION . '/configuration.php';

    /* Create the Application */
    $mainframe = JFactory::getApplication('site');
    /**************************************************/
    // Your code starts here...
    /**************************************************/

    if (!JFactory::getUser()->id)
    {
    header("HTTP/1.0 403 access denied");

    echo "Access denied";

    exit();
    }

    $requestedFile = str_replace(JUri::root(), "", JUri::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;

    if (isset($_SERVER['HTTP_RANGE']))
    {
    if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
    {
    $begin = intval($matches[0]);

    if (!empty($matches[1]))
    {
    $end = intval($matches[1]);
    }
    }
    }

    if ($begin > 0 || $end < $size)
    {
    header('HTTP/1.0 206 Partial Content');
    }
    else
    {
    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('Accept-Ranges: bytes');
    header('Content-Length:' . ($end - $begin));
    header("Content-Range: bytes $begin-$end/$size");
    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;
    }