-
Notifications
You must be signed in to change notification settings - Fork 4
/
webshare.php
282 lines (249 loc) · 5.92 KB
/
webshare.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php /** @noinspection SpellCheckingInspection */
/**
* Class SynoFileHostingWebshare
*
* @author Radovan Kepak <radovan@kepak.eu>
*/
class SynoFileHostingWebshare
{
/**
* Webshare API link
*/
const API_URL = 'https://webshare.cz/api';
/**
* @var string
*/
protected $url;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $password;
/**
* @var string
*/
protected $salt;
/**
* @var string
*/
protected $token;
/**
* SynoFileHostingWebshare constructor.
*
* @param $url string
* @param $username string
* @param $password string
*/
public function __construct($url, $username, $password)
{
$this->url = $url;
$this->username = $username;
$this->password = $password;
}
/**
* Try to get direct link for file
*
* @return array
*/
public function GetDownloadInfo()
{
try {
if ($this->isDirectLink($this->url)) {
$link = $this->url;
} else {
$ident = $this->getIdent($this->url);
if (!$ident) {
throw new \Exception('Identifier not found', ERR_NOT_SUPPORT_TYPE);
}
if (!$link = $this->getDirectLink($ident)) {
throw new \Exception('Link not found', ERR_FILE_NO_EXIST);
}
}
return [DOWNLOAD_URL => $link];
} catch (\Exception $e) {
return [DOWNLOAD_ERROR => $e->getCode()];
}
}
/**
* Get identifier from link
*
* @param string $url
* @return string|null
*/
protected function getIdent($url)
{
if (
@preg_match('~^https?://(?:beta\.)?webshare\.cz(?:/|#|/#|#/|/#/)file/(?P<ident>\w+)(?:/.*)?$~i', trim($url), $matches)
&& isset($matches['ident'])
) {
return $matches['ident'];
}
return null;
}
/**
* Is direct link inserted?
*
* @param string $url
* @return string|null
*/
protected function isDirectLink($url)
{
if (@preg_match('~^https?://(vip\.)?\d+\.dl\.webshare\.cz/.*$~i', trim($url))) {
return $url;
}
return null;
}
/**
* Get link for download
*
* @param string $ident
* @return string|false
* @throws \Exception
*/
protected function getDirectLink($ident)
{
if (!$this->getSalt()) {
throw new \Exception('Salt can`t be loaded', LOGIN_FAIL);
}
if (!$token = $this->getToken()) {
throw new \Exception('User can`t be logged!', LOGIN_FAIL);
}
$data = ['wst' => $token, 'ident' => $ident];
$response = $this->makeRequest('file_link', $data);
return $response ? $this->getXmlParam($response, 'link') : false;
}
/**
* Get salt
*
* @return string|false
*/
protected function getSalt()
{
if ($this->salt === null) {
$this->salt = false;
$response = $this->makeRequest('salt', [
'username_or_email' => $this->username,
]);
if ($response) {
$this->salt = $this->getXmlParam($response, 'salt');
}
}
return $this->salt;
}
/**
* @param string $action
* @param array $data
* @return bool|string
*/
protected function makeRequest($action, array $data)
{
$headers = ['Accept' => 'application/json'];
$url = self::API_URL . "/{$action}/";
$response = $this->request($url, $headers, $data);
$status = $this->getXmlParam($response, 'status');
return $status === 'OK' ? $response : false;
}
/**
* @param string $url
* @param array $headers
* @param array $data
* @return string
*/
protected function request($url, array $headers = [], array $data = [])
{
$curl = @curl_init();
//@curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
@curl_setopt($curl, CURLOPT_POST, true);
@curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
@curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
@curl_setopt($curl, CURLOPT_USERAGENT, DOWNLOAD_STATION_USER_AGENT);
@curl_setopt($curl, CURLOPT_COOKIEFILE, '/tmp/webshare.cookies.l');
@curl_setopt($curl, CURLOPT_TIMEOUT, 15);
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($curl, CURLOPT_URL, $url);
$response = @curl_exec($curl);
@curl_close($curl);
return $response;
}
/**
* Get param from XML response
*
* @param $xml string
* @param $key string
* @return bool|string
*/
protected function getXmlParamFallback($xml, $key)
{
$element = @preg_quote($key, '~');
if (@preg_match('~<(' . $element . ')>(?P<value>.*?)</(?1)>~i', $xml, $matches)) {
return (string)$matches['value'];
}
return false;
}
/**
* Get param from XML response
*
* @param $xml string
* @param $key string
* @return bool|string
*/
protected function getXmlParam($xml, $key)
{
if (!class_exists('SimpleXMLElement')) {
return $this->getXmlParamFallback($xml, $key);
}
try {
$data = new \SimpleXMLElement($xml);
return isset($data->{$key}) ? (string) $data->{$key} : false;
} catch (\Exception $e) {
return false;
}
}
/**
* Get token
*
* @param string salt
* @return null|string
*/
protected function getToken()
{
if ($this->token === null) {
$this->token = false;
if (!$salt = $this->getSalt()) {
return false;
}
$response = $this->makeRequest('login', [
'username_or_email' => $this->username,
'password' => sha1(crypt($this->password, '$1$' . $salt . '$')),
'digest' => md5($this->username . ':Webshare:' . $this->password),
]);
if ($response) {
$this->token = $this->getXmlParam($response, 'token');
}
}
return $this->token;
}
/**
* Verify account
*
* @return int
*/
public function Verify()
{
if (!$this->getSalt()) {
return LOGIN_FAIL;
}
if (!$token = $this->getToken()) {
return LOGIN_FAIL;
}
$response = $this->makeRequest('user_data', ['wst' => $token]);
if ((int)$this->getXmlParam($response, 'vip') === 1) {
return USER_IS_PREMIUM;
}
return USER_IS_FREE;
}
}