<?php
 // Collect WordPress Blog Postings that Mentioned Pass In Other - RJM Programming - August and October, 2016

 set_time_limit(0);
 
 $pdate = "";
 $isize = 0;
 $found = false;

 
 function pluckfirstreal($pcont) {
   $xpcont=$pcont;
   if (strpos($xpcont, "[/caption]</p>") !== false) {
     $xpcont=explode("[/caption]</p>", $pcont)[1];
   } else if (strpos($xpcont, "[/caption]") !== false) {
     $xpcont=explode("[/caption]", $pcont)[1];
   }
   $paras = explode("</p", str_replace("<p", "</p", $xpcont));
   if (sizeof($paras) > 2) {
     $oth = explode("<", $paras[1]);
     $retval = " ... ";    
     for ($i=0; $i<sizeof($oth); $i++) {
      $huh = explode(">", $oth[$i]);
      $retval .= str_replace("'", "`", str_replace("}", " ", str_replace("{", " ", $huh[-1 + sizeof($huh)])));
     }
     return substr($retval,0,200); 
   } 
   return "";
 }


 function permalinkit($ins) {
   global $pdate;
   $outs=strtolower($ins);
   $outs=str_replace("+","",$outs);
   $outs=str_replace(".","",$outs);
   $outs=str_replace("#","",$outs);
   $outs=str_replace("/","",$outs); 
   $outs=str_replace(" ","-",$outs); 
   $outs=str_replace(" ","-",$outs); 
   $outs=str_replace("\\", "",$outs); 
   $outs=str_replace("!", "",$outs); 
   $outs=str_replace(",", "",$outs); 
   $outs=str_replace("---","-",$outs); 
   $outs=str_replace("--","-",$outs);
   if (str_replace('code-download-table','',$outs) != $outs) {
   $xp=explode("GETME", $pdate);
   $prexp=explode("/", $xp[0]);
   $postprexp=explode(".", $prexp[-1 + sizeof($prexp)]);
   $extis = str_replace("GETME","",str_replace("-","",str_replace("_","",$postprexp[-1 + sizeof($postprexp)])));
   $outs="../getmelist.htm?topoff=150&tsp=" . rand(100, 2000000) . "#" . $postprexp[0] . "." . str_replace($extis,"",$postprexp[-1 + sizeof($postprexp)]) . "GETME" . $extis;
   }  
   return $outs;
 }

 $delim = "<!doctype html><html><head><title>Is Mentioned By - RJM Programming - August, 2016</title><script type='text/javascript'> var coption='option'; var cdisabled='disabled'; var chidden='hidden'; var cblank='_blank'; var isize=0;  function dothis() {  } </script></head><body onload='dothis(); '></body></html>";
 $retval = "";
 $ourtitle = "";
 $yourtitle = "";
 $criteria = "";
 if (isset($_GET['title'])) {
   $yourtitle = urldecode($_GET['title']);
   $ourtitle=str_replace('$%#','',str_replace('"$%#','',($yourtitle . '$%#')));
 } else if (isset($_POST['title'])) {
   $yourtitle = urldecode($_POST['title']);
   $ourtitle=str_replace('$%#','',str_replace('"$%#','',($yourtitle . '$%#')));
 } 
 $ourother = $ourtitle;
 
 if ($ourtitle != "") {
 $words = explode(" ", str_replace(" Tutorial", "", $ourtitle));
 if (sizeof($words) > 5) {
   $criteria = $words[0] . " " . $words[1] . " " . $words[2] . " " . $words[3];
   $ourother = $criteria;
 } else if (sizeof($words) > 4) {
   $criteria = $words[0] . " " . $words[1] . " " . $words[2];
   $ourother = $criteria;
 } else if (sizeof($words) > 3) {
   $criteria = $words[0] . " " . $words[1];
   $ourother = $criteria;
 }
 }

 if ($ourtitle != "") {
 $hostname = 'localhost';

 /*** mysql username ***/
 $username = 'username';

 /*** mysql ***/
 $password = 'password';

 $dbname = 'database_name';
 if (isset($_GET['dbname'])) $dbname = $_GET['dbname'];
 $tname = 'wp_posts';



 $link = mysqli_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
 }
 mysqli_select_db($link,$dbname);
 $secresult="";
 
if (isset($_GET['debug'])) {
 file_put_contents("x.xxx","S");
}
 $afterone='';
 $noteq='!';
 $midclause="";
 $afterand='';
 if (strpos($ourtitle, ' ') !== false) {
  $orbit= " 
OR " . $tname . ".post_content like '%" . str_replace('youllneverfindthis','%',permalinkit($ourtitle)) . "%' " . " 
OR " . $tname . ".post_content like '%" . str_replace('youllneverfindthis','%',$ourother) . "%' " . " 
OR " . $tname . ".post_content like '%" . str_replace('youllneverfindthis','%',$ourtitle) . "%') ";
  $midclause=" UNION SELECT " . $tname . ".post_title, " . $tname . ".post_content as guid, ' ' as post_content, -1 as tfind, (-480 + TIMESTAMPDIFF(MINUTE,NOW()," . $tname . ".post_modified)) as tother, " . $tname . ".post_modified as post_date FROM " . $tname . " WHERE " . $tname . ".post_status = 'publish' AND " . $tname . ".post_modified > NOW() - INTERVAL 1 MONTH ";
  $afterand=" (1 = 1" . $afterone . " OR LOWER(" . $tname . ".post_title) " . $noteq . "= LOWER('" . $ourtitle . "')) " . "
AND (" . $tname . ".post_content like CONCAT(CONCAT('%?p='," . $tname . ".guid), '%') ";
 } else {
  $noteq='';
  $afterone='1';
  $orbit= " SUBSTRING(" . $tname . ".post_content,1,240) like '%" . str_replace('youllneverfindthis','%',$yourtitle) . "%' ";
 }


if (isset($_GET['debug'])) {
 file_put_contents("xxx.xxx","SELECT " . $tname . ".post_title, " . $tname . ".guid, " . $tname . ".post_content, LOCATE('" . $ourtitle . "'," . $tname . ".post_content) as tfind, LOCATE('" . $ourother . "'," . $tname . ".post_title) as tother, " . $tname . ".post_date as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND " . $afterand . $orbit . " UNION SELECT " . $tname . ".post_title, " . $tname . ".post_content as guid, ' ' as post_content, -1 as tfind, (-480 + TIMESTAMPDIFF(MINUTE,NOW()," . $tname . ".post_modified)) as tother, " . $tname . ".post_modified as post_date FROM " . $tname . " WHERE " . $tname . ".post_status = 'publish' AND " . $tname . ".post_modified > NOW() - INTERVAL 1 MONTH " . " UNION SELECT 'Code Download Table' as post_title, " . $tname . ".post_content as guid, '' as post_content, 1 as tfind, 0 as tother, CURTIME() as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND LOWER(" . $tname . ".post_title) = LOWER('" . $ourtitle . "') " . " 
AND " . $tname . ".post_content like '%GETME%' ORDER BY post_date ");
}
 $res = mysqli_query($link,"SELECT " . $tname . ".post_title, " . $tname . ".guid, " . $tname . ".post_content, LOCATE('" . $ourtitle . "'," . $tname . ".post_content) as tfind, LOCATE('" . $ourother . "'," . $tname . ".post_title) as tother, " . $tname . ".post_date as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND " . $afterand . $orbit . $midclause . " UNION SELECT 'Code Download Table' as post_title, " . $tname . ".post_content as guid, '' as post_content, 1 as tfind, 0 as tother, CURTIME() as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND LOWER(" . $tname . ".post_title) = LOWER('" . $ourtitle . "') " . " 
AND " . $tname . ".post_content like '%GETME%' ORDER BY post_date ");

if (isset($_GET['debug'])) {
 file_put_contents("x.xx","SELECT " . $tname . ".post_title, " . $tname . ".guid, " . $tname . ".post_content, LOCATE('" . $ourtitle . "'," . $tname . ".post_content) as tfind, LOCATE('" . $ourother . "'," . $tname . ".post_title) as tother, " . $tname . ".post_date as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND " . $afterand . $orbit . $midclause . " UNION SELECT 'Code Download Table' as post_title, " . $tname . ".post_content as guid, '' as post_content, 1 as tfind, 0 as tother, CURTIME() as post_date FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND LOWER(" . $tname . ".post_title) = LOWER('" . $ourtitle . "') " . " 
AND " . $tname . ".post_content like '%GETME%' ORDER BY post_date ");
}

 if ($res == 0) {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxz","");
}
  if (1 == 2) echo("<b>Error " . mysqli_errno($link) . ": " . mysqli_error($link) . "</b>");
 } else if (mysqli_num_rows($res) == 0) {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxy","");
} 
  if (1 == 2) echo("<b>Query executed successfully</b>");
  $retval=$delim;
  $retval = str_replace(" }", " parent.document.getElementById('d" . permalinkit($ourtitle)  . "').innerHTML='<select style=background-color:yellow; id=" . permalinkit($ourtitle) . " onchange=window.open(this.value,\"_blank\"); title=\"Is mentioned by\"><option value=\"\">Sadly, this tutorial is not mentioned by any others, yet</option></select>'; }", $retval);
 } else {

  while (($r_array = mysqli_fetch_row($res))) {
  if ($r_array[3] == "-1" && strpos(("" . $r_array[4]), "-") !== false) {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxw","");
}
    if ($secresult == "") {
      $secresult="&nbsp;<span id=xdrpl><select style=\"background-color:orange;display:inline-block;width:80px;\" id=mr" . permalinkit($ourtitle) . " onchange=\"if (this.value.trim().length == 0) { var optslst=document.getElementsByTagName(coption); for (var ioptslst=0; ioptslst<optslst.length; ioptslst++) { if (optslst[ioptslst].outerHTML.indexOf(this.value) != -1) { if (optslst[ioptslst].outerHTML.indexOf(cdisabled) != -1) { optslst[ioptslst].removeAttribute(cdisabled); }  if (optslst[ioptslst].outerHTML.indexOf(chidden) != -1) { optslst[ioptslst].removeAttribute(chidden); }  }  } } else { window.open(this.value,cblank); }\" title=\"Modified recently (over last week)\"><option value=\"\">Modified</option></select></span>&nbsp;";
    }
    if ($r_array[4] <= -11520) {
    $blks=(40 - floor((0 - $r_array[4]) / (24 * 60)));
    $optv='<option value="">Enable and show through to ' . (40 - $blks) . ' days';
    $altv=' title=""';
    for ($iblks=0; $iblks<$blks; $iblks++) {
    $altv=str_replace(' title="', ' title=" ', $altv);
    $optv=str_replace(' value="', ' value=" ', $optv);
    }
    if (strpos($secresult, $optv) !== false) { $secresult=str_replace($optv . " ... ",$optv . " ... " . $r_array[0] . " and ",$secresult);  $optv=""; } else { $optv .= " ... " . $r_array[0] . "</option>"; }
    $secresult=str_replace('>Modified</option>','>Modified</option>' . $optv . '<option' . $altv . ' value=http://www.rjmprogramming.com.au/ITblog/' . permalinkit($r_array[0]) . ' disabled hidden>' . $r_array[0] . '</option>', $secresult); 
    } else {
    $secresult=str_replace('>Modified</option>','>Modified</option><option value=http://www.rjmprogramming.com.au/ITblog/' . permalinkit($r_array[0]) . '>' . $r_array[0] . '</option>', $secresult); 
    }
   } else if (strtolower($r_array[0]) == strtolower($ourtitle)) {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxs","");
}
    $isize++;
    $retval = str_replace(" }", " isize++; theseopts=theseopts.replace(' size=' + eval(-1 + isize) + ' ', ' size=' + isize + ' ').replace('>','><option value=http://www.rjmprogramming.com.au/ITblog/" . permalinkit($r_array[0]) . " selected>" . $r_array[0] . pluckfirstreal($r_array[2]) . "</option>'); }", $retval); 
   } else {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxr","");
}
    $pdate = $r_array[1];
    if ($retval == "") {
      $retval=$delim;
      if (strpos($ourtitle, ' ') === false) {
      $retval = str_replace(" }", " var theseoptions='<select style=background-color:yellow; id=" . permalinkit($ourtitle) . " onchange=window.open(this.value,\"_blank\"); title=\"Same Cut to the Chase action item\"><option id=oimb value=\"\">Same Cut to the Chase action item " . substr($yourtitle,0,(-1 + strlen($yourtitle))) . " ...</option></select>'; }", $retval);
     if (isset($_GET['debug'])) {
 file_put_contents("xx.xxv","");
}
      } else {
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxq","");
}
      $retval = str_replace(" }", " var theseoptions='<select style=background-color:yellow; id=" . permalinkit($ourtitle) . " onchange=window.open(this.value,\"_blank\"); title=\"Is mentioned by\"><option id=oimb value=\"\">This tutorial is mentioned by ...</option></select>'; }", $retval);
      }
      $retval = str_replace(" }", " var theseopts='<select si' + 'ze=0 style=width:100%;background-color:#f0f0f0; class=select_ms id=z" . permalinkit($ourtitle) . " onchange=changed(this.value,ocb); title=\"Is part of a blog posting thread ... and you can select multiple tutorials to show\"></selec' + 't>'; }", $retval);
      //$retval = str_replace(" }", " var theseopts='<select size=0 style=width:100%;background-color:#f0f0f0; class=z" . permalinkit($ourtitle) . " id=z" . permalinkit($ourtitle) . " onchange=changed(this.value,ocb); title=\"Is part of a blog posting thread ... and you can select multiple tutorials to show\"></select>';  }", $retval);
    }
    if ($r_array[3] != "0") $retval = str_replace(" }", " theseoptions=theseoptions.replace('</select>','<option value=http://www.rjmprogramming.com.au/ITblog/" . permalinkit($r_array[0]) . ">" . $r_array[0] . "</option></select>'); }", $retval); 
    if ($criteria != "") {
      if (str_replace($criteria, "", $r_array[0]) != $r_array[0] && $r_array[4] != "0") {
     if (isset($_GET['debug'])) {
 file_put_contents("xx.xxu","");
}        $found = true;
        $isize++;
        $retval = str_replace(" }", " isize++; theseopts=theseopts.replace(' size=' + eval(-1 + isize) + ' ', ' size=' + isize + ' ').replace('>','><option value=http://www.rjmprogramming.com.au/ITblog/" . permalinkit($r_array[0]) . ">" . $r_array[0] . pluckfirstreal($r_array[2]) . "</option>'); }", $retval); 
      }
    }
   }
  }
  if ($retval != "") {
    if (!$found) {
      $retval = str_replace(" }", " theseopts='';  }", $retval);
    } else {
      $retval = str_replace(" }", " theseoptions+=('&nbsp;' + theseopts);   }", $retval);
    }
if (isset($_GET['debug'])) {
 file_put_contents("xx.xxp","");
}
    $retval = str_replace(" }", " parent.document.getElementById('d" . permalinkit($ourtitle)  . "').innerHTML=theseoptions; parent.checkclass(\"\"); }", $retval);
    //$retval = str_replace(" }", " parent.document.getElementById('d" . permalinkit($ourtitle)  . "').innerHTML=theseoptions;  }", $retval);
  }
 }
     if (isset($_GET['debug'])) {
 file_put_contents("xx.xxt","");
}
 mysqli_close($link);
 if ($retval != "") {
    if ($secresult != "") {
      $retval=str_replace("></selec' + 't>';", "></selec' + 't>' + '" . $secresult . "'; ", $retval);
    }
    
    echo $retval;
 } else if ($secresult != "") {
    echo $secresult;
 }
 }
?>
