<?php
  // recent-posts-2.php
  // Place image on recent-posts-2 li's of //www.rjmprogramming.com.au/wordpress blog
  // RJM Programming
  // December, 2014
  ini_set('max_execution_time', 60000);

  $kshstuff='#!/bin/ksh' . "\n";
  if (file_exists(dirname(__FILE__) . "/rpfollowup.ksh")) {
   unlink(dirname(__FILE__) . "/rpfollowup.ksh");
  }

  date_default_timezone_set('Australia/Perth');
  $curdateis=date('Y/m/d h:i:s');
  $numtoshow=7;
  $noff=0;
   $cury=substr('0000' . date('Y',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-4,4);
   $curm=substr('0' . date('m',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);
   $curd=substr('0' . date('d',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);

//  echo $curdateis . ' ' . $curd; //date('d',strtotime ( '-' . ($numtoshow + $noff) . ' day' , strtotime ( $curdateis) ));
//  exit;
  $spanalt="";
  $altalt="''";
  
  function ourcopy($fromi, $toi) {
    $altnf=$fromi;
    if (strpos($fromi,'rjmprogramming.com.au/') !== false) {
      $altnf=str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . explode('#',explode('?',explode('rjmprogramming.com.au/',$fromi)[1])[0])[0]);
      //file_put_contents('xz.xz', $altnf . "\n" . $fromi . "\n" . $toi);
      if (!file_exists($altnf)) {
      //file_put_contents('xzz.xzz', $altnf . "\n" . $fromi . "\n" . $toi);
        $winfnmis=basename($altnf);
        $windiris=str_replace($winfnmis,'',$altnf);
      //file_put_contents('xzzz.xzzz', str_replace(basename($altnf),'',$altnf) . 'slideshow_0.zip');
        if (file_exists(str_replace(basename($altnf),'',$altnf) . 'slideshow_0.zip')) {
      //file_put_contents('xzzzy.xzzzy', str_replace(basename($altnf),'',$altnf) . 'slideshow_0.zip');
         $retv=shell_exec("unzip -l " . $windiris . "slideshow_0.zip " . $winfnmis);
         if (strpos($retv, $winfnmis) !== false) {
      //file_put_contents('xzzzyy.xzzzyy', str_replace(basename($altnf),'',$altnf) . 'slideshow_0.zip');
          if (!file_exists(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis)) {
      //file_put_contents('xzzzyyy.xzzzyyy', str_replace(basename($altnf),'',$altnf) . 'slideshow_0.zip');
          $rt="unzip " . $windiris . "slideshow_0.zip " . $winfnmis . " -d " . str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR;
          $rt.=shell_exec("unzip " . $windiris . "slideshow_0.zip " . $winfnmis . " -d " . str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR);
      //file_put_contents('xzzzyyyc.xzzzyyyc', str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis);
          copy(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis, $toi);
          if (file_exists(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis)) {
      //file_put_contents('xzzzyyycx.xzzzyyycx', str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis);
          unlink(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . '..' . DIRECTORY_SEPARATOR . $winfnmis);
          return true;
          }
          } 
         }
        }
      }
    }
    return copy($fromi, $toi);
  }
  
  function minuszerobetter($infp) {
    if (file_exists(dirname(__FILE__) . "/" . explode(".", $infp)[0] . "-1.jpeg")) {
      if (file_exists(dirname(__FILE__) . "/" .  explode(".", $infp)[0] . "-0.jpeg")) {
      return explode(".", $infp)[0] . "-0.jpeg";
      } else {
      return explode(".", $infp)[0] . ".jpg";
      }
    }
    if (!file_exists(dirname(__FILE__) . "/" . $infp)) { return str_replace("jpeg","jpg",$infp); } 
    return $infp;
  }
  
  if (file_exists(dirname(__FILE__) . "/wpage.txt")) {
   $classtagcloud=file_get_contents("http://www.rjmprogramming.com.au/ITblog/?p=14234");
   $classtags=explode('<div class="tagcloud"', str_replace("http:","",str_replace("https:","",$classtagcloud)));
   if (sizeof($classtags) > 1) {
     file_put_contents(dirname(__FILE__) . '/divtagcloud.html', str_replace('<a ','<a target=_blank ','<html><body><div class="tagcloud"' . explode('</div>', $classtags[1])[0] . '</div></body></html>'));
   }
   //echo "here1";
   $numtoshow = 8;
   $noff=1;
   //$cury=substr('0000' . date('Y',strtotime ( '-' . ($numtoshow + $noff) . ' day' , strtotime ( $curdateis) ) ),-4,4);
   //$curm=substr('0' . date('m',strtotime ( '-' . ($numtoshow + $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);
   //$curd=substr('0' . date('d',strtotime ( '-' . ($numtoshow + $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);
   if (1 == 4) {
   $spanalt='<span alt="' . $cury . '/' .$curm . '/' . $curd . '" title="' . $cury . '/' .$curm . '/' . $curd . '"></span>';
   } else {
   $altalt='"' . $cury . '/' .$curm . '/' . $curd . "'";
   }
   $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>";
     $htmlis = "<html><head><script type='text/javascript'> " . 
        " var one_o_five=600, eightthousand=8000; if (navigator.userAgent.match('CriOS')) { eightthousand=1000; if (parent.window) { eightthousand=1001;   }    }   function postpost() { var ims, jms=0;  ims=parent.document.getElementsByTagName('a'); for (jms=0;jms<ims.length; jms++) {  if (ims[jms].target == '_blank' && ims[jms].href.indexOf('ITblog/') != -1) { ims[jms].target='_top'; } }  } function postonl() { var ims, jms=0; if (navigator.userAgent.match('CriOS')) {  if (eightthousand == 1001) { setTimeout(postpost,7000);   }    ims=document.getElementsByTagName('a'); for (jms=0;jms<ims.length; jms++) { ims[jms].src=ims[jms].src.replace('-0.jpeg','.jpg');  }  } } function ol() {" . 
        " if ((location.search.split('totalwidth=')[1] ? decodeURIComponent(location.search.split('totalwidth=')[1].split('&')[0]).replace(/\+/g,' ') : '') == '' && navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { setTimeout(postonl,eightthousand); " . 
        " one_o_five=105;" .  
        " document.getElementById('dtable').style.width='260px';" .  
        " document.getElementById('dtable').style.overflow='scroll'; } else if (1 == 2) {" .  
        " for (var j=3; j<=8; j++) { document.getElementById(j).style.display='none';  } } else { setTimeout(postonl,1000); } } </script><style> td { vertical-align: top; } </style></head><body onload='ol();' data-style='width:260px;'><div id='dtable'><table id='mytable' 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></div></body></html>";
   $thisimg = "";
   $thistitle = "";
   $thishref = "";
   $spanalt="";
   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=" . $ahrefis[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
      } else if (substr($thisimg,0,1) == '/') {  // ... and what about a relative image URL
        $thisimg='http://www.rjmprogramming.com.au' . $thisimg;
      } else if (substr($thisimg,0,3) == '../') {  // ... and what about a relative image URL
        $thisimg='http://www.rjmprogramming.com.au' . substr($thisimg,2);
      } else if (substr($thisimg,0,2) == './') {  // ... and what about a relative image URL
        $thisimg='http://www.rjmprogramming.com.au/ITblog' . substr($thisimg,1);
      }
//echo "\n" . str_replace('https:','http:',$thisimg) . "\n";
      if (strpos($thisimg, 'rjmprogramming.com.au/') !== false) {
      if (!file_exists(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . explode('rjmprogramming.com.au/', $thisimg)[1])) {
      ourcopy($thisimg, dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg");
      } else {
      copy(str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR) . explode('rjmprogramming.com.au/', $thisimg)[1], dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg");
      }
      } else {
      $icont = file_get_contents(str_replace('https:','http:',$thisimg));
//echo "\n" . strlen($icont) . "\n";
      file_put_contents(dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg", $icont);
      }
      if (file_exists(dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg")) {
        unlink(dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg");
      }
      $zis=0;
      while (file_exists(dirname(__FILE__) . "/" . $narray[$thisij] . "-" . $zis . ".jpeg")) {
        unlink(dirname(__FILE__) . "/" . $narray[$thisij] . "-" . $zis . ".jpeg");
        $zis++;
      }
  //echo "herep " . $narray[$thisij] . ' ' . "/bin/convert " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg  -resize 105 -quality 50 -strip " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg >> ~rjmprogr/public_html/rpok.txt 2>> ~rjmprogr/public_html/rpbad.txt";
      $kshstuff.="/bin/convert " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg  -resize 105 -quality 50 -strip " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg" . "\n"; 
      //exec("/bin/convert " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpg  -resize 105 -quality 50 -strip " . dirname(__FILE__) . "/" . $narray[$thisij] . ".jpeg"); 
  //echo "hereq";
      sleep(5);
      $prehtmlis=$htmlis;
      $htmlis = str_replace("<td id=" . ($thisij + 1) . "></td>", "<td id=" . ($thisij + 1) . "><a target=_blank href='" . $thishref  . "' title='" . $thistitle . "'>" . $spanalt . "<img onmouseover=\"this.src=this.src.replace('-0.jpeg','.jpg').replace('.jpeg','.jpg'); this.style.width=one_o_five;\" onmouseout='this.style.width=105;' style='width:105px;' alt=" . $altalt . " title='" . $thistitle . "' src='//www.rjmprogramming.com.au/PHP/" . minuszerobetter($narray[$thisij] . ".jpeg") . "?rand=" . rand(0,12345678) . "' /></a></td>", $htmlis);
      $thisij--;
      if ($prehtmlis != $htmlis) {
   $noff++;
   $cury=substr('0000' . date('Y',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-4,4);
   $curm=substr('0' . date('m',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);
   $curd=substr('0' . date('d',strtotime ( '-' . ($numtoshow - $noff) . ' day' , strtotime ( $curdateis) ) ),-2,2);
      }
   if (1 == 4) {
   $spanalt='<span alt="' . $cury . '/' .$curm . '/' . $curd . '" title="' . $cury . '/' .$curm . '/' . $curd . '"></span>';
   } else {
   $altalt='"' . $cury . '/' .$curm . '/' . $curd . '"';
   }
    }
    file_put_contents(dirname(__FILE__) . "/zero.html", str_replace(" href='",' href="',str_replace("#andabit=-70' title=", '" title=', str_replace("#andabit=-7x' title='","#andabit=-70' title='", str_replace("0' title='","0#andabit=-7x' title='",str_replace("1' title='","1#andabit=-7x' title='",str_replace("2' title='","2#andabit=-7x' title='",str_replace("3' title='","3#andabit=-7x' title='",str_replace("4' title='","4#andabit=-7x' title='",str_replace("5' title='","5#andabit=-7x' title='",str_replace("6' title='","6#andabit=-7x' title='",str_replace("7' title='","7#andabit=-7x' title='",str_replace("8' title='","8#andabit=-7x' title='",str_replace("9' title='","9#andabit=-7x' title='",    str_replace("/wordpress/", "/ITblog/", $htmlis)))))))))))))));
   }
   //echo "here2";
   unlink(dirname(__FILE__) . "/wpage.txt");
  }
     //echo "here3";
  file_put_contents(dirname(__FILE__) . "/rpfollowup.ksh", $kshstuff . "\nrm -f " . dirname(__FILE__) . "/rpfollowup.ksh\nexit");
?>
