1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: nicolas |
5
|
|
|
* Date: 05/01/18 |
6
|
|
|
* Time: 12:00 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Devgiants\Service; |
10
|
|
|
|
11
|
|
|
use Buzz\Browser; |
12
|
|
|
use Buzz\Message\Request; |
13
|
|
|
use Buzz\Util\Cookie; |
14
|
|
|
use Buzz\Util\CookieJar; |
15
|
|
|
use Buzz\Message\MessageInterface; |
16
|
|
|
use Buzz\Util\Url; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Devgiants\Exception\AuthenticationException; |
19
|
|
|
|
20
|
|
|
class LiveboxTools { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Browser |
24
|
|
|
*/ |
25
|
|
|
protected $browser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $currentToken; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CookieJar |
34
|
|
|
*/ |
35
|
|
|
protected $cookieJar; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* LiveboxTools constructor. |
39
|
|
|
*/ |
40
|
|
|
public function __construct() { |
41
|
|
|
$this->browser = new Browser(); |
42
|
|
|
$this->cookieJar = new CookieJar(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Browser |
47
|
|
|
*/ |
48
|
|
|
public function getBrowser() { |
49
|
|
|
return $this->browser; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $host |
55
|
|
|
* @param $username |
56
|
|
|
* @param $password |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function authenticate( $host, $username, $password ) { |
61
|
|
|
|
62
|
|
|
// Get token response |
63
|
|
|
$response = $this->browser->post( |
64
|
|
|
"$host/ws", |
65
|
|
|
[ |
66
|
|
|
'Content-Type' => 'application/x-sah-ws-1-call+json; charset=UTF-8', |
67
|
|
|
'Authorization' => 'X-Sah-Login', |
68
|
|
|
], |
69
|
|
|
json_encode( [ |
70
|
|
|
'service' => 'sah.Device.Information', |
71
|
|
|
'method' => 'createContext', |
72
|
|
|
'parameters' => [ |
73
|
|
|
'applicationName' => 'so_sdkut', |
74
|
|
|
'username' => $username, |
75
|
|
|
'password' => $password, |
76
|
|
|
], |
77
|
|
|
] ) |
78
|
|
|
|
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
// Create sessid cookie |
83
|
|
|
$cookie = new Cookie(); |
84
|
|
|
|
85
|
|
|
$cookie->fromSetCookieHeader( (string) $response->getHeader( 'Set-Cookie' ), $host ); |
86
|
|
|
|
87
|
|
|
// Add cookie to JAR |
88
|
|
|
$this->cookieJar->addCookie( $cookie ); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
$json = json_decode( $response->getContent() ); |
92
|
|
|
|
93
|
|
|
if ( ( $json->status === 0 ) && isset( $json->data->contextID ) ) { |
94
|
|
|
$this->currentToken = $json->data->contextID; |
95
|
|
|
|
96
|
|
|
return $json->data->contextID; |
97
|
|
|
} else { |
98
|
|
|
// TODO handle |
99
|
|
|
throw new AuthenticationException(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getCookieHeaderForRequest() { |
108
|
|
|
$cookieString = "Cookie: "; |
109
|
|
|
|
110
|
|
|
foreach ( $this->cookieJar->getCookies() as $cookie ) { |
111
|
|
|
$cookieString .= "{$cookie->getName()}={$cookie->getValue()}; "; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
return $cookieString; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $method |
120
|
|
|
* @param $url |
121
|
|
|
* @param array $parameters |
122
|
|
|
* |
123
|
|
|
* @return MessageInterface |
124
|
|
|
*/ |
125
|
|
|
public function createRequest( $method, $url, $parameters = [] ) { |
126
|
|
|
// Create request from URL |
127
|
|
|
$request = new Request( $method ); |
128
|
|
|
$request->fromUrl( new Url( $url ) ); |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
// Add headers |
132
|
|
|
$request->setHeaders( [ |
133
|
|
|
'X-Context' => $this->currentToken, |
134
|
|
|
'X-Prototype-Version' => '1.7', |
135
|
|
|
'Content-Type' => 'application/x-sah-ws-1-call+json; charset=UTF-8', |
136
|
|
|
'Accept' => 'text/javascript', |
137
|
|
|
] ); |
138
|
|
|
|
139
|
|
|
// Add cookie header |
140
|
|
|
$request->addHeader( $this->getCookieHeaderForRequest() ); |
141
|
|
|
|
142
|
|
|
// Set content |
143
|
|
|
$request->setContent( json_encode( $parameters ) ); |
144
|
|
|
|
145
|
|
|
$response = $this->browser->send( $request ); |
146
|
|
|
|
147
|
|
|
return $response; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return \Buzz\Util\CookieJar |
152
|
|
|
*/ |
153
|
|
|
public function getCookieJar() { |
154
|
|
|
return $this->cookieJar; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $host |
159
|
|
|
*/ |
160
|
|
|
public function logout( $host ) { |
161
|
|
|
$this->browser->post( "$host/logout" ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|