add_search.php
changeset 0 a4bc6ef5dbfe
child 1 29ae580f12bd
equal deleted inserted replaced
-1:000000000000 0:a4bc6ef5dbfe
       
     1 <?php
       
     2 	// URL validate function
       
     3 	function validUrl($str)
       
     4 	{
       
     5 		return ( preg_match('/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? TRUE : FALSE;
       
     6 	}
       
     7 	
       
     8 	// get user ID from cookie
       
     9 	$currID = $_COOKIE['h4c_ID'];
       
    10 	
       
    11 	if ($currID == "") 
       
    12 	{
       
    13 		$currID = 1;
       
    14 	}
       
    15 	
       
    16 	// get form values	
       
    17 	$search_term = $_GET['search_term'];
       
    18 	$search_type = $_GET['search_type'];
       
    19 	
       
    20 	// check if valid url and send back to details page if not
       
    21 	if (!validUrl($search_term)) {
       
    22 		die(header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php?alert=urlerror'));
       
    23 	} 
       
    24 	
       
    25 	
       
    26 	// check to see if private or public
       
    27 	if ($search_type == "private")
       
    28 	{
       
    29 		$private_status = 1;
       
    30 	} else {
       
    31 		$private_status = 0;
       
    32 		$currID = 1;
       
    33 	}
       
    34 	
       
    35 	// retrieve cookie if it exists
       
    36 	if ($search_term != "") 
       
    37 	{	
       
    38 		$conn = mysql_connect('localhost','root','') or die(mysql_error());
       
    39 		mysql_select_db('h4c_db');
       
    40 
       
    41 		$query = mysql_query("INSERT INTO requests_tbl (req_ID, user_ID, req_val, req_response, req_created) VALUES ('', '$currID', '$search_term', 0, CURDATE())");
       
    42 		$query2 =  mysql_query("SELECT MAX(req_ID) as id FROM requests_tbl");
       
    43 		mysql_close();
       
    44 		
       
    45 		while ($db_field2 = mysql_fetch_assoc($query2)) 
       
    46 		{	
       
    47 			$reqID = $db_field2['id'];
       
    48 		}
       
    49 		
       
    50 		// construct message string
       
    51 		$message = "#" . $currID . "#" . $reqID . "#" . $search_term;
       
    52 		
       
    53 		$host="134.226.36.154";
       
    54 		$port = 8080;
       
    55 		$timeout = 30;
       
    56 	
       
    57 		$sk=fsockopen($host,$port,$errnum,$errstr,$timeout);
       
    58 		  
       
    59 		if (!is_resource($sk)) 
       
    60 		{
       
    61 			exit("connection fail: ".$errnum." ".$errstr);
       
    62 		} 
       
    63 		else 
       
    64 		{
       
    65 			fwrite($sk, $message);
       
    66 		  	$result = "";
       
    67 		  
       
    68 			while (!feof($sk)) {
       
    69 				$result .= fgets($sk, 1024);
       
    70 			}
       
    71 		}
       
    72 		  
       
    73 		fclose($sk);
       
    74 		
       
    75 		//header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/details.php?alert=add');
       
    76 	} 
       
    77 ?>
       
    78 
       
    79 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
    80 <html xmlns="http://www.w3.org/1999/xhtml">
       
    81 <head>
       
    82 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
    83 <title>H4C - deleting request information...</title>
       
    84 <link href="files/h4c_main.css" rel="stylesheet" type="text/css" media="screen" />
       
    85 </head>
       
    86 
       
    87 <body>
       
    88 <?php
       
    89 	// FOR TEST PURPOSES - CHECK QUERYSTRING
       
    90 	//print "search term = " . $search_term;
       
    91 	
       
    92 	if ($search_term == "") 
       
    93 	{
       
    94 		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>";
       
    95 	}
       
    96 	
       
    97 	print ("<p>MSG = " . $message . "</p>");
       
    98 	print ("<p>RESULT = " . $result . "</p>");
       
    99 ?>
       
   100 </body>
       
   101 </html>