From de699154d4e25a69646f7d51f3da2dfb1ed51d23 Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 27 Jan 2023 10:35:29 +0000 Subject: [PATCH 1/5] Add tests to Makefile --- Makefile | 63 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 82b4f2e..09c6de4 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,71 @@ -# First goal is always run with just `make` -all: - /bin/php php/update-listing.php +port = 8080 +output = output -data: +# First goal is the default with `make`. + +# List make scripts. +list: + grep "^[^[:space:]]*:" Makefile --before-context=1 --group-separator="" + +## Using make dependencies is duplicating behaviour but reads better. +# /bin/php php/update-listing.php +# Refresh listing and generate HTML. +all: fetch html + +# Fetch room listing. +fetch: /bin/php php/fetch-servers.php +# Generate HTML from data. html: /bin/php php/generate-html.php -dev: - xdg-open http://localhost:8080 +# Last item run in foreground to receive interrupts. + +# Serve a local copy which responds to file changes. +dev: open make server & make watchdog -lan-dev: +# Serve a local copy on LAN which responds to file changes. +lan-dev: open ip addr | fgrep -e ' 192.' -e ' 10.' - xdg-open http://localhost:8080 make lan-server & make watchdog +# Serve a local copy. server: - /bin/php -S localhost:8080 -t output + /bin/php -S localhost:$(port) -t $(output) +# Serve a local copy on all interfaces. lan-server: - /bin/php -S 0.0.0.0:8080 -t output + /bin/php -S 0.0.0.0:$(port) -t $(output) + +# Open locally served page in browser. +open: + xdg-open http://localhost:$(port) >/dev/null 2>/dev/null & disown -# Update html on file change -# Doesn't check for new files +# Update HTML on file change. Doesn't check for new files. watchdog: find . | entr -n -s "make html" +# Remove artefacts +clean: + rm -r cache + rm -r output/*.html + +# Build everything from scratch and test functionality. +test: clean all open server + +# Build everything from scratch and test functionality on LAN. +test-lan: clean all open lan-server + +# -- Aliases -- +serve: server + +lan-serve: lan-server + +data: fetch + +watch: watchdog + From 4f0b01182939575c4ce64369a0b85934b31277ad Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 27 Jan 2023 10:36:10 +0000 Subject: [PATCH 2/5] Cache QR invites from SOGS --- sites/+components/qr_modals.php | 59 ++++++++------------------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/sites/+components/qr_modals.php b/sites/+components/qr_modals.php index b0844b0..38d0f44 100644 --- a/sites/+components/qr_modals.php +++ b/sites/+components/qr_modals.php @@ -1,56 +1,32 @@ preview_link . "invite.png"; } /* - * @Deprecated - * Use Google API to generate QR codes and encode them as base64 + * Fetch QR codes from SOGS server and encode them as base64 */ - function base64_qr_code($room_id, $join_url, $size = "512x512") { - // Could use http_build_query() instead, but I won't break what works. - // https://developers.google.com/chart/infographics/docs/qr_codes + function base64_qr_code($room_id, $room, $size = "512x512") { $png_cached = room_qr_code_cached($room_id); - if (file_exists($png_cached)) + if (file_exists($png_cached)) { // fwrite(STDERR, "QR code found for " . $room_id . PHP_EOL); return base64_encode(file_get_contents($png_cached)); -// fwrite(STDERR, "QR code NOT found for " . $room_id . PHP_EOL); - $data = urlencode($join_url); - $api_url = - "https://chart.googleapis.com/chart?cht=qr" . - "&chs=$size" . - "&chl=$data" . - "&chld=L|0"; - // error correction level: L = 7%, M = 15%, Q = 25%, H = 30% - // | margin in number of rows - $png = file_get_contents($api_url); + } +// fwrite(STDERR, "QR code NOT found for " . $room_id . PHP_EOL); + $png = file_get_contents(room_invite_png($room_id, $room)); file_put_contents($png_cached, $png); return base64_encode($png); } - - file_exists($QR_CODES) or mkdir($QR_CODES, 0700); // @Deprecated + + file_exists($QR_CODES) or mkdir($QR_CODES, 0700); ?> From f19e19df90673ce65f3538c3a4d934a2632d290c Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 27 Jan 2023 10:36:34 +0000 Subject: [PATCH 3/5] Improve logging --- .phpenv | 4 +++- php/fetch-servers.php | 13 +++++++++++-- php/generate-html.php | 7 ++++++- php/update-listing.php | 5 ----- php/utils/logging.php | 27 +++++++++++++++++++++++++++ sites/+components/qr_modals.php | 1 + 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 php/utils/logging.php diff --git a/.phpenv b/.phpenv index 3c59c08..960b551 100644 --- a/.phpenv +++ b/.phpenv @@ -7,6 +7,8 @@ $TEMPLATES_ROOT="$PROJECT_ROOT/sites"; $LANGUAGES_ROOT="$PROJECT_ROOT/languages"; + include_once "$PROJECT_ROOT/php/utils/logging.php"; + // set timeout for file_get_contents() ini_set('default_socket_timeout', 6); // in seconds, default is 60 @@ -16,6 +18,6 @@ // do not report warnings (timeouts, SSL/TLS errors) error_reporting(E_ALL & ~E_WARNING); - + date_default_timezone_set('UTC'); ?> \ No newline at end of file diff --git a/php/fetch-servers.php b/php/fetch-servers.php index c5c4482..fc16076 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -80,7 +80,8 @@ // write output to disk global $output; file_put_contents($output, json_encode($info_arrays)); // overwrites existing file - echo("Done. " . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers have been found." . PHP_EOL); + log_info("Done. "); + log_info(Found . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers." . PHP_EOL); } /* @@ -96,13 +97,16 @@ $sd_pre = "https://session.directory/?all=groups" ; // this one has to be expanded first // get awesome session group list html + log_info("Requesting Awesome Session Group list."); $asgl_html = file_get_contents($asgl); // get lokilocker.com html + log_info("Requesting Lokilocker Mods Open Group list."); $ll_html = file_get_contents($ll); // get session.directory html $sd_html = ""; + log_info("Requesting session.directory list."); $sd_pre_html = file_get_contents($sd_pre); $sd_pattern = "/view_session_group_user_lokinet\.php\?id=\d+/"; preg_match_all($sd_pattern, $sd_pre_html, $sd_links); @@ -114,8 +118,10 @@ $sd_html = $sd_html . file_get_contents($link) . PHP_EOL; } + log_info("Done fetching sources."); + // merge all html into a single string - return( + return ( $asgl_html . PHP_EOL . $ll_html . PHP_EOL . $sd_html . PHP_EOL @@ -258,6 +264,7 @@ $endpoint = "/rooms"; $json_url = $server_url . $endpoint; // $json = file_get_contents($json_url); + log_info("Polling $server_url for rooms."); $json = curl_get_contents($json_url); // circumvents flaky routing // echo("URL: " . $server_url . " - JSON URL: " . $json_url . PHP_EOL); // echo("JSON: " . $json . PHP_EOL); @@ -267,6 +274,7 @@ $json_rooms = array(); // if response was not empty if($json_obj) { + log_info("Received response from $server_url."); foreach($json_obj as $json_room) { $token = $json_room->token; // room "name" $users_per_second = $json_room->active_users / $json_room->active_users_cutoff; @@ -307,6 +315,7 @@ if($legacy_rooms) { $result = $legacy_rooms; } else { + log_info("Failed to receive response from $server_url."); $result = null; } } diff --git a/php/generate-html.php b/php/generate-html.php index 7df70db..af80d4f 100644 --- a/php/generate-html.php +++ b/php/generate-html.php @@ -19,8 +19,10 @@ // Do not render auxilliary PHP files. if (str_contains("$phppath", "/+") || $phppath[0] == "+") continue; - + + $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); + $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); $docpath = str_replace(".php", ".html", $docpath); // This works? Yes, yes it does. @@ -28,8 +30,11 @@ // otherwise we could include the documents in an ob_* wrapper. // Same as shell_exec, except we don't have to escape quotes. + log_info("Generating output for $relpath."); $document = `cd "$TEMPLATES_ROOT"; php $phppath`; file_put_contents($docpath, $document); } + + log_info("Done generating HTML."); ?> diff --git a/php/update-listing.php b/php/update-listing.php index 1f86095..9cf40cd 100644 --- a/php/update-listing.php +++ b/php/update-listing.php @@ -1,8 +1,3 @@ -Running, please wait... -This script will usually take approximately 4 minutes to run. -It will take longer if the Chinese servers are spasming out -or if you are running this for the first time. - \ No newline at end of file diff --git a/sites/+components/qr_modals.php b/sites/+components/qr_modals.php index 38d0f44..adf6e2d 100644 --- a/sites/+components/qr_modals.php +++ b/sites/+components/qr_modals.php @@ -21,6 +21,7 @@ return base64_encode(file_get_contents($png_cached)); } // fwrite(STDERR, "QR code NOT found for " . $room_id . PHP_EOL); + log_info("Fetching QR code for $room_id."); $png = file_get_contents(room_invite_png($room_id, $room)); file_put_contents($png_cached, $png); return base64_encode($png); From 6b25bc82f334eaab3f993561993be09b348d974b Mon Sep 17 00:00:00 2001 From: gravel Date: Fri, 27 Jan 2023 14:13:06 +0000 Subject: [PATCH 4/5] Change dev server HTTP port from 8080 to 8081 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 09c6de4..8f29aa8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -port = 8080 +port = 8081 output = output # First goal is the default with `make`. From 84b2f7bda0c576a63a0cb8edba642c6e52f6f68e Mon Sep 17 00:00:00 2001 From: mdPlusPlus Date: Mon, 30 Jan 2023 19:46:59 +0100 Subject: [PATCH 5/5] Fix unquoted string --- php/fetch-servers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/fetch-servers.php b/php/fetch-servers.php index fc16076..c98d3c8 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -81,7 +81,7 @@ global $output; file_put_contents($output, json_encode($info_arrays)); // overwrites existing file log_info("Done. "); - log_info(Found . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers." . PHP_EOL); + log_info("Found " . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers." . PHP_EOL); } /*