Files
website/includes/transfemwebring_contr.inc.php

156 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
$JS_SCRIPT_URL = [
"selected_source" => 1,
"sources" =>
[["url" => "https://fem.nz/ring/transfemring.js",
"source" => "internet"],
["url" => "includes/transfemring.js",
"source" => "local"]]
];
$VALID_WIDGET_TYPES = ["transgender", "nonbinary"];
$widget_type = $VALID_WIDGET_TYPES[1];
$id = 8;
/* Implement fallback if scraping doesn't work */
function scrapeSitesFromSource(array $source) {
function getSiteArray(array $source) {
function getFile(array $source){
$selected_source = $source["sources"][$source["selected_source"]];
if ($selected_source["source"] === "local") {
$file = file_get_contents($selected_source["url"]);
} else {
$curl = curl_init($selected_source["url"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$file = curl_exec($curl);
if ($file === false) {
echo "Error: " . curl_error($curl);
exit;
}
curl_close($curl);
}
return $file;
}
function extractSites(string $script) {
$start_pos = strpos($script,"sites: [") + 8; // needle has 9 characters
$end_pos = strpos($script, "]", $start_pos);
$sites = substr($script, $start_pos, $end_pos - $start_pos);
return $sites;
}
return extractSites(getFile($source));
}
function primarySplit(string $array) {
$i = 0;
while (strlen($array) !== 0) {
// Strip coma if found
if (str_starts_with($array,',')) {
$array = substr($array,1);
}
// Get substring position and length
$start_pos = 1;
$end_pos = strpos($array,"}",1);
$item_len = $end_pos - $start_pos;
// Save website into array, and strip array
$return_array[$i] = substr($array,$start_pos,$item_len);
$array = substr($array,$item_len+2); // +2 from { and }
$i++;
}
return $return_array;
}
function secondarySplit(string $sub_array) {
while (strlen($sub_array) !== 0){
if (str_starts_with($sub_array,',')) {
$sub_array = substr($sub_array,1);
}
$key_start_pos = 1;
$key_end_pos = strpos($sub_array,"\"",1);
$key_len = $key_end_pos - $key_start_pos;
$key = substr($sub_array,$key_start_pos,$key_len);
$value_start_pos = $key_len + 3; // +3 from " , " and :
// Check if key is quoted
if (strpos($sub_array,"\"",$value_start_pos) != $value_start_pos) {
$keyhasQuotes = FALSE;
$value_end_pos = strpos($sub_array,",");
} else {
$value_start_pos++; // +1 from "
$value_end_pos = strpos($sub_array,"\"",$value_start_pos);
$keyhasQuotes = TRUE;
}
$value_len = $value_end_pos - $value_start_pos;
$value = substr($sub_array,$value_start_pos,$value_len);
$return_sub_array[$key] = $value;
$sub_sub_array_len = $key_len + $value_len + 3; // +3 from " , " and :
if ($keyhasQuotes) {
$sub_sub_array_len = $sub_sub_array_len + 2; // +2 from " and "
}
$sub_array = substr($sub_array,$sub_sub_array_len);
}
return $return_sub_array;
}
$websites = primarySplit(getSiteArray($source));
for ($i = 0; $i < sizeof($websites); $i++) {
$return_websites[$i] = secondarySplit($websites[$i]);
}
return $return_websites;
}
$transfermring_sites = scrapeSitesFromSource($JS_SCRIPT_URL);
$root = "https://fem.nz";
// Add checks if id is at the start
for ($i = 0; $i < count($transfermring_sites); $i++){
if ($transfermring_sites[$i]["id"] == $id){
$prev = $transfermring_sites[($i-1)]["site_link"];
$current = $transfermring_sites[($i)]["site_link"];
$next = $transfermring_sites[($i+1)]["site_link"];
}
}
$prev = 'https://' . $prev;
$current = 'https://' . $current;
$next = 'https://' . $next;
$file = [
"nonbinary" => [
"arrow_path" => "../../../img/webrings/transfem_enby_arrow.png",
"banner_path" => "../../../img/webrings/transfem_enby_banner.png"
],
"transgender" => [
"arrow_path" => "../../../img/webrings/transfem_trans_arrow.png",
"banner_path" => "../../../img/webrings/transfem_trans_banner.png"
]
];