<?php
  // recent-posts-2.php
  // Place image on recent-posts-2 li's of //www.rjmprogramming.com.au/wordpress blog
  // RJM Programming
  // December, 2014
  if (file_exists(dirname(__FILE__) . "/wpage.txt")) {
   //echo "here1";
   $numtoshow = 8;
   $narray = array("one", "two", "three", "four", "five", "six", "seven", "eight");
   $wpage = file_get_contents(dirname(__FILE__) . "/wpage.txt"); //"//www.rjmprogramming.com.au/PHP/wpage.txt");
   $warticles = explode('<div class="entry-content">', $wpage);
   $htmlis = "<html><head><script type='text/javascript'> var one_o_five=600; function ol() { if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { one_o_five=105; for (var j=3; j<=8; j++) { document.getElementById(j).style.display='none';  } } } </script><style> td { vertical-align: top; } </style></head><body onload='ol();' style='width:260px;'><table style='overflow:scroll;'><tr><td id=1></td><td id=2></td><td id=3></td><td id=4></td><td id=5></td><td id=6></td><td id=7></td><td id=8></td></tr></table></body></html>";
   $thisimg = "";
   $thistitle = "";
   $thishref = "";
   if (sizeof($warticles) >= $numtoshow) {
   //echo "here1a";
    $thisij = sizeof($narray) - 1;
    for ($jh=(sizeof($warticles) - 1); $jh>=(sizeof($warticles) - $numtoshow); $jh--) {
      $ahrefis = explode("var dv=document.getElementById(", $warticles[$jh]);
      if (sizeof($ahrefis) > 1) {
   //echo " here1b=" . $thesehref[0] . " ";
      if (substr($ahrefis[sizeof($ahrefis) - 1], 0, 1) == "'") {
        $thesehref = explode("'", substr($ahrefis[sizeof($ahrefis) - 1], 1));
        $thishref = "//www.rjmprogramming.com.au/ITblog/?p=" . str_replace("d", "", $thesehref[0]);
      } else if (substr($ahrefis[sizeof($ahrefis) - 1], 0, 1) == '"') {
        $thesehref = explode('"', substr($ahrefis[sizeof($ahrefis) - 1], 1));
        $thishref = "//www.rjmprogramming.com.au/ITblog/?p=" . str_replace("d", "", $thesehref[0]);
      } else {
        $thesehref = explode(" ", $ahrefis[sizeof($ahrefis) - 1]);
        $thishref = "//www.rjmprogramming.com.au/ITblog/?p=" . str_replace("d", "", $thesehref[0]);
      }
      } else {
      $hrefis = explode("href=", $warticles[$jh]);
   //echo " here1c ";
      if (substr($hrefis[1], 0, 1) == "'") {
        $thesehref = explode("'", substr($hrefis[1], 1));
        $thishref = $thesehref[0];
      } else if (substr($hrefis[1], 0, 1) == '"') {
        $thesehref = explode('"', substr($hrefis[1], 1));
        $thishref = $thesehref[0];
      } else {
        $thesehref = explode(" ", $hrefis[1]);
        $thishref = $thesehref[0];
      }
      }
      $titleis = explode("title=", $warticles[$jh]);
      if (substr($titleis[1], 0, 1) == "'") {
        $thesetitle = explode("'", substr($titleis[1], 1));
        $thistitle = $thesetitle[0];
      } else if (substr($titleis[1], 0, 1) == '"') {
        $thesetitle = explode('"', substr($titleis[1], 1));
        $thistitle = $thesetitle[0];
      } else {
        $thesetitle = explode(" ", $titleis[1]);
        $thistitle = $thesetitle[0];
      }
      $imgis = explode("src=", $warticles[$jh]);
      if (substr($imgis[1], 0, 1) == "'") {
        $theseimg = explode("'", substr($imgis[1], 1));
        $thisimg = $theseimg[0];
      } else if (substr($imgis[1], 0, 1) == '"') {
        $theseimg = explode('"', substr($imgis[1], 1));
        $thisimg = $theseimg[0];
      } else {
        $theseimg = explode(" ", $imgis[1]);
        $thisimg = $theseimg[0];
      }
      if (substr($thisimg,0,2) == '//') $thisimg='http:' . $thisimg;    // new SSL specific line 4/5/2017
      $icont = file_get_contents($thisimg);
      file_put_contents(dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg", $icont);
      exec("/usr/local/cpanel/3rdparty/bin/convert " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg  -resize 105 -quality 50 -strip " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg"); 
      $htmlis = str_replace("<td id=" . ($thisij + 1) . "></td>", "<td id=" . ($thisij + 1) . "><a target=_blank href='" . $thishref  . "' title='" . $thistitle . "'><img onmouseover=\"this.src=this.src.replace('.jpeg','.jpg'); this.style.width=one_o_five;\" onmouseout='this.style.width=105;' style='width:105px;' alt='" . $thistitle . "' title='" . $thistitle . "' src='//www.rjmprogramming.com.au/PHP/" . $narray[$thisij] . ".jpeg" . "' /></a></td>", $htmlis);
      $thisij--;
    }
    file_put_contents(dirname(__FILE__) . "/zero.html", str_replace("/wordpress/", "/ITblog/", $htmlis));
   }
   //echo "here2";
   unlink(dirname(__FILE__) . "/wpage.txt");
  }
     //echo "here3";

?>
