Preliminary adition to transing the internet webring, and tweaks and optimization to previous code regarding javascript to php rewrite, more concise and legible code functions.
All checks were successful
Website Sync / website-sync (push) Successful in 5s
All checks were successful
Website Sync / website-sync (push) Successful in 5s
This commit is contained in:
@@ -2,148 +2,8 @@
|
||||
|
||||
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 = [
|
||||
// File sources, images & scripts
|
||||
$TRANSFEMRING_FILES = [
|
||||
"nonbinary" => [
|
||||
"arrow_path" => "../../../img/webrings/transfem_enby_arrow.png",
|
||||
"banner_path" => "../../../img/webrings/transfem_enby_banner.png"
|
||||
@@ -153,3 +13,100 @@ $file = [
|
||||
"banner_path" => "../../../img/webrings/transfem_trans_banner.png"
|
||||
]
|
||||
];
|
||||
|
||||
$TRANSFEMRING_SCRIPT_SOURCE = [
|
||||
"selected_source" => 1,
|
||||
"sources" =>
|
||||
[["url" => "https://fem.nz/ring/transfemring.js",
|
||||
"source" => "internet"],
|
||||
["url" => "/transfemwebring.js",
|
||||
"source" => "local"]]
|
||||
];
|
||||
|
||||
// Specific variables for script
|
||||
$TRANSFEMRING_ROOT = "https://fem.nz";
|
||||
$TRANSFEMRING_VALID_WIDGET_TYPES = ["transgender", "nonbinary"];
|
||||
|
||||
// Personal identifiers/choices
|
||||
$TRANSFEMRING_WIDGET_TYPE = $TRANSFEMRING_VALID_WIDGET_TYPES[1];
|
||||
$TRANSFEMRING_ID = 8;
|
||||
|
||||
// Functions
|
||||
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;
|
||||
}
|
||||
|
||||
$transfemring_sites = scrapeWebsiteSites($TRANSFEMRING_SCRIPT_SOURCE,'sites: [',']');
|
||||
$transfemring_sites = primarySplit($transfemring_sites);
|
||||
for ($i = 0; $i < sizeof($transfemring_sites); $i++) {
|
||||
$depured_transfemring_sites[$i] = secondarySplit($transfemring_sites[$i]);
|
||||
}
|
||||
$transfemring_sites = $depured_transfemring_sites;
|
||||
|
||||
for ($i = 1; $i < count($transfemring_sites); $i++){
|
||||
if ($transfemring_sites[$i]['id'] == $TRANSFEMRING_ID) {
|
||||
|
||||
$transfem_prev = 'https://' . $transfemring_sites[($i-1)]["site_link"];
|
||||
$transfem_current = 'https://' . $transfemring_sites[($i)]["site_link"];
|
||||
$transfem_next = 'https://' . $transfemring_sites[($i+1)]["site_link"];
|
||||
$found = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($found)) {
|
||||
echo ("ERROR ID NOT FOUND");
|
||||
}
|
||||
Reference in New Issue
Block a user