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

 set_time_limit(0);

 function permalinkit($ins) {
   $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);
   return $outs;
 }

 $delim = "<!doctype html><html><head><title>Is Mentioned By - RJM Programming - August, 2016</title><script type='text/javascript'> function dothis() {  }  </script></head><body onload='dothis();'></body></html>";
 $retval = "";
 $ourtitle = "";
 if (isset($_GET['title'])) {
   $ourtitle = urldecode($_GET['title']);
 } else if (isset($_POST['title'])) {
   $ourtitle = urldecode($_POST['title']);
 } 

 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 = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($dbname);
 $res = mysql_query("SELECT " . $tname . ".post_title, " . $tname . ".guid FROM " . $tname . " 
WHERE " . $tname . ".post_status = 'publish' " . "  
AND LOWER(" . $tname . ".post_title) != LOWER('" . $ourtitle . "') " . " 
AND (" . $tname . ".post_content like CONCAT(CONCAT('%?p='," . $tname . ".guid), '%') " . "
OR " . $tname . ".post_content like '%" . str_replace('youllneverfindthis','%',permalinkit($ourtitle)) . "%' " . "
OR " . $tname . ".post_content like '%" . str_replace('youllneverfindthis','%',$ourtitle) . "%')");

 if ($res == 0) {
  if (1 == 2) echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  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 = mysql_fetch_row($res))) {
    if ($retval == "") {
      $retval=$delim;
      $retval = str_replace(" }", " var theseoptions='<select style=background-color:yellow id=" . permalinkit($ourtitle) . " onchange=window.open(this.value,\"_blank\"); title=\"Is mentioned by\"><option value=\"\">This tutorial is mentioned by ...</option></select>'; }", $retval);
    }
    $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 ($retval != "") $retval = str_replace(" }", " parent.document.getElementById('d" . permalinkit($ourtitle)  . "').innerHTML=theseoptions; }", $retval);
 }
 mysql_close($link);
 if ($retval != "") {
    echo $retval;
 }
 }
?>

