1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see AppUtils\URLInfo_ConnectionTester} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage URLInfo |
7
|
|
|
* @see AppUtils\URLInfo_ConnectionTester |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Used to test whether an URL exists / can be connected to. |
16
|
|
|
* |
17
|
|
|
* @package Application Utils |
18
|
|
|
* @subpackage URLInfo |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class URLInfo_ConnectionTester implements Interface_Optionable |
22
|
|
|
{ |
23
|
|
|
use Traits_Optionable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var URLInfo |
27
|
|
|
*/ |
28
|
|
|
private $url; |
29
|
|
|
|
30
|
|
|
public function __construct(URLInfo $url) |
31
|
|
|
{ |
32
|
|
|
$this->url = $url; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getDefaultOptions() : array |
36
|
|
|
{ |
37
|
|
|
return array( |
38
|
|
|
'verify-ssl' => true, |
39
|
|
|
'curl-verbose' => false |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether to verify the host's SSL certificate, in |
45
|
|
|
* case of an https connection. |
46
|
|
|
* |
47
|
|
|
* @param bool $verifySSL |
48
|
|
|
* @return URLInfo_ConnectionTester |
49
|
|
|
*/ |
50
|
|
|
public function setVerifySSL(bool $verifySSL=true) : URLInfo_ConnectionTester |
51
|
|
|
{ |
52
|
|
|
$this->setOption('verify-ssl', $verifySSL); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isVerifySSLEnabled() : bool |
57
|
|
|
{ |
58
|
|
|
return $this->getBoolOption('verify-ssl'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setVerboseMode(bool $enabled=true) : URLInfo_ConnectionTester |
62
|
|
|
{ |
63
|
|
|
$this->setOption('curl-verbose', $enabled); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function isVerboseModeEnabled() : bool |
68
|
|
|
{ |
69
|
|
|
return $this->getBoolOption('curl-verbose'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function canConnect() : bool |
73
|
|
|
{ |
74
|
|
|
requireCURL(); |
75
|
|
|
|
76
|
|
|
$ch = curl_init(); |
77
|
|
|
|
78
|
|
|
if(!is_resource($ch)) |
79
|
|
|
{ |
80
|
|
|
throw new BaseException( |
81
|
|
|
'Could not initialize a new cURL instance.', |
82
|
|
|
'Calling curl_init returned false. Additional information is not available.', |
83
|
|
|
URLInfo::ERROR_CURL_INIT_FAILED |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if($this->isVerboseModeEnabled()) |
88
|
|
|
{ |
89
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->url->getNormalized()); |
93
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
94
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
95
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
96
|
|
|
|
97
|
|
|
if(!$this->isVerifySSLEnabled()) |
98
|
|
|
{ |
99
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
100
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if($this->url->hasUsername()) |
104
|
|
|
{ |
105
|
|
|
curl_setopt($ch, CURLOPT_USERNAME, $this->url->getUsername()); |
106
|
|
|
curl_setopt($ch, CURLOPT_PASSWORD, $this->url->getPassword()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
curl_exec($ch); |
110
|
|
|
|
111
|
|
|
$http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); |
112
|
|
|
|
113
|
|
|
curl_close($ch); |
114
|
|
|
|
115
|
|
|
return ($http_code === 200) || ($http_code === 302); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|