| Current Path : /proc/thread-self/root/proc/thread-self/root/home/digilove/.cagefs/tmp/ |
| Current File : //proc/thread-self/root/proc/thread-self/root/home/digilove/.cagefs/tmp/phpSuUfmh |
<?php
/**
* @package Shortcode Ultimate
* @author BdThemes http://www.bdthemes.com
* @copyright Copyright (C) BdThemes Ltd
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
$jinput = JFactory::getApplication()->input;
if ($jinput->getString('id') || $jinput->getString('download')) {
require_once('file_controller.php');
require_once('downloader.class.php');
require_once('mdownloader.php');
//get saved data
$id = $jinput->getString('id') ;
$id = substr($id, 0, 30);
$sdata = Su_FileManager::getFileInfo($id);
if(!$sdata->name){
exit;
}
$params = json_decode($sdata->params);
$file = $sdata->name;
$arrName = explode('/', $sdata->name);
$save_as = $arrName[sizeof($arrName) - 1];
if(@$params->save_as){
$save_as = $params->save_as;
}
if(@$params->resumable == 'yes'){
$resumable = true;
} else {
$resumable = false;
}
$speed = @$params->download_speed ? $params->download_speed : 5;
$mode = Downloader::DOWNLOAD_FILE;
$record = 0;
// Start Download
$downloader = new MDownloader($id, $file, $mode);
$downloader = $downloader->resumable($resumable);
$downloader = $downloader->speedLimit($speed);
$downloader = $downloader->setDownloadName($save_as);
$downloader = $downloader->autoExit(true);
$downloader = $downloader->recordDownloaded($record);
$downloader->download();
} else {
echo 'File Doesn\'t Exists!';
}
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
/**
* PHP Advanced Downloader
*
* This class is used for downloading files or data string which could be data of files 'binary string'
* or just a plain string 'txt string'.
* downloading in data mode allows to specify file name with extension to download in
* downloading in file mode allows only to change downloade file name
*
* this class benifits:
* protect files from direct access, control resumability, control speed, control download name,
* control downloading with authentication, calculate downloaded bandwidth
*
* Some ideas has been taken from 'Nguyen Quoc Bao' , but code built from scratch
*
* @author ahmed saad <a7mad.sa3d.2014@gmail.com>
* @copyright ahmed saad 22 October 2014
* @link http://www.facebook.com/abu.sa3d
* @version 1.0.0
* @package PHP Advanced Downloader
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*
*/
class Downloader
{
const DOWNLOAD_FILE = 1;
const DOWNLOAD_DATA = 2;
protected $_data;
protected $_filename;
protected $_file_basename;
protected $_file_extension;
protected $_mime;
protected $_extensions_mime_arr;
protected $_last_modified_time;
protected $_full_size;
protected $_required_download_size;
protected $_downloaded = 0;
protected $_seek_start = 0;
protected $_seek_end;
protected $_is_partial;
protected $_is_resumable = true;
protected $_speed_limit;
protected $_buffer_size = 2048;
protected $_auto_exit = false;
protected $_use_authentiaction = false;
protected $_auth_username;
protected $_auth_password;
protected $_auth_callback;
protected $_record_downloaded;
protected $_record_downloaded_callback;
/**
* Downloader constructor
*
* Constructor will prepare data file and calculate start byte and end byte
*
* @param string $to_download file path or data string
* @param integer $download_mode file mode or data mode
*/
public function __construct( $to_download, $download_mode = self::DOWNLOAD_FILE )
{
global $HTTP_SERVER_VARS;
$this->_initialize();
if( $download_mode == self::DOWNLOAD_FILE )
{
// Download by file path
$this->_download_mode = $download_mode;
if( !is_file( $to_download ) )
{
// Not Found
$this->_setHeader( 'HTTP/1.0 404 File Not Found' );
exit();
}
else if( !is_readable( $to_download ) || !( $this->_data = fopen( $to_download, 'rb' ) ) ) // Try To Open File
{
// File is not readable, couldnot open
$this->_setHeader( 'HTTP/1.0 403 Forbidden File Not Accissible.' );
exit();
}
$this->_full_size = filesize( $to_download );
$info = pathinfo( $to_download );
$this->_filename = $info['filename'];
$this->_file_basename = $info[ 'basename' ];
$this->_file_extension= $info[ 'extension' ];
$this->_mime = $this->_getMimeOf( $this->_file_extension );
$this->_last_modified_time = filemtime( $to_download );
}
else if( $download_mode == self::DOWNLOAD_DATA )
{
// Download By Data String
$this->_download_mode = $download_mode;
if( is_file( $to_download ) )
{
// the given is a file so we will convert it to data string
$this->_data = file_get_contents( $to_download );
// $this->_data = implode( '', file( $to_download ) );
$info = pathinfo( $to_download );
$this->_filename = $info[ 'filename' ];
$this->_basename = $info[ 'basename' ];
$this->_file_extension = $info[ 'extension' ];
}
else
{
// The give data may be binary data or basic string or whatever in string formate
// so we will assume by default that the given string is basic txt file
// you can change this behaviour via setDownloadName() method and pass to it file basename
$this->_data = $to_download;
$this->_filename = 'file';
$this->_file_extension = 'txt';
$this->_basename = $this->_filename . '.' . $this->_file_extension;
}
$this->_full_size = strlen( $this->_data );
$this->_mime = $this->_getMimeOf( $this->_file_extension );
$this->_last_modified_time = time();
}
else
{
// Bad Request
$this->_setHeader( 'HTTP/1.0 400 Bad Request Download Mode Error' );
exit();
}
// Range
if( isset( $_SERVER['HTTP_RANGE'] ) || isset( $HTTP_SERVER_VARS['HTTP_RANGE'] ) )
{
// Partial Download Request, for Resumable
$this->_is_partial = true;
$http_range = isset( $_SERVER['HTTP_RANGE'] ) ? $_SERVER['HTTP_RANGE'] : $HTTP_SERVER_VARS['HTTP_RANGE'];
if( stripos( 'bytes' ) === false )
{
// Bad Request for range
$this->_setHeader( 'HTTP/1.0 416 Requested Range Not Satisfiable' );
exit();
}
$range = substr( $http_range , strlen('bytes=') );
// $range = str_replace( 'bytes=', '', $http_range );
$range = explode( '-', $range, 3 );
// full_size = 100byte
// range = bytes=0-99
// seek_start = 0, seek_end = 99
// Set Seek
// Let Keep Default behaviour to be resumable, later immeduiatelt after downloading
// we will check if resumability is turned off we will ovverride the comming three lines to be non resumable
$this->_seek_start = ( $range[0] > 0 && $range[0] < $this->_full_size - 1 ) ? $range[0] : 0;
$this->_seek_end = ( $range[1] > 0 && $range[1] < $_full_size && $range[1] > $this->_seek_start ) ? $range[1] : $this->_full_size - 1;
$this->_required_download_size = $this->_seek_end - $this->_seek_start + 1;
}
else
{
// Full File Download Request
$this->_is_partial = false;
$this->_seek_start = 0;
$this->_seek_end = $this->_full_size - 1;
$this->_required_download_size = $this->_full_size;
}
// Construct End
}
/**
* Start download process
*
* @return null
*/
public function download()
{
// Actual Download Steps
// Check If Authentication Required
if( $this->_use_authentiaction )
{
if( !$this->_authenticate() )
{
// Authenticate Headers, this Will Popup authentication process then redirect back to the same request with provided username, password
$this->_setHeader( 'WWW-Authenticate', 'Basic realm="This Process Require authentication, please provide your cridentials."' );
$this->_setHeader( 'HTTP/1.0 401 Unauthorized' );
$this->_setHeader( 'Status', '401 Unauthorized' );
// Exit if auto exit is enabled
if( $this->_auto_exit )
exit();
return false; // Making sure That script stops here
}
}
// check resumability, Headers Stage
if( $this->_is_partial )
{
// Resumable Request
if( $this->_is_resumable )
{
// Allow to resume
// Resume Headers >>>
$this->_setHeader( 'HTTP/1.0 206 Partial Content' );
$this->_setHeader( 'Status', '206 Partial Content' );
$this->_setHeader( 'Accept-Ranges', 'bytes' );
$this->_setHeader( 'Content-range', 'bytes ' . $this->_seek_start . '-' . $this->_seek_end . '/' . $this->_full_size );
}
else
{
// Turn off resume capability
$this->_seek_start = 0;
$this->_seek_end = $this->_full_size - 1;
$this->_required_download_size = $this->_full_size;
}
}
// Commom Download Headers content type, content disposition, content length and Last Modified Goes Here >>>
$this->_setHeader( 'Content-Type', $this->_mime );
$this->_setHeader( 'Content-Disposition', 'attachment; filename=' . $this->_file_basename );
$this->_setHeader( 'Content-Length', $this->_required_download_size );
$this->_setHeader( 'Last-Modified', date( 'D, d M Y H:i:s \G\M\T', $this->_last_modified_time ) );
// End Headers Stage
// Work On Download Speed Limit
if( $this->_speed_limit )
{
// how many buffers ticks per second
$buf_per_second = 10; //10
// how long one buffering tick takes by micro second
$buf_micro_time = 150; // 100
// Calculate sleep micro time after each tick
$sleep_micro_time = round( ( 1000000 - ( $buf_per_second * $buf_micro_time ) ) / $buf_per_second );
// Calculate required buffer per one tick, make sure it is integer so round the result
$this->_buffer_size = round( $this->_speed_limit * 1024 / $buf_per_second );
}
// Immediatl