From de9f9944d45a54540a7911b43f5eb12f78cc07a6 Mon Sep 17 00:00:00 2001 From: GlazerMann Date: Sat, 20 Jan 2024 14:45:17 -0600 Subject: [PATCH] Optimize CONTEXT_INSECURE code (#4369) --- expandFns.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/expandFns.php b/expandFns.php index 8b2f57a8f4..550995a1cb 100644 --- a/expandFns.php +++ b/expandFns.php @@ -6,10 +6,12 @@ require_once 'big_jobs.php'; // @codeCoverageIgnore // Allow cheap journals to work -const CONTEXT_INSECURE = array( - 'ssl' => ['verify_peer' => FALSE, 'verify_peer_name' => FALSE, 'allow_self_signed' => TRUE, 'security_level' => 0, 'verify_depth' => 0], - 'http' => ['ignore_errors' => TRUE, 'max_redirects' => 40, 'timeout' => BOT_HTTP_TIMEOUT * 1.0, 'follow_location' => 1, 'header'=> ['Connection: close'], "user_agent" => BOT_USER_AGENT] - ); +if (!defined('CONTEXT_INSECURE')) { + define('CONTEXT_INSECURE', stream_context_create(array( + 'ssl' => ['verify_peer' => FALSE, 'verify_peer_name' => FALSE, 'allow_self_signed' => TRUE, 'security_level' => 0, 'verify_depth' => 0], + 'http' => ['ignore_errors' => TRUE, 'max_redirects' => 40, 'timeout' => BOT_HTTP_TIMEOUT * 1.0, 'follow_location' => 1, 'header'=> ['Connection: close'], "user_agent" => BOT_USER_AGENT])) + ); +} final class HandleCache { // Greatly speed-up by having one array of each kind and only look for hash keys, not values @@ -182,9 +184,8 @@ function is_doi_works(string $doi) : ?bool { throttle_dx(); $url = "https://doi.org/" . doi_encode($doi); - $context = stream_context_create(CONTEXT_INSECURE); set_time_limit(120); - $headers_test = @get_headers($url , TRUE, $context); + $headers_test = @get_headers($url , TRUE, CONTEXT_INSECURE); if ($headers_test === FALSE) { if (strpos($doi, '10.2277/') === 0) return FALSE; // Rogue if (preg_match('~^10\.1038/nature\d{5}$~i', $doi)) return FALSE; // Nature dropped the ball @@ -195,13 +196,13 @@ function is_doi_works(string $doi) : ?bool { sleep(2); // @codeCoverageIgnore report_inline(' .'); // @codeCoverageIgnore set_time_limit(120); // @codeCoverageIgnore - $headers_test = @get_headers($url , TRUE, $context); // @codeCoverageIgnore + $headers_test = @get_headers($url , TRUE, CONTEXT_INSECURE); // @codeCoverageIgnore } if ($headers_test === FALSE) { sleep(5); // @codeCoverageIgnore set_time_limit(120); // @codeCoverageIgnore report_inline(' .'); // @codeCoverageIgnore - $headers_test = @get_headers($url , TRUE, $context); // @codeCoverageIgnore + $headers_test = @get_headers($url , TRUE, CONTEXT_INSECURE); // @codeCoverageIgnore } if ($headers_test === FALSE) { if (!in_array($doi, NULL_DOI_LIST)) bot_debug_log('Got NULL for DOI: ' . echoable($doi)); @@ -214,7 +215,7 @@ function is_doi_works(string $doi) : ?bool { sleep(5); set_time_limit(120); report_inline(' .'); - $headers_test = @get_headers($url , TRUE, $context); + $headers_test = @get_headers($url , TRUE, CONTEXT_INSECURE); /** We trust previous failure, so fail and null are both false **/ if ($headers_test === FALSE) return FALSE; return (bool) interpret_doi_header($headers_test); @@ -1420,16 +1421,15 @@ function hdl_works(string $hdl) : string|null|false { function is_hdl_works(string $hdl) : string|null|false { $hdl = trim($hdl); // See if it works - $context = stream_context_create(CONTEXT_INSECURE); usleep(100000); $test_url = "https://hdl.handle.net/" . $hdl; set_time_limit(120); - $headers_test = @get_headers($test_url, TRUE, $context); + $headers_test = @get_headers($test_url, TRUE, CONTEXT_INSECURE); if ($headers_test === FALSE) { sleep(5); // @codeCoverageIgnore set_time_limit(120); // @codeCoverageIgnore report_inline(' .'); // @codeCoverageIgnore - $headers_test = @get_headers($test_url, TRUE, $context); // @codeCoverageIgnore + $headers_test = @get_headers($test_url, TRUE, CONTEXT_INSECURE); // @codeCoverageIgnore } if ($headers_test === FALSE) return NULL; // most likely bad, but will recheck again and again if (empty($headers_test['Location']) && empty($headers_test['location'])) return FALSE; // leads nowhere