add_search.php
changeset 0 a4bc6ef5dbfe
child 1 29ae580f12bd
--- /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>