<?php
// member.php
// RJM Programming
// May, 2015
// Thanks to PHP and MySql Web Development by Luke Welling, Laura Thomson
// Thanks to Professional PHP Programming by Jesus Castagnetto, Haeish Rawat, Sascha Schumann, Chris Scollo, Deepak Veliath ... chapter 20
include "db_connect.php";
$link=null;
$dbname="users";
$utable="musers";
$ucolumn="username";
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'];
}

function moderatepostings($bthere, $ib) {
  global $dbname, $link;
  $morestr="";
  $firstgo=true;
  try {
    if (!$link) $link = db_connect(); 
    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($dbname);
    $result = mysql_query("select * from posting where pstatus='moderating'");
    if (mysql_num_rows($result) > 0) {
     for ($count = $ib; $row = mysql_fetch_row($result, MYSQL_NUM); ++$count) {
      if ($bthere == "" && firstgo) {
       $firstgo=false;
       $morestr='<form id="f' . $count . '" action="./member.php" method="POST"><input type="submit" name="b' . $count . '" id="b' . $count . '" value="Agree to Moderation of ' . urldecode($row[1]) . '"></input></form>'; 
      } else {
       $firstgo=false;
       $morestr.='</td></tr><hr><tr><td><form id="f' . $count . '" action="./member.php" method="POST"><input type="submit" name="b' . $count . '" id="b' . $count . '" value="Agree to Moderation of ' . urldecode($row[1]) . '"></input></form>'; 
      }
     }
    }
  } catch (Exception $e) {
    die('Could not connect: ' . mysql_error());
  }
  return $morestr;
}

function insertposting($cont) {
  global $dbname, $link;
  $pusername="";
  if ( isset( $_POST['ucolumn'] ) ) {
    $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());
  }
}


function updateposting($cont) {
  global $dbname, $link;
  try {
    if (!$link) $link = db_connect(); 
    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($dbname);
    $result = mysql_query("update posting set pstatus='published' where content='".urlencode(str_replace("Agree to Moderation of ","",$cont))."' or content='".str_replace("Agree to Moderation of ","",$cont)."')");
  } catch (Exception $e) {
    die('Could not connect: ' . mysql_error());
  }
}

for ($j=0; $j<200; $j++) {
  if (isset($_POST['b' . $j])) {
    $fx=urlencode(str_replace("Agree to Moderation of ","",urldecode($_POST['b' . $j]))) . ".aml";
    if (file_exists($fx)) {
      unlink($fx);
      $qw=@file_get_contents("what_we_have_in_common.html");
      file_put_contents("what_we_have_in_common.html", str_replace("Agree to Moderation of ","",urldecode($_POST['b' . $j])) . "\n" . $qw);
    } else if (str_replace(".aml", "", $fx) != "") {
      $qw=@file_get_contents("what_we_have_in_common.html");
      file_put_contents("what_we_have_in_common.html", str_replace("Agree to Moderation of ","",urldecode($_POST['b' . $j])) . "\n" . $qw);
      updateposting(urldecode($_POST['b' . $j]));
    }
  }
}

$nextpage="next.php";
$nextval="Next Page";

$logpage="login.php";
if (!file_exists("loginpage.name")) {
   $loginpage=@file_get_contents("loginpage.name");
   if (strpos($loginpage, ".php") !== false) $logpage=$loginpage;
}
if (!isset($_SESSION['login']) && !isset($_POST['login'])) {
   header("Location: " . $logpage . str_replace("?&", "?", ("?" . str_replace("backto=", "lastone=", $_SERVER['QUERY_STRING']) . "&backto=" . $_SERVER['PHP_SELF'])));
   exit;
}
$_SESSION['login']=true;
$backbuttonhtml="";
   $backbuttonhtml="";
   $i=0;
   foreach (glob("*.aml") as $filename) {
     if (substr($filename, 1, 1) == ":") { // Windows
      $parts=explode("\\", $filename);
     } else {
      $parts=explode("/", $filename);
     }
     $thisfile=urldecode(str_replace(".aml", "", $parts[-1 + sizeof($parts)]));
     if ($backbuttonhtml == "") {
      $backbuttonhtml='<form id="f' . $i . '" action="./member.php" method="POST"><input type="submit" name="b' . $i . '" id="b' . $i . '" value="Agree to Moderation of ' . $thisfile . '"></input></form>'; 
     } else {
      $backbuttonhtml.='</td></tr><hr><tr><td><form id="f' . $i . '" action="./member.php" method="POST"><input type="submit" name="b' . $i . '" id="b' . $i . '" value="Agree to Moderation of ' . $thisfile . '"></input></form>'; 
     }
     $i++;
   }
   if ($backbuttonhtml == "") {
      $backbuttonhtml='<br><br><form id="f' . $i . '" action="./member.php" method="POST"><input style="width:90%;" type="text" name="b' . $i . '" id="b' . $i . '" value=""></input><br><input type="submit" value="Okay This Email Suggestion"></input></form><br><br>'; 
   } else {
      $backbuttonhtml.='</td></tr><hr><tr><td><br><br><form id="f' . $i . '" action="./member.php" method="POST"><input style="width:90%;" type="text" name="b' . $i . '" id="b' . $i . '" value=""></input><br><input type="submit" value="Okay This Email Suggestion"></input></form><br><br>'; 
   }
   $i++;
   $backbuttonhtml.=moderatepostings($backbuttonhtml, $i);
if (strpos($_SERVER['HTTP_REFERER'], $logpage) === false && strpos($_SERVER['HTTP_REFERER'], $nextpage) === false && strlen($_SERVER['HTTP_REFERER']) > 0) {
   $backpage=$_SERVER['HTTP_REFERER'];
   $backvals=explode("/", $backpage);
   $backval= strtoupper(substr($backvals[sizeof($backvals) - 1],0,1)) . str_replace(".php", " Page", substr($backvals[sizeof($backvals) - 1],1));
}
?>
<!doctype html>
<html>
<body>
<div align="center">
<table align="center" style='background-color:yellow;'><tr><td><?php echo $backbuttonhtml; ?><form style='display:none;' action='<?php echo $nextpage; ?>' method='post'><input type='hidden' name='login' value='true'></input><input type='submit' id='subm' name='subm' value='<?php echo $nextval; ?>'></input></form></td></tr></table>
<br>
<br>
<a href="<?php echo $logpage; ?>?logout=<?php echo urlencode(str_replace('=form','=y', $_SERVER['QUERY_STRING'])); ?>" title="Logout">Logout</a>
</div>
</body>
</html>
