<?php
// moderation_captcha.php
// Moderation ideas
// RJM Programming
// November, 2015
include "db_connect.php";

$msg="";
$amsg="";

if ($_POST) {
  session_start();
  if ($_POST['captcha'] != $_SESSION['digit']) {
   $msg="Sorry, the CAPTCHA code entered was incorrect!";
  }
  session_destroy();
}

$link=null;
$hrbit="";
$dprefix="";
$dmidbit="";
$dsuffix="";
$dbname="users";
$utable="musers";
$ucolumn="username";
$suffix="";
if ( isset( $_GET['utable'] ) ) {
  $utable=$_GET['utable'];
}
if ( isset( $_POST['utable'] ) ) {
  $utable=$_POST['utable'];
}
if ( isset( $_GET['dbname'] ) ) {
  $dbname=$_GET['dbname'];
}
if ( isset( $_POST['dbname'] ) ) {
  $dbname=$_POST['dbname'];
}
if ( isset( $_GET['ucolumn'] ) ) {
  $ucolumn=$_GET['ucolumn'];
}
if ( isset( $_POST['ucolumn'] ) ) {
  $ucolumn=$_POST['ucolumn'];
}

if ( isset($_GET['admin'])) {
  $hrbit=""; //"<hr style='margin-top:-180px;'>";
}

$captcha_bit="
<p><img src='captcha.php' width='120' height='30' border='1' alt='CAPTCHA'></p>
<p><input type='text' size='6' maxlength='5' name='captcha' value=''><br>
<small>copy the digits from the image into this box</small></p>
";

function insertposting($cont) {
  global $dbname;
  $pusername="";
  if ( isset( $_POST['username'] ) ) {
    $pusername=urldecode($_POST['username']);
  }
  $temail="";
  if ( isset( $_POST['email'] ) ) {
    $temail=urldecode($_POST['email']);
  }
  $pstatus="moderating";
  try {
    if (!$link) $link = db_connect(); 
    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($dbname);
    $result = mysql_query("insert into posting values (NULL,'".$cont."', '".$pusername."', '".$temail."', '".$pstatus."')");
  } catch (Exception $e) {
    die('Could not connect: ' . mysql_error());
  }
}

if ($msg == "") {
if (isset($_POST['myidea'])) {
 $suffix=urldecode($_POST['myidea']);
}
if (isset($_POST['mymidea'])) {
 $msg='?';
 $thisidea=urldecode($_POST['mymidea']);
     $msg='@' . $thisidea;
 if ($thisidea != '') {
     $msg='!';
     date_default_timezone_set('Australia/Perth');
     $amsg="<a style='display:none;' id='aemail' href='mailto:rmetcalfe@rjmprogramming.com.au?subject=" . str_replace(' ','%20',("Things That We Have in Common ... " . $thisidea . " ... " . date('l jS \of F Y h:i:s A'))) . "&body=" . str_replace(' ','%20',("Things That We Have in Common " . date('l jS \of F Y h:i:s A') . " ... " . $thisidea)) . "'>email</a>";
     $msg="Things That We Have in Common  " . date('l jS \of F Y h:i:s A') . " ... " . $thisidea . " ... has been sent for moderation (within 24 hours).";
     $from = "From: rmetcalfe@rjmprogramming.com.au \r\n";
     try {
      mail("rmetcalfe@rjmprogramming.com.au", "Things That We Have in Common ... " . $thisidea . " ... " . date('l jS \of F Y h:i:s A'), $thisidea, $from);
      //$amsg="";
     } catch (Exception $e) { }
 }
}
if (isset($_POST['mymfileidea'])) {
 $msg='?';
 $thisidea=urldecode($_POST['mymfileidea']);
 file_put_contents(urlencode($thisidea) . ".aml", "");
     $msg='@' . $thisidea;
 if ($thisidea != '') {
     $msg='!';
     date_default_timezone_set('Australia/Perth');
     $dprefix="<div align='center'><table><tr><td>";
     $dmidbit="</td><td valign='top'>";
     $dsuffix="</td></tr></table></div>";
     $amsg="<hr>" . $dprefix . "<div align='center' style='width:100%;border:0;z-index:5;'><iframe frameborder=0 src='./login.php' title='Admin login' style='height:410px;width:100%;'></iframe></div>";
     $msg="Things That We Have in Common  " . date('l jS \of F Y h:i:s A') . " ... " . $thisidea . " ... file has been flagged for moderation (within 24 hours).";
 }
}
if (isset($_POST['mymdbidea'])) {
 $msg='?';
 $thisidea=urldecode($_POST['mymdbidea']);
     $msg='@' . $thisidea;
 if ($thisidea != '') {
     insertposting(urlencode($_POST['mymdbidea']));
     $msg='!';
     date_default_timezone_set('Australia/Perth');
     $dprefix="<div align='center'><table><tr><td>";
     $dmidbit="</td><td valign='top'>";
     $dsuffix="</td></tr></table></div>";
     $amsg="<hr>" . $dprefix . "<div align='center' style='width:100%;border:0;z-index:5;'><iframe frameborder=0 src='./login.php' title='Admin login' style='height:410px;width:100%;'></iframe></div>";
     $msg="Things That We Have in Common  " . date('l jS \of F Y h:i:s A') . " ... " . $thisidea . " ... has been flagged in the database for moderation (within 24 hours).";
 }
}
}
if (isset($_GET['admin'])) {
     $dprefix="<div align='center'><table><tr><td>";
     $dmidbit="</td><td valign='top'>";
     $dsuffix="</td></tr></table></div>";
     $amsg="<hr>" . $dprefix . "<div align='center' style='border:0;z-index:5;'><iframe frameborder=0 src='./login.php' title='Admin login' style='height:410px;'></iframe></div>";
}
echo "<!doctype html>
<html>
<head>
<title>Things That We Have in Common ... " . $msg . "</title>";
?>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style> td { padding: 8 8 8 8; } </style>
<script type='text/javascript'>
 var cnt=0, thiscnt=0;
 var sbut=null;
 var mform=null;
 var sbutfile=null;
 var mformfile=null;
 var sbutdb=null;
 var mformdb=null;
 function submitform() {
   var mmod=document.getElementById('mymod' + thiscnt);
   if (mmod != null) {
    document.getElementById('mymidea').value=decodeURIComponent(mmod.title);
   } 
   //mform=document.getElementById('fmoderation');
   sbut=document.getElementById('submit');
   //mform.submit();
   sbut.click();
   setTimeout(andthen, 1000);
 }
 function submitfileform() {
   var mmodfile=document.getElementById('myfilemod' + thiscnt);
   if (mmodfile != null) {
    document.getElementById('mymfileidea').value=decodeURIComponent(mmodfile.title);
   } 
   //mformfile=document.getElementById('filemoderation');
   sbutfile=document.getElementById('filesubmit');
   //mformfile.submit();
   sbutfile.click();
   setTimeout(andthenfile, 1000);
 }
 function submitdbform() {
   var mmoddb=document.getElementById('mydbmod' + thiscnt);
   if (mmoddb != null) {
    document.getElementById('mymdbidea').value=decodeURIComponent(mmoddb.title);
   } 
   //mformdb=document.getElementById('dbmoderation');
   sbutdb=document.getElementById('dbsubmit');
   //mformdb.submit();
   sbutdb.click();
   setTimeout(andthendb, 1000);
 }
 function andthen() {
   document.getElementById('mymidea').value='';
 }
 function andthenfile() {
   document.getElementById('mymideafile').value='';
 }
 function andthendb() {
   document.getElementById('mymideadb').value='';
 }
 function emailclient() {
   var aemailo=document.getElementById('aemail');
   if (aemailo != null) aemailo.click();
 }
 function fcnt(ot) {
   thiscnt=eval(ot.id.replace('mymod',''));
 }
 function filecnt(ot) {
   thiscnt=eval(ot.id.replace('myfilemod',''));
 }
 function dbcnt(ot) {
   thiscnt=eval(ot.id.replace('mydbmod',''));
 }

 function checkForm(form) {
    if (!form.captcha.value.match(/^\d{5}$/)) {
      alert('Please enter the CAPTCHA digits in the box provided');
      form.captcha.focus();
      return false;
    }
    return true;
 }
</script>
</head>
<body onload=" sbut=document.getElementById('submit'); mform=document.getElementById('fmoderation'); if (1 == 2) { document.getElementById('mymidea').focus(); } emailclient(); " style=" background-color: lightblue; ">
<h1 id='myh1' align='center'>Things That We Have in Common</h1>
<h3 align='center'>It seems to me that there is so much more that should unite people, rather than divide.</h3>
<h3 align='center'>What do you think?</h3>
<?php global $msg;  echo "<h4 align='center'>" . $msg . "</h4>"; ?>
<?php global $amsg; echo $amsg; ?>
<?php global $hrbit; echo $hrbit; ?>

<?php
   $cont="";
   if (file_exists("what_we_have_in_common.html")) {
    $cont=@file_get_contents("what_we_have_in_common.html");
   }
  
   if (strpos($cont, '<') !== false) {
    $dprefix="";
    $dmidbit="";
    $dsufix="";
    echo $cont . $suffix;
   } else {
    echo $dmidbit . "<table style=' border: 3px solid orange; background-color: yellow;  padding: 8 8 8 8; ' align='center'><tbody id='mytbody'><tr><th>Ideas (for what we have in common)</th><th id='hother'></th></tr>" . "\n";
    $lines=explode("\n", $cont);
    for ($cnt=1; $cnt<=sizeof($lines); $cnt++) {
      echo "<tr><td id='ltd" . $cnt . "'>" . $lines[-1 + $cnt] . "</td><td id='rtd" . $cnt . "'></td></tr>";
    }
    if ($suffix != "") {
      $cnt++;
      echo "<tr><td id='ltd" . $cnt . "'>" . $suffix . "</td><td id='rtd" . $cnt . "'></td></tr>";
    }
    echo "</tbody></table>" . $dsuffix . "\n";
   }
?>

<!--hr-->
<div style=' background-color: lightgreen; padding: 8 8 8 8; ' align='center' id='dmoderation'>
<h1 align='center'>What do you think?</h1>
<form  onsubmit="return checkForm(this);" action='./moderation_captcha.php' method='POST' align='center' name='nomoderation' id='nomoderation'>
<?php global $captcha_bit; if (1 == 1) { echo $captcha_bit; } ?>
Private Thought (ie. no moderation, but uses CAPTCHA, and with success, will appear for you, in list above):<br> <input style='width:100%;' type='text' name='myidea' id='myidea' value='' onblur=" if (this.value != '') { if (1 == 2) { document.getElementById('mytbody').innerHTML+='<tr><td>' + this.value + '</td><td></td>';  this.value=''; } else {  document.getElementById('nosubmit').click();   } }  "></input><br><br>
<input style='display:none;' id='nosubmit' type='submit' value='Moderate'></input>
</form><hr>
<form  onsubmit="return checkForm(this);" action='./moderation_captcha.php' method='POST' align='center' name='fmoderation' id='fmoderation'>
<?php global $captcha_bit; if (1 == 1) { echo $captcha_bit; } ?>
Email Moderation (ie. click resultant moderation button and, with CAPTCHA success as well, will be sent for moderation and, perhaps, be included in list above within 24 hours):<br> <input style='width:100%;' type='text' name='mymidea' id='mymidea' value='' onblur=" if (this.value != '') { cnt++; thiscnt=cnt; document.getElementById('mytbody').innerHTML+='<tr><td><input id=mymod' + cnt + ' style=background-color:pink; type=button onmousedown=fcnt(this); onclick=setTimeout(submitform,1000); value=Moderate title=' + encodeURIComponent(this.value) + '></input>' + ' ' + this.value + '</td><td></td>'; this.value='';  }   "></input><br><br>
<input style='display:none;' id='submit' type='submit' value='Moderate'></input>
</form><hr>
<form  onsubmit="return checkForm(this);" action='./moderation_captcha.php' method='POST' align='center' name='filemoderation' id='filemoderation'>
<?php global $captcha_bit; if (1 == 1) { echo $captcha_bit; } ?>
File Moderation (ie. click resultant moderation button and, with CAPTCHA success as well, will be sent for moderation and, perhaps, be included in list above within 24 hours):<br> <input style='width:100%;' type='text' name='mymfileidea' id='mymfileidea' value='' onblur=" if (this.value != '') { cnt++; thiscnt=cnt; document.getElementById('mytbody').innerHTML+='<tr><td><input id=myfilemod' + cnt + ' style=background-color:pink; type=button onmousedown=filecnt(this); onclick=setTimeout(submitfileform,1000); value=Moderate title=' + encodeURIComponent(this.value) + '></input>' + ' ' + this.value + '</td><td></td>'; this.value='';  }   "></input><br><br>
<input style='display:none;' id='filesubmit' type='submit' value='Moderate'></input>
</form><hr>
<form  onsubmit="return checkForm(this);" action='./moderation_captcha.php' method='POST' align='center' name='dbmoderation' id='dbmoderation'>
<?php global $captcha_bit; if (1 == 1) { echo $captcha_bit; } ?>
Database Moderation (ie. click resultant moderation button and, with CAPTCHA success as well, will be sent for moderation and, perhaps, be included in list above within 24 hours):<br> <input style='width:100%;' type='text' name='mymdbidea' id='mymdbidea' value='' onblur=" if (this.value != '') { cnt++; thiscnt=cnt; document.getElementById('mytbody').innerHTML+='<tr><td><input id=mydbmod' + cnt + ' style=background-color:pink; type=button onmousedown=dbcnt(this); onclick=setTimeout(submitdbform,1000); value=Moderate title=' + encodeURIComponent(this.value) + '></input>' + ' ' + this.value + '</td><td></td>'; this.value='';  }   "></input><br><br>
<input style='display:none;' id='dbsubmit' type='submit' value='Moderate'></input>
</form><hr>
</div>
</body>
</html>

  
