<?php
// remote_files.php
// RJM Programming
// March, 2023
// Thanks to https://php-legacy-docs.zend.com/manual/php5/en/features.remote-files

$blurb="";
$pblurb="";
$cfor="For ";
$title="";
$pregt="@\<title\>(.*)\</title\>@i";
$matcho=1;
$curo=0;
if (isset($_POST['url'])) {
$file = fopen ("http:" . str_replace("http:","",str_replace("https:","",str_replace("+"," ",urldecode($_POST['url'])))), "r");
$pblurb="Looking in <a target=_blank href='" . str_replace("+"," ",urldecode($_POST['url'])) . "' title='" . str_replace("+"," ",urldecode($_POST['url'])) . "'>" . str_replace("+"," ",urldecode($_POST['url'])) . "</a> ";
$cfor="for ";
} else if (isset($_GET['url'])) {
$file = fopen ("http:" . str_replace("http:","",str_replace("https:","",str_replace("+"," ",urldecode($_GET['url'])))), "r");
$pblurb="Looking in <a target=_blank href='" . str_replace("+"," ",urldecode($_GET['url'])) . "' title='" . str_replace("+"," ",urldecode($_GET['url'])) . "'>" . str_replace("+"," ",urldecode($_GET['url'])) . "</a> ";
$cfor="for ";
} else {
$file = fopen ("http://www.rjmprogramming.com.au/index.php", "r");
$cfor="Looking in " . "http://www.rjmprogramming.com.au/index.php" . " for ";
}
if (isset($_POST['type'])) {
$pregt="@\\<" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>(.*)\\</" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>@i";
$pregt="@\\>(.*)\\</" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>@i";
$pblurb.=$cfor . str_replace("+"," ",urldecode($_GET['type'])) . " element ";
$cfor=" ";
} else if (isset($_GET['type'])) {
$pregt="@\\<" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>(.*)\\</" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>@i";
$pregt="@\\>(.*)\\</" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>@i";
$pblurb.=$cfor . str_replace("+"," ",urldecode($_GET['type'])) . " element ";
$cfor=" ";
} else {
$pregt="@\<title\>(.*)\</title\>@i";
$cfor.="title " . " element ";
}
if (isset($_POST['ord'])) {
$matcho=intval(str_replace("+"," ",urldecode($_POST['ord'])));
$pblurb.=$cfor . "number " . $matcho . " ... <br>";
} else if (isset($_GET['ord'])) {
$matcho=intval(str_replace("+"," ",urldecode($_GET['ord'])));
$pblurb.=$cfor . "number " . $matcho . " ... <br>";
} else {
$matcho=1;
$cfor.="number " . $matcho . " ... <br>";
}
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
}
while (!feof ($file)) {
    $line = fgets ($file, 1024);
    /* This only works if the title and its tags are on one line */
    if (preg_match ($pregt, $line, $out)) {
        $title = $out[1];
        $curo++;
        if ($curo >= $matcho) { $blurb="$pblurb";  break; }
    }
}
fclose($file);
echo "<p>${blurb}${title}</p>";
exit;
?>