Completed
Push — master ( 1e5574...2c7872 )
by C
06:04
created

Shareonlinebiz::fetchDownloadInfo()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 37
Code Lines 19

Duplication

Lines 19
Ratio 51.35 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 37
rs 8.439
cc 5
eloc 19
nc 13
nop 1
1
<?php
2
namespace Tartana\Host;
3
use GuzzleHttp\Psr7\Request;
4
use Tartana\Entity\Download;
5
use Tartana\Host\Common\Http;
6
7
class Shareonlinebiz extends Http
8
{
9
10
	public function fetchDownloadInfo (array $downloads)
11
	{
12
		foreach ($downloads as $download)
13
		{
14
			try
15
			{
16
				// Getting the link information
17
				$res = $this->getClient()->request('get', 'https://api.share-online.biz/linkcheck.php?md5=1&links=' . urlencode($download->getLink()));
18
				$csv = explode(';', $res->getBody()->getContents());
19 View Code Duplication
				if (count($csv) >= 5)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
				{
21
					if ($csv[1] != 'OK')
22
					{
23
						$download->setState(Download::STATE_DOWNLOADING_ERROR);
24
						$download->setMessage($csv[1]);
25
					}
26
					else
27
					{
28
						$download->setFileName($csv[2]);
29
						$download->setSize($csv[3]);
30
					}
31
				}
32
				else
33
				{
34
					parent::fetchDownloadInfo([
35
							$download
36
					]);
37
				}
38
			}
39
			catch (\Exception $e)
40
			{
41
				$this->log('Exception fetching head for connection test: ' . $e->getMessage());
42
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
43
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
44
			}
45
		}
46
	}
47
48
	protected function getUrlToDownload (Download $download)
49
	{
50
		$res = $this->getClient()->request('get', $download->getLink());
51
		$html = $res->getBody()->getContents();
52
53
		if (! preg_match(';var dl="(.+?)";s', $html, $match))
54
		{
55
			$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_MD5');
56
			return null;
57
		}
58
59
		$this->log('Share online base64 decoded: ' . base64_decode($match[1]) . ' real url: ' . $match[1]);
60
61
		return base64_decode($match[1]);
62
	}
63
64
	protected function login ()
65
	{
66
		if ($this->hasCookie('a'))
67
		{
68
			return true;
69
		}
70
71
		$args = [
72
				'user' => trim($this->getConfiguration()->get('shareonlinebiz.username')),
73
				'pass' => trim($this->getConfiguration()->get('shareonlinebiz.password'))
74
		];
75
76
		if (! $args['user'])
77
		{
78
			return false;
79
		}
80
81
		$res = $this->getClient()->request('post', 'https://www.share-online.biz/user/login', [
82
				'form_params' => $args
83
		]);
84
		$html = $res->getBody()->getContents();
85
		return strpos($html, $args['user']) !== false;
86
	}
87
}
88