|
| 1 | +<?php |
| 2 | + class rtsp_client { |
| 3 | + private $addr; |
| 4 | + private $port; |
| 5 | + private $user; |
| 6 | + private $pass; |
| 7 | + |
| 8 | + private $cseq = 1; |
| 9 | + private $auth = array(); |
| 10 | + |
| 11 | + private $socket; |
| 12 | + |
| 13 | + function __construct($addr, $port, $user = false, $pass = false) { |
| 14 | + $this->addr = $addr; |
| 15 | + $this->port = $port; |
| 16 | + $this->user = $user; |
| 17 | + $this->pass = $pass; |
| 18 | + |
| 19 | + $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
| 20 | + } |
| 21 | + |
| 22 | + public function connect() { |
| 23 | + if (socket_connect($this->socket, $this->addr, $this->port)) { |
| 24 | + return true; |
| 25 | + } else { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public function disconnect() { |
| 31 | + socket_shutdown($socket); |
| 32 | + socket_close($socket); |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + * public function is_connected() { |
| 38 | + * |
| 39 | + * } |
| 40 | + */ |
| 41 | + |
| 42 | + public function socket_last_error() { |
| 43 | + return socket_last_error($this->socket); |
| 44 | + } |
| 45 | + |
| 46 | + private function digest($user, $realm, $password, $nonce, $method, $uri) { |
| 47 | + $ha1 = hash("md5", $user.":".$realm.":".$password); |
| 48 | + $ha2 = hash("md5", $method.":".$uri); |
| 49 | + $result = hash("md5", $ha1.":".$nonce.":".$ha2); |
| 50 | + return $result; |
| 51 | + } |
| 52 | + |
| 53 | + public function request($method, $uri, $options = array()) { |
| 54 | + if (count($this->auth) > 0) { $options['Authorization'] = $this->auth['authtype'].' username="'.$this->user.'",realm="'.$this->auth['realm'].'",nonce="'.$this->auth['nonce'].'",uri="'.$this->auth['uri'].'",response="'.$this->digest($this->user, $this->auth['realm'], $this->pass, $this->auth['nonce'], $method, $this->auth['uri']).'"'; } |
| 55 | + $request = $method." ".$uri." RTSP/1.0\r\n"; |
| 56 | + $request .= "CSeq: ".$this->cseq++."\r\n"; |
| 57 | + foreach ($options as $k => $v) { |
| 58 | + $request .= $k.": ".$v."\r\n"; |
| 59 | + } |
| 60 | + $request .= "\r\n"; |
| 61 | + socket_write($this->socket, $request); |
| 62 | + |
| 63 | + $response = ''; |
| 64 | + while ($r = socket_read($this->socket, 2048)) { |
| 65 | + $response .= $r; |
| 66 | + if (strpos($response, "\r\n\r\n")) { break; } |
| 67 | + } |
| 68 | + $response = $this->interpret_response($response); |
| 69 | + if (isset($response['WWW-Authenticate'])) { |
| 70 | + // Did we already try to authenticate or do we not have credentials set? |
| 71 | + if (!$this->user || !$this->pass || isset($options['Authorization'])) { return 1; } // Authorization failed |
| 72 | + |
| 73 | + $wwwauth = preg_split('/(?<!,) /', $response['WWW-Authenticate']); |
| 74 | + if (count($wwwauth) != 2) { return 2; } // Invalid WWW-Authenticate string |
| 75 | + |
| 76 | + $authtype = $wwwauth[0]; |
| 77 | + if ($authtype != 'Digest') { return 3; } // Unsupported auth method |
| 78 | + |
| 79 | + $authdata = array(); |
| 80 | + $x = preg_split('/, */', $wwwauth[1]); |
| 81 | + foreach ($x as $a) { |
| 82 | + $y = preg_split('/=/', $a); |
| 83 | + $y[1] = preg_replace('/"/', '', $y[1]); |
| 84 | + $authdata[$y[0]] = (isset($y[1])) ? $y[1] : ''; |
| 85 | + } |
| 86 | + $this->auth = array( |
| 87 | + 'authtype'=>$authtype, |
| 88 | + 'realm'=>$authdata['realm'], |
| 89 | + 'nonce'=>$authdata['nonce'], |
| 90 | + 'uri'=>$uri |
| 91 | + ); |
| 92 | + $options['Authorization'] = $this->auth['authtype'].' username="'.$this->user.'",realm="'.$this->auth['realm'].'",nonce="'.$this->auth['nonce'].'",uri="'.$this->auth['uri'].'",response="'.$this->digest($this->user, $this->auth['realm'], $this->pass, $this->auth['nonce'], $method, $this->auth['uri']).'"'; |
| 93 | + return $this->request($method, $uri, $options); |
| 94 | + } |
| 95 | + return $response; |
| 96 | + } |
| 97 | + |
| 98 | + private function interpret_response($response) { |
| 99 | + $return = array(); |
| 100 | + $lines = preg_split('/\r\n/', $response); |
| 101 | + foreach ($lines as $k => $v) { |
| 102 | + if ($k == 0) { |
| 103 | + $r = preg_split('/ /', $v); |
| 104 | + $return['proto'] = (isset($r[0])) ? $r[0] : ''; |
| 105 | + $return['code'] = (isset($r[1])) ? $r[1] : ''; |
| 106 | + $return['msg'] = (isset($r[2])) ? $r[2] : ''; |
| 107 | + } else { |
| 108 | + $r = preg_split('/: /', $v); |
| 109 | + if (isset($r[0])) { $return[$r[0]] = (isset($r[1])) ? $r[1] : ''; } |
| 110 | + } |
| 111 | + } |
| 112 | + return $return; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | +?> |
0 commit comments