_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"); } } }