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