2013-10-07 12:17:06 +00:00
< ? php
/**
* This file is used to download files
* from one server to our server
* in prior version , this file was not so reliable
* this time it has complete set of instruction
* and proper downloader
* @ Author : Arslan Hassan
* @ License : Attribution Assurance License -- http :// www . opensource . org / licenses / attribution . php
* @ Since : 01 July 2009
*/
include ( " ../includes/config.inc.php " );
include ( " ../includes/classes/curl/class.curl.php " );
2014-04-01 13:42:33 +00:00
//error_reporting(E_ALL ^E_NOTICE);/**/
2014-05-14 06:18:02 +00:00
ini_set ( 'max_execution_time' , 3000 );
2013-10-07 12:17:06 +00:00
if ( isset ( $_POST [ 'check_url' ]))
{
$url = $_POST [ 'check_url' ];
$types_array = preg_replace ( '/,/' , ' ' , strtolower ( config ( 'allowed_types' )));
$types_array = explode ( ' ' , $types_array );
$file_ext = strtolower ( getExt ( strtolower ( $url )));
if ( checkRemoteFile ( $url ) && in_array ( $file_ext , $types_array ) )
{
echo json_encode ( array ( 'ok' => 'yes' ));
} else
echo json_encode ( array ( 'err' => 'Invalid remote url' ));
exit ();
}
2014-04-01 13:42:33 +00:00
//error_reporting(E_ALL); /**/
2013-10-07 12:17:06 +00:00
/**
* Call back function of cURL handlers
* when it downloads a file , it works with php >= 5.3 . 0
* @ param $download_size total file size of the file
* @ param $downloaded total file size that has been downloaded
* @ param $upload_size total file size that has to be uploaded
* @ param $uploadsed total file size that is uploaded
*
* Writes the log in file
*/
if ( ! isCurlInstalled ())
{
exit ( json_encode ( array ( " error " => " Sorry, we do not support remote upload " )));
}
//checking if user is logged in or not
if ( ! userid ())
exit ( json_encode ( array ( 'error' => 'You are not logged in' )));
2014-04-01 13:42:33 +00:00
if ( isset ( $_POST [ 'youtube' ]))
2013-10-07 12:17:06 +00:00
{
$youtube_url = $_POST [ 'file' ];
$filename = $_POST [ 'file_name' ];
$ParseUrl = parse_url ( $youtube_url );
parse_str ( $ParseUrl [ 'query' ], $youtube_url_prop );
$YouTubeId = isset ( $youtube_url_prop [ 'v' ]) ? $youtube_url_prop [ 'v' ] : '' ;
if ( ! $YouTubeId )
{
exit ( json_encode ( array ( " error " => " Invalid youtube url " )));
}
2014-05-14 06:18:02 +00:00
//for devolpment
2014-05-14 06:23:53 +00:00
$content = json_decode ( '{"apiVersion":"2.1","data":{"id":"QfRAHfquzM0","uploaded":"2008-01-01T09:21:43.000Z","updated":"2014-04-18T22:16:28.000Z","uploader":"thegreatpakistani","category":"Education","title":"Pakistan National Anthem","description":"The Beautiful Pakistan National Anthem.","thumbnail":{"sqDefault":"http://i1.ytimg.com/vi/QfRAHfquzM0/default.jpg","hqDefault":"http://i1.ytimg.com/vi/QfRAHfquzM0/hqdefault.jpg"},"player":{"default":"http://www.youtube.com/watch?v=QfRAHfquzM0&feature=youtube_gdata_player","mobile":"http://m.youtube.com/details?v=QfRAHfquzM0"},"content":{"5":"http://www.youtube.com/v/QfRAHfquzM0?version=3&f=videos&app=youtube_gdata","1":"rtsp://r2---sn-5hn7su7z.c.youtube.com/CiILENy73wIaGQnNzK76HUD0QRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://r2---sn-5hn7su7z.c.youtube.com/CiILENy73wIaGQnNzK76HUD0QRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":71,"rating":4.2875,"likeCount":"789","ratingCount":960,"viewCount":503860,"favoriteCount":0,"commentCount":1146,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}' );
//$content = json_decode('http://gdata.youtube.com/feeds/api/videos/'.$YouTubeId.'?v=2&alt=jsonc');
2014-05-14 06:18:02 +00:00
//$content = xml2array('http://gdata.youtube.com/feeds/api/videos/'.$YouTubeId);
$content = $content -> data ;
2014-03-19 13:26:54 +00:00
/* $match_arr =
2013-10-07 12:17:06 +00:00
array
(
" title " => " /<meta name= \" title \" content= \" (.*) \" >/ " ,
" description " => " /<meta name= \" description \" content= \" (.*) \" >/ " ,
" tags " => " /<meta name= \" keywords \" content= \" (.*) \" >/ " ,
" embed_code " => " /<meta name= \" keywords \" content= \" (.*) \" >/ " ,
" duration " => " /<span class= \" video-time \" >([0-9 \ :]+)< \ /span>/ "
);
$vid_array = array ();
foreach ( $match_arr as $title => $match )
{
preg_match ( $match , $content , $matches );
$vid_array [ $title ] = $matches [ 1 ];
} */
2014-05-14 06:18:02 +00:00
$vid_array [ 'title' ] = $content -> title ;
$vid_array [ 'description' ] = $content -> description ;
$vid_array [ 'tags' ] = $content -> title ;
$vid_array [ 'duration' ] = $content -> duration ;
2013-10-07 12:17:06 +00:00
$vid_array [ 'thumbs' ] =
array ( 'http://i3.ytimg.com/vi/' . $YouTubeId . '/1.jpg' , 'http://i3.ytimg.com/vi/' .
$YouTubeId . '/2.jpg' , 'http://i3.ytimg.com/vi/' . $YouTubeId . '/3.jpg' ,
'big' => 'http://i3.ytimg.com/vi/' . $YouTubeId . '/0.jpg' );
$vid_array [ 'embed_code' ] = ' < object width = " 425 " height = " 344 " >
< param name = " movie " value = " http://www.youtube.com/v/'. $YouTubeId .' " >
</ param >< param name = " allowFullScreen " value = " true " ></ param >
< param name = " allowscriptaccess " value = " always " ></ param >
< embed src = " http://www.youtube.com/v/'. $YouTubeId .' "
type = " application/x-shockwave-flash "
width = " 425 " height = " 344 "
allowscriptaccess = " always "
allowfullscreen = " true " >
</ embed ></ object > ' ;
$vid_array [ 'category' ] = array ( $cbvid -> get_default_cid ());
$vid_array [ 'file_name' ] = $filename ;
$vid_array [ 'userid' ] = userid ();
$duration = $vid_array [ 'duration' ];
2014-03-19 13:26:54 +00:00
/* $duration = explode ( " : " , $duration );
$sep = count ( $duration );
if ( $sep == 3 )
$duration = ( $duration [ 0 ] * 60 * 60 ) + ( $duration [ 1 ] * 60 ) + ( $duration [ 2 ]);
else
$duration = ( $duration [ 0 ] * 60 ) + ( $duration [ 1 ]);
*/
2013-10-07 12:17:06 +00:00
$vid = $Upload -> submit_upload ( $vid_array );
if ( error ())
{
2014-05-13 11:51:08 +00:00
//exit(json_encode(array('error'=>error('single'))));
2013-10-07 12:17:06 +00:00
}
if ( ! function_exists ( 'get_refer_url_from_embed_code' ))
{
exit ( json_encode ( array ( 'error' => " Clipbucket embed module is not installed " )));
}
$ref_url = get_refer_url_from_embed_code ( unhtmlentities ( stripslashes ( $vdetails [ 'embed_code' ])));
$ref_url = $ref_url [ 'url' ];
$db -> update ( tbl ( " video " ), array ( " status " , " refer_url " , " duration " ), array ( 'Successful' , $ref_url , $duration ), " videoid=' $vid ' " );
//Downloading thumb
foreach ( $vid_array [ 'thumbs' ] as $tid => $thumb )
{
if ( $tid != 'big' )
$thumbId = $tid + 1 ;
else
$thumbId = 'big' ;
snatch_it ( urlencode ( $thumb ), THUMBS_DIR , $filename . " - $thumbId .jpg " );
}
exit ( json_encode ( array ( 'vid' => $vid ,
'title' => $vid_array [ 'title' ], 'desc' => $vid_array [ 'description' ],
'tags' => $vid_array [ 'tags' ])));
}
2014-03-18 13:44:20 +00:00
$logDetails = array ();
2014-04-01 12:52:57 +00:00
/*
A callback accepting five parameters . The first is the cURL resource ,
the second is the total number of bytes expected to be downloaded in
this transfer , the third is the number of bytes downloaded so far ,
the fourth is the total number of bytes expected to be uploaded in
this transfer , and the fifth is the number of bytes uploaded so far .
*/
2014-03-19 13:26:54 +00:00
function callback ( $resource , $download_size , $downloaded , $upload_size , $uploaded ){
2014-03-18 13:44:20 +00:00
global $curl , $log_file , $file_name , $ext , $logDetails ;
2013-10-07 12:17:06 +00:00
$fo = fopen ( $log_file , 'w+' );
2014-04-01 13:42:33 +00:00
$info = curl_getinfo ( $resource );
2014-03-19 13:26:54 +00:00
2014-04-01 12:52:57 +00:00
/* $download_bytes = $download_size - $downloaded ;
2013-10-07 12:17:06 +00:00
$cur_speed = $info [ 'speed_download' ];
2014-03-19 13:26:54 +00:00
if ( $cur_speed > 0 )
$time_eta = $download_bytes / $cur_speed ;
2013-10-07 12:17:06 +00:00
else
2014-03-19 13:26:54 +00:00
$time_eta = 0 ;
//$download_size = (int) $download_size;
2014-04-01 12:52:57 +00:00
$time_took = $info [ 'total_time' ]; */
2014-04-01 13:54:44 +00:00
if ( is_object ( $resource )){
$curl_info = array (
'total_size' => $download_size ,
'downloaded' => $downloaded ,
//'speed_download' => $info['speed_download'],
//'time_eta' => $time_eta,
//'time_took'=> $time_took,
//'file_name' => ($file_name.'.'.$ext),
);
} else {
// for some curl extensions
$curl_info = array (
'total_size' => $resource ,
'downloaded' => $download_size ,
//'speed_download' => $info['speed_download'],
//'time_eta' => $time_eta,
//'time_took'=> $time_took,
//'file_name' => ($file_name.'.'.$ext),
);
}
2013-10-07 12:17:06 +00:00
fwrite ( $fo , json_encode ( $curl_info ));
2014-03-18 13:44:20 +00:00
$logDetails = $curl_info ;
2014-03-19 13:26:54 +00:00
//echo $log_file;
2013-10-07 12:17:06 +00:00
fclose ( $fo );
2014-03-19 13:26:54 +00:00
//file_put_contents($log_file, json_encode($curl_info));
2013-10-07 12:17:06 +00:00
}
$file = $_POST [ 'file' ];
$file_name = mysql_clean ( $_POST [ 'file_name' ]);
2014-03-19 13:26:54 +00:00
// $file = "http://clipbucket.dev/abc.mp4";
// $file_name = "abc";
2014-03-18 13:44:20 +00:00
2013-10-07 12:17:06 +00:00
$log_file = TEMP_DIR . '/' . $file_name . '_curl_log.cblog' ;
2014-04-01 12:52:57 +00:00
2013-10-07 12:17:06 +00:00
//For PHP < 5.3.0
$dummy_file = TEMP_DIR . '/' . $file_name . '_curl_dummy.cblog' ;
2014-04-01 12:52:57 +00:00
2013-10-07 12:17:06 +00:00
$ext = getExt ( $file );
$svfile = TEMP_DIR . '/' . $file_name . '.' . $ext ;
//Checking for the url
if ( empty ( $file ))
{
2014-03-19 13:26:54 +00:00
echo " error " ;
2013-10-07 12:17:06 +00:00
$array [ 'error' ] = " Please enter file url " ;
echo json_encode ( $array );
exit ();
}
//Checkinf if extension is wrong
$types = strtolower ( $Cbucket -> configs [ 'allowed_types' ]);
$types_array = preg_replace ( '/,/' , ' ' , $types );
$types_array = explode ( ' ' , $types_array );
$extension_whitelist = $types_array ;
if ( ! in_array ( $ext , $extension_whitelist ))
{
$array [ 'error' ] = " This file type is not allowed " ;
echo json_encode ( $array );
exit ();
}
$curl = new curl ( $file );
$curl -> setopt ( CURLOPT_FOLLOWLOCATION , true ) ;
2014-04-01 12:52:57 +00:00
2013-10-07 12:17:06 +00:00
//Checking if file size is not that goood
if ( ! is_numeric ( $curl -> file_size ) || $curl -> file_size == '' )
{
$array [ 'error' ] = " Unknown file size " ;
echo json_encode ( $array );
exit ();
}
if ( phpversion () < '5.3.0' )
{
2014-03-19 13:26:54 +00:00
echo " in less than 5.3 " ;
2013-10-07 12:17:06 +00:00
//Here we will get file size and write it in a file
//called dummy_log
$darray = array (
'file_size' => $curl -> file_size ,
'file_name' => $file_name . '.' . $ext ,
'time_started' => time (),
'byte_size' => 0
);
$do = fopen ( $dummy_file , 'w+' );
fwrite ( $do , json_encode ( $darray ));
fclose ( $do );
}
//Opening video file
$temp_fo = fopen ( $svfile , 'w+' );
2014-03-19 13:26:54 +00:00
$curlOpt = " " ;
2013-10-07 12:17:06 +00:00
$curl -> setopt ( CURLOPT_FILE , $temp_fo );
// Set up the callback
if ( phpversion () >= '5.3.0' )
{
$curl -> setopt ( CURLOPT_NOPROGRESS , false );
$curl -> setopt ( CURLOPT_PROGRESSFUNCTION , 'callback' );
}
$curl -> exec ();
if ( $theError = $curl -> hasError ())
{
$array [ 'error' ] = $theError ;
echo json_encode ( $array );
}
//Finish Writing File
fclose ( $temp_fo );
2014-03-19 13:26:54 +00:00
//var_dump($curlOpt);
2013-10-07 12:17:06 +00:00
2014-04-01 12:52:57 +00:00
sleep ( 2 );
2014-03-18 13:44:20 +00:00
$details = $logDetails ; //file_get_contents($log_file);
//$details = json_decode($details,true);
2014-04-01 13:42:33 +00:00
$targetFileName = $file_name . '.' . $ext ;
$Upload -> add_conversion_queue ( $targetFileName );
2013-10-07 12:17:06 +00:00
2014-04-01 12:53:27 +00:00
if ( file_exists ( $log_file ))
unlink ( $log_file );
if ( file_exists ( $dummy_file ))
unlink ( $dummy_file );
2013-10-07 12:17:06 +00:00
$quick_conv = config ( 'quick_conv' );
$use_crons = config ( 'use_crons' );
//Inserting data
$title = urldecode ( mysql_clean ( getName ( $file )));
$title = $title ? $title : " Untitled " ;
$vidDetails = array
(
'title' => $title ,
'description' => $title ,
'tags' => genTags ( str_replace ( ' ' , ', ' , $title )),
'category' => array ( $cbvid -> get_default_cid ()),
'file_name' => $file_name ,
'userid' => userid (),
2014-03-18 13:44:20 +00:00
'file_directory' => createDataFolders ()
2013-10-07 12:17:06 +00:00
);
2014-03-18 13:44:20 +00:00
2013-10-07 12:17:06 +00:00
$vid = $Upload -> submit_upload ( $vidDetails );
echo json_encode ( array ( 'vid' => $vid ));
2014-04-01 13:42:33 +00:00
2013-10-07 12:17:06 +00:00
if ( $quick_conv == 'yes' || $use_crons == 'no' )
{
//exec(php_path()." -q ".BASEDIR."/actions/video_convert.php &> /dev/null &");
if ( stristr ( PHP_OS , 'WIN' )) {
2014-03-18 13:44:20 +00:00
exec ( php_path () . " -q " . BASEDIR . " /actions/video_convert_test.php $targetFileName sleep " );
2013-10-07 12:17:06 +00:00
} else {
2014-03-18 13:44:20 +00:00
exec ( php_path () . " -q " . BASEDIR . " /actions/video_convert_test.php $targetFileName sleep&> /dev/null & " );
2013-10-07 12:17:06 +00:00
}
}
?>