From 395b082b5c9ab0b07c2d2a925adeba3c50aeca20 Mon Sep 17 00:00:00 2001 From: mdPlusPlus Date: Tue, 3 Jan 2023 22:00:30 +0100 Subject: [PATCH] Move helper functions to separate file --- get_online_session_communities.php | 76 +++--------------------------- helper_functions.php | 67 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 helper_functions.php diff --git a/get_online_session_communities.php b/get_online_session_communities.php index c638855..b93ec17 100644 --- a/get_online_session_communities.php +++ b/get_online_session_communities.php @@ -1,6 +1,8 @@ http://sog.caliban.org/im?public_key=118df8c6c471ac0468c7c77e1cdc12f24a139ee8a07c6e3bf4e7855640dad821" rel="nofollow">http://sog.caliban.org/im?public_key=118df8c6c471ac0468c7c77e1cdc12f24a139ee8a07c6e3bf4e7855640dad821 //TODO: Figure out why the regex does match those foreach($result as &$entry) { -// if(str_contains($entry, "\"")) { // str_contains() requires PHP 8 - if(strpos($entry, "\"")) { + if(strpos($entry, "\"")) { // if(str_contains($entry, "\"")) { // str_contains() requires PHP 8 $entry = explode("\"", $entry)[0]; // split on " and take first part } } @@ -223,29 +224,6 @@ return $result; } - /* - * Helper function for reduce_servers - */ - function url_is_reachable($url) { - global $curl_connecttimeout_ms; - global $curl_timeout_ms; - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS , $curl_connecttimeout_ms); - curl_setopt($ch, CURLOPT_TIMEOUT_MS, $curl_timeout_ms); - curl_exec($ch); - $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); -// echo($url . " is " . $retcode . PHP_EOL); - if ($retcode == 200) { - return true; - } - else { - return false; - } - } - /* * Takes an input like this: * [0] => http://1.2.3.4 @@ -638,17 +616,13 @@ " " . PHP_EOL; // suffix - // span over 6 columns (id, name, description, users, preview, join link) - //$span_count = 6; $suffix = "" . PHP_EOL . "" . PHP_EOL . " " . PHP_EOL . - //" " . PHP_EOL . " " . PHP_EOL . " " . PHP_EOL . " " . PHP_EOL . - //" " . PHP_EOL . " " . PHP_EOL . " " . PHP_EOL . "
" . count($table_lines) . " unique Session Communities have been found." . count($table_lines) . " unique Session Communities have been found.
Last checked X minutes ago.Last checked X minutes ago.
" . PHP_EOL; @@ -687,45 +661,9 @@ } /* - * file_get_contents alternative that circumvents flaky routing to Chinese servers + * Queries the first found room for a server for its actual public key */ - function curl_get_contents($url) { - $connecttimeout = 2; // wait at most X seconds to connect - $timeout = 3; // can't take longer than X seconds for the whole curl process - $sleep = 2; // sleep between tries in seconds - $retries = 120; - // takes at most ($timeout + $sleep) * retries seceonds - // 3 + 2 * 150 = 5 * 120 = 600s = 10m - - $contents = false; - $retcode = 404; - $counter = 1; - - while(!$contents && $counter <= $retries) { -// echo("Trial #" . $counter . PHP_EOL); - $curl = curl_init($url); -// curl_setopt($curl, CURLOPT_VERBOSE, true); - - curl_setopt($curl, CURLOPT_AUTOREFERER, true); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connecttimeout); - curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); - - $contents = curl_exec($curl); - $retcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); - - curl_close($curl); - - $counter++; - sleep($sleep); - } - - if($retcode != 200) { - return false; - } else { - return $contents; - } + function get_pubkey_from_server($server_url) { + //TODO } ?> diff --git a/helper_functions.php b/helper_functions.php new file mode 100644 index 0000000..8c58013 --- /dev/null +++ b/helper_functions.php @@ -0,0 +1,67 @@ +