<?php
// Wordpress Blog Design a Course - RJM Programming - October, 2014
$thetopic = "";
$bodyi = "";
$thetime = "";

$ctn = "
 function ChangeTutNow(x) {
  var chuh, ahuh; 
  if (multi == 1) {
  if (document.getElementById('coursesq').value == '' || isonclick == 0) {
  for (var i = 0; i < x.options.length; i++) {
     if(x.options[i].selected == true){
          chuh = x.options[i].value;
          if (chuh != '') {
          ahuh = chuh.split('=');
  if (ahuh[ahuh.length - 1] != '') {  
   var bhuh=ahuh[1].split('&');  
   document.getElementById('coursesq').value = document.getElementById('coursesq').value + comma + bhuh[0];   
   document.getElementById('courselsq').value = document.getElementById('courselsq').value + comma + ahuh[ahuh.length - 1].replace(/_/g, ' ');  
   document.getElementById('sofar1').innerHTML = document.getElementById('sofar1').innerHTML + '<' + 'br' + '>' + ahuh[ahuh.length - 1].replace(/_/g, ' ');    
   document.getElementById('topic').innerHTML = document.getElementById('topic').innerHTML.replace(ahuh[ahuh.length - 1],'').replace(ahuh[ahuh.length - 1],''); 
   comma=','; 
 } 
      }
      }
      }
  }
   }
   return true;
 }
";

function via($infind) {
  global $thetopic;
  $huh = file_get_contents(dirname(__FILE__) . "/../index.html");
  $sels = explode("<select ", $huh);
  if (sizeof($sels) >= 3) {
    if ($infind == "") {
      $selin = explode("</select>", $sels[2]);
      $xsel = explode(">", $selin[0]);
      $ih = str_replace(($xsel[0] . ">"), "", $selin[0]);
      return "<select " . $xsel[0] . ">" . str_replace("show blog, toggle sort order", "will use as basis for your Course Name", $ih) . "</select>";
    } else {
      $selin = explode("</select>", $sels[2]);
      $xsel = explode(">", $selin[0]);
      $ih = str_replace(($xsel[0] . ">"), "", $selin[0]);
      $newih = "<option value=''>Please select tutorials, in order, that belong to your " . $thetopic . " Course Design ...</OPTION>";
      $lotsnewih = explode("</option>", $ih);
      for ($i=0; $i<sizeof($lotsnewih); $i++) {
        if (strpos($lotsnewih[$i], ">" . $infind . " ") !== false) $newih = str_replace("</OPTION>", "</OPTION>" . str_replace("MyNew-", $infind . "MyNew-", $lotsnewih[$i]) . "</option>", $newih);
      }
      return "<select size=20 multiple " . str_replace("tuts'", $infind . "tuts'", $xsel[0]) . ">" . str_replace("</OPTION>", "</option>", $newih) . "</select>";
    }
  }
  return "";
}

if (isset($_GET['show'])) {
   if (file_exists("../PHP/allip_courses.php")) {
     $bodyi .= file_get_contents(dirname(__FILE__) . "/../PHP/allip_courses.php");
   }
   if (file_exists("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")) {
     $bodyi .= "<iframe style='border: 2px solid green;' id='bodyi' src='../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html' width='100%' height='100'></iframe>";
   } 
   if (isset($_GET['iplink'])) {
     if (file_exists("../PHP/ip_" . $_GET['iplink'] . ".php")) {
       $pra = str_replace("\n", "", file_get_contents("../PHP/ip_" . $_GET['iplink'] . ".php")); 
       if (file_exists("../PHP/" . $pra . "_courses.html")) {
        if (!file_exists("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")) {
         file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", "<html><head><style> .coursea { background-image: url('course.png'); background-color: #cccccc; opacity: 1.0;   } .acourse { opacity: 1.0;  font-style:bold; font-size:24px; text-decoration:none; } </style><script type='text/javascript'> var comma=''; </script><title>Ask About Course</title></head><body><table cellpadding=20 cellspacing=20><tbody><tr></tr></tbody></table><a id='emailfriends' style='display:none;' href='mailto:place@friends.here?subject=" . $subject . "&body=" . $body . "'>Email Friends</a></body></html>");
        }
        $obits = file_get_contents(dirname(__FILE__) . "/../PHP/" . $pra . "_courses.html");
        $obitstr = explode("<tr>", $obits);
        $obitsetr = explode("</tr>", $obitstr[1]);
        file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", str_replace("</tr>", $obitsetr[0] . "</tr>", file_get_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")));
        //$bodyi .= "<iframe style='border: 2px solid blue;' id='bodyii' src='../PHP/" . $pra . "_courses.html' width='100%' height='100'></iframe>";
       }      
     }
   } 
}

if (isset($_GET['iptime'])) {
  $hans = "";
  if (isset($_GET['email'])) {
    //echo " <" . "script type='text/javascript'> document.getElementById('bodyi').contentDocument.getElementById('emailfriends').click(); </" . "script>  ";
    $bodyi = str_replace("_courses.html", "_courses.html?email=y&ans=" . $_GET['ans'], $bodyi);
  }
  $ipstuff = explode("_", $_GET['iptime']);
  $ipis = $ipstuff[0];
  $timeis = $ipstuff[1];
  $cname = str_replace("~", " ", str_replace("  ", "++", str_replace("%20%20", "++", $ipstuff[2])));
  $lwis = " linking word for Course is " . $timeis;
  if (isset($_GET['ans'])) {
    if (substr($_GET['ans'],0,1) == "x" || substr($_GET['ans'],0,1) == "X") {
      $hans = "&ask=y";
      $lwis = "";
    } 
  }
  file_put_contents("../PHP/ip_" . $timeis . ".php", $ipis);
  $ddstuff = "<option value='http://www.rjmprogramming.com.au/wordpress/?page_id=10014" . $hans . "&show=y&iplink=" . $timeis . "#content'>" . $cname . $lwis . "</option>";
  file_put_contents("../PHP/allip_courses.php", str_replace("none;", "block;", str_replace("</select>", $ddstuff . "</select>", file_get_contents("../PHP/allip_courses.php"))));
}

if (isset($_POST['coursename'])) {
  $tuts = explode(",", $_POST['coursesq']);
  $ltuts = explode(",", $_POST['courselsq']);
  $prev = "";
  $next = "";
  $conload = "";
  $subject = str_replace(" ", "%20", $_POST['coursename'] . " at Robert Metcalfe Blog ( http://www.rjmprogramming.com.au/wordpress ) ... date/time that links from dropdown is ... *@*");
  $body = "http://www.rjmprogramming.com.au/wordpress/?page_id=10014&show=y&iplink=*@*";
  $first = "http://www.rjmprogramming.com.au/wordpress/?p=" . $tuts[0];
  for ($i=0; $i<sizeof($tuts); $i++) {
     if (($i + 1) < sizeof($tuts)) $next = $tuts[$i + 1];
     if ($prev == "" && $next == "") {
       file_put_contents("../PHP/" . $tuts[$i] . "_" . $_SERVER['REMOTE_ADDR'] . ".npg", "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i]);
       $first .= "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i];
     } else if ($prev == "") {
       file_put_contents("../PHP/" . $tuts[$i] . "_" . $_SERVER['REMOTE_ADDR'] . ".npg", "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i]);
       $first .= "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i];
     } else if ($next == "") {
       file_put_contents("../PHP/" . $tuts[$i] . "_" . $_SERVER['REMOTE_ADDR'] . ".npg", "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i]);
     } else {
       file_put_contents("../PHP/" . $tuts[$i] . "_" . $_SERVER['REMOTE_ADDR'] . ".npg", "&pp=" . $prev . "&pn=" . $next . "&title=" . str_replace("#", "%23", str_replace(" ", "~", urlencode($_POST['coursename']))) . " " . $ltuts[$i]);
     }
     $prev = $tuts[$i];
     $next = "";
  }
  if (!file_exists("../PHP/allip_courses.php")) {
    file_put_contents("../PHP/allip_courses.php", "<select style='display:none;' onchange=' var okays=this.value.split(\"=\"); var aokay=okays[okays.length - 1]; if (this.value.indexOf(\"ask=\") > -1) { aokay=prompt(\"Sharer has asked that you get date/time code right.   What is it?\", \"\");  } if (aokay == okays[okays.length - 1]) { location.href = this.value; } else { alert(\"Sorry, that date/time guess was wrong.\");  location.href = \"http://www.rjmprogramming.com.au/wordpress\"; } '><option value=''>Please, optionally, select a Shared Course Design as below ...</option></select>&nbsp;");
  }
  if (file_exists("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")) {
    file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", str_replace("</tr>", "<td class='coursea'><a class='acourse' target=_blank title='" . $_POST['coursename'] .  "' href='" . $first . "'>" . $_POST['coursename'] . "</a></td></tr>", file_get_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")));
    $thetime = str_replace("-", "", str_replace(":", "", date("Y-m-d-H:i:s", filemtime("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html"))));
    $thinge = explode("iplink=", str_replace("'", "", str_replace('"', '', file_get_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html"))));
    $thingo = explode(">", $thinge[1]);
    file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", str_replace($thingo[0], $thetime, file_get_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")));
    $conload = " onload=' var andemail=\"\"; var preans=\"" . $thetime . "\"; var ans=prompt(\"If you are willing to share your Course (and to email Share information to friends add any character to start ... x means they need to enter date/time correctly ... and) click OK now else clear time and click OK\", \"" . $thetime . "\"); if (ans != \"\" && ans != null) { if (preans != ans) { andemail=\"&email=y&ans=\" + ans; } location.href = document.URL.replace(\"?show=y\", \"\") + \"?iptime=" . $_SERVER['REMOTE_ADDR'] . "_" . $thetime . "_" . str_replace("%20", "~", str_replace("_", "~", str_replace(" ", "~", $_POST['coursename']))) . "&show=y\" + andemail;  }  ' ";
  } else {
    file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", "<html><head><style> .coursea { background-image: url('course.png'); background-color: #cccccc; opacity: 1.0;   } .acourse { opacity: 1.0;  font-style:bold; font-size:24px; text-decoration:none; } </style><script type='text/javascript'> function conload() { var comma=''; var email=location.search.split('email=')[1] ? location.search.split('email=')[1].split('&')[0] : ''; var ans=location.search.split('ans=')[1] ? location.search.split('ans=')[1].split('&')[0] : ''; if (email != '') { document.getElementById('emailfriends').click(); } } </script><title>Ask About Course</title></head><body onload='conload();'><table cellpadding=4 cellspacing=4><tbody><tr><td class='coursea'><a class='acourse' target=_blank title='" . $_POST['coursename'] .  "' href='" . $first . "'>" . $_POST['coursename'] . "</a></td></tr></tbody></table><a id='emailfriends' style='display:none;' href='mailto:place@friends.here?subject=" . $subject . "&amp;body=" . $body . "'>Email Friends</a></body></html>");
    $thetime = str_replace("-", "", str_replace(":", "", date("Y-m-d-H:i:s", filemtime("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html"))));
    file_put_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html", str_replace("*@*", $thetime, file_get_contents("../PHP/" . $_SERVER['REMOTE_ADDR'] . "_courses.html")));
    $conload = " onload=' var andemail=\"\"; var preans=\"" . $thetime . "\"; var ans=prompt(\"If you are willing to share your Course (and to email Share information to friends add any character to start ... x means they need to enter date/time correctly ... and) click OK now else clear time and click OK\", \"" . $thetime . "\"); if (ans != \"\" && ans != null) { if (preans != ans) { andemail=\"&email=y&ans=\" + ans; } location.href = document.URL.replace(\"?show=y\", \"\") + \"?iptime=" . $_SERVER['REMOTE_ADDR'] . "_" . $thetime . "_" . str_replace("%20", "~", str_replace("_", "~", str_replace(" ", "~", $_POST['coursename']))) . "&show=y\" + andemail;  }  ' ";
  }
  echo "<html><head><script type='text/javascript'> var comma=''; " . $ctn . " </script><title>Ask About Course</title></head><body" . $conload . " style='background-color:yellow;'>" . $bodyi . "<h1 align='center'>Course Designer</h1><h2>Thanks for your <a target=_blank title='" . $_POST['coursename'] .  "' href='" . $first . "'>" . $_POST['coursename'] . "</a> Course Design</h2><form align='center' action='./design_a_course.php' method='POST'><input type='hidden' name='coursesq' id='coursesq' value=''></input><input type='text' style='display:none;' name='coursename' id='coursename' value=''></input><div id='list' style='display:block;'>" . str_replace("ChangeTutNow(this);", " var ahuh=this.value.split(\"=\"); location.href = \"./design_a_course.php?topic=\" + ahuh[ahuh.length - 1].replace(\"#\", \"%23\"); ", via("")) . "</div><div id='topic' style='display:none;'>" . via("C++") . "</div><br><br><input style='display:none;' align='center' type='submit' name='submit' id='submit' value='Submit'></input></form>" . "</body></html>";
} else if (isset($_GET['topic'])) {
  $pretopics = explode("title=", $_GET['topic']);
  $topics = explode(" ", str_replace("_", " ", str_replace("  ", "++",  $pretopics[sizeof($pretopics) - 1])));
  $thetopic = $topics[0];
  echo "<html><head><script type='text/javascript'> var isonclick=1; var multi=1; var comma=''; " . $ctn . " </script><title>Ask About Course</title></head><body style='background-color:lightgray;'>" . $bodyi . "<h1 align='center'>" . $thetopic . " Course Designer</h1><h3 id='sofar'></h3><br><form align='center' action='./design_a_course.php?show=y' method='POST'><input type='hidden' name='courselsq' id='courselsq' value=''></input><input type='hidden' name='coursesq' id='coursesq' value=''></input>Course Name: <input type='text' style='display:block;' name='coursename' id='coursename' value='" . $topics[0] . "'></input><br><div id='list' style='display:none;'>" . via("") . "</div><select id='bc' onchange=' if (multi == 1) { multi = 0; document.getElementById(\"topic\").innerHTML = document.getElementById(\"topic\").innerHTML.replace(/size=20 multiple/g, \"\");  } else { multi=1; document.getElementById(\"topic\").innerHTML = document.getElementById(\"topic\").innerHTML.replace(/select /g, \"select size=20 multiple \");  } '><option value='Multiple'>Multiple (entry mode below)</option><option value='Single'>Single (has advantage that date order can be changed)</option></select><div id='topic' style='display:block;'>" . str_replace("ChangeTutNow(this);", " if (multi == 0) { var ahuh=this.value.split(\"=\"); if (ahuh[ahuh.length - 1] != \"\") {  var bhuh=ahuh[1].split(\"&\");  document.getElementById(\"coursesq\").value = document.getElementById(\"coursesq\").value + comma + bhuh[0];   document.getElementById(\"courselsq\").value = document.getElementById(\"courselsq\").value + comma + ahuh[ahuh.length - 1].replace(/_/g, \" \");  document.getElementById(\"sofar\").innerHTML = document.getElementById(\"sofar\").innerHTML + \"<\" + \"br\" + \">\" + ahuh[ahuh.length - 1].replace(/_/g, \" \");    document.getElementById(\"topic\").innerHTML = document.getElementById(\"topic\").innerHTML.replace(ahuh[ahuh.length - 1],\"\").replace(ahuh[ahuh.length - 1],\"\"); comma=\",\"; } } ", via($topics[0])) . "</div><br><br><input onclick='return(ChangeTutNow(document.getElementById(\"" . $thetopic . "tuts\")));' onmouseover=' isonclick=0; ChangeTutNow(document.getElementById(\"" . $thetopic . "tuts\")); isonclick=1; ' align='center' type='submit' name='submit' id='submit' value='Create Your " . $thetopic . " Course Design'></input></form>" . "<h3 id='sofar1'></h3></body></html>";
} else {
  echo "<html><head><script type='text/javascript'> var comma=''; " . $ctn . " </script><title>Ask About Course</title></head><body style='background-color:yellow;'>" . $bodyi . "<h1 align='center'>Course Designer</h1><form align='center' action='./design_a_course.php?show=y' method='POST'><input type='hidden' name='coursesq' id='coursesq' value=''></input><input type='text' style='display:none;' name='coursename' id='coursename' value=''></input><div id='list' style='display:block;'>" . str_replace("ChangeTutNow(this);", " var ahuh=this.value.split(\"=\"); location.href = \"./design_a_course.php?topic=\" + ahuh[ahuh.length - 1].replace(\"#\", \"%23\"); ", via("")) . "</div><div id='topic' style='display:none;'>" . via("C++") . "</div><br><br><input style='display:none;' align='center' type='submit' name='submit' id='submit' value='Submit'></input></form>" . "</body></html>";
}
?>


