get_room_identifier(); $icon_cached = room_icon_path($room_id); $icon_resized = room_icon_path_resized($room_id, $size); $icon_expired = file_exists($icon_cached) && filemtime($icon_cached) < strtotime("-1 hour"); // Re-fetch icons periodically. if (!file_exists($icon_cached) || $icon_expired) { $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 an empty icon."); } // Never overwrite with an empty file. if (!(file_exists($icon_cached) && filesize($icon_cached) > 0 && empty($icon))) { file_put_contents($icon_cached, $icon); } } if (!file_exists($icon_resized) || $icon_expired) { $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); if (!imagewebp($gd_resized, $icon_resized)) { log_info("Converting image for $room_id to $size failed"); } } if (filesize($icon_resized) == 0) { return ""; } return room_icon_path_relative($room_id, $size); } function room_icon_safety(\CommunityRoom $room): int { global $ICON_ALLOWLIST, $ICON_BLOCKLIST; if (in_array($room->get_room_identifier(), $ICON_BLOCKLIST)) { return -1; } if (in_array($room->server->get_hostname(), $ICON_ALLOWLIST)) { return 1; } if (in_array($room->server->get_hostname(), $ICON_BLOCKLIST)) { return -1; } if ($room->has_nsfw_keywords()) { return -1; } return 0; } file_exists($ROOM_ICONS_CACHE) or mkdir($ROOM_ICONS_CACHE, 0755, true); file_exists($ROOM_ICONS) or mkdir($ROOM_ICONS, 0755, true); ?>