Browse Source

stream un peu plus oo, plus delog en base

master
Cabillot Julien 6 years ago
parent
commit
dda6bf0d11
2 changed files with 80 additions and 14 deletions
  1. 19
      controller/stream.php
  2. 75
      lib/Stream.php

19
controller/stream.php

@ -3,20 +3,19 @@
function getStream()
{
if (!isset($_GET['myurl'])) {
// TODO : ajouter log
echo 'pas de get';
exit();
}
$premInfos = PremiumizeMe::getLink($_GET['myurl']);
$db = DbMongo::get();
$coll = $db->stream;
$coll->insert([
'timestamp' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'url' => $_GET['myurl'],
'premURL' => $premInfos['location'],
'filename' => $premInfos['filename']
]);
$result = Stream::get($premInfos['location'], $premInfos['filename']);
$myStream = new Stream(
$_GET['myurl'],
$premInfos['location'],
$premInfos['filename'],
$_SERVER['REMOTE_ADDR']
);
$myStream->get();
}

75
lib/Stream.php

@ -1,6 +1,36 @@
<?php
class Stream {
public $_id;
public $timestamp;
public $premURL;
public $filename;
public $cstate;
public $url;
public $ip;
function __construct($url, $premURL, $filename, $ip)
{
$this->_id = new MongoId();
$this->timestamp = time();
$this->premURL = $premURL;
$this->filename = $filename;
$this->cstate = 'begin';
$this->url = $url;
$this->ip = $ip;
$db = DbMongo::get();
$coll = $db->stream;
$coll->insert([
'_id' => $this->_id,
'timestamp' => $this->timestamp,
'ip' => $this->ip,
'url' => $this->url,
'cstate' => $this->cstate,
'filename' => $this->filename
]);
}
/**
* Telecharge depuis $url vers $out.
*
@ -8,10 +38,11 @@ class Stream {
*
* @return array Informations sur le transfert
*/
static function get($url, $filename) {
function get()
{
set_time_limit(0);
$ch = curl_init($url);
$ch = curl_init($this->premURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_HEADER, true);
@ -20,15 +51,51 @@ class Stream {
$res = curl_exec($ch);
curl_close($ch);
if (!preg_match('/Content-Disposition: attachment; filename=/', $res)) {
$res .= "Content-Disposition: attachment; filename=\"".$this->filename."\"; filename*=UTF-8''".$this->filename."\n";
}
foreach(explode("\n", $res) as $headerLine) {
header($headerLine);
}
$ch = curl_init($url);
$ch = curl_init($this->premURL);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_exec($ch);
$retour = curl_getinfo($ch);
curl_close($ch);
return $retour;
$this->cstate = 'finished';
$db = DbMongo::get();
$coll = $db->stream;
$dbRet = $coll->update(
['_id' => $this->_id],
['$set' => ['cstate' => $this->cstate]]
);
$this->addEvent([
'timestamp' => time(),
'state' => $this->cstate,
'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']
]
]);
}
private function addEvent($event)
{
$db = DbMongo::get();
$coll = $db->stream;
$dbRet = $coll->update(
['_id' => $this->_id],
['$push' => ['event' => $event]]
);
}
}
Loading…
Cancel
Save