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.

170 lines
5.5 KiB

6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. class PremiumizeMe {
  3. /**
  4. * Converti le lien d'un hoster en un lien premiumizeme + informations
  5. *
  6. * @param string $url Url d'un hoster
  7. *
  8. * @return array[string]mixed Informations sur le lien premiumizeme
  9. */
  10. static function getLink($url) {
  11. $fields = [
  12. 'method' => 'directdownloadlink',
  13. 'params[login]' => getenv('PREM_LOGIN'),
  14. 'params[pass]' => getenv('PREM_PASS'),
  15. 'params[link]' => $url
  16. ];
  17. $httpFields = http_build_query($fields);
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. $curlResult = curl_exec($ch);
  22. curl_close($ch);
  23. $data = json_decode($curlResult);
  24. // TODO : exception et non exit bourrin
  25. if (400 === $data->status) {
  26. echo 'Erreur : Je ne supporte pas de fournisseur, merci de revenir a la page des t&eacute;l&eacute;chargement et d\'en choisir un autre.<br><br>Erreur exacte :<br>'.$data->statusmessage;
  27. exit();
  28. }
  29. if (null === $data->result) {
  30. if (503 === $data->status) {
  31. echo 'Erreur : Il semble que le lien ne fonctionne plus.<br>Vous pouvez verifier vous-m&ecirc;me ici : <a href="'.$url.'">'.$url.'</a><br><br>';
  32. echo 'Erreur exacte :<br>'.$data->statusmessage;
  33. } else {
  34. echo 'Erreur : <br>'.$data->statusmessage;
  35. }
  36. exit();
  37. } else {
  38. $retour = get_object_vars($data->result);
  39. return $retour;
  40. }
  41. }
  42. /**
  43. * Retourne les informations sur l'etat actuel du compte.
  44. *
  45. * @return array[string]mixed
  46. */
  47. static function getStatus() {
  48. $fields = [
  49. 'method' => 'accountstatus',
  50. 'params[login]' => getenv('PREM_LOGIN'),
  51. 'params[pass]' => getenv('PREM_PASS')
  52. ];
  53. $httpFields = http_build_query($fields);
  54. $ch = curl_init();
  55. curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  57. $curlResult = curl_exec($ch);
  58. curl_close($ch);
  59. $data = json_decode($curlResult);
  60. $retour = get_object_vars($data->result);
  61. $tmpDT = new DateTime();
  62. $tmpDT->setTimestamp($retour['expires']);
  63. $retour['expires'] = $tmpDT;
  64. return $retour;
  65. }
  66. /**
  67. * Retourne la liste des hosters ainsi que les regexps lie.
  68. *
  69. * @return array[mixed]mixed
  70. */
  71. static function getHosters() {
  72. $fields = [
  73. 'method' => 'hosterlist',
  74. 'params[login]' => getenv('PREM_LOGIN'),
  75. 'params[pass]' => getenv('PREM_PASS')
  76. ];
  77. $httpFields = http_build_query($fields);
  78. $ch = curl_init();
  79. curl_setopt($ch, CURLOPT_URL, 'http://api.premiumize.me/pm-api/v1.php?'.$httpFields);
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  81. $curlResult = curl_exec($ch);
  82. curl_close($ch);
  83. $data = json_decode($curlResult);
  84. $retour = get_object_vars($data->result);
  85. foreach($retour['hosterlist'] as $hoster) {
  86. $retour['hostershort'][] = strstr($hoster, '.', true);
  87. }
  88. return $retour;
  89. }
  90. /**
  91. * Telecharge le fichier
  92. *
  93. * @return array Informations sur le transfert
  94. */
  95. public static function get(Download $element)
  96. {
  97. // On le creer tout de suite pour eviter des conflits de process
  98. touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$element->filename);
  99. $premInfos = PremiumizeMe::getLink($element->url);
  100. if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename']) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename'])) {
  101. $element->addEvent(['state' => 'dupp' ]);
  102. $element->updateState('dupp');
  103. return false;
  104. }
  105. // TODO : gérer les erreurs ici !!!!
  106. $element->addEvent([
  107. 'state' => 'converted',
  108. 'premURL' => $premInfos['location'],
  109. 'filename' => $premInfos['filename']
  110. ]);
  111. $fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'], 'w+');
  112. $ch = curl_init($premInfos['location']);
  113. curl_setopt($ch, CURLOPT_FILE, $fp);
  114. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  116. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  117. curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT_OUT);
  118. curl_exec($ch);
  119. $retour = curl_getinfo($ch);
  120. curl_close($ch);
  121. fclose($fp);
  122. $element->updateState('downloaded');
  123. $element->addEvent([
  124. 'state' => 'downloaded',
  125. 'curlinfo' => [
  126. 'http_code' => $retour['http_code'],
  127. 'total_time' => $retour['total_time'],
  128. 'namelookup_time' => $retour['namelookup_time'],
  129. 'connect_time' => $retour['connect_time'],
  130. 'pretransfer_time' => $retour['pretransfer_time'],
  131. 'size_download' => $retour['size_download'],
  132. 'speed_download' => $retour['speed_download'],
  133. 'starttransfer_time' => $retour['starttransfer_time']
  134. ]
  135. ]);
  136. if (200 !== $retour['http_code']) {
  137. $element->updateState('error');
  138. return false;
  139. }
  140. $element->updateFilename($premInfos['filename']);
  141. rename(
  142. DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$premInfos['filename'],
  143. DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$premInfos['filename']
  144. );
  145. $element->updateState('downloaded');
  146. return $retour;
  147. }
  148. }