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.

TVIP311PI.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 11/24/15
  6. * Time: 3:10 PM
  7. */
  8. namespace App\Http\Business\Cameras\Trendnet;
  9. use App\Http\Business\Cameras\AbstractCamera;
  10. use App\Http\DBO\CamerasDbo;
  11. use GuzzleHttp\Client;
  12. use GuzzleHttp\Exception\RequestException;
  13. use Luticate\Utils\LuLog;
  14. class TVIP311PI extends AbstractCamera
  15. {
  16. const URI = "/Streaming/channels/1/httpPreview";
  17. private $_client;
  18. private $_url;
  19. public function __construct(CamerasDbo $camera)
  20. {
  21. parent::__construct($camera);
  22. $this->_client = new Client();
  23. $data = $camera->getData();
  24. $this->_url = "http://" . $data["Host"] . ":" . $data["Port"] . self::URI;
  25. }
  26. public function prepareImage()
  27. {
  28. $response = null;
  29. $username = $this->_camera->getData()["Username"];
  30. $passwd = $this->_camera->getData()["Password"];
  31. $host = $this->_camera->getData()["Host"];
  32. $port = $this->_camera->getData()["Port"];
  33. $uri = self::URI;
  34. $nonce = "";
  35. try {
  36. $this->_client->request('GET', $this->_url);
  37. } catch (RequestException $e) {
  38. $response = $e->getResponse();
  39. if ($response != null && $response->getStatusCode() == 401) {
  40. $nonce = array();
  41. preg_match("/nonce=\"([a-f0-9]+)\"/", $response->getHeader("WWW-Authenticate")[0], $nonce);
  42. $nonce = $nonce[1];
  43. }
  44. else {
  45. LuLog::log($e);
  46. }
  47. } catch (\Exception $e) {
  48. LuLog::log($e);
  49. }
  50. $cnonce = md5("42_" . rand());
  51. $realm = "TV-IP311PI";
  52. $resp = md5(md5("$username:$realm:$passwd").":$nonce:".md5("GET:$uri"));
  53. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  54. if(socket_connect($sock, $host, $port))
  55. {
  56. socket_write($sock, "GET $uri HTTP/1.1\r\n".
  57. "Host: $host\r\n".
  58. "Authorization: Digest username=\"$username\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", response=\"$resp\", cnonce=\"$cnonce\", nc=\"00000001\"".
  59. "Connection: close\r\n".
  60. "Accept: */*\r\n\r\n");
  61. $data = "";
  62. while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 50000)
  63. $data .= $buf;
  64. socket_close($sock);
  65. if(substr_count($data, "--boundarySample") >= 2)
  66. {
  67. $data = substr($data, strpos($data, "\r\n\r\n") + 4);
  68. $data = substr($data, strpos($data, "\r\n\r\n") + 4);
  69. $data = substr($data, 0, strpos($data, "--boundarySample") - 2);
  70. $this->_image = $this->_imagine->load($data);
  71. }
  72. else {
  73. abort(500, "Failed to found boundary in device response");
  74. }
  75. }
  76. else {
  77. abort(500, "Failed to connect to device");
  78. }
  79. }
  80. }