<html>
<head>
<title></title>
</head>
<body>

<h2>Show Log</h2>

<?php
  /************************************************************/
  // show the opengatem usage log in the web page.
  // the query condition is acquired from the html input from.
  // default is as follows: any user, any device, last 3 days 
  // database connecttion parameters might be modified.
  // As this script should be used only by the administrators, 
  // it should be protected by some access control method.
  /************************************************************/

  // set default values
  $userId=$device='%';
  $now=date('Y-m-d H:i:s');
  $fromTime=date('Y-m-d H:i:s', strtotime($now." -3 day"));
  $toTime=$now;

  // get request data
  if(isset($_GET['userId']))$userId=$_GET['userId'];
  if(isset($_GET['device']))$device=$_GET['device'];
  if(isset($_GET['fromTime']))$fromTime=$_GET['fromTime'];
  if(isset($_GET['toTime']))$toTime=$_GET['toTime'];

  // show html table for query parameters
  print("<p><table>");
  print("<form method=GET action=showlog.php");
  print("<tr><td>UserId</td><td><input size=30 type=text name=userId value='".$userId."'></td></tr>");
  print("<tr><td>Device</td><td><input size=30 type=text name=device value='".$device."'></td></tr>");
  print("<tr><td>FromTime</td><td><input size=30 type=text name=fromTime value='".$fromTime."'></td></tr>");
  print("<tr><td>ToTime</td><td><input size=30 type=text name=toTime value='".$toTime."'></td></tr>");
  print("<tr><td></td><td><input type=submit value='send'></td></tr>");
  print("</table></p>");

  // connect and access to MySql db
  $link = mysqli_connect('localhost', 'root', '');
  if (!$link) die('Cannot connet to DB'.mysqli_error());

  $db_selected = mysqli_select_db($link, 'opengatem');
  if (!$db_selected) die('Cannot select DB'.mysqli_error());

  mysqli_set_charset($link, 'utf8');

  $result = mysqli_query($link, 'SELECT * FROM sessionview where userId like "'.$userId.'" and device like "'.$device.'" and "'.$fromTime.'"<openTime and openTime<"'.$toTime.'"');

  if (!$result) die('Fail query'.mysqli_error());

  // show header line
  print("<table border=1>");
  print('<tr>');
  $count=0;
  while ($field = mysqli_fetch_field($result)) {
    print('<td>'.$field->name.'</td>');
    $count++;
  }
  print('</tr>');

  // show rows
  while ($row = mysqli_fetch_row($result)) {
    print('<tr>');
    for($i=0; $i<$count; $i++){
      print('<td>'.$row[$i].'</td>');
    }
    print('</tr>');
  }

  print("</table>");
  $close_flag = mysqli_close($link);

?>

</body>
</html>
