1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use App\Models\Release; |
4
|
|
|
use Blacklight\NZB; |
5
|
|
|
use Blacklight\utility\Utility; |
6
|
|
|
use Chumper\Zipper\Zipper; |
7
|
|
|
use Colors\Color; |
8
|
|
|
use Blacklight\XXX; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use Tuna\CloudflareMiddleware; |
11
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
12
|
|
|
use GuzzleHttp\Cookie\SetCookie; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
use Illuminate\Support\Facades\Log; |
15
|
|
|
use GuzzleHttp\Cookie\FileCookieJar; |
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
use GuzzleHttp\Exception\RequestException; |
18
|
|
|
|
19
|
|
|
if (! function_exists('getRawHtml')) { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $url |
23
|
|
|
* @param bool $cookie |
24
|
|
|
* |
25
|
|
|
* @return bool|mixed|string |
26
|
|
|
*/ |
27
|
|
|
function getRawHtml($url, $cookie = false) |
28
|
|
|
{ |
29
|
|
|
$cookieJar = new CookieJar(); |
30
|
|
|
$client = new Client(['headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246']]); |
31
|
|
|
if ($cookie !== false) { |
32
|
|
|
$cookie = $cookieJar->setCookie(SetCookie::fromString($cookie)); |
|
|
|
|
33
|
|
|
$client = new Client(['cookies' => $cookie, 'headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246']]); |
34
|
|
|
} |
35
|
|
|
try { |
36
|
|
|
$response = $client->get($url)->getBody()->getContents(); |
37
|
|
|
$jsonResponse = json_decode($response, true); |
38
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
39
|
|
|
$response = $jsonResponse; |
40
|
|
|
} |
41
|
|
|
} catch (RequestException $e) { |
42
|
|
|
if (config('app.debug') === true) { |
43
|
|
|
Log::error($e->getMessage()); |
44
|
|
|
} |
45
|
|
|
$response = false; |
46
|
|
|
} catch (\RuntimeException $e) { |
47
|
|
|
if (config('app.debug') === true) { |
48
|
|
|
Log::error($e->getMessage()); |
49
|
|
|
} |
50
|
|
|
$response = false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $response; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (! function_exists('getRawHtmlThroughCF')) { |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $url |
61
|
|
|
* |
62
|
|
|
* @return bool|mixed|string |
63
|
|
|
*/ |
64
|
|
|
function getRawHtmlThroughCF($url) |
65
|
|
|
{ |
66
|
|
|
$client = new Client(['cookies' => new FileCookieJar('cookies.txt'), 'headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246']]); |
67
|
|
|
$client->getConfig('handler')->push(CloudflareMiddleware::create()); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$response = $client->get($url)->getBody()->getContents(); |
71
|
|
|
$jsonResponse = json_decode($response, true); |
72
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
73
|
|
|
$response = $jsonResponse; |
74
|
|
|
} |
75
|
|
|
} catch (RequestException $e) { |
76
|
|
|
if (config('app.debug') === true) { |
77
|
|
|
Log::error($e->getMessage()); |
78
|
|
|
} |
79
|
|
|
$response = false; |
80
|
|
|
} catch (\RuntimeException $e) { |
81
|
|
|
if (config('app.debug') === true) { |
82
|
|
|
Log::error($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
$response = false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (! function_exists('makeFieldLinks')) { |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $data |
95
|
|
|
* @param $field |
96
|
|
|
* @param $type |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
function makeFieldLinks($data, $field, $type) |
102
|
|
|
{ |
103
|
|
|
$tmpArr = explode(', ', $data[$field]); |
104
|
|
|
$newArr = []; |
105
|
|
|
$i = 0; |
106
|
|
|
foreach ($tmpArr as $ta) { |
107
|
|
|
if (trim($ta) === '') { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
if ($type === 'xxx' && $field === 'genre') { |
111
|
|
|
$ta = (new XXX())->getGenres(true, $ta); |
112
|
|
|
$ta = $ta['title']; |
113
|
|
|
} |
114
|
|
|
if ($i > 7) { |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
$newArr[] = '<a href="'.WWW_TOP.'/'.ucfirst($type).'?'.$field.'='.urlencode($ta).'" title="'.$ta.'">'.$ta.'</a>'; |
|
|
|
|
118
|
|
|
$i++; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return implode(', ', $newArr); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (! function_exists('getUserBrowseOrder')) { |
125
|
|
|
/** |
126
|
|
|
* @param string $orderBy |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
function getUserBrowseOrder($orderBy): array |
131
|
|
|
{ |
132
|
|
|
$order = ($orderBy === '' ? 'username_desc' : $orderBy); |
133
|
|
|
$orderArr = explode('_', $order); |
134
|
|
|
switch ($orderArr[0]) { |
135
|
|
|
case 'username': |
136
|
|
|
$orderField = 'username'; |
137
|
|
|
break; |
138
|
|
|
case 'email': |
139
|
|
|
$orderField = 'email'; |
140
|
|
|
break; |
141
|
|
|
case 'host': |
142
|
|
|
$orderField = 'host'; |
143
|
|
|
break; |
144
|
|
|
case 'createdat': |
145
|
|
|
$orderField = 'created_at'; |
146
|
|
|
break; |
147
|
|
|
case 'lastlogin': |
148
|
|
|
$orderField = 'lastlogin'; |
149
|
|
|
break; |
150
|
|
|
case 'apiaccess': |
151
|
|
|
$orderField = 'apiaccess'; |
152
|
|
|
break; |
153
|
|
|
case 'apirequests': |
154
|
|
|
$orderField = 'apirequests'; |
155
|
|
|
break; |
156
|
|
|
case 'grabs': |
157
|
|
|
$orderField = 'grabs'; |
158
|
|
|
break; |
159
|
|
|
case 'roles_id': |
160
|
|
|
$orderField = 'users_role_id'; |
161
|
|
|
break; |
162
|
|
|
case 'rolechangedate': |
163
|
|
|
$orderField = 'rolechangedate'; |
164
|
|
|
break; |
165
|
|
|
default: |
166
|
|
|
$orderField = 'username'; |
167
|
|
|
break; |
168
|
|
|
} |
169
|
|
|
$orderSort = (isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1])) ? $orderArr[1] : 'desc'; |
170
|
|
|
|
171
|
|
|
return [$orderField, $orderSort]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (! function_exists('getUserBrowseOrdering')) { |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
function getUserBrowseOrdering(): array |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
'username_asc', |
184
|
|
|
'username_desc', |
185
|
|
|
'email_asc', |
186
|
|
|
'email_desc', |
187
|
|
|
'host_asc', |
188
|
|
|
'host_desc', |
189
|
|
|
'createdat_asc', |
190
|
|
|
'createdat_desc', |
191
|
|
|
'lastlogin_asc', |
192
|
|
|
'lastlogin_desc', |
193
|
|
|
'apiaccess_asc', |
194
|
|
|
'apiaccess_desc', |
195
|
|
|
'apirequests_asc', |
196
|
|
|
'apirequests_desc', |
197
|
|
|
'grabs_asc', |
198
|
|
|
'grabs_desc', |
199
|
|
|
'role_asc', |
200
|
|
|
'role_desc', |
201
|
|
|
'rolechangedate_asc', |
202
|
|
|
'rolechangedate_desc', |
203
|
|
|
'verification_asc', |
204
|
|
|
'verification_desc', |
205
|
|
|
]; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (! function_exists('createGUID')) { |
210
|
|
|
/** |
211
|
|
|
* @return string |
212
|
|
|
* @throws \Exception |
213
|
|
|
*/ |
214
|
|
|
function createGUID(): string |
215
|
|
|
{ |
216
|
|
|
$data = random_bytes(16); |
217
|
|
|
$data[6] = \chr(\ord($data[6]) & 0x0f | 0x40); // set version to 0100 |
218
|
|
|
$data[8] = \chr(\ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 |
219
|
|
|
|
220
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(sodium_bin2hex($data), 4)); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (! function_exists('getSimilarName')) { |
225
|
|
|
/** |
226
|
|
|
* @param string $name |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
function getSimilarName($name): string |
231
|
|
|
{ |
232
|
|
|
return implode(' ', \array_slice(str_word_count(str_replace(['.', '_'], ' ', $name), 2), 0, 2)); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (! function_exists('color')) { |
237
|
|
|
/** |
238
|
|
|
* @param string $string |
239
|
|
|
* |
240
|
|
|
* @return \Colors\Color |
241
|
|
|
*/ |
242
|
|
|
function color($string = ''): Color |
243
|
|
|
{ |
244
|
|
|
return new Color($string); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if (! function_exists('human_filesize')) { |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param $bytes |
252
|
|
|
* @param int $decimals |
253
|
|
|
* |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
function human_filesize($bytes, $decimals = 0): string |
257
|
|
|
{ |
258
|
|
|
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
259
|
|
|
$factor = floor((\strlen($bytes) - 1) / 3); |
260
|
|
|
|
261
|
|
|
return round(sprintf("%.{$decimals}f", $bytes / (1024 ** $factor)), $decimals).@$size[$factor]; |
|
|
|
|
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if (! function_exists('bcdechex')) { |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param $dec |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
function bcdechex($dec) |
273
|
|
|
{ |
274
|
|
|
$hex = ''; |
275
|
|
|
do { |
276
|
|
|
$last = bcmod($dec, 16); |
277
|
|
|
$hex = dechex($last).$hex; |
|
|
|
|
278
|
|
|
$dec = bcdiv(bcsub($dec, $last), 16); |
279
|
|
|
} while ($dec > 0); |
280
|
|
|
|
281
|
|
|
return $hex; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if (! function_exists('runCmd')) { |
286
|
|
|
/** |
287
|
|
|
* Run CLI command. |
288
|
|
|
* |
289
|
|
|
* |
290
|
|
|
* @param string $command |
291
|
|
|
* @param bool $debug |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
function runCmd($command, $debug = false) |
296
|
|
|
{ |
297
|
|
|
if ($debug) { |
298
|
|
|
echo '-Running Command: '.PHP_EOL.' '.$command.PHP_EOL; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$process = new Process($command); |
|
|
|
|
302
|
|
|
$process->run(); |
303
|
|
|
$output = $process->getOutput(); |
304
|
|
|
|
305
|
|
|
if ($debug) { |
306
|
|
|
echo '-Command Output: '.PHP_EOL.' '.$output.PHP_EOL; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $output; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if (! function_exists('escapeString')) { |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param $string |
317
|
|
|
* |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
function escapeString($string) |
321
|
|
|
{ |
322
|
|
|
return DB::connection()->getPdo()->quote($string); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
if (! function_exists('realDuration')) { |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param $milliseconds |
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
function realDuration($milliseconds) |
334
|
|
|
{ |
335
|
|
|
$time = round($milliseconds / 1000); |
336
|
|
|
|
337
|
|
|
return sprintf('%02dh:%02dm:%02ds', $time / 3600, $time / 60 % 60, $time % 60); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if (! function_exists('is_it_json')) { |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param array|string $isIt |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
|
|
function is_it_json($isIt) |
348
|
|
|
{ |
349
|
|
|
if (is_array($isIt)) { |
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
json_decode($isIt); |
353
|
|
|
|
354
|
|
|
return json_last_error() === JSON_ERROR_NONE; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if (! function_exists('getZipped')) { |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param array $guids |
362
|
|
|
* |
363
|
|
|
* @return string |
364
|
|
|
* @throws \Exception |
365
|
|
|
*/ |
366
|
|
|
function getZipped(array $guids = []): string |
367
|
|
|
{ |
368
|
|
|
$nzb = new NZB(); |
369
|
|
|
$zipped = new Zipper(); |
370
|
|
|
$zippedFileName = now()->format('Ymdhis') . '.nzb.zip'; |
371
|
|
|
$zippedFilePath = resource_path() . '/tmp/' . $zippedFileName; |
372
|
|
|
|
373
|
|
|
foreach ($guids as $guid) { |
374
|
|
|
$nzbPath = $nzb->NZBPath($guid); |
375
|
|
|
|
376
|
|
|
if ($nzbPath) { |
377
|
|
|
$nzbContents = Utility::unzipGzipFile($nzbPath); |
378
|
|
|
|
379
|
|
|
if ($nzbContents) { |
380
|
|
|
$filename = $guid; |
381
|
|
|
$r = Release::getByGuid($guid); |
382
|
|
|
if ($r) { |
383
|
|
|
$filename = $r['searchname']; |
384
|
|
|
} |
385
|
|
|
$zipped->make($zippedFilePath)->addString($filename . '.nzb', $nzbContents); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
$zipped->close(); |
391
|
|
|
|
392
|
|
|
return File::isFile($zippedFilePath) ? $zippedFilePath : ''; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|