Files
website/includes/common_webrings.inc.php
2026-03-16 17:10:25 -03:00

45 lines
1.3 KiB
PHP

<?php
// Gets a file given an array with sources
function getFile(array $source){
$selected_source = $source["sources"][$source["selected_source"]];
if ($selected_source["source"] === "local") {
$file = file_get_contents(__DIR__ . $selected_source["url"]);
} else if ($selected_source["source"] === "internet") {
$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);
}
unset($selected_source,$curl);
return $file;
}
// Returns the given sub-string given a initial search pattern, and a final end pattern.
function extractString(string $string,string $start_pattern, string $end_pattern) {
$start_pos = strpos($string,$start_pattern,strlen($start_pattern));
$end_pos = strpos($string,$end_pattern, $start_pos);
$extracted_string = substr($string, $start_pos, $end_pos - $start_pos);
unset($start_pos,$end_pos);
return $extracted_string;
}
// Self-explanatory
function scrapeWebsiteSites(array $source,string $start_pattern, string $end_pattern) {
$script = getFile($source);
$websites = extractString($script,$start_pattern,$end_pattern);
unset($script);
return $websites;
}