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

$preblurb="<h1>Remote Files</h1><h3>RJM Programming - March, 2023</h3><hr><br>";
$uval="";
$tval="";
$oval="1";
$blurb="";
$pblurb="";
$cfor="For ";
$title="";
$pregt="@\<title\>(.*)\</title\>@i";
$matcho=1;
$curo=0;
if (isset($_POST['url']) && $_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> ";
$uval=str_replace("+"," ",urldecode($_POST['url']));
$cfor="for ";
} else if (isset($_GET['url']) && $_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> ";
$uval=str_replace("+"," ",urldecode($_GET['url']));
$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']) && $_POST['type'] != '') {
$pregt="@\\<" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>(.*)\\</" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>@i";
$pregt="@\\>(.*)\\</" . str_replace("+"," ",urldecode($_POST['type'])) . "\\>@i";
$tval=str_replace("+"," ",urldecode($_POST['type']));
$pblurb.=$cfor . str_replace("+"," ",urldecode($_GET['type'])) . " element ";
$cfor=" ";
} else if (isset($_GET['type']) && $_GET['type'] != '') {
$pregt="@\\<" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>(.*)\\</" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>@i";
$pregt="@\\>(.*)\\</" . str_replace("+"," ",urldecode($_GET['type'])) . "\\>@i";
$tval=str_replace("+"," ",urldecode($_GET['type']));
$pblurb.=$cfor . str_replace("+"," ",urldecode($_GET['type'])) . " element ";
$cfor=" ";
} else {
$pregt="@\<title\>(.*)\</title\>@i";
$cfor.="title " . " element ";
}
if (isset($_POST['ord']) && $_POST['ord'] != '') {
$matcho=intval(str_replace("+"," ",urldecode($_POST['ord'])));
$oval="$matcho";
$pblurb.=$cfor . "number " . $matcho . " ... <br>";
} else if (isset($_GET['ord']) && $_GET['ord'] != '') {
$matcho=intval(str_replace("+"," ",urldecode($_GET['ord'])));
$oval="$matcho";
$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);
$postblurb="<hr><br><form action=./remote_files.php method=GET><input ondblclick=\"if (this.value.length == 0) { this.value='http://www.rjmprogramming.com.au/index.php'; }\" style=width:60%;' type=url name=url value='" . $uval . "' title='URL [http://www.rjmprogramming.com.au/index.php]' placeholder='URL [http://www.rjmprogramming.com.au/index.php]'></input><br><input ondblclick=\"if (this.value.length == 0) { this.value='title'; }\" style=width:60%;' type=text name=type value='" . $tval . "' title='Element type [title]' placeholder='Element type [title] ... Number [1] below ...'></input><br><input style=width:60%;' type=number name=ord value='" . $oval . "' min=1 step=1 title='Number [1]' placeholder='Number [1]'></input><br><br><input type=submit value=Search></input></form>";
echo "<html><head><title>Remote Files - RJM Programming - March, 2023</title></head><body>${preblurb}<p>${blurb}${title}</p>${postblurb}</body></html>";
exit;
?>