Kerry Hartnett & Darren McCready N4C web app
authoralex.mcmahon@cs.tcd.ie
Tue, 19 May 2009 00:30:06 +0100
changeset 0 a4bc6ef5dbfe
child 1 29ae580f12bd
Kerry Hartnett & Darren McCready N4C web app
add_search.php
app_concepts/h4c_app_design1.png
app_concepts/images/logo.gif
app_concepts/screengrabs/requests_tbl.gif
app_concepts/screengrabs/users_tbl.gif
delete.php
details.php
files/h4c_main.css
images/bg.gif
images/logo.gif
images/x.gif
images/x2.gif
index.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/add_search.php	Tue May 19 00:30:06 2009 +0100
@@ -0,0 +1,101 @@
+<?php
+	// URL validate function
+	function validUrl($str)
+	{
+		return ( preg_match('/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? TRUE : FALSE;
+	}
+	
+	// get user ID from cookie
+	$currID = $_COOKIE['h4c_ID'];
+	
+	if ($currID == "") 
+	{
+		$currID = 1;
+	}
+	
+	// get form values	
+	$search_term = $_GET['search_term'];
+	$search_type = $_GET['search_type'];
+	
+	// check if valid url and send back to details page if not
+	if (!validUrl($search_term)) {
+		die(header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php?alert=urlerror'));
+	} 
+	
+	
+	// check to see if private or public
+	if ($search_type == "private")
+	{
+		$private_status = 1;
+	} else {
+		$private_status = 0;
+		$currID = 1;
+	}
+	
+	// retrieve cookie if it exists
+	if ($search_term != "") 
+	{	
+		$conn = mysql_connect('localhost','root','') or die(mysql_error());
+		mysql_select_db('h4c_db');
+
+		$query = mysql_query("INSERT INTO requests_tbl (req_ID, user_ID, req_val, req_response, req_created) VALUES ('', '$currID', '$search_term', 0, CURDATE())");
+		$query2 =  mysql_query("SELECT MAX(req_ID) as id FROM requests_tbl");
+		mysql_close();
+		
+		while ($db_field2 = mysql_fetch_assoc($query2)) 
+		{	
+			$reqID = $db_field2['id'];
+		}
+		
+		// construct message string
+		$message = "#" . $currID . "#" . $reqID . "#" . $search_term;
+		
+		$host="134.226.36.154";
+		$port = 8080;
+		$timeout = 30;
+	
+		$sk=fsockopen($host,$port,$errnum,$errstr,$timeout);
+		  
+		if (!is_resource($sk)) 
+		{
+			exit("connection fail: ".$errnum." ".$errstr);
+		} 
+		else 
+		{
+			fwrite($sk, $message);
+		  	$result = "";
+		  
+			while (!feof($sk)) {
+				$result .= fgets($sk, 1024);
+			}
+		}
+		  
+		fclose($sk);
+		
+		//header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php?alert=add');
+	} 
+?>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>H4C - deleting request information...</title>
+<link href="files/h4c_main.css" rel="stylesheet" type="text/css" media="screen" />
+</head>
+
+<body>
+<?php
+	// FOR TEST PURPOSES - CHECK QUERYSTRING
+	//print "search term = " . $search_term;
+	
+	if ($search_term == "") 
+	{
+		print "<p>You did not enter a valid search term. Click the link to return to the <a href='details.php'>search page</a>.</p>";
+	}
+	
+	print ("<p>MSG = " . $message . "</p>");
+	print ("<p>RESULT = " . $result . "</p>");
+?>
+</body>
+</html>
Binary file app_concepts/h4c_app_design1.png has changed
Binary file app_concepts/images/logo.gif has changed
Binary file app_concepts/screengrabs/requests_tbl.gif has changed
Binary file app_concepts/screengrabs/users_tbl.gif has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/delete.php	Tue May 19 00:30:06 2009 +0100
@@ -0,0 +1,36 @@
+<?php
+	// get request ID to delete
+	$req_ID = $_GET['req_id'];
+	
+	$conn = mysql_connect('localhost','root','') or die(mysql_error());
+	mysql_select_db('h4c_db');
+	
+	// retrieve cookie if it exists
+	if ($req_ID != "") 
+	{	
+		$query = mysql_query("DELETE FROM requests_tbl WHERE (req_ID = '$req_ID')");
+		header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php?alert=del');
+	} 
+	
+	mysql_close();
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>H4C - deleting request information...</title>
+<link href="files/h4c_main.css" rel="stylesheet" type="text/css" media="screen" />
+</head>
+
+<body>
+<?php
+	// FOR TEST PURPOSES - CHECK QUERYSTRING
+	//print "req id = " . $req_ID;
+	
+	if ($req_ID == "") 
+	{
+		print "The search item you requested to delete does not exist. Click the link to return to the <a href='details.php'>search page</a>.";
+	}
+?>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/details.php	Tue May 19 00:30:06 2009 +0100
@@ -0,0 +1,199 @@
+<?php 
+	// read user ID from cookie, if cookie doesn't exist, set to 1 (master)
+	$currID = $_COOKIE['h4c_ID'];
+	
+	if ($currID == "") 
+	{
+		$currID = 1;
+	}
+	
+	$status = $_GET['alert'];
+	$view_mode = $_GET['view_mode'];
+	$view_order = $_GET['view_order'];
+	
+	
+	// connect to the database
+	$conn = mysql_connect('localhost','root','') or die(mysql_error());
+	mysql_select_db('h4c_db');
+	
+	// search server cache for received websites
+	if ($currID != 1)
+	{
+		$query_cache = mysql_query("SELECT * FROM requests_tbl WHERE (user_ID = '$currID' OR user_ID = 1)");
+	} 
+	else 
+	{
+		$query_cache = mysql_query("SELECT * FROM requests_tbl WHERE (user_ID = 1)");
+	}
+	
+	while ($db_fieldcache = mysql_fetch_assoc($query_cache)) 
+	{
+		// send command to squid and return value
+		$curr_reqID = $db_fieldcache["req_ID"];
+		$commandline = "squidclient -m HEAD -p 8080 -H \"Cache-Control: only-if-cached\n\" -u " . $db_fieldcache["req_ID"] . " -w " . $db_fieldcache["user_ID"] . " " . $db_fieldcache["req_val"] . " |grep X-Cache: |grep MISS";
+		exec($commandline, $cache_results);
+		
+		// find out if site is available and update the database
+		if (strpos($cache_results, "HIT") == true)
+		{
+			$query_update = mysql_query("UPDATE responses_tbl SET req_response = '1' WHERE req_ID = '$curr_reqID'");
+		} 
+		else 
+		{
+			$query_update = mysql_query("UPDATE responses_tbl SET req_response = '0' WHERE req_ID = '$curr_reqID'");
+		}
+	}
+	
+	
+	// pick order to show (user specified)
+	
+	if ($view_order == "pend")
+	{
+		$order_sql = "req_response ASC";
+	}
+	else if ($view_order == "date_asc")
+	{
+		$order_sql = "req_created ASC";
+	}
+	else if ($view_order == "avail")
+	{
+		$order_sql = "req_response DESC";
+	}
+	else
+	{
+		$order_sql = "req_created DESC";
+	} 
+	
+	// pick relervant mySQl QUERY
+	if ($view_mode == "public")
+	{
+		$query = mysql_query("SELECT * FROM requests_tbl WHERE (user_ID = 1) ORDER BY " . $order_sql);
+	}
+	else if ($view_mode == "all")
+	{
+		$query = mysql_query("SELECT * FROM requests_tbl WHERE (user_ID = '$currID' OR user_ID = 1) ORDER BY " . $order_sql);
+	}
+	else 
+	{
+		$query = mysql_query("SELECT * FROM requests_tbl WHERE (user_ID = '$currID') ORDER BY " . $order_sql);
+	}
+	
+	
+	// Construct search information message
+	if ($view_mode == "public") 
+	{
+		$search_info = "There are " . mysql_num_rows($query) . " saved public searches.";
+	} else if ($view_mode == "private") {
+		$search_info = "You have " . mysql_num_rows($query) . " saved private searches.";
+	} else {
+		$search_info = "There are " . mysql_num_rows($query) . " saved searches.";
+	}
+?>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>H4C - Welcome. <?php print $search_info; ?></title>
+<link href="files/h4c_main.css" rel="stylesheet" type="text/css" media="screen" />
+</head>
+
+<body>
+<div id="container">
+    <div id="header">
+        <h1><span>H4C</span></h1>
+    	<p id="credits">HTML Requester v. 1.00</p>
+    </div>
+    <?php 
+		// show status messages based on user interaction
+		if ($status == "del") 
+		{
+			print "<div id='delete_alert'><span>You have successfully deleted the selected search.</span><a href='details.php'><img class='img_right' src='images/x.gif' width='14' height='20' /></a><div class='clear'></div></div>";
+		} else if ($status == "add")
+		{
+			print "<div id='add_alert'><span>You have successfully added a new search.</span><a href='details.php'><img class='img_right' src='images/x.gif' width='14' height='20' /></a><div class='clear'></div></div>";
+		} else if ($status == "urlerror") 
+		{
+			print "<div id='delete_alert'><span>You have entered an malformed url. Please use this format: <em><strong>http://www.yourwebsite.com</strong></em></span><a href='details.php'><img class='img_right' src='images/x.gif' width='14' height='20' /></a><div class='clear'></div></div>";
+		}
+	?>
+    <div id="search">
+   	  <h2>Open a new search:</h2>
+        <form action="add_search.php" method="get" enctype="multipart/form-data" name="search" target="_parent">
+        <input name="search_term" type="text" value="http://www.yourwebsite.com" size="21" />
+        <?php
+			// 
+		  	if ($currID != 1)
+			{
+				print "<select name='search_type' size='1'>";
+          		print "<option value='public' selected>Public</option>";
+            	print "<option value='private'>Private</option></select>";	
+			}
+		  ?>
+          <input id="search_button" name="submit" type="submit" value="Search" />
+        </form>
+    </div>
+    <div id="results">
+    	<h2><?php print $search_info; ?></h2>
+		<?php
+            // customise view links based on view
+            
+			print "<div id='view_options'><form action='details.php' method='get' enctype='multipart/form-data' name='search' target='_parent'>";
+			
+			if ($currID != 1)
+            {
+                print "<select name='view_mode' size='1'>";
+                print "<option value='private' selected>Private searches</option>";
+                print "<option value='public'>Public searches</option>";
+                print "<option value='all'>All searches</option></select> ";
+            }
+			
+			print "<select name='view_order' size='1'>";
+            print "<option value='date_desc' selected>Newest &gt; Oldest</option>";
+            print "<option value='date_asc'>Oldest &gt; Newest</option>";
+            print "<option value='avail'>Available &gt; Pending</option> ";
+			print "<option value='pend'>Pending &gt; Available</option></select> ";
+            print "<input name='submit' type='submit' value='Update view' /></form></div>";
+        ?>
+		<?php
+            //display current search requests
+            $search_count = 1;
+			
+			while ($db_field = mysql_fetch_assoc($query)) 
+			{		 
+				if ($db_field['user_ID'] != 1 and $view_mode == "all")
+				{
+					print "<div class='search_result_private'><span class='result_no'>" . $search_count . "</span> ";
+					print "<span class='search_txt'>Search:</span><span class='search_input'><em>" . $db_field['req_val'] . "</em></span> ";
+				} else {
+					print "<div class='search_result'><span class='result_no'>" . $search_count . "</span> ";
+					print "<span class='search_txt'>Search:</span><span class='search_input'><em>" . $db_field['req_val'] . "</em></span> ";
+				}
+				
+  				print "<span class='results_txt'>Status:</span>";
+				
+				if ($db_field['req_response'] == 0)
+				{
+					print "<span class='result_output_none'>Result pending</span>";
+					print "<a href='delete.php?req_id=" . $db_field['req_ID'] . "'><img src='images/x.gif' width='14' height='20' /></a>";
+				} else {
+					print "<span class='result_output'><a href='" . $db_field['req_val'] ."' target='_blank'>View website</a></span>";
+					print "<a href='delete.php?req_id=" . $db_field['req_ID'] . "'><img src='images/x.gif' width='14' height='20' /></a>";
+				}
+                print "<div class='clear'></div></div>";
+				
+				$search_count++;
+            }
+            
+            // if no requests, display message
+            if (mysql_num_rows($query) < 1) 
+            {
+                print '<p>You have no previous searches please use the search form to create a new search.</p>';
+            }
+            
+            mysql_close();
+        ?>
+    </div>
+</div>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/files/h4c_main.css	Tue May 19 00:30:06 2009 +0100
@@ -0,0 +1,212 @@
+@charset "utf-8";
+/* CSS Document by Darren McCready for Intel 2009.*/
+
+* {
+	border:none;
+	margin:0;
+	padding:0;
+}
+
+body {
+	background:#fff;
+	color:#000;
+	font-family:Trebuchet MS, Geneva, Arial, Helvetica, sans-serif;
+	font-size:0.8em;
+	background:url(../images/bg.gif) top center repeat-y;
+}
+
+#container {
+	width:760px;
+	margin:0 auto;
+}
+
+#header {
+	display:block;
+	width:760px;
+	height:66px;
+	background:#3b495d;
+	color:#fff;
+	border-bottom:3px solid #fff;
+}
+
+#credits {
+	display:block;
+	width:140px;
+	float:right;
+	text-align:right;
+	padding:10px;
+	font-size:0.85em;
+}
+
+h1 {
+	float:left;
+	background:url(../images/logo.gif) center no-repeat;
+	width:205px;
+	height:66px;
+}
+
+h1 span {
+	display:none;
+}
+
+h2 {
+	font-size:1.2em;
+	padding:5px;
+	color:#fff;
+	font-weight:500;
+}
+
+#search h2 {
+	background:#77ACA1;
+}
+
+#results h2 {
+	background:#BB9F71;
+}
+
+#search {
+	float:left;
+	width:170px;
+	background:#D6E6E3;
+	padding:5px;
+}
+
+#results {
+	float:right;
+	width:570px;
+	background:#EEE7DB;
+	padding:5px;
+}
+
+.search_result {
+	display:block;
+	padding:10px;
+	border-bottom:1px solid #D4C3A6;
+}
+
+.search_result_private {
+	display:block;
+	padding:10px;
+	background:#ffc;
+	border-bottom:1px solid #D4C3A6;
+}
+
+.result_no {
+	display:block;
+	width:32px;
+	background:#fff;
+	border:1px solid #BB9F71;
+	float:left;
+	text-align:center;
+}
+
+.search_txt {
+	display:block;
+	width:60px;
+	float:left;
+	text-align:center;
+}
+
+.search_input {
+	display:block;
+	width:225px;
+	background:#fff;
+	border:1px solid #BB9F71;
+	float:left;
+	text-align:center;
+	margin:0 5px;
+	overflow:hidden;
+}
+
+.results_txt {
+	display:block;
+	width:55px;
+	float:left;
+	text-align:center;
+}
+
+.clear {
+	clear:both;
+}
+
+.result_output a {
+	display:block;
+	width:120px;
+	border:1px solid #005338;
+	background:#349E69;
+	color:#fff;
+	float:left;
+	text-align:center;
+	margin:0 5px;
+}
+
+.result_output a:hover {
+	text-decoration:none;
+	background:#4DC488;
+	border:1px solid #096;
+}
+
+.result_output_none {
+	display:block;
+	width:120px;
+	background:#c66;
+	border:1px solid #933;
+	color:#fff;
+	float:left;
+	text-align:center;
+	margin:0 5px;
+}
+
+form {
+	padding:9px 5px;
+}
+
+#search_button {
+	float:right;
+	margin-right:5px;
+}
+
+#delete_alert, #add_alert {
+	display:block;
+	width:740px;
+	padding:10px;
+	color:#fff;
+	text-align:center !important;
+}
+
+#delete_alert {
+	background:#c00;
+}
+
+#add_alert {
+	background:#090;
+}
+
+.img_right {
+	float:right;
+	display:block;
+}
+
+#delete_alert span, #add_alert span {
+	float:left;
+}
+
+#view_options {
+	display:block;
+	padding:5px;
+	background:#E4D9C6;
+}
+
+#view_options a,{
+	padding-right:10px;
+	color:#000;
+}
+
+#view_options span {
+	padding-right:10px;
+	color:#A6854F;
+}
+
+#view_options a:hover {
+	text-decoration:none;
+}
\ No newline at end of file
Binary file images/bg.gif has changed
Binary file images/logo.gif has changed
Binary file images/x.gif has changed
Binary file images/x2.gif has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/index.php	Tue May 19 00:30:06 2009 +0100
@@ -0,0 +1,61 @@
+<?php
+	$conn = mysql_connect('localhost','root','') or die(mysql_error());
+	mysql_select_db('h4c_db');
+	
+	// retrieve cookie if it exists
+	if (isset($_COOKIE['h4c_ID'])) 
+	{	
+		$currID = $_COOKIE['h4c_ID'];
+	} 
+	else 
+	{
+		// SQL queries
+		$result = mysql_query("INSERT INTO users_tbl (user_ID) VALUES ('')");
+		$user_id =  mysql_query("SELECT MAX(user_ID) as id FROM users_tbl");
+		
+		$expiry = time() + (3600 * 24 * 30 * 12); 
+		
+		//create cookies
+		while ($db_field2 = mysql_fetch_assoc($user_id)) {		 
+		 	setcookie('h4c_ID', $db_field2['id'] , $expiry, '/', '', 0);
+		}
+		
+		if (!isset($_COOKIE['h4c_ID'])) 
+		{
+			$currID = 1;
+		}
+	}
+	
+	mysql_close();
+	
+	header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php');
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>H4C - checking your user status...</title>
+</head>
+
+<body>
+<?php
+	// FOR TEST PURPOSES - CHECK COOKIE VALUES
+	/*
+	print '<p><strong>COOKIES------------------------------------------------------------------------</strong></p>';
+	print 'COOKIE ID = ' . $currID . '<br />';
+	print 'COOKIE Status = ' . $currStatus . '</p>';
+	
+	// FOR TEST PURPOSES - READ USERS
+	$result2 = mysql_query('SELECT * FROM users_tbl');
+	
+	print '<p><strong>LIST OF USERS -----------------------------------------------------------------</strong></p><p>';
+	
+	while ($db_field = mysql_fetch_assoc($result2)) {
+		print 'USERID: ' . $db_field['user_ID'] . ' | EMAIL: ' . $db_field['user_email'] . ' | USERSTATUS: ' . $db_field['user_status'] . '<br />';
+	}
+	
+	print '</p>';
+	*/
+?>
+</body>
+</html>