|
|
<?php
class Stream { public $_id; public $timestamp; public $premURL; public $filename; public $cstate; public $url; public $ip; public $hide; static public $validStates = ['begin', 'finished'];
function __construct($url, $premURL, $filename, $ip) { $this->_id = new MongoId(); $this->timestamp = time(); $this->premURL = $premURL; $this->filename = $filename; $this->cstate = 'getlink'; $this->url = $url; $this->ip = $ip; $this->hide = false;
$db = DbMongo::get(); $coll = $db->stream; $values = [ '_id' => $this->_id, 'timestamp' => $this->timestamp, 'ip' => $this->ip, 'url' => $this->url, 'cstate' => $this->cstate, 'filename' => $this->filename, 'hide' => $this->hide ]; $coll->insert($values); }
/** * Telecharge depuis $url vers $out. * * @param string $url URL a telecharger * * @return array Informations sur le transfert */ function get() { set_time_limit(0);
$ch = curl_init($this->premURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $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($this->premURL); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_exec($ch); $retour = curl_getinfo($ch); curl_close($ch);
$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]] ); }
static function listByState($state) { $tmpDT = new DateTime(); $db = DbMongo::get(); $coll = $db->stream; $retour = [];
// TODO : lister statut possible
foreach ($coll->find(['cstate' => $state]) as $element) { $element['time'] = $tmpDT->setTimestamp($element['timestamp']); $retour[] = $element; }
return $retour; }
static function clear($state) { $db = DbMongo::get(); $coll = $db->stream;
$coll->update( [], [ '$set' => [ 'hide' => true ]], [ 'multiple' => true ] ); }}
|