<?php
//gotoend.php
// Get to end of list of postings
// RJM Programming - January, 2015

$optbits=";";
$midbit = "";

function revcmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
}

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

function server_remote_addr() {
    if (isset($_GET['theipis'])) return urldecode($_GET['theipis']);
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return $rma;
}
 
 function localmaybeone($tarray) {
   global $optbits;
   if (isset($_GET['byorder'])) {
    $hbits = explode(' class="entry-title"', $tarray);
    if (sizeof($hbits) > 1) {
     if (!file_exists("localsearch_" . server_remote_addr() . ".yzy")) file_put_contents("localsearch_" . server_remote_addr() . ".yzy", "<select onchange=window.open(this.value); id=asel><OPTION value=>Optionally select alphabetical blog post below ...</OPTION>");
     for ($ip=1; $ip<sizeof($hbits); $ip++) {
      $eabits = explode('</', $hbits[$ip]);
      $preabits = explode('>', $eabits[0]);
      if (strtolower($preabits[sizeof($preabits) - 1]) != "not found" && strpos($optbits, ";" . $preabits[sizeof($preabits) - 1] . ";") === false) $optbits .= $preabits[sizeof($preabits) - 1] . ";";
     }
    }
   }
 } 

  $navbelow = "#nav-below";
  if (isset($_GET['max']) && isset($_GET['step'])) {
   $oururl =  "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'];
   if (str_replace("&paged=", "", $_SERVER['QUERY_STRING']) == $_SERVER['QUERY_STRING']) {
     $ourmax = $_GET['max'];
     $ourstep = $_GET['step'];
     $factor = -1;
     $prevmax = $ourmax;
     $prevstep = $ourstep;
     if (isset($_GET['byorder'])) {
     if (file_exists("localsearch_" . server_remote_addr() . ".yzy")) unlink("localsearch_" . server_remote_addr() . ".yzy");
     $ourmax = 1;
     $oururl = "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'];
     $cont = @file_get_contents($oururl);
     if ($cont === FALSE) $cont = "Not Found";
         localmaybeone($cont);
     } else {
     $oururl = "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'] . "&paged=" . $_GET['max'];
     // echo $oururl;
     $cont = @file_get_contents($oururl);
     if ($cont === FALSE) $cont = "Not Found";
     }
     if (str_replace("Not Found", "", $cont) != $cont || str_replace("Nothing Found", "", $cont) != $cont) {
       while ((str_replace("Not Found", "", $cont) != $cont || str_replace("Nothing Found", "", $cont) != $cont) && ($ourstep > 1 || isset($_GET['byorder'])) && $ourmax > 1) {
         localmaybeone($cont);
         if (!isset($_GET['byorder'])) {
         $ourstep = $ourstep / 2;
         }
         $ourmax = $ourmax + ($ourstep * $factor);
         $oururl =  "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'] . "&paged=" . $ourmax;
     $cont = @file_get_contents($oururl);
     if ($cont === FALSE) $cont = "Not Found";
       }
       $factor = (0 - $factor);
       $ourstep = 1;
       $ij = 0;
       while ((str_replace("Not Found", "", $cont) == $cont && str_replace("Nothing Found", "", $cont) == $cont) && $ij < 256) {
	    $ij = $ij + 1;
         $ourmax = $ourmax + ($ourstep * $factor);
         $oururl =  "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'] . "&paged=" . $ourmax;
     $cont = @file_get_contents($oururl);
     if ($cont === FALSE) {
       $cont = "Not Found";
     } else {
         localmaybeone($cont);
       $huhp = explode('<div id="post-', $cont);
       if (sizeof($huhp) > 1) {
         $nexthuhp = explode('"', $huhp[sizeof($huhp) - 1]);
         $navbelow = "#post-" . $nexthuhp[0];
       }
     }
       }
       $factor = (0 - $factor);
       $ourmax = $ourmax + ($ourstep * $factor);
       $oururl =  "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING'] . "&paged=" . $ourmax;
      
     }
   }
   if (file_exists("localsearch_" . server_remote_addr() . ".yzy")) {
    if (isset($_GET['byorder'])) $navbelow="";
    $exoptbits=explode(";", $optbits);
    if (isset($_GET['reverse'])) {
    usort($exoptbits, "revcmp");
    } else {
    usort($exoptbits, "cmp");
    }
    for ($ii=0; $ii<sizeof($exoptbits); $ii++) {
     if ($exoptbits[$ii] != "") {
      file_put_contents("localsearch_" . server_remote_addr() . ".yzy", file_get_contents("localsearch_" . server_remote_addr() . ".yzy") . "\n<option value=http://www.rjmprogramming.com.au/ITblog/" . str_replace(" ","-",str_replace("/","",str_replace(",","",strtolower($exoptbits[$ii])))) . ">" . $exoptbits[$ii] . "</option>");
     }
    }
    file_put_contents("localsearch_" . server_remote_addr() . ".yzy", str_replace("\n", "", file_get_contents("localsearch_" . server_remote_addr() . ".yzy")) . "</select>");
   }
   if (isset($_GET['byorder']) || $oururl != "http://www.rjmprogramming.com.au/wordpress/?" . $_SERVER['QUERY_STRING']) header("Location: " . str_replace("byorder=", "orbyder=", $oururl) . $midbit . $navbelow);
  }
?>
