Passed
Push — develop-3.3.x ( eebe3b...d48415 )
by Mario
02:53
created

esi_controller::check_curl_info()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2024 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
use phpbb\config\config;
14
15
class esi_controller
16
{
17
	protected $config;
18
	protected $ppde_ext_manager;
19
	private $response = '';
20
	private $response_status = '';
21
22
	/**
23
	 * Constructor.
24
	 *
25
	 * @param config            $config           Configuration object
26
	 * @param extension_manager $ppde_ext_manager Extension manager object
27
	 */
28
	public function __construct(
29
		config $config,
30
		extension_manager $ppde_ext_manager
31
	)
32
	{
33
		$this->config = $config;
34
		$this->ppde_ext_manager = $ppde_ext_manager;
35
	}
36
37
	/**
38
	 * Check TLS configuration.
39
	 *
40
	 * This method checks the TLS configuration by making a cURL request to a
41
	 * specified TLS host. It decodes the JSON response and compares the TLS version
42
	 * with the allowed versions defined in the extension metadata. If a match is found,
43
	 * it updates the 'ppde_tls_detected' configuration value with the detected TLS version.
44
	 *
45
	 * @return void
46
	 */
47
	public function check_tls(): void
48
	{
49
		// Reset settings to false
50
		$this->config->set('ppde_tls_detected', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of phpbb\config\config::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
		$this->config->set('ppde_tls_detected', /** @scrutinizer ignore-type */ false);
Loading history...
51
		$this->response = '';
52
53
		$ext_meta = $this->ppde_ext_manager->get_ext_meta();
54
		$this->check_curl($ext_meta['extra']['security-check']['tls']['tls-host']);
55
56
		// Analyse response
57
		$json = json_decode($this->response, false);
0 ignored issues
show
Bug introduced by
It seems like $this->response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
		$json = json_decode(/** @scrutinizer ignore-type */ $this->response, false);
Loading history...
58
59
		if ($json !== null && in_array($json->tls_version, $ext_meta['extra']['security-check']['tls']['tls-version'], true))
60
		{
61
			$this->config->set('ppde_tls_detected', $json->tls_version);
62
		}
63
	}
64
65
	/**
66
	 * Check if cURL is available and make a request to the specified host.
67
	 *
68
	 * @param string $host The host to send the request to.
69
	 *
70
	 * @return bool True if the request is successful, false otherwise.
71
	 */
72
	public function check_curl(string $host): bool
73
	{
74
		if ($this->is_curl_loaded())
75
		{
76
			return $this->execute_curl_request($host);
77
		}
78
79
		return false;
80
	}
81
82
	/**
83
	 * Check if the cURL extension is loaded and the curl_init function is available.
84
	 *
85
	 * @return bool Returns true if cURL is available, false otherwise.
86
	 */
87
	private function is_curl_loaded(): bool
88
	{
89
		return extension_loaded('curl') && function_exists('curl_init');
90
	}
91
92
	/**
93
	 * Execute a cURL request to the specified host and store the response and status.
94
	 *
95
	 * @param string $host The host to send the cURL request to.
96
	 *
97
	 * @return bool Returns true if the cURL request is successful, false otherwise.
98
	 */
99
	private function execute_curl_request(string $host): bool
100
	{
101
		$ch = curl_init($host);
102
103
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
104
105
		$this->response = curl_exec($ch);
106
		$this->response_status = (string) curl_getinfo($ch, CURLINFO_HTTP_CODE);
107
108
		curl_close($ch);
109
110
		return $this->response !== false || $this->response_status !== '0';
111
	}
112
113
	/**
114
	 * Determine if the remote server is reachable using cURL.
115
	 *
116
	 * This method retrieves the host from the extension metadata and uses the `check_curl()`
117
	 * method to determine if a connection can be established. It then updates the
118
	 * 'ppde_curl_detected' configuration value with the check result.
119
	 *
120
	 * @return void
121
	 */
122
	public function set_remote_detected(): void
123
	{
124
		$ext_meta = $this->ppde_ext_manager->get_ext_meta();
125
		$this->config->set('ppde_curl_detected', $this->check_curl($ext_meta['extra']['version-check']['host']));
126
	}
127
128
	/**
129
	 * Retrieve and store cURL version information in the configuration.
130
	 *
131
	 * This method attempts to retrieve cURL version information using `curl_version()`.
132
	 * If successful, it updates the 'ppde_curl_version' and 'ppde_curl_ssl_version'
133
	 * configuration values.
134
	 *
135
	 * @return void
136
	 */
137
	public function set_curl_info(): void
138
	{
139
		// Get cURL version information
140
		if ($curl_info = $this->check_curl_info())
141
		{
142
			$this->config->set('ppde_curl_version', $curl_info['version']);
143
			$this->config->set('ppde_curl_ssl_version', $curl_info['ssl_version']);
144
		}
145
	}
146
147
	/**
148
	 * Check if the `curl_version()` function is available and retrieve cURL version information.
149
	 *
150
	 * @return array|bool Returns an array containing cURL version information if available, false otherwise.
151
	 */
152
	public function check_curl_info()
153
	{
154
		if (function_exists('curl_version'))
155
		{
156
			return curl_version();
157
		}
158
159
		return false;
160
	}
161
}
162