<?php require_once('Connections/CCOM.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if ((isset($_GET['m_id'])) && ($_GET['m_id'] != "") && (isset($_GET['del']))) {
  $deleteSQL = sprintf("DELETE FROM memberdata WHERE m_id=%s",
                       GetSQLValueString($_GET['m_id'], "int"));

  mysql_select_db($database_CCOM, $CCOM);
  $Result1 = mysql_query($deleteSQL, $CCOM) or die(mysql_error());

  $deleteGoTo = "memberadmin.php";
  
  header(sprintf("Location: %s", $deleteGoTo));
}

$colname_Recordset1 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Recordset1 = $_SESSION['MM_Username'];
}
mysql_select_db($database_CCOM, $CCOM);
$query_Recordset1 = sprintf("SELECT * FROM memberdata WHERE m_username = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $CCOM) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_CCOM, $CCOM);
$query_Recordset2 = "SELECT * FROM memberdata WHERE m_level <> 'admin' ORDER BY m_id DESC";
$Recordset2 = mysql_query($query_Recordset2, $CCOM) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>網站會員系統(管理介面)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function tfm_confirmLink(message) { //v1.0
	if(message == "") message = "Ok to continue?";	
	document.MM_returnValue = confirm(message);
}
</script>
</head>
<body bgcolor="#99cc00">
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
   <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="720">
	  <tr>
	   <td><img name="memberSystem_r1_c1" src="images/memberSystem_r1_c1.jpg" width="5" height="88" border="0" alt=""></td>
	   <td><img name="memberSystem_r1_c2" src="images/memberSystem_r1_c2.jpg" width="193" height="88" border="0" alt=""></td>
	   <td><img name="memberSystem_r1_c3" src="images/memberSystem_r1_c3.jpg" width="280" height="88" border="0" alt=""></td>
	   <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="222">
	     <tr>
	       <td height="64" align="right" valign="bottom"><table border="0" cellpadding="0" cellspacing="0">
	         <tr>
	           <td><a href="index.php"><img name="memberSystem_r2_c4" src="images/memberSystem_r2_c4.jpg" width="48" height="53" border="0" alt=""></a></td>
	           <td width="11">&nbsp;</td>
	           <td><a href="memberzone.php"><img name="memberSystem_r2_c8" src="images/memberSystem_r2_c8.jpg" width="48" height="53" border="0" alt=""></a></td>
	           <td width="11">&nbsp;</td>
	           <td><a href="memberadmin.php"><img name="memberSystem_r2_c10" src="images/memberSystem_r2_c10.jpg" width="48" height="53" border="0" alt=""></a></td>
	           </tr>
	         </table></td>
	       </tr>
	     <tr>
	       <td><img name="memberSystem_r3_c4" src="images/memberSystem_r3_c4.jpg" width="222" height="24" border="0" alt=""></td>
	       </tr>
	     </table></td>
	   <td><img name="memberSystem_r1_c11" src="images/memberSystem_r1_c11.jpg" width="20" height="88" border="0" alt=""></td>
	  </tr>
	</table></td>
  </tr>
  <tr>
   <td background="images/memberSystem_r4_c1.jpg"><table width="96%" border="0" align="center" cellpadding="10" cellspacing="0">
     <tr valign="top">
       <td><h1>管理-會員列表</h1>
         <div class="searchDiv">
           <form name="form1" method="post" action="">
             <label>
               <strong>
               <select name="colname" id="colname">
                 <option value="m_name" selected>姓名</option>
                 <option value="m_nick">暱稱</option>
                 <option value="m_username">帳號</option>
               </select>
               </strong>
               <input type="text" name="keyword" id="keyword">
             </label>
             <input type="submit" name="button" id="button" value="查詢">
             <input type="button" name="button2" id="button2" value="顯示所有資料">
           </form>
         </div>
<table width="100%" border="0" cellspacing="1" cellpadding="4">
  <tr class="head1">
             <td align="center">姓名</td>
             <td align="center">暱稱</td>
             <td align="center">帳號</td>
             <td align="center">編輯</td>
           </tr>
           <?php do { ?>
             <tr>
               <td align="center" bgcolor="#E6E6E6"><?php echo $row_Recordset2['m_name']; ?></td>
               <td align="center" bgcolor="#E6E6E6"><?php echo $row_Recordset2['m_nick']; ?></td>
               <td align="center" bgcolor="#E6E6E6"><?php echo $row_Recordset2['m_username']; ?></td>
               <td align="center" bgcolor="#E6E6E6"><a href="memberUpdata.php?m_id=<?php echo $row_Recordset2['m_id']; ?>">修改</a> <a href="memberadmin.php?m_id=<?php echo $row_Recordset2['m_id']; ?>&del=true" onClick="tfm_confirmLink('您確定要刪除這筆資料嗎？？？');return document.MM_returnValue">刪除</a></td>
             </tr>
             <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
         </table>
         
         
         <hr size="1" noshade>
         <p>&nbsp;</p>
         </td>
       <td width="180"><p class="head1">管理員資料</p>
         <table width="100%" border="0" cellspacing="0" cellpadding="4">

           <tr>
             <td align="center" valign="baseline"><strong></strong> <?php echo $row_Recordset1['m_nick']; ?>您好，</td>
           </tr>
           <tr>
             <td align="center" valign="baseline"><a href="<?php echo $logoutAction ?>">登出系統</a></td>
           </tr>
         </table>
         <p>&nbsp;</p></td>
     </tr>
   </table></td>
  </tr>
  <tr>
   <td><img name="memberSystem_r6_c1" src="images/memberSystem_r6_c1.jpg" width="720" height="10" border="0" alt=""></td>
  </tr>
  <tr>
   <td><div id="siteinfo"></div></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset2);
?>
