<?php
 // Misspelling Corrections on WordPress Blog Postings - RJM Programming - April, 2018

set_time_limit(0);
$tablestr ="";
$cdel=",";
$from ="";
$to="";
if (isset($_GET['from'])) {
 $from=urldecode($_GET['from']);
}
if (isset($_GET['to'])) {
 $to=urldecode($_GET['to']);
}

if ($from != "" && strpos(str_replace(" ", "", strtolower(urldecode($_SERVER['QUERY_STRING']))), "astringthatislikeanapplicationpassword") !== false) {

 $reptxt = "";
 if (file_exists("old_new_wp_misspellings.txt")) {
   $reptxt = @file_get_contents("old_new_wp_misspellings.txt");
 }
 date_default_timezone_set('Australia/Perth');
 $sdate = date('l jS \of F Y h:i:s A');
 
 $hostname = 'database_hostname';

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

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

 $dbname = 'database_name';
 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }

 mysql_select_db($dbname);
 $res = mysql_query("SELECT t1.ID, t1.post_title, t1.post_content  
FROM " . $dbname . ".wps_posts t1
WHERE t1.post_content LIKE '%" . $from . "%' and t1.post_status in ('publish','future')");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
 while (($r_array = mysql_fetch_row($res))) {
    mysql_select_db($dbname);
  if (!isset($_GET['commit'])) {
    $reptxt .= "UPDATE " . $dbname . ".wps_posts SET post_content=replace(post_content,'" . $from . "','" . $to  . "') WHERE ID=" . $r_array[0] . " and post_title='" . $r_array[1] . "' and '" . $sdate . "'='" . $sdate . "'\n";
    if ($tablestr == "") { 
      $tablestr="<h1>Commit to the WordPress Blog Checkbox Changes found going from '" . $from . "' to '" . $to . "'  <input onclick=\"document.getElementById('isubmit').click();\" type=submit value='Commit to the SQL Relevant to Checkboxes Below' style=background-color:yellow;></input></h1><br><table border=20><tbody id=mytbody><tr><th style=width:10%;>Title</th><th style=width:10%;>Okay for this Change?</th><th style=width:300px;>Look 4.1.1</th></tr></tbody></table><br><form method=GET action=./old_new_wp_misspellings.php><input type=hidden name=astringthatislikeanapplicationpassword value=y></input><input type=hidden name=from value='" . $from . "'></input><input type=hidden name=to value='" . $to . "'></input><input type=hidden name=commit id=commit value=',"; // . $r_array[0] . ","; // . "'></input><input type=submit value=Commit></input></form>";     
    } 
    $tablestr.=$r_array[0] . $cdel;
    $tablestr=str_replace("</tr></tbody></table><br><form method=", "</tr><tr><td>" . $r_array[1] . "</td><td><input onchange=\"document.getElementById('commit').value=document.getElementById('commit').value.replace('," . $r_array[0] . ",',',');\" type=checkbox checked></input></td><td style=width:300px;><!--div style='overflow:hidden; -ms-transform: scale(0.2, 0.2); -webkit-transform: scale(0.2, 0.2); transform: scale(0.2, 0.2);'--><details><summary>Reveal WordPress 4.1.1 Look '" . $from . "' to '" . $to  . "' proposed changes ...</summary>" . str_replace($from,$to,$r_array[2]) . "</details><!--/div--></td></tr></tbody></table><br><form method=", $tablestr);
  } else if (strpos(("," . urldecode($_GET['commit']) . ","),("," . $r_array[0] . ",")) !== false) {
    mysql_query("UPDATE " . $dbname . ".wps_posts SET post_content=replace(post_content,'" . $from . "','" . $to  . "') WHERE ID=" . $r_array[0] . " and post_title='" . $r_array[1] . "'");
  }
    mysql_select_db($dbname);
 }
 if ($reptxt != "" && !isset($_GET['commit'])) {
    file_put_contents("old_new_wp_misspellings.txt", $reptxt);
    $tablestr.="'></input><input id=isubmit type=submit value='Commit to the SQL Relevant to Checkboxes Below' style=background-color:yellow;></input></form><br><textarea cols=80 rows=100>" . $reptxt . "</textarea>";
    echo "<!doctype html><html><head><title>WordPress Blog Generic Misspellings Fix - RJM Programming - April, 2018</title><script type='text/javascript' src='//www.rjmprogramming.com.au/wordpress/wp-admin/js/noughtsandcrosses.js'></script>
<script type='text/javascript' src='//www.rjmprogramming.com.au/wordpress/wp-admin/js/englishverb.js'></script>
<script type='text/javascript' src='//www.rjmprogramming.com.au/ITblog/wp-admin/js/wajax.js'></script>
<script type='text/javascript' src='//www.rjmprogramming.com.au/HTMLCSS/longhover_sup.js?period=5000&from=+&to=-'></script>
<body>" . $tablestr . "</body></html>";
 } else if (isset($_GET['commit'])) {
    if (file_exists("old_new_wp_misspellings.txt")) unlink("old_new_wp_misspellings.txt");
    echo "<!doctype html><html><body><p>Commit to MySql database done.</p></body></html>";
 }
}
 mysql_close($link);
} else if (strpos($_SERVER['QUERY_STRING'], "answer=") !== false) {
echo "<!doctype html><html><body><p>Oops</p></body></html>";
} else if ($tablestr != "") {
echo "<!doctype html><html><body>" . $tablestr . "</body></html>";
} else {
if (file_exists("old_new_wp_misspellings.txt")) unlink("old_new_wp_misspellings.txt");
echo "<!doctype html><html><body onload=\" var huh=prompt('Why is a duck?', '');  var from=prompt('Substitute WordPress Blog Posting Misspelling From?  ... please, no single quotes', '');  var to=prompt('Substitute WordPress Blog Posting Misspelling To?  ... please, no single quotes', ''); if (huh != null) { location.href='./old_new_wp_misspellings.php?answer=' + huh + '&from=' + encodeURIComponent(from) + '&to=' + encodeURIComponent(to); } \">" . $tablestr . "</body></html>";
}
?>
