<?php
 // Automate update of rjmprogramming.com.au landing page's id="tuts" select dropdown, with added options of  sort order change and separate window blog display
 // RJM Programming - July, 2014
 $retval = "<option selected='selected' value=' '>Tutorials (show blog, toggle sort order) ...</option>";
 $retvalalt = "<option selected='selected' value=' '>Tutorials (show blog, toggle sort order) ...</option>";
 /*** mysql hostname ***/
 $hostname = 'localhost'; 

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

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

 $dbname = 'wordpress_database_name';
 if (isset($_GET['dbname'])) $dbname = $_GET['dbname'];
 $tname = 'posts_table_name';

 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($dbname);
 // <option id='MyNew-20140327' onclick='windowopen("http://www.rjmprogramming.com.au/wordpress/?p=6976","_blank"); ' value='http://www.rjmprogramming.com.au/wordpress/?p=6976'>Linux Monitor Processes Primer Tutorial (20140327)</option>
 $res = mysql_query("SELECT  ID, post_date, post_title, guid, DATE_FORMAT(post_date,'%Y%m%d') As pdate FROM " . $tname . " WHERE post_status='publish' ORDER BY post_date DESC");
 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 {
  $j = 1;
  $bval = '  ';
  while (($r_array = mysql_fetch_row($res))) {
    $retval .= "\n<option id='MyNew-" . $r_array[4] . "' onclick='windowopen(\"" . $r_array[3] . "\",\"_blank\"); ' value='" . $r_array[3] . "'>" . $r_array[2] . " (" . $r_array[4] . ")</option>";
    if (($j % 19) == 0) {
      $retval .= "\n<option value='" . $bval . "'>Tutorials (toggle sort order) ...</option>";
      $bval .= ' ';
    }
    $j++;
  }
 }
 $res = mysql_query("SELECT  ID, post_date, post_title, guid, DATE_FORMAT(post_date,'%Y%m%d') As pdate FROM " . $tname . " WHERE post_status='publish' ORDER BY post_title");
 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 {
  $j = 1;
  $bval = '  ';
  while (($r_array = mysql_fetch_row($res))) {
    $retvalalt .= "<option id='MyNew-" . $r_array[4] . "' onclick='windowopen(\"" . $r_array[3] . "\",\"_blank\"); ' value='" . $r_array[3] . "'>" . $r_array[2] . " (" . $r_array[4] . ")</option>";
    if (($j % 19) == 0) {
      $retvalalt .= "<option value='" . $bval . "'>Tutorials (toggle sort order) ...</option>";
      $bval .= ' ';
    }
    $j++;
  }
 }
 mysql_close($link);
 $newhtml = $retval;
 if (file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html")) {
   if (!file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html_original")) {
     copy(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html",str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html_original"); 
   }
   $whtml = str_replace("<select", "~",  str_replace("</select", "~", file_get_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html")));
   $earr = explode("~", $whtml);
   if (count($earr) == 5) {
    $newearr4 = explode("~", str_replace("<script", "~",  str_replace("</script>", "~", $earr[4])));
    $selarr = explode(">", $earr[3]);
    $newhtml = $earr[0] . "<select" . $earr[1] . "</select" . $earr[2] . "<select" . $selarr[0] . ">" . $retval . "</select" . $newearr4[0];
 $ethis = "<script>\n function toggle() {  var dq = String.fromCharCode(34); \n";
 $ethis .= ' var xnewstr="", os=document.getElementById("tuts"); ' . "\n";
 $ethis .= ' if (wasstr == "") { var isdq=os.innerHTML.split(dq);  wasstr=isdq[0];  for (var iii=1; iii<isdq.length; iii++) { wasstr = wasstr + dq + isdq[iii]; }   } ' . "\n";
 $exp = explode('"', $retvalalt);
 $ethis .= " var newstr='" . str_replace('onclick="windowopen(', "onclick=' + \"'\" + 'windowopen(", str_replace("'", '"', $exp[0])) . "'; " . "\n";
 for ($ii=1; $ii<sizeof($exp); $ii++) { 
   $ethis .= "\n" . " newstr = newstr + dq + \"" . $exp[$ii] . "\";  " . "\n"; 
 } 
 $ethis .= " if (tval != 0) { tval = 0; os.innerHTML = newstr; } else { tval = 1; os.innerHTML = wasstr; } " . "\n";
 $ethis .= " } \n";
 $ethis .= "</script>\n";
 
 if (str_replace("if (val.value.length != 0) {", "", $newhtml) != $newhtml) {
   $newhtml = str_replace("if (val.value.length != 0) {","if ( val.value.length == 1 && bzgoes == 0 ) { bzgoes = 1; window.open(\"http://www.rjmprogramming.com.au/wordpress\",\"Robert Metcalfe Blog\",\"height=429, left=278, top=360, width=400\"); } else if (val.value.trim() == '') { toggle(); } else if ( val.value.length != 0 ) {", $newhtml);
 } 
 if (str_replace("var wasstr = '';", "", $newhtml) == $newhtml) {
   $newhtml = str_replace("function windowopen(myurl,mytarget) {", " var tval = 1;\n var wasstr = '';\n var bval='  ';\n var bzgoes=0; \n function windowopen(myurl,mytarget) {", $newhtml);
 } 
 if (str_replace("</body>", "", $newhtml) == $newhtml) {
   $newhtml .= $ethis . "</body></html>";
 } else {
   $newhtml = str_replace("</body>", $ethis . "</body>", $newhtml);
 }
    file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html", $newhtml);
    //chown(str_replace("/PHP", "/", dirname(__FILE__)) . "/Welcome.html", "normaluser");
     if (file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html")) {
   if (!file_exists(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html_original")) {
     copy(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html",str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html_original"); 
   }
   $whtml = str_replace("<select", "~",  str_replace("</select", "~", file_get_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html")));
   $earr = explode("~", $whtml);
   if (count($earr) == 5) {
    $newearr4 = explode("~", str_replace("<script", "~",  str_replace("</script>", "~", $earr[4])));
    $selarr = explode(">", $earr[3]);
    $newhtml = $earr[0] . "<select" . $earr[1] . "</select" . $earr[2] . "<select" . $selarr[0] . ">" . $retval . "</select" . $newearr4[0];
 $ethis = "<script>\n function toggle() {  var dq = String.fromCharCode(34); \n";
 $ethis .= ' var xnewstr="", os=document.getElementById("tuts"); ' . "\n";
 $ethis .= ' if (wasstr == "") {  var isdq=os.innerHTML.split(dq);  wasstr=isdq[0];  for (var iii=1; iii<isdq.length; iii++) { wasstr = wasstr + dq + isdq[iii]; }   } ' . "\n";
 $exp = explode('"', $retvalalt);
 $ethis .= " var newstr='" . str_replace('onclick="windowopen(', "onclick=' + \"'\" + 'windowopen(", str_replace("'", '"', $exp[0])) . "'; " . "\n";
 for ($ii=1; $ii<sizeof($exp); $ii++) { 
   $ethis .= "\n" . " newstr = newstr + dq + \"" . $exp[$ii] . "\";  " . "\n"; 
 } 
 $ethis .= " if (tval != 0) { tval = 0; os.innerHTML = newstr; } else { tval = 1; os.innerHTML = wasstr; } " . "\n";
 $ethis .= " } \n";
 $ethis .= "</script>\n";
 
 if (str_replace("if (val.value.length != 0) {", "", $newhtml) != $newhtml) {
   $newhtml = str_replace("if (val.value.length != 0) {","if ( val.value.length == 1 && bzgoes == 0 ) { bzgoes = 1; window.open(\"http://www.rjmprogramming.com.au/wordpress\",\"Robert Metcalfe Blog\",\"height=429, left=278, top=360, width=400\"); } else if (val.value.trim() == '') { toggle(); } else if ( val.value.length != 0 ) {", $newhtml);
 } 
 if (str_replace("var wasstr = '';", "", $newhtml) == $newhtml) {
   $newhtml = str_replace("function windowopen(myurl,mytarget) {", " var tval = 1;\n var wasstr = '';\n var bval='  '; \n\n var bzgoes=0; \n function windowopen(myurl,mytarget) {", $newhtml);
 } 
 if (str_replace("</body>", "", $newhtml) == $newhtml) {
   $newhtml .= $ethis . "</body></html>";
 } else {
   $newhtml = str_replace("</body>", $ethis . "</body>", $newhtml);
 }
    file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html", $newhtml);
    //chown(str_replace("/PHP", "/", dirname(__FILE__)) . "/index.html", "normaluser");
   } else {
    $newhtml = $retval;
   }
   } 
   } else {
    $newhtml = $retval;
   }
 } 
 $ethis = "<script>\n function toggle() { var dq = String.fromCharCode(34);  \n";
 $ethis .= ' var xnewstr="", os=document.getElementById("tuts"); ' . "\n";
 $ethis .= ' if (wasstr == "") {  var isdq=os.innerHTML.split(dq);  wasstr=isdq[0];  for (var iii=1; iii<isdq.length; iii++) { wasstr = wasstr + dq + isdq[iii]; }   } ' . "\n";
 $exp = explode('"', $retvalalt);
 $ethis .= " var newstr='" . str_replace('onclick="windowopen(', "onclick=' + \"'\" + 'windowopen(", str_replace("'", '"', $exp[0])) . "'; " . "\n";
 for ($ii=1; $ii<sizeof($exp); $ii++) { 
   $ethis .= "\n" . " newstr = newstr + dq + \"" . $exp[$ii] . "\"; " . "\n"; 
 } 
 $ethis .= " if (tval != 0) { tval = 0; os.innerHTML = newstr; } else { tval = 1; os.innerHTML = wasstr; } " . "\n";
 $ethis .= " }  \n";
 $ethis .= "</script>\n";
 
 if (str_replace("if (val.value.length != 0) {", "", $newhtml) != $newhtml) {
   $newhtml = str_replace("if (val.value.length != 0) {","if ( val.value.length == 1 && bzgoes == 0 ) { bzgoes = 1; window.open(\"http://www.rjmprogramming.com.au/wordpress\",\"Robert Metcalfe Blog\",\"height=429, left=278, top=360, width=400\"); } else if (val.value.trim() == '') { toggle(); } else if ( val.value.length != 0 ) {", $newhtml);
 } 
 if (str_replace("var wasstr = '';", "", $newhtml) == $newhtml) {
   $newhtml = str_replace("function windowopen(myurl,mytarget) {", " var tval = 1;\n var wasstr = '';\n var bval='  '; \n\n var bzgoes=0; \n function windowopen(myurl,mytarget) {", $newhtml);
 } 
 if (str_replace("</body>", "", $newhtml) == $newhtml) {
   $newhtml .= $ethis . "</body></html>";
 } else {
   $newhtml = str_replace("</body>", $ethis . "</body>", $newhtml);
 }
 file_put_contents(str_replace("/PHP", "/", dirname(__FILE__)) . "/tutorial_options.html", $newhtml);
 echo $retval;
?>

