Your IP : 10.10.0.253


Current Path : /var/www/components/com_yendifvideoshare/controllers/
Upload File :
Current File : /var/www/components/com_yendifvideoshare/controllers/download.php

<?php
/**
 * @version    1.2.9
 * @package    Com_YendifVideoShare
 * @author     PluginsWare Interactive Pvt. Ltd <admin@yendifplayer.com>
 * @copyright  Copyright (c) 2012 - 2021 PluginsWare Interactive Pvt. Ltd. All Rights Reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

// Prevent direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class YendifVideoShareControllerDownload extends YendifVideoShareController {
	
	public function download() {
		$app = JFactory::getApplication();
		$db  = JFactory::getDbo(); 		
		
        $query = "SELECT mp4 FROM #__yendifvideoshare_videos WHERE id=" . $app->input->getInt( 'id' ) . ' AND published=1';
		$db->setQuery( $query );
		$file = $db->loadResult();

		if ( empty( $file ) ) {
			die( 'Invalid File URL.' );
           	exit;
        }

		// Vars
		$isRemoteFile  = true;
        $formattedPath = '';        	
		$mimeType      = 'video/mp4'; 
		$fileSize      = '';		

		// Removing spaces and replacing with %20 ascii code
        $file = $this->removeSpaces( $file );

		// Detect the file type	
		if ( strpos( $file, 'media/yendifvideoshare/' ) !== false ) {
			$parsed = explode( 'media', $file );
			$file = JURI::root() . 'media' . $parsed[1];

			$isRemoteFile = false;
		} else {
			if ( strpos( $file, JURI::root() ) !== false ) {
				$isRemoteFile = false;
			}
		}		        		
          
        if ( preg_match( '#http://#', $file ) || preg_match( '#https://#', $file ) ) {
          	$formattedPath = 'url';
        } else {
          	$formattedPath = 'filepath';
        }

		if ( $isRemoteFile ) {
			$formattedPath = 'url';
		}
        
        if ( $formattedPath == 'url' ) {
          	$fileHeaders = @get_headers( $file );
  
          	if ( $fileHeaders[0] == 'HTTP/1.1 404 Not Found' ) {
           		die( 'File is not readable or not found.' );
           		exit;
          	}          
        } elseif ( $formattedPath == 'filepath' ) {		
          	if ( ! @is_readable( $file ) ) {
				die( 'File is not readable or not found.' );
               	exit;
          	}
        }
        
       	// Fetching File Size Located in Remote Server
       	if ( $isRemoteFile && $formattedPath == 'url' ) {         
          	$data = @get_headers( $file, true );
          
          	if ( ! empty( $data['Content-Length'] ) ) {
          		$fileSize = (int) $data[ 'Content-Length' ];          
          	} else {               
               	// If get_headers fails then try to fetch fileSize with curl
               	$ch = @curl_init();

               	if ( ! @curl_setopt( $ch, CURLOPT_URL, $file ) ) {
                 	@curl_close( $ch );
                 	@exit;
               	}
               
               	@curl_setopt( $ch, CURLOPT_NOBODY, true );
               	@curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
               	@curl_setopt( $ch, CURLOPT_HEADER, true );
               	@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
               	@curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
               	@curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
               	@curl_exec( $ch );
               
               	if ( ! @curl_errno( $ch ) ) {
                	$httpStatus = (int) @curl_getinfo( $ch, CURLINFO_HTTP_CODE );
                    if ( $httpStatus >= 200 && $httpStatus <= 300 )
                    	$fileSize = (int) @curl_getinfo( $ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
               	}

               	@curl_close( $ch );               
          	}          
		} else {         
		   	if ( $formattedPath == 'url' ) {		   
			   $data = @get_headers( $file, true );
			   $fileSize = (int) $data['Content-Length'];			   
		   	} elseif ( $formattedPath == 'filepath' ) {		   
		       $fileSize = (int) @filesize( $file );			   			   
		   	}		   
       	}
          
		// Get the extension of the file
		$path_parts = @pathinfo( $file );

		switch ( $path_parts['extension'] ) {          
			case 'mp3':
				$mimeType = "audio/mpeg";
				break;
			case 'wav':
				$mimeType = "audio/x-wav";
				break;
			case 'au':
				$mimeType = "audio/basic";
				break;
			case 'snd':
				$mimeType = "audio/basic";
				break;
			case 'm3u':
				$mimeType = "audio/x-mpegurl";
				break;
			case 'ra':
				$mimeType = "audio/x-pn-realaudio";
				break;
			case 'mp2':
				$mimeType = "video/mpeg";
				break;
			case 'mov':
				$mimeType = "video/quicktime";
				break;
			case 'qt':
				$mimeType = "video/quicktime";
				break;
			case 'mp4':
				$mimeType = "video/mp4";
				break;
			case 'm4a':
				$mimeType = "audio/mp4";
				break;
			case 'mp4a':
				$mimeType = "audio/mp4";
				break;
			case 'm4p':
				$mimeType = "audio/mp4";
				break;
			case 'm3a':
				$mimeType = "audio/mpeg";
				break;
			case 'm2a':
				$mimeType = "audio/mpeg";
				break;
			case 'mp2a':
				$mimeType = "audio/mpeg";
				break;
			case 'mp2':
				$mimeType = "audio/mpeg";
				break;
			case 'mpga':
				$mimeType = "audio/mpeg";
				break;
			case '3gp':
				$mimeType = "video/3gpp";
				break;
			case '3g2':
				$mimeType = "video/3gpp2";
				break;
			case 'mp4v':
				$mimeType = "video/mp4";
				break;
			case 'mpg4':
				$mimeType = "video/mp4";
				break;
			case 'm2v':
				$mimeType = "video/mpeg";
				break;
			case 'm1v':
				$mimeType = "video/mpeg";
				break;
			case 'mpe':
				$mimeType = "video/mpeg";
				break;
			case 'avi':
				$mimeType = "video/x-msvideo";
				break;
			case 'midi':
				$mimeType = "audio/midi";
				break;
			case 'mid':
				$mimeType = "audio/mid";
				break;
			case 'amr':
				$mimeType = "audio/amr";
				break;            
		
			default:
				$mimeType = "application/octet-stream";
		}
        
        // Off output buffering to decrease Server usage
        @ob_end_clean();
        
        if ( ini_get( 'zlib.output_compression' ) ) {
        	ini_set( 'zlib.output_compression', 'Off' );
        }
        
        header( 'Content-Description: File Transfer' );
        header( 'Content-Type: '. $mimeType );
        header( 'Content-Disposition: attachment; filename=' . (string) @basename( $file ) );
        header( 'Content-Transfer-Encoding: binary' );
        header( 'Expires: Wed, 07 May 2013 09:09:09 GMT' );
	    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
	    header( 'Cache-Control: post-check=0, pre-check=0', false );
	    header( 'Cache-Control: no-store, no-cache, must-revalidate' );
	    header( 'Pragma: no-cache' );
        header( 'Content-Length: '. $fileSize);        
        
        // Will Download 1 MB in chunkwise
        $chunk = 1 * ( 1024 * 1024 );
        $nfile = @fopen( $file, 'rb' );
        while ( ! feof( $nfile ) ) {                 
        	print( @fread( $nfile, $chunk ) );
            @ob_flush();
            @flush();
        }
        @fclose( $filen );		
	}

  	private function removeSpaces( $url ) {  
		$url = preg_replace( '/\s+/', '%20', trim( $url ) );  
	  	$url = str_replace( '         ', '%20', $url );
	  	$url = str_replace( '        ', '%20', $url );
	  	$url = str_replace( '       ', '%20', $url );
	  	$url = str_replace( '      ', '%20', $url );
	  	$url = str_replace( '     ', '%20', $url );
	  	$url = str_replace( '    ', '%20', $url );
	  	$url = str_replace( '   ', '%20', $url );
	  	$url = str_replace( '  ', '%20', $url );
	  	$url = str_replace( ' ', '%20', $url );
	  
	  	return $url;  		 
  	}
			
}