<?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="";
$cval="";
$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 (isset($_POST['containing']) && $_POST['containing'] != '') {
if ($pregt == "@\<title\>(.*)\</title\>@i") {
$pregt="@\\>(.*)" . str_replace("+"," ",urldecode($_POST['containing'])) . "\\</@i";
} else {
$pregt=str_replace("(.*)", "(.*)" . str_replace("+"," ",urldecode($_POST['containing'])), $pregt);
}
$cval=str_replace("+"," ",urldecode($_POST['containing']));
$pblurb=str_replace(" ... <br>","",$pblurb);
if ($tval == "") {
$pblurb=str_replace(" numbering up to 1 "," in unlimited search ",str_replace(" number "," numbering up to ",$pblurb));
}
$pblurb=str_replace(" numbering up to 1 "," in unlimited search ",$pblurb) . " containing " . str_replace("+"," ",urldecode($_POST['containing'])). " ... <br>";
$cfor=" ";
} else if (isset($_GET['containing']) && $_GET['containing'] != '') {
if ($pregt == "@\<title\>(.*)\</title\>@i") {
$pregt="@\\>(.*)" . str_replace("+"," ",urldecode($_GET['containing'])) . "\\</@i";
} else {
$pregt=str_replace("(.*)", "(.*)" . str_replace("+"," ",urldecode($_GET['containing'])), $pregt);
}
$cval=str_replace("+"," ",urldecode($_GET['containing']));
$pblurb=str_replace(" ... <br>","",$pblurb);
if ($tval == "") {
$pblurb=str_replace(" numbering up to 1 "," in unlimited search ",str_replace(" number "," numbering up to ",$pblurb));
}
$pblurb=str_replace(" numbering up to 1 "," in unlimited search ",$pblurb) . " containing " . str_replace("+"," ",urldecode($_GET['containing'])). " ... <br>";
$cfor=" ";
}
//echo str_replace('<','&lt;',str_replace('>','&gt;',$pregt));
//exit;
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)) {
        if ($cval == "") {
        $title = $out[1];
        } else if ($title == "") {
        $title .= $out[1] . $cval;
        } else {
        $title .= "<br>" . $out[1] . $cval;
        }
        $curo++;
        if ($curo >= $matcho) { $blurb=str_replace(" numbering up to 1 containing "," in unlimited search containing ",$pblurb); if ($cval != "" && $tval == "" && $matcho == 1) { $curo=$curo; } else { 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><input style=width:60%;' type=text name=containing value='" . $cval . "' title='Containing []' placeholder='Containing []'></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;
?>