Rev 1181 | Rev 1190 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1181 | Rev 1182 | ||
---|---|---|---|
Line 338... | Line 338... | ||
338 | * @return bool |
338 | * @return bool |
339 | * @throws OIDplusException |
339 | * @throws OIDplusException |
340 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
340 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
341 | */ |
341 | */ |
342 | function url_post_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
342 | function url_post_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
- | 343 | if (class_exists(OIDplus::class)) { |
|
- | 344 | if (OIDplus::baseConfig()->getValue('OFFLINE_MODE', false)) { |
|
- | 345 | $reason = _L('OIDplus is running in offline mode due to the base configuration setting %1.', 'OFFLINE_MODE'); |
|
- | 346 | return false; |
|
- | 347 | } |
|
- | 348 | } |
|
- | 349 | ||
343 | if (function_exists('curl_init')) { |
350 | if (function_exists('curl_init')) { |
344 | return true; |
351 | return true; |
345 | } else { |
352 | } else { |
346 | $reason = _L('Please install the PHP extension %1, so that OIDplus can connect to the Internet.', '<code>php_curl</code>'); |
353 | $reason = _L('Please install the PHP extension %1, so that OIDplus can connect to the Internet.', '<code>php_curl</code>'); |
347 | return false; |
354 | return false; |
Line 355... | Line 362... | ||
355 | * @param string $userAgent |
362 | * @param string $userAgent |
356 | * @return string|false |
363 | * @return string|false |
357 | * @throws \ViaThinkSoft\OIDplus\OIDplusException |
364 | * @throws \ViaThinkSoft\OIDplus\OIDplusException |
358 | */ |
365 | */ |
359 | function url_post_contents(string $url, array $params=array(), array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
366 | function url_post_contents(string $url, array $params=array(), array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
- | 367 | $require_ssl = str_starts_with(strtolower($url),'https:'); |
|
- | 368 | if (!url_post_contents_available($require_ssl, $reason)) { |
|
- | 369 | throw new OIDplusException(_L('This feature is not available, because OIDplus cannot connect to the Internet.').' '.$reason); |
|
- | 370 | } |
|
- | 371 | ||
360 | $postFields = http_build_query($params); |
372 | $postFields = http_build_query($params); |
361 | 373 | ||
362 | $headers = array( |
374 | $headers = array( |
363 | "User-Agent: $userAgent", |
375 | "User-Agent: $userAgent", |
364 | "Content-Length: ".strlen($postFields) |
376 | "Content-Length: ".strlen($postFields) |
Line 385... | Line 397... | ||
385 | $error_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
397 | $error_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
386 | @curl_close($ch); |
398 | @curl_close($ch); |
387 | if ($error_code >= 400) return false; |
399 | if ($error_code >= 400) return false; |
388 | if ($res === false) return false; |
400 | if ($res === false) return false; |
389 | } else { |
401 | } else { |
390 | throw new OIDplusException(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl')); |
- | |
- | 402 | $res = false; |
|
- | 403 | assert(false); |
|
391 | } |
404 | } |
- | 405 | ||
392 | return $res; |
406 | return $res; |
393 | } |
407 | } |
394 | 408 | ||
395 | /** |
409 | /** |
396 | * @param bool $require_ssl |
410 | * @param bool $require_ssl |
Line 398... | Line 412... | ||
398 | * @return bool |
412 | * @return bool |
399 | * @throws OIDplusException |
413 | * @throws OIDplusException |
400 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
414 | * @throws \ViaThinkSoft\OIDplus\OIDplusConfigInitializationException |
401 | */ |
415 | */ |
402 | function url_get_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
416 | function url_get_contents_available(bool $require_ssl=true, string &$reason=null): bool { |
- | 417 | if (class_exists(OIDplus::class)) { |
|
- | 418 | if (OIDplus::baseConfig()->getValue('OFFLINE_MODE', false)) { |
|
- | 419 | $reason = _L('OIDplus is running in offline mode due to the base configuration setting %1.', 'OFFLINE_MODE'); |
|
- | 420 | return false; |
|
- | 421 | } |
|
- | 422 | } |
|
- | 423 | ||
403 | if (function_exists('curl_init')) { |
424 | if (function_exists('curl_init')) { |
404 | // Via cURL |
425 | // Via cURL |
405 | return true; |
426 | return true; |
406 | } else { |
427 | } else { |
407 | // Via file_get_contents() |
428 | // Via file_get_contents() |
Line 423... | Line 444... | ||
423 | * @param array $extraHeaders |
444 | * @param array $extraHeaders |
424 | * @param string $userAgent |
445 | * @param string $userAgent |
425 | * @return string|false |
446 | * @return string|false |
426 | */ |
447 | */ |
427 | function url_get_contents(string $url, array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
448 | function url_get_contents(string $url, array $extraHeaders=array(), string $userAgent='ViaThinkSoft-OIDplus/2.0') { |
- | 449 | $require_ssl = str_starts_with(strtolower($url),'https:'); |
|
- | 450 | if (!url_get_contents_available($require_ssl, $reason)) { |
|
- | 451 | throw new OIDplusException(_L('This feature is not available, because OIDplus cannot connect to the Internet.').' '.$reason); |
|
- | 452 | } |
|
- | 453 | ||
428 | $headers = array("User-Agent: $userAgent"); |
454 | $headers = array("User-Agent: $userAgent"); |
429 | foreach ($extraHeaders as $name => $val) { |
455 | foreach ($extraHeaders as $name => $val) { |
430 | $headers[] = "$name: $val"; |
456 | $headers[] = "$name: $val"; |
431 | } |
457 | } |
432 | if (function_exists('curl_init')) { |
458 | if (function_exists('curl_init')) { |