Bearsampp 2026.7.11
Loading...
Searching...
No Matches
HttpClient Class Reference

Static Public Member Functions

static checkInternetState ()
static getApiJson ($url)
static getChangelogUrl ($utmSource=true)
static getCurlHttpHeaders ($url)
static getFopenHttpHeaders ($url)
static getGithubUrl ($type='user', $user=APP_GITHUB_USER, $repo=null, $branch=null, $path=null)
static getGithubUserUrl ()
static getHeaders ($host, $port, $ssl=false)
static getHttpHeaders ($pingUrl)
static getLatestVersion ($url)
static getRemoteFilesize ($url, $humanFileSize=true)
static getWebsiteUrl ($path='', $fragment='', $utmSource=true)
static getWebsiteUrlNoUtm ($path='', $fragment='')
static setupCurlHeaderWithToken ()

Detailed Description

Definition at line 19 of file class.httpclient.php.

Member Function Documentation

◆ checkInternetState()

checkInternetState ( )
static

Checks the current state of the internet connection.

This method attempts to reach a well-known website (e.g., www.google.com) to determine the state of the internet connection. It returns true if the connection is successful, otherwise it returns false.

Returns
bool True if the internet connection is active, false otherwise.

Definition at line 409 of file class.httpclient.php.

410 {
411 $connected = @fsockopen('www.google.com', 80);
412 if ($connected) {
413 fclose($connected);
414
415 return true; // Internet connection is active
416 } else {
417 return false; // Internet connection is not active
418 }
419 }

Referenced by QuickPick\getQuickpickMenu(), and QuickPick\installModule().

Here is the caller graph for this function:

◆ getApiJson()

getApiJson ( $url)
static

Sends a GET request to the specified URL and returns the response.

Parameters
string$urlThe URL to send the GET request to.
Returns
string The trimmed response data from the URL.

Definition at line 298 of file class.httpclient.php.

299 {
301
302 $ch = curl_init();
303 curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
304 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
305 curl_setopt($ch, CURLOPT_VERBOSE, false); // Set to false to avoid polluting logs unless needed
306 curl_setopt($ch, CURLOPT_URL, $url);
307 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
308 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
309 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
310 $data = curl_exec($ch);
311 if (curl_errno($ch)) {
312 Log::error('CURL Error (' . curl_errno($ch) . '): ' . curl_error($ch) . ' (URL: ' . $url . ')');
313 }
314
315 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
316 if ($httpCode >= 400) {
317 Log::error('HTTP Error ' . $httpCode . ' for URL: ' . $url);
318 }
319
320 // curl_close() is deprecated in PHP 8.5+ as it has no effect since PHP 8.0
321 // The resource is automatically closed when it goes out of scope
322 if (PHP_VERSION_ID < 80500) {
323 curl_close($ch);
324 }
325
326 return $data === false ? '' : trim($data);
327 }
static setupCurlHeaderWithToken()
static error($data, $file=null)

References Log\error(), and setupCurlHeaderWithToken().

Referenced by getLatestVersion().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getChangelogUrl()

getChangelogUrl ( $utmSource = true)
static

Constructs the URL to the changelog page, optionally including UTM parameters.

Parameters
bool$utmSourceWhether to include UTM source parameters.
Returns
string The URL to the changelog page.

Definition at line 172 of file class.httpclient.php.

173 {
174 return self::getWebsiteUrl('doc/changelog', null, $utmSource);
175 }
static getWebsiteUrl($path='', $fragment='', $utmSource=true)

References getWebsiteUrl().

Here is the call graph for this function:

◆ getCurlHttpHeaders()

getCurlHttpHeaders ( $url)
static

Retrieves HTTP headers from a given URL using cURL.

This method initializes a cURL session, sets various options to fetch headers including disabling SSL peer verification, and executes the request. It logs the raw response for debugging purposes and parses the headers from the response.

Parameters
string$urlThe URL from which to fetch the headers.
Returns
array An array of headers if successful, otherwise an empty array.

Definition at line 99 of file class.httpclient.php.

100 {
101 $result = array();
102
103 $ch = curl_init();
104 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
105 curl_setopt($ch, CURLOPT_VERBOSE, true);
106 curl_setopt($ch, CURLOPT_HEADER, true);
107 curl_setopt($ch, CURLOPT_URL, $url);
108 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
109 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
110
111 $response = @curl_exec($ch);
112 if (empty($response)) {
113 return $result;
114 }
115
116 Log::trace('getCurlHttpHeaders:' . $response);
117 $responseHeaders = explode("\r\n\r\n", $response, 2);
118 if (!isset($responseHeaders[0]) || empty($responseHeaders[0])) {
119 return $result;
120 }
121
122 return explode("\n", $responseHeaders[0]);
123 }
$result
static trace($data, $file=null)

References $response, $result, and Log\trace().

Referenced by getHttpHeaders().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFopenHttpHeaders()

getFopenHttpHeaders ( $url)
static

Retrieves HTTP headers from a given URL using the fopen function.

This method creates a stream context to disable SSL peer and peer name verification, which allows self-signed certificates. It attempts to open the URL and read the HTTP response headers.

Parameters
string$urlThe URL from which to fetch the headers.
Returns
array An array of headers if successful, otherwise an empty array.

Definition at line 66 of file class.httpclient.php.

67 {
68 $result = array();
69
70 $context = stream_context_create(array(
71 'ssl' => array(
72 'verify_peer' => false,
73 'verify_peer_name' => false,
74 'allow_self_signed' => true,
75 )
76 ));
77
78 $fp = @fopen($url, 'r', false, $context);
79 if ($fp) {
80 $meta = stream_get_meta_data($fp);
81 $result = isset($meta['wrapper_data']) ? $meta['wrapper_data'] : $result;
82 fclose($fp);
83 }
84
85 return $result;
86 }

References $result.

Referenced by getHttpHeaders().

Here is the caller graph for this function:

◆ getGithubUrl()

getGithubUrl ( $type = 'user',
$user = APP_GITHUB_USER,
$repo = null,
$branch = null,
$path = null )
static

Generates various GitHub URLs based on the specified type.

Parameters
string$typeThe type of URL ('user', 'repo', 'raw'). Defaults to 'user'.
string$userThe GitHub username. Defaults to 'Bearsampp'.
string | null$repoThe repository name (required for 'repo' and 'raw' types).
string | null$branchThe branch name (required for 'raw' type).
string | null$pathThe file path (required for 'raw' type).
Returns
string|false The generated URL or false on invalid input.

Definition at line 187 of file class.httpclient.php.

187 {
188 if (empty($user) || !is_string($user)) {
189 return false;
190 }
191
192 // Encode as URL path segment (not query encoding)
193 $user = rawurlencode($user);
194
195 switch ($type) {
196 case 'user':
197 return "https://github.com/{$user}";
198
199 case 'repo':
200 if (empty($repo) || !is_string($repo)) {
201 return false;
202 }
203 $repo = rawurlencode($repo);
204 return "https://github.com/{$user}/{$repo}";
205
206 case 'issues':
207 if (empty($repo) || !is_string($repo)) {
208 return false;
209 }
210 $repo = rawurlencode($repo);
211 return "https://github.com/{$user}/{$repo}/issues";
212
213 case 'raw':
214 if (empty($repo) || empty($branch) || empty($path) || !is_string($repo) || !is_string($branch) || !is_string($path)) {
215 return false;
216 }
217 $repo = rawurlencode($repo);
218 $branch = rawurlencode($branch);
219
220 $path = ltrim($path, '/');
221 $segments = array_map('rawurlencode', explode('/', $path));
222 $pathEncoded = implode('/', $segments);
223
224 return "https://raw.githubusercontent.com/{$user}/{$repo}/{$branch}/{$pathEncoded}";
225
226 default:
227 return false;
228 }
229 }

References APP_GITHUB_USER.

Referenced by getGithubUserUrl().

Here is the caller graph for this function:

◆ getGithubUserUrl()

getGithubUserUrl ( )
static

Gets the GitHub user URL for Bearsampp.

Returns
string The GitHub user URL.

Definition at line 236 of file class.httpclient.php.

237 {
238 return self::getGithubUrl('user', APP_GITHUB_USER);
239 }
static getGithubUrl($type='user', $user=APP_GITHUB_USER, $repo=null, $branch=null, $path=null)
const APP_GITHUB_USER
Definition root.php:16

References APP_GITHUB_USER, and getGithubUrl().

Referenced by ActionAbout\__construct(), and ActionAbout\processWindow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getHeaders()

getHeaders ( $host,
$port,
$ssl = false )
static

Retrieves the initial response line from a specified host and port using a socket connection.

This method optionally uses SSL and creates a stream context similar to getFopenHttpHeaders. It attempts to connect to the host and port, reads the first line of the response, and parses it. Detailed debug information is logged for each header line received.

Parameters
string$hostThe host name or IP address to connect to.
int$portThe port number to connect to.
bool$sslWhether to use SSL (defaults to false).
Returns
array An array containing the first line of the response, split into parts, or an empty array if unsuccessful.

Definition at line 254 of file class.httpclient.php.

255 {
256 $result = array();
257 $context = stream_context_create(array(
258 'ssl' => array(
259 'verify_peer' => false,
260 'verify_peer_name' => false,
261 'allow_self_signed' => true,
262 )
263 ));
264
265 $fp = @stream_socket_client(($ssl ? 'ssl://' : '') . $host . ':' . $port, $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $context);
266 if ($fp) {
267 $out = fgets($fp);
268 $result = explode(PHP_EOL, $out);
269 @fclose($fp);
270 }
271
272 if (!empty($result)) {
273 $rebuildResult = array();
274 foreach ($result as $row) {
275 $row = trim($row);
276 if (!empty($row)) {
277 $rebuildResult[] = $row;
278 }
279 }
280 $result = $rebuildResult;
281
282 Log::debug('getHeaders:');
283 foreach ($result as $header) {
284 Log::debug('-> ' . $header);
285 }
286 }
287
288 return $result;
289 }
$port
static debug($data, $file=null)

References $port, $result, and Log\debug().

Referenced by BinMailpit\checkPort(), and BinXlight\checkPort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getHttpHeaders()

getHttpHeaders ( $pingUrl)
static

Retrieves HTTP headers from a given URL using either cURL or fopen, depending on availability.

Parameters
string$pingUrlThe URL to ping for headers.
Returns
array An array of HTTP headers.

Definition at line 28 of file class.httpclient.php.

29 {
30 if (function_exists('curl_version')) {
32 } else {
34 }
35
36 if (!empty($result)) {
37 $rebuildResult = array();
38 foreach ($result as $row) {
39 $row = trim($row);
40 if (!empty($row)) {
41 $rebuildResult[] = $row;
42 }
43 }
44 $result = $rebuildResult;
45
46 Log::debug('getHttpHeaders:');
47 foreach ($result as $header) {
48 Log::debug('-> ' . $header);
49 }
50 }
51
52 return $result;
53 }
static getFopenHttpHeaders($url)
static getCurlHttpHeaders($url)

References $result, Log\debug(), getCurlHttpHeaders(), and getFopenHttpHeaders().

Referenced by BinApache\checkPort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLatestVersion()

getLatestVersion ( $url)
static

Fetches the latest version information from a given url.

Parameters
string$urlThe URL to fetch version information from.
Returns
array|null Returns an array with 'version' and 'url' if successful, null otherwise.

Definition at line 336 of file class.httpclient.php.

337 {
339 if (empty($result)) {
340 Log::error('Cannot retrieve latest github info: empty result or error for URL: ' . $url);
341
342 return null;
343 }
344
345 $resultArray = json_decode($result, true);
346 if ($resultArray === null) {
347 Log::error('Failed to decode JSON response from: ' . $url . '. Response snippet: ' . substr($result, 0, 100));
348 return null;
349 }
350
351 if (isset($resultArray['tag_name']) && isset($resultArray['assets'][0]['browser_download_url'])) {
352 $tagName = $resultArray['tag_name'];
353 $downloadUrl = $resultArray['assets'][0]['browser_download_url'];
354 $name = $resultArray['name'];
355 Log::debug('Latest version tag name: ' . $tagName);
356 Log::debug('Download URL: ' . $downloadUrl);
357 Log::debug('Name: ' . $name);
358
359 return ['version' => $tagName, 'html_url' => $downloadUrl, 'name' => $name];
360 } else {
361 Log::error('Tag name, download URL, or name not found in the response: ' . $result);
362
363 return null;
364 }
365 }
static getApiJson($url)

References $result, Log\debug(), Log\error(), and getApiJson().

Referenced by ActionCheckVersion\__construct(), and ActionCheckVersion\processWindow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getRemoteFilesize()

getRemoteFilesize ( $url,
$humanFileSize = true )
static

Retrieves the file size of a remote file.

Parameters
string$urlThe URL of the remote file.
bool$humanFileSizeWhether to return the size in a human-readable format.
Returns
mixed The file size, either in bytes or as a formatted string.

Definition at line 389 of file class.httpclient.php.

390 {
391 $size = 0;
392
393 $data = get_headers($url, true);
394 if (isset($data['Content-Length'])) {
395 $size = intval($data['Content-Length']);
396 }
397
398 return $humanFileSize ? Util::humanFileSize($size) : $size;
399 }
static humanFileSize(int $size, string $unit='')

References Util\humanFileSize().

Here is the call graph for this function:

◆ getWebsiteUrl()

getWebsiteUrl ( $path = '',
$fragment = '',
$utmSource = true )
static

Constructs a complete website URL with optional path, fragment, and UTM source parameters.

Parameters
string$pathOptional path to append to the base URL.
string$fragmentOptional fragment to append to the URL.
bool$utmSourceWhether to include UTM source parameters. Defaults to true.
Returns
string The constructed URL.

Definition at line 134 of file class.httpclient.php.

135 {
136 global $bearsamppCore;
137
138 $url = APP_WEBSITE;
139 if (!empty($path)) {
140 $url .= '/' . ltrim($path, '/');
141 }
142 if ($utmSource) {
143 $url = rtrim($url, '/') . '/?utm_source=bearsampp-' . $bearsamppCore->getAppVersion();
144 }
145 if (!empty($fragment)) {
146 $url .= $fragment;
147 }
148
149 return $url;
150 }
global $bearsamppCore
const APP_WEBSITE
Definition root.php:14

References $bearsamppCore, and APP_WEBSITE.

Referenced by getChangelogUrl(), TplAppApache\getMenuApache(), TplAppMailpit\getMenuMailpit(), TplAppMariadb\getMenuMariadb(), TplAppMemcached\getMenuMemcached(), TplAppMysql\getMenuMysql(), TplAppNodejs\getMenuNodejs(), TplAppPhp\getMenuPhp(), TplAppPostgresql\getMenuPostgresql(), TplAppXlight\getMenuXlight(), QuickPick\getQuickpickMenu(), TplApp\getSectionMenuRight(), getWebsiteUrlNoUtm(), and ActionAbout\processWindow().

Here is the caller graph for this function:

◆ getWebsiteUrlNoUtm()

getWebsiteUrlNoUtm ( $path = '',
$fragment = '' )
static

Constructs a website URL without UTM parameters.

Parameters
string$pathOptional path to append to the base URL.
string$fragmentOptional fragment to append to the URL.
Returns
string The constructed URL without UTM parameters.

Definition at line 160 of file class.httpclient.php.

161 {
162 return self::getWebsiteUrl($path, $fragment, false);
163 }

References getWebsiteUrl().

Referenced by ActionAbout\__construct().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupCurlHeaderWithToken()

setupCurlHeaderWithToken ( )
static

Sets up cURL headers with token for API requests.

Returns
array The array of cURL headers.

Definition at line 372 of file class.httpclient.php.

373 {
374 // Return headers with User-Agent, which is required by GitHub API
375 return array(
376 'User-Agent: ' . APP_GITHUB_USERAGENT . ' (https://github.com/' . APP_GITHUB_USER . '/' . APP_GITHUB_REPO . ')',
377 'Accept: application/vnd.github.v3+json'
378 );
379 }
const APP_GITHUB_USERAGENT
Definition root.php:18
const APP_GITHUB_REPO
Definition root.php:17

References APP_GITHUB_REPO, APP_GITHUB_USER, and APP_GITHUB_USERAGENT.

Referenced by getApiJson().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: