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/Makefile b/Makefile index 82b4f2e..8f29aa8 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 = 8081 +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 + diff --git a/php/fetch-servers.php b/php/fetch-servers.php index d1bb738..17069ae 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -82,7 +82,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); } /* @@ -98,13 +99,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); @@ -116,8 +120,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 @@ -260,6 +266,7 @@ $endpoint = "/rooms?all=1"; $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); @@ -269,6 +276,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; @@ -309,6 +317,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 b0844b0..adf6e2d 100644 --- a/sites/+components/qr_modals.php +++ b/sites/+components/qr_modals.php @@ -1,56 +1,33 @@ 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); + 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); } - - file_exists($QR_CODES) or mkdir($QR_CODES, 0700); // @Deprecated + + file_exists($QR_CODES) or mkdir($QR_CODES, 0700); ?>