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
|
|
|
|
19
|
|
|
class LiveboxTools { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Browser |
23
|
|
|
*/ |
24
|
|
|
protected $browser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $currentToken; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CookieJar |
33
|
|
|
*/ |
34
|
|
|
protected $cookieJar; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* LiveboxTools constructor. |
38
|
|
|
*/ |
39
|
|
|
public function __construct() { |
40
|
|
|
$this->browser = new Browser(); |
41
|
|
|
$this->cookieJar = new CookieJar(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Browser |
46
|
|
|
*/ |
47
|
|
|
public function getBrowser() { |
48
|
|
|
return $this->browser; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $host |
54
|
|
|
* @param $username |
55
|
|
|
* @param $password |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function authenticate( $host, $username, $password ) { |
60
|
|
|
|
61
|
|
|
// Get token response |
62
|
|
|
$response = $this->browser->post( |
63
|
|
|
"$host/ws", |
64
|
|
|
[ |
65
|
|
|
'Content-Type' => 'application/x-sah-ws-1-call+json; charset=UTF-8', |
66
|
|
|
'Authorization' => 'X-Sah-Login', |
67
|
|
|
], |
68
|
|
|
json_encode( [ |
69
|
|
|
'service' => 'sah.Device.Information', |
70
|
|
|
'method' => 'createContext', |
71
|
|
|
'parameters' => [ |
72
|
|
|
'applicationName' => 'so_sdkut', |
73
|
|
|
'username' => $username, |
74
|
|
|
'password' => $password, |
75
|
|
|
], |
76
|
|
|
] ) |
77
|
|
|
|
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
// Create sessid cookie |
82
|
|
|
$cookie = new Cookie(); |
83
|
|
|
$cookie->fromSetCookieHeader( $response->getHeader( 'Set-Cookie' ), $host ); |
84
|
|
|
|
85
|
|
|
// Add cookie to JAR |
86
|
|
|
$this->cookieJar->addCookie( $cookie ); |
87
|
|
|
|
88
|
|
|
// foreach($response->getHeader('Set-Cookie') as $cookieString) { |
|
|
|
|
89
|
|
|
// $cookie = new CookieJar(); |
|
|
|
|
90
|
|
|
// } |
91
|
|
|
|
92
|
|
|
// $response = $this->client->post( "$host/ws", [ |
|
|
|
|
93
|
|
|
// RequestOptions::HEADERS => [ |
|
|
|
|
94
|
|
|
// 'Content-Type' => 'application/x-sah-ws-1-call+json; charset=UTF-8', |
|
|
|
|
95
|
|
|
// 'Authorization' => 'X-Sah-Login', |
|
|
|
|
96
|
|
|
// ], |
97
|
|
|
// RequestOptions::JSON => [ |
|
|
|
|
98
|
|
|
// 'service' => 'sah.Device.Information', |
|
|
|
|
99
|
|
|
// 'method' => 'createContext', |
|
|
|
|
100
|
|
|
// 'parameters' => [ |
|
|
|
|
101
|
|
|
// 'applicationName' => 'so_sdkut', |
|
|
|
|
102
|
|
|
// 'username' => $username, |
|
|
|
|
103
|
|
|
// 'password' => $password, |
|
|
|
|
104
|
|
|
// ], |
105
|
|
|
// ], |
106
|
|
|
// ] ); |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
// foreach ( $response->getHeader( 'Set-Cookie' ) as $cookieString ) { |
|
|
|
|
110
|
|
|
// $cookie = SetCookie::fromString( $cookieString ); |
|
|
|
|
111
|
|
|
// $cookie->setDomain( '192.168.1.1' ); |
|
|
|
|
112
|
|
|
// // var_dump( $this->cookieJar->setCookie($cookie)); |
113
|
|
|
// $this->cookieJar->save(); |
|
|
|
|
114
|
|
|
// } |
115
|
|
|
// echo($this->cookieJar->count()); |
|
|
|
|
116
|
|
|
|
117
|
|
|
// var_dump( $this->cookieJar->toArray() ); |
|
|
|
|
118
|
|
|
// die(); |
119
|
|
|
|
120
|
|
|
$json = json_decode( $response->getContent() ); |
121
|
|
|
|
122
|
|
|
if ( ( $json->status === 0 ) && isset( $json->data->contextID ) ) { |
123
|
|
|
$this->currentToken = $json->data->contextID; |
124
|
|
|
|
125
|
|
|
return $json->data->contextID; |
126
|
|
|
} else { |
127
|
|
|
// TODO handle |
128
|
|
|
throw new \AuthenticationException(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getCookieHeaderForRequest() { |
137
|
|
|
$cookieString = "Cookie: "; |
138
|
|
|
|
139
|
|
|
foreach($this->cookieJar->getCookies() as $cookie) { |
140
|
|
|
$cookieString .= "{$cookie->getName()}={$cookie->getValue()}; "; |
141
|
|
|
} |
142
|
|
|
// array_map( function ( Cookie $cookie ) use ( $cookieString ) { |
|
|
|
|
143
|
|
|
// $cookieString .= "{$cookie->getName()}={$cookie->getValue()}; "; |
144
|
|
|
// }, $this->cookieJar->getCookies() ); |
145
|
|
|
|
146
|
|
|
return $cookieString; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $method |
151
|
|
|
* @param $url |
152
|
|
|
* @param array $parameters |
153
|
|
|
* |
154
|
|
|
* @return MessageInterface |
155
|
|
|
*/ |
156
|
|
|
public function createRequest( $method, $url, $parameters = [] ) { |
157
|
|
|
// Create request from URL |
158
|
|
|
$request = new Request( $method ); |
159
|
|
|
$request->fromUrl( new Url( $url ) ); |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
// Add headers |
163
|
|
|
$request->setHeaders( [ |
164
|
|
|
'X-Context' => $this->currentToken, |
165
|
|
|
'X-Prototype-Version' => '1.7', |
166
|
|
|
'Content-Type' => 'application/x-sah-ws-1-call+json; charset=UTF-8', |
167
|
|
|
'Accept' => 'text/javascript', |
168
|
|
|
] ); |
169
|
|
|
|
170
|
|
|
// Add cookie header |
171
|
|
|
$request->addHeader( $this->getCookieHeaderForRequest() ); |
172
|
|
|
|
173
|
|
|
// Set content |
174
|
|
|
$request->setContent( json_encode( $parameters ) ); |
175
|
|
|
|
176
|
|
|
$response = $this->browser->send( $request ); |
177
|
|
|
|
178
|
|
|
return $response; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return \Buzz\Util\CookieJar |
183
|
|
|
*/ |
184
|
|
|
public function getCookieJar() { |
185
|
|
|
return $this->cookieJar; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $host |
190
|
|
|
*/ |
191
|
|
|
public function logout( $host ) { |
192
|
|
|
$response = $this->browser->post( "$host/logout" ); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths