diff --git a/.phpenv b/.phpenv index 0941227..7487b2f 100644 --- a/.phpenv +++ b/.phpenv @@ -7,6 +7,9 @@ $LANGUAGES_ROOT="$PROJECT_ROOT/languages"; $QR_CODES="$DOCUMENT_ROOT/qr-codes"; $QR_CODES_RELATIVE="qr-codes"; + $ROOM_ICONS_CACHE="$CACHE_ROOT/icons"; + $ROOM_ICONS="$DOCUMENT_ROOT/icons"; + $ROOM_ICONS_RELATIVE="icons"; include_once "$PROJECT_ROOT/php/utils/logging.php"; diff --git a/php/fetch-servers.php b/php/fetch-servers.php index 7a946f4..0bdcfb8 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -24,7 +24,7 @@ function main() { global $CACHE_ROOT, $ROOMS_FILE, $KNOWN_SERVERS, $KNOWN_PUBKEYS; - // Create default directories with conservative permissions. + // Create default directories.. file_exists($CACHE_ROOT) or mkdir($CACHE_ROOT, 0700); // Query our sources and store the resulting HTML. diff --git a/php/servers/known-servers.php b/php/servers/known-servers.php index 888b8dd..ff3bbbd 100644 --- a/php/servers/known-servers.php +++ b/php/servers/known-servers.php @@ -54,4 +54,9 @@ "session.hwreload.it" => "4b3e75eedd2116b4dab0bcb6443b0e9fbfce7bcf1d35970bdad8a57a0113fb20", "sogs.k9net.org" => "fdcb047eb78520e925fda512a45ae74c6e2de9e0df206b3c0471bf1509919559", ); + + /** These hostnames are considered to have safe room icons. */ + $ICON_ALLOWLIST = [ + "open.getsession.org" + ]; ?> diff --git a/php/utils/room-icons.php b/php/utils/room-icons.php new file mode 100644 index 0000000..8280f0a --- /dev/null +++ b/php/utils/room-icons.php @@ -0,0 +1,84 @@ +server->get_hostname(), $ICON_ALLOWLIST)) { + return null; + } + if ($room->has_nsfw_keywords()) { + return null; + } + $room_id = $room->get_room_identifier(); + $icon_cached = room_icon_path($room_id); + $icon_resized = room_icon_path_resized($room_id, $size); + if (file_exists($icon_resized)) { + return room_icon_path_relative($room_id, $size); + } + if (!file_exists($icon_cached)) { + $icon_url = $room->get_icon_url(); + if (empty($icon_url)) { + return null; + } + log_debug("Fetching icon for $room_id."); + $icon = file_get_contents($icon_url); + if (empty($icon)) { + log_info("$room_id returned empty icon."); + } + // Cache result, no matter if empty + file_put_contents($icon_cached, $icon); + } + $icon_cached_contents = file_get_contents($icon_cached); + if (empty($icon_cached_contents)) { + file_put_contents($icon_resized, ""); + return ""; + } + // Resize image + $gd_image = imagecreatefromstring($icon_cached_contents); + $gd_resized = imagescale($gd_image, $width, $height); + imagewebp($gd_resized, to: $icon_resized); + return room_icon_path_relative($room_id, $size); + } + + file_exists($ROOM_ICONS_CACHE) or mkdir($ROOM_ICONS_CACHE, 0755, true); + file_exists($ROOM_ICONS) or mkdir($ROOM_ICONS, 0755, true); +?> \ No newline at end of file diff --git a/php/utils/room-invites.php b/php/utils/room-invites.php index cb9e4a7..9cbbfa1 100644 --- a/php/utils/room-invites.php +++ b/php/utils/room-invites.php @@ -9,7 +9,7 @@ } /** - * Return remote path to room invite code. + * Return server-relative path to room invite code. * @param string $room_id Id of room to locate QR code for. */ function room_qr_code_path_relative(string $room_id): string { @@ -19,7 +19,7 @@ /** - * Fetch QR invite of the given room and return its local path. + * Fetch QR invite of the given room and return its relative path. * @param \CommunityRoom $room * @return string */ @@ -31,6 +31,9 @@ } log_debug("Fetching QR code for $room_id."); $png = file_get_contents($room->get_invite_url()); + if (empty($png)) { + log_warning("$room_id returned empty QR code."); + } file_put_contents($png_cached, $png); return room_qr_code_path_relative($room_id); } diff --git a/php/utils/servers-rooms.php b/php/utils/servers-rooms.php index 493c670..f3c4e30 100644 --- a/php/utils/servers-rooms.php +++ b/php/utils/servers-rooms.php @@ -273,7 +273,7 @@ $this->tags = [...$this->tags, ...$tags]; } - private function has_nsfw_keywords(): bool { + public function has_nsfw_keywords(): bool { // Description not included due to false positives. $blob = strtolower($this->name) . " " . diff --git a/sites/+components/tbl_communities.php b/sites/+components/tbl_communities.php index 2418ef4..7497de3 100644 --- a/sites/+components/tbl_communities.php +++ b/sites/+components/tbl_communities.php @@ -2,6 +2,7 @@ require_once "$PROJECT_ROOT/php/utils/utils.php"; require_once "$PROJECT_ROOT/php/utils/servers-rooms.php"; require_once "$PROJECT_ROOT/php/utils/room-invites.php"; + require_once "$PROJECT_ROOT/php/utils/room-icons.php"; /** * @var CommunityRoom[] $rooms @@ -75,6 +76,7 @@ data-hostname="=$hostname?>" data-staff='=$staff_json?>' data-tags='=$tags_json?>' + ' Can pass absence of icon */ ?> >