You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sessioncommunities.online/php/fetch-servers.php

132 lines
4.0 KiB
PHTML

<?php
// requires php-curl
require_once 'getenv.php';
require_once 'utils/utils.php';
require_once 'servers/known-servers.php';
include_once "$LANGUAGES_ROOT/language_flags.php"; // actually runs fine without it
require_once 'utils/servers-rooms.php';
function main() {
global $LOGGING_VERBOSITY;
// Get join links -> Add known servers ->
// De-dupe based on base URL ->
// Test domains -> De-dupe based on pubkey
$options = getopt("v", ["verbose"]);
if (isset($options["v"]) or isset($options["verbose"])) {
$LOGGING_VERBOSITY = LoggingVerbosity::Debug;
}
global $CACHE_ROOT, $ROOMS_FILE, $KNOWN_SERVERS, $KNOWN_PUBKEYS;
file_exists($CACHE_ROOT) or mkdir($CACHE_ROOT, 0700);
$html_pages = query_known_sources();
// Find join links in each HTML document and concatenate the results.
$join_links = array_merge([], ...array_map('parse_join_links', $html_pages));
/**
* @var CommunityServer[] $servers
*/
$servers = CommunityServer::from_join_urls($join_links);
// Add known hosts.
$servers = [...CommunityServer::from_known_hosts($KNOWN_SERVERS, $KNOWN_PUBKEYS), ...$servers];
$servers = CommunityServer::dedupe_by_url($servers);
$servers = CommunityServer::poll_reachable($servers);
$servers = CommunityServer::dedupe_by_pubkey($servers);
$servers_total = count($servers);
$rooms_total = count_rooms($servers);
// Output query results to file.
log_info("Done fetching communities.");
log_info(
"Found $rooms_total unique Session Communities " .
"on $servers_total servers." . PHP_EOL
);
file_put_contents($ROOMS_FILE, json_encode($servers));
}
/**
* Iteratively crawls an index for individual Session Community details.
* @return string[]
*/
function crawl_source_index($html, $url_base, $item_url_pattern) {
preg_match_all($item_url_pattern, $html, $match_result);
$matched_links = $match_result[0];
foreach ($matched_links as $link) {
$link = $url_base . $link;
log_debug("Requesting $link");
$pages[] = file_get_contents($link);
log_debug($http_response_header[0]);
// Supposed to be "HTTP/1.1 200 OK"
}
return $pages;
}
/**
* Fetches known sources of Session Community join links.
*/
function query_known_sources() {
global $SOURCES;
log_info("Requesting Awesome Session Group list...");
$pages_asgl[] = file_get_contents($SOURCES['ASGL']);
log_debug($http_response_header[0]); // Supposed to be "HTTP/1.1 200 OK"
log_info("Requesting Lokilocker Mods Open Group list...");
$pages_loki[] = file_get_contents($SOURCES['LOKI']);
log_debug($http_response_header[0]); // Supposed to be "HTTP/1.1 200 OK"
log_info("Requesting session.directory list...");
$index_sdir = file_get_contents($SOURCES['SDIR']);
log_debug($http_response_header[0]); // Supposed to be "HTTP/1.1 200 OK"
log_info("Crawling session.directory list...");
$pages_sdir = crawl_source_index(
$index_sdir,
$SOURCES['SDIR-BASE'],
$SOURCES['SDIR-PATTERN']
);
log_info('Done fetching sources.');
return [...$pages_asgl, ...$pages_loki, ...$pages_sdir];
}
/*
* Debug function to see which communities use pinned messages already
*/
function print_pinned_messages($room_assignments_arr) {
// for each server a.k.a. public key do
foreach($room_assignments_arr as $pubkey => $room_assignment) {
// for every room do
foreach($room_assignment[1] as $room_array) {
// info:
// $room_array = array(
// "token" => bla,
// "name" => Blabla,
// "active_users" => -1,
// "description" => Blabla bla bla
//);
$server_url = $room_assignment[0];
$room_json_url = $server_url . "/room/" . $room_array["token"];
echo($room_json_url . PHP_EOL);
$contents = file_get_contents($room_json_url);
if($contents) {
// print_r($contents);
$json_obj = json_decode($contents);
$pinned_messages = $json_obj->pinned_messages;
echo("Pinned messages:" . PHP_EOL);
print_r($pinned_messages);
}
}
}
}
// run main function
main();
?>