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.

134 lines
3.8 KiB

6 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
  1. <?php
  2. class Stream {
  3. public $_id;
  4. public $timestamp;
  5. public $premURL;
  6. public $filename;
  7. public $cstate;
  8. public $url;
  9. public $ip;
  10. public $hide;
  11. static public $validStates = ['begin', 'finished'];
  12. function __construct($url, $premURL, $filename, $ip)
  13. {
  14. $this->_id = new MongoId();
  15. $this->timestamp = time();
  16. $this->premURL = $premURL;
  17. $this->filename = $filename;
  18. $this->cstate = 'getlink';
  19. $this->url = $url;
  20. $this->ip = $ip;
  21. $this->hide = false;
  22. $db = DbMongo::get();
  23. $coll = $db->stream;
  24. $values = [
  25. '_id' => $this->_id,
  26. 'timestamp' => $this->timestamp,
  27. 'ip' => $this->ip,
  28. 'url' => $this->url,
  29. 'cstate' => $this->cstate,
  30. 'filename' => $this->filename,
  31. 'hide' => $this->hide
  32. ];
  33. $coll->insert($values);
  34. }
  35. /**
  36. * Telecharge depuis $url vers $out.
  37. *
  38. * @param string $url URL a telecharger
  39. *
  40. * @return array Informations sur le transfert
  41. */
  42. function get()
  43. {
  44. set_time_limit(0);
  45. $ch = curl_init($this->premURL);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
  48. curl_setopt($ch, CURLOPT_HEADER, true);
  49. curl_setopt($ch, CURLOPT_NOBODY, true);
  50. curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  51. $res = curl_exec($ch);
  52. curl_close($ch);
  53. if (!preg_match('/Content-Disposition: attachment; filename=/', $res)) {
  54. $res .= "Content-Disposition: attachment; filename=\"".$this->filename."\"; filename*=UTF-8''".$this->filename."\n";
  55. }
  56. foreach(explode("\n", $res) as $headerLine) {
  57. header($headerLine);
  58. }
  59. $ch = curl_init($this->premURL);
  60. curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  61. curl_exec($ch);
  62. $retour = curl_getinfo($ch);
  63. curl_close($ch);
  64. $this->cstate = 'finished';
  65. $db = DbMongo::get();
  66. $coll = $db->stream;
  67. $dbRet = $coll->update(
  68. ['_id' => $this->_id],
  69. ['$set' => ['cstate' => $this->cstate]]
  70. );
  71. $this->addEvent([
  72. 'timestamp' => time(),
  73. 'state' => $this->cstate,
  74. 'curlinfo' => [
  75. 'http_code' => $retour['http_code'],
  76. 'total_time' => $retour['total_time'],
  77. 'namelookup_time' => $retour['namelookup_time'],
  78. 'connect_time' => $retour['connect_time'],
  79. 'pretransfer_time' => $retour['pretransfer_time'],
  80. 'size_download' => $retour['size_download'],
  81. 'speed_download' => $retour['speed_download'],
  82. 'starttransfer_time' => $retour['starttransfer_time']
  83. ]
  84. ]);
  85. }
  86. private function addEvent($event)
  87. {
  88. $db = DbMongo::get();
  89. $coll = $db->stream;
  90. $dbRet = $coll->update(
  91. ['_id' => $this->_id],
  92. ['$push' => ['event' => $event]]
  93. );
  94. }
  95. static function listByState($state)
  96. {
  97. $tmpDT = new DateTime();
  98. $db = DbMongo::get();
  99. $coll = $db->stream;
  100. $retour = [];
  101. // TODO : lister statut possible
  102. foreach ($coll->find(['cstate' => $state]) as $element) {
  103. $element['time'] = $tmpDT->setTimestamp($element['timestamp']);
  104. $retour[] = $element;
  105. }
  106. return $retour;
  107. }
  108. static function clear($state)
  109. {
  110. $db = DbMongo::get();
  111. $coll = $db->stream;
  112. $coll->update(
  113. [],
  114. [ '$set' => [ 'hide' => true ]],
  115. [ 'multiple' => true ]
  116. );
  117. }
  118. }