Shareonlinebiz::fetchDownloadInfo()   B
last analyzed

Complexity

Conditions 6
Paths 15

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 21
cts 21
cp 1
rs 8.439
cc 6
eloc 22
nc 15
nop 1
crap 6
1
<?php
2
namespace Tartana\Host;
3
4
use GuzzleHttp\Psr7\Request;
5
use Tartana\Entity\Download;
6
use Tartana\Host\Common\Http;
7
8
class Shareonlinebiz extends Http
9
{
10
11 4
	public function fetchDownloadInfo(array $downloads)
12
	{
13 4
		foreach ($downloads as $download) {
14
			try {
15
				// Getting the link information
16 4
				$res = $this->getClient()->request(
17 4
					'get',
18 4
					'https://api.share-online.biz/linkcheck.php?md5=1&links=' . urlencode($download->getLink())
19
				);
20 3
				$csv = explode(';', $res->getBody()->getContents());
21 3
				if (count($csv) >= 2) {
22 2
					if ($csv[1] != 'OK') {
23 1
						$download->setState(Download::STATE_DOWNLOADING_ERROR);
24 1
						$download->setMessage($csv[1]);
25 1
					} elseif (count($csv) >= 5) {
26 1
						$download->setFileName($csv[2]);
27 1
						$download->setSize($csv[3]);
28 2
						$download->setHash(trim($csv[4]));
29
					}
30
				} else {
31 1
					parent::fetchDownloadInfo([
32 3
						$download
33
					]);
34
				}
35 1
			} catch (\Exception $e) {
36 1
				$this->log('Exception fetching file info for connection test: ' . $e->getMessage());
37 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
38 4
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
39
			}
40
		}
41 4
	}
42
43 6
	protected function getUrlToDownload(Download $download)
44
	{
45 6
		$res  = $this->getClient()->request('get', $download->getLink());
46 6
		$html = $res->getBody()->getContents();
47
48 6
		if (!preg_match(';var dl="(.+?)";s', $html, $match)) {
49 2
			return null;
50
		}
51
52 4
		$this->log('Share online base64 decoded: ' . base64_decode($match[1]) . ' real url: ' . $match[1]);
53
54 4
		return base64_decode($match[1]);
55
	}
56
57 8
	protected function login()
58
	{
59 8
		if ($this->getCookie('a')) {
60 1
			return true;
61
		}
62
63
		$args = [
64 7
			'user' => trim($this->getConfiguration()->get('shareonlinebiz.username')),
65 7
			'pass' => trim($this->getConfiguration()->get('shareonlinebiz.password'))
66
		];
67
68 7
		if (!$args['user']) {
69 1
			return false;
70
		}
71
72 6
		$res  = $this->getClient()->request('post', 'https://www.share-online.biz/user/login', [
73 6
			'form_params' => $args
74
		]);
75 6
		$html = $res->getBody()->getContents();
76 6
		return strpos($html, $args['user']) !== false;
77
	}
78
}
79