Gestion download au travers de premiumizeme
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
3.0 KiB

<?php
class DLink {
/**
* Telecharge le fichier
*
* @return array Informations sur le transfert
*/
public static function get(Download $element)
{
// On le creer tout de suite pour eviter des conflits de process
touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$element->filename);
$tmpFilename = $element->_id.'-'.time();
$fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$tmpFilename, 'w+');
$hp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$tmpFilename.'.headers', 'w+');
#https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl
$ch = curl_init($element->url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT_OUT);
curl_setopt($ch, CURLOPT_WRITEHEADER, $hp);
curl_exec($ch);
fclose($fp);
$retour = curl_getinfo($ch);
// https://stackoverflow.com/questions/11842721/cant-get-remote-filename-to-file-get-contents-and-then-store-file
if (curl_errno($ch)) {
// TODO: gerer les erreurs
}
curl_close($ch);
rewind($hp);
$headers = stream_get_contents($hp);
fclose($hp);
//unlink(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$tmpFilename.'.header');
$element->addEvent([
'state' => 'downloaded',
'curlinfo' => [
'http_code' => $retour['http_code'],
'total_time' => $retour['total_time'],
'namelookup_time' => $retour['namelookup_time'],
'connect_time' => $retour['connect_time'],
'pretransfer_time' => $retour['pretransfer_time'],
'size_download' => $retour['size_download'],
'speed_download' => $retour['speed_download'],
'starttransfer_time' => $retour['starttransfer_time']
]
]);
if (200 !== $retour['http_code']) {
$element->updateState('error');
return false;
}
$element->updateState('downloaded');
$realFilename = self::getRealFilename($element->url, $headers);
// TODO: à adapter
if (file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$realFilename)) {
$element->addEvent(['state' => 'dupp' ]);
$element->updateState('dupp');
// TODO: remove temp
return false;
}
$element->updateFilename($realFilename);
rename(
DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$tmpFilename,
DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$realFilename
);
return $retour;
}
public static function getRealFilename($url, $headers)
{
if (preg_match('/Content-Disposition: .*filename=([^ ]+)/', $headers, $matches)) {
return $matches[1];
}
$stripped_url = preg_replace('/\\?.*/', '', $url);
return basename($stripped_url);
}
}