diff --git a/controller/download.php b/controller/download.php index 4a00d47..f4a98ba 100644 --- a/controller/download.php +++ b/controller/download.php @@ -1,5 +1,7 @@ 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); + } + +} diff --git a/lib/Download.php b/lib/Download.php index 61ed2e2..87059c3 100644 --- a/lib/Download.php +++ b/lib/Download.php @@ -4,10 +4,12 @@ class Download { public $_id; public $timestamp; public $premURL; + public $premFilename; public $url; public $filename; public $cstate; - static public $validStates = ['added', 'downloaded', 'dupp']; + public $worker; + static public $validStates = ['added', 'downloaded', 'downloading', 'dupp']; /** * Retourne les infos sur le prochain fichier a telecharger @@ -24,49 +26,37 @@ class Download { $myDownload = new Download(); $myDownload->db2Inst($dbRet); - $dbRet = $coll->update( - ['_id' => $myDownload->_id], - ['$set' => ['cstate' => 'downloading']] - ); + $myDownload->updateState('downloading'); $myDownload->addEvent([ - 'timestamp' => time(), 'state' => 'downloading' ]); return $myDownload; } - static function addDownload($url) + static function addDownload($url, $worker, $dtype) { - $premInfos = PremiumizeMe::getLink($url); - $myTime = time(); $myDownload = new Download(); - $db = DbMongo::get(); - $coll = $db->download; + $db = DbMongo::get(); + $coll = $db->download; $values = [ '_id' => new MongoId(), - 'timestamp' => $myTime, - 'premURL' => $premInfos['location'], - 'url' => $url, - 'filename' => $premInfos['filename'], - 'cstate' => 'added' - ]; + 'timestamp' => time(), + 'cstate' => 'added', + 'url' => $url, + 'dtype' => $dtype, + 'from' => $_SERVER['REMOTE_ADDR'], + 'worker' => $worker + ]; // TODO : verifier le retour $coll->insert($values); $myDownload->db2Inst($values); - - $myDownload->addEvent([ - 'timestamp' => $myTime, - 'ip' => $_SERVER['REMOTE_ADDR'], - 'url' => $url, - 'state' => 'added' - ]); - + return $myDownload; } @@ -74,19 +64,24 @@ class Download { { $this->_id = $dbRet['_id']; $this->timestamp = $dbRet['timestamp']; - $this->premURL = $dbRet['premURL']; $this->url = $dbRet['url']; - $this->filename = $dbRet['filename']; $this->cstate = $dbRet['cstate']; + $this->dtype = $dbRet['dtype']; + $this->worker = $dbRet['worker']; } - private function addEvent($event) + public function addEvent($event) { $db = DbMongo::get(); $coll = $db->download; $dbRet = $coll->update( ['_id' => $this->_id], - ['$push' => ['event' => $event]] + ['$push' => [ + 'event' => array_merge( + [ 'timestamp' => time() ], + $event + ) + ]] ); } @@ -98,71 +93,41 @@ class Download { public function get() { set_time_limit(0); - if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename)) { - $this->addEvent([ - 'timestamp' => time(), - 'state' => 'dupp' - ]); - - $db = DbMongo::get(); - $coll = $db->download; - $dbRet = $coll->update( - ['_id' => $this->_id], - ['$set' => ['cstate' => 'dupp']] - ); - return false; + if ('premiumizeme' === $this->dtype) { + PremiumizeMe::get($this); + } elseif ('dlink' === $this->dtype) { + DLink::get($this); } - // On le creer tout de suite pour eviter des conflits de process - touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename); - - $fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, 'w+'); - $ch = curl_init($this->premURL); - 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, $_SERVER['HTTP_USER_AGENT']); - curl_exec($ch); - $retour = curl_getinfo($ch); - curl_close($ch); - fclose($fp); - - $this->addEvent([ - 'timestamp' => time(), - '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']) { - $db = DbMongo::get(); - $coll = $db->download; - $dbRet = $coll->update( - ['_id' => $this->_id], - ['$set' => ['cstate' => 'error']] - ); - return false; - } + // TODO: coder un changeState + option c'est utilisé à trop d'endroit mee sur premiumizeme + public function updateState($state, $extras = []) + { + $db = DbMongo::get(); + $coll = $db->download; + $dbRet = $coll->update( + ['_id' => $this->_id], + ['$set' => [ + 'cstate' => $state, + 'extras' => $extras + ]] + ); + $this->cstate = $state; + } - rename(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename); - + + public function updateFilename($filename) + { $db = DbMongo::get(); $coll = $db->download; $dbRet = $coll->update( ['_id' => $this->_id], - ['$set' => ['cstate' => 'downloaded']] + ['$set' => [ + 'filename' => $filename + ]] ); - - return $retour; - } + $this->filename = $filename; + } /** * Lance le telechargement du prochain fichier a dl @@ -186,8 +151,10 @@ class Download { foreach ($coll->find(['cstate' => $state])->sort(['timestamp' => -1]) as $element) { $element['time'] = DateTime::createFromFormat('U', $element['timestamp']); - - if (file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$element['filename'])) { + + if (empty($element['filename'])) { + $element['href'] = false; + } elseif (file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$element['filename'])) { $element['href'] = true; } else { $element['href'] = false; diff --git a/lib/PremiumizeMe.php b/lib/PremiumizeMe.php index 89af6ee..40b09a1 100644 --- a/lib/PremiumizeMe.php +++ b/lib/PremiumizeMe.php @@ -95,6 +95,76 @@ class PremiumizeMe { foreach($retour['hosterlist'] as $hoster) { $retour['hostershort'][] = strstr($hoster, '.', true); } + return $retour; + } + + /** + * 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); + + $premInfos = PremiumizeMe::getLink($element->url); + + if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename']) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'])) { + $element->addEvent(['state' => 'dupp' ]); + $element->updateState('dupp'); + return false; + } + + // TODO : gérer les erreurs ici !!!! + $element->addEvent([ + 'state' => 'converted', + 'premURL' => $premInfos['location'], + 'filename' => $premInfos['filename'] + ]); + + $fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], 'w+'); + $ch = curl_init($premInfos['location']); + + 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_exec($ch); + $retour = curl_getinfo($ch); + curl_close($ch); + fclose($fp); + + $element->updateState('downloaded'); + $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->updateFilename($premInfos['filename']); + rename( + DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], + DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'] + ); + + $element->updateState('downloaded'); + return $retour; } + } diff --git a/root/js/my.js b/root/js/my.js index a0938c3..6ee16aa 100644 --- a/root/js/my.js +++ b/root/js/my.js @@ -20,7 +20,9 @@ function subURL() { url: "/download/", type: 'post', data: { - myurls: url + myurls: url, + dtype: dtype.value, + worker: worker.value }, dataType: 'json', success: function(data) { diff --git a/templates/status.php b/templates/status.php index d2380e6..d97df2c 100644 --- a/templates/status.php +++ b/templates/status.php @@ -22,6 +22,14 @@