Logo  

CS479/579 - Web Programming II

Displaying ./code/images/index.php

<?php
include "config.php";
include "auth.php";

$auth = new Auth();
$A = $auth->authenticated();
//if ($auth->authenticated() == false) header("location: login.php");

?>
<!DOCTYPE html>
<html>
<head>
 <title> Image site </title>
 <meta charset='utf-8'>
 <link rel=stylesheet href=style.css type=text/css>
 <script>
 function loadurl(url, callback) {
   var req = new XMLHttpRequest();
   req.open("GET", url, true);
   req.onreadystatechange = function() {
     if (req.readyState == 4 && req.status == 200)
       callback(JSON.parse(req.responseText));
   };
   req.send(null);
 }

 function like(iid) {
   loadurl("like.php?like&iid="+iid, function(num) {
     console.log("like:", num);
     if (num == "fail") return;
     document.getElementById("i"+iid+".like").innerHTML = num;
   });
 }
 function dislike(iid) {
   loadurl("like.php?dislike&iid="+iid, function(num) {
     if (num == "fail") return;
     document.getElementById("i"+iid+".dislike").innerHTML = num;
   });
 }
 </script>
</head>
<body>
<form method=get action=search.php name=search></form>
<table class=main>
<thead>
<tr>
 <th> Images
 <th> Explore
<?php
 if ($A) echo "<th style='width: 250px;'>
   <form method=post action=upload.php name=upload enctype='multipart/form-data'>
   <input type='file' name=upload[] multiple onchange='document.forms.upload.submit();'>
   </form>";
?>
 <th> <label class=search>&#9906; <input form=search type=text id=search placeholder='Search images'></label>
 <td><?php
  if ($A) echo "<a style='float: left;' href='logout.php'> Logout </a>";
  else echo "<a style='float: left;' href='login.php'> Login </a> <button style='float: right; margin-right: 50px;' class=button type=submit onclick='document.location=signup.php'> Signup </button>"
?>
</thead>
</table>
<div style='width: 90vw; margin: 0 auto;'>
<?php

 if ($A) {
   $uid = $auth->uid;
   $stmt = $myconn->prepare("select iid, location, name, meta from img_image WHERE uid=?");
   $stmt->bind_param("i", $uid);
   $stmt->bind_result($iid, $location, $name, $meta);
   $stmt->execute();
   while ($stmt->fetch()) {
     $mdata = json_decode($meta, true);
     echo "<table style='float: left;' ><tr><td>";
     echo "<a href='uploads/$location'><img style='padding: 15px;' src='uploads/thumb/$location'></a>";
     echo "<tr><td align=center>
       {$mdata['width']}x{$mdata['height']}
       <button onclick='like($iid);'>&#8679;</button>
         <span id='i{$iid}.like'> {$mdata['likes']} </span>
       <button onclick='dislike($iid);'>&#8681;</button>
         <span id='i{$iid}.dislike'> {$mdata['dislikes']} </span>";
     echo "</table>";
   }
 }
?>
</span>
<div style='clear: both;'>
This space intentionally left blank.
</div>

<!--<div class=header>
<span class=sitename> Images </span>
<span class=dropdown> Explore </span>
<label class=search>&#9906; <input form=search type=text id=search></label>
<a href=login.php class=normal> Login </a>
<button class=button type=submit onclick='document.location=signup.php'> Signup </button>
</div>
<br><br>-->


</body>
</html>