PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Chat-API/mediauploader.php

https://gitlab.com/jalon/doadoronline
PHP | 88 lines | 67 code | 14 blank | 7 comment | 3 complexity | 36a242eabfaba4e22d326018917f812c MD5 | raw file
  1. <?php
  2. /**
  3. * Media uploader class
  4. */
  5. class WhatsMediaUploader
  6. {
  7. protected static function sendData($host, $POST, $HEAD, $filepath, $mediafile, $TAIL)
  8. {
  9. $sock = fsockopen("ssl://" . $host, 443);
  10. fwrite($sock, $POST);
  11. fwrite($sock, $HEAD);
  12. //write file data
  13. $buf = 1024;
  14. $totalread = 0;
  15. $fp = fopen($filepath, "r");
  16. while ($totalread < $mediafile['filesize']) {
  17. $buff = fread($fp, $buf);
  18. fwrite($sock, $buff, $buf);
  19. $totalread += $buf;
  20. }
  21. //echo $TAIL;
  22. fwrite($sock, $TAIL);
  23. sleep(1);
  24. $data = fgets($sock, 8192);
  25. $data .= fgets($sock, 8192);
  26. $data .= fgets($sock, 8192);
  27. $data .= fgets($sock, 8192);
  28. $data .= fgets($sock, 8192);
  29. $data .= fgets($sock, 8192);
  30. $data .= fgets($sock, 8192);
  31. fclose($sock);
  32. list($header, $body) = preg_split("/\R\R/", $data, 2);
  33. $json = json_decode($body);
  34. if ( ! is_null($json)) {
  35. return $json;
  36. }
  37. return false;
  38. }
  39. public static function pushFile($uploadResponseNode, $messageContainer, $mediafile, $selfJID)
  40. {
  41. //get vars
  42. $url = $uploadResponseNode->getChild("media")->getAttribute("url");
  43. $filepath = $messageContainer["filePath"];
  44. $to = $messageContainer["to"];
  45. return self::getPostString($filepath, $url, $mediafile, $to, $selfJID);
  46. }
  47. protected static function getPostString($filepath, $url, $mediafile, $to, $from)
  48. {
  49. $host = parse_url($url, PHP_URL_HOST);
  50. //filename to md5 digest
  51. $cryptoname = md5($filepath) . "." . $mediafile['fileextension'];
  52. $boundary = "zzXXzzYYzzXXzzQQ";
  53. if (is_array($to)) {
  54. $to = implode(',', $to);
  55. }
  56. $hBAOS = "--" . $boundary . "\r\n";
  57. $hBAOS .= "Content-Disposition: form-data; name=\"to\"\r\n\r\n";
  58. $hBAOS .= $to . "\r\n";
  59. $hBAOS .= "--" . $boundary . "\r\n";
  60. $hBAOS .= "Content-Disposition: form-data; name=\"from\"\r\n\r\n";
  61. $hBAOS .= $from . "\r\n";
  62. $hBAOS .= "--" . $boundary . "\r\n";
  63. $hBAOS .= "Content-Disposition: form-data; name=\"file\"; filename=\"" . $cryptoname . "\"\r\n";
  64. $hBAOS .= "Content-Type: " . $mediafile['filemimetype'] . "\r\n\r\n";
  65. $fBAOS = "\r\n--" . $boundary . "--\r\n";
  66. $contentlength = strlen($hBAOS) + strlen($fBAOS) + $mediafile['filesize'];
  67. $POST = "POST " . $url . "\r\n";
  68. $POST .= "Content-Type: multipart/form-data; boundary=" . $boundary . "\r\n";
  69. $POST .= "Host: " . $host . "\r\n";
  70. $POST .= "User-Agent: " . Constants::WHATSAPP_USER_AGENT . "\r\n";
  71. $POST .= "Content-Length: " . $contentlength . "\r\n\r\n";
  72. return self::sendData($host, $POST, $hBAOS, $filepath, $mediafile, $fBAOS);
  73. }
  74. }