123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 11/24/15
- * Time: 3:10 PM
- */
-
- namespace App\Http\Business\Cameras\Trendnet;
-
-
- use App\Http\Business\Cameras\AbstractCamera;
- use App\Http\DBO\CamerasDbo;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\RequestException;
- use Luticate\Utils\LuLog;
-
- class TVIP311PI extends AbstractCamera
- {
- const URI = "/Streaming/channels/1/httpPreview";
-
- private $_client;
- private $_url;
-
- public function __construct(CamerasDbo $camera)
- {
- parent::__construct($camera);
- $this->_client = new Client();
- $data = $camera->getData();
- $this->_url = "http://" . $data["Host"] . ":" . $data["Port"] . self::URI;
- }
-
- public function prepareImage()
- {
- $response = null;
- $username = $this->_camera->getData()["Username"];
- $passwd = $this->_camera->getData()["Password"];
- $host = $this->_camera->getData()["Host"];
- $port = $this->_camera->getData()["Port"];
- $uri = self::URI;
- $nonce = "";
-
- try {
- $this->_client->request('GET', $this->_url);
- } catch (RequestException $e) {
- $response = $e->getResponse();
- if ($response != null && $response->getStatusCode() == 401) {
- $nonce = array();
- preg_match("/nonce=\"([a-f0-9]+)\"/", $response->getHeader("WWW-Authenticate")[0], $nonce);
- $nonce = $nonce[1];
- }
- else {
- LuLog::log($e);
- }
-
- } catch (\Exception $e) {
- LuLog::log($e);
- }
-
- $cnonce = md5("42_" . rand());
- $realm = "TV-IP311PI";
- $resp = md5(md5("$username:$realm:$passwd").":$nonce:".md5("GET:$uri"));
- $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- if(socket_connect($sock, $host, $port))
- {
- socket_write($sock, "GET $uri HTTP/1.1\r\n".
- "Host: $host\r\n".
- "Authorization: Digest username=\"$username\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", response=\"$resp\", cnonce=\"$cnonce\", nc=\"00000001\"".
- "Connection: close\r\n".
- "Accept: */*\r\n\r\n");
- $data = "";
- while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 50000)
- $data .= $buf;
- socket_close($sock);
- if(substr_count($data, "--boundarySample") >= 2)
- {
- $data = substr($data, strpos($data, "\r\n\r\n") + 4);
- $data = substr($data, strpos($data, "\r\n\r\n") + 4);
- $data = substr($data, 0, strpos($data, "--boundarySample") - 2);
- $this->_image = $this->_imagine->load($data);
- }
- else {
- abort(500, "Failed to found boundary in device response");
- }
- }
- else {
- abort(500, "Failed to connect to device");
- }
- }
- }
|