details.php_lessold
changeset 15 3ad193634e5d
equal deleted inserted replaced
14:e2ac834162fb 15:3ad193634e5d
       
     1 <?php 
       
     2 	function remove_http($url = '')
       
     3 	{
       
     4 		if ($url == 'http://' OR $url == 'https://')
       
     5 		{
       
     6 			return $url;
       
     7 		}
       
     8 		$matches = substr($url, 0, 7);
       
     9 		if ($matches=='http://') 
       
    10 		{
       
    11 			$url = substr($url, 7);		
       
    12 		}
       
    13 		else
       
    14 		{
       
    15 			$matches = substr($url, 0, 8);
       
    16 			if ($matches=='https://') 
       
    17 			$url = substr($url, 8);
       
    18 		}
       
    19 		return $url;
       
    20 	}
       
    21 
       
    22 	// read user ID from cookie, if cookie doesn't exist, set to 1 (master)
       
    23 	$currID = $_COOKIE['uid'];
       
    24 	
       
    25 	if ($currID == "") 
       
    26 	{
       
    27 		$currID = 1;
       
    28 		$mysql = new mysqli("localhost", "www-data", "www-data", "members");
       
    29         	$result = $mysql->query("SELECT sid, sid_dir FROM sessions WHERE ((uid = '$currID' or uid = 1) AND (expiration_date = DATE_ADD(NOW(), INTERVAL 24 HOUR)))");
       
    30         	$row = $result->fetch_assoc();
       
    31         	$tsid1 = $row["sid"];
       
    32 		$tsid2 = $row["sid_dir"];
       
    33         	$result->close();
       
    34 
       
    35 
       
    36 	}
       
    37  	// read session IDs from cookie, if cookie doesn't exist, set to 06D5VlLQTbM57LL7IBMW38yHkFpb1XVa (master)
       
    38 
       
    39 
       
    40 
       
    41 //echo $_COOKIE["sid1"];
       
    42         $currSID1 = $_COOKIE['sid1'];
       
    43 	if ($currSID1 == "")
       
    44         {
       
    45        //         $currSID1 = "06D5VlLQTbM57LL7IBMW38yHkFpb1XVa";
       
    46 		$currSID1 = $tsid1;
       
    47 echo $currSID1;
       
    48 
       
    49         }
       
    50 
       
    51         $currSID2 = $_COOKIE['sid2'];
       
    52 	if ($currSID2 == "")
       
    53         {
       
    54 //                $currSID2 = "06D5VlLQTbM57LL7IBMW38yHkFpb1XVa";
       
    55 		$currSID2 = $tsid2;
       
    56 echo $currSID2;
       
    57         }
       
    58 
       
    59 print_r($_COOKIE);
       
    60 	
       
    61 	$status = $_GET['alert'];
       
    62 	$view_mode = $_GET['view_mode'];
       
    63 	$view_order = $_GET['view_order'];
       
    64 	
       
    65 	
       
    66 	// connect to the database
       
    67 	$conn = mysql_connect('localhost','www-data','www-data') or die(mysql_error());
       
    68 	mysql_select_db('members');
       
    69 	
       
    70 	// search server cache for received websites
       
    71 	// anonymous uid=1, sid1=06D5VlLQTbM57LL7IBMW38yHkFpb1XVa & sid2 =06D5VlLQTbM57LL7IBMW38yHkFpb1XVa
       
    72 //	if (($currID != 1) && ($currSID1 != "06D5VlLQTbM57LL7IBMW38yHkFpb1XVa"))
       
    73         if ($currID != 1) 
       
    74 
       
    75 	{
       
    76 		$query_cache = mysql_query("SELECT * FROM requests_tbl WHERE (uid = '$currID' OR uid = 1)");
       
    77 	} 
       
    78 	else 
       
    79 	{
       
    80 		$query_cache = mysql_query("SELECT * FROM requests_tbl WHERE (uid = 1)");
       
    81                         while ($db_fieldcache = mysql_fetch_assoc($query_cache))
       
    82                         {
       
    83                         // send command to squid and return value
       
    84     #                    $command2 = "squidclient -m HEAD -p 8080 -H \"Cache-Control: only-if-cached\n\" -u " . $db_fieldcache["req_ID"] . " -w " . $db_fieldcache["uid"] . " " . $db_fieldcache["req_val"] . " |grep X-Cache: |grep MISS";
       
    85 #                    $command2 = "squidclient -m HEAD -p 8080" . $db_fieldcache["req_val"] . " |grep X-Cache: |grep MISS";
       
    86 //                    $command2 = "squidclient -m HEAD -p 8080 -u {$db_fieldcache["req_ID"]} -w $currSID1 http://localhost/router_stage/{$db_fieldcache["req_ID"]}/$strip_url_val/ |grep X-Cache: |grep MISS";
       
    87                     $command2 = "squidclient -m HEAD -p 8080 -u www-data -w www-data http://localhost/router_stage/{$db_fieldcache["req_ID"]}/$strip_url_val/ |grep X-Cache: |grep MISS";
       
    88                         exec($command2, $cache_results);
       
    89                         // find out if site is available and update the database
       
    90                                 if (strpos($cache_results, "HIT") == true)
       
    91                                         {
       
    92                                         $query_update = mysql_query("UPDATE responses_tbl SET req_response = '1' WHERE req_ID = '$curr_reqID'");
       
    93                                         }
       
    94 /*                                        else
       
    95                                         {
       
    96                                         $query_update = mysql_query("UPDATE responses_tbl SET req_response = '0' WHERE req_ID = '$curr_reqID'");
       
    97                                         $curr_reqID = $db_fieldcache["req_ID"];
       
    98                                         $http_proxy = "http://localhost:8080";
       
    99                                         $staging = "/var/www/private/HTMLrequester/staging/";
       
   100                                         $wget_options = "--delete-after -N -r -l 3 --no-remove-listing -p --max-redirect 10 -t 1 -d";
       
   101                                         $strip_url_val = remove_http($db_fieldcache['req_val']);
       
   102                                         $command1 = "wget -P $staging $wget_options http://localhost/router_stage/{$db_fieldcache["req_ID"]}/$strip_url_val/";
       
   103                                         exec($command1, $wget_results);
       
   104 
       
   105                                         }
       
   106 */
       
   107 			}
       
   108 	}
       
   109 	
       
   110 	while ($db_fieldcache = mysql_fetch_assoc($query_cache)) 
       
   111 	{
       
   112 		$filename = "/var/www/private/HTMLrequester/router_stage/{$db_fieldcache["req_ID"]}/";
       
   113 		if (file_exists($filename)) 
       
   114     			{
       
   115     			$query_update = mysql_query("UPDATE requests_tbl SET req_response = '1' WHERE req_ID = '$curr_reqID'");
       
   116     			} 
       
   117 		else 
       
   118     			{
       
   119    			$query_update = mysql_query("UPDATE requests_tbl SET req_response = '0' WHERE req_ID = '$curr_reqID'");
       
   120 			}  
       
   121 	}
       
   122 	
       
   123 	
       
   124 	// pick order to show (user specified)
       
   125 	
       
   126 	if ($view_order == "pend")
       
   127 	{
       
   128 		$order_sql = "req_response ASC";
       
   129 	}
       
   130 	else if ($view_order == "date_asc")
       
   131 	{
       
   132 		$order_sql = "req_created ASC";
       
   133 	}
       
   134 	else if ($view_order == "avail")
       
   135 	{
       
   136 		$order_sql = "req_response DESC";
       
   137 	}
       
   138 	else
       
   139 	{
       
   140 		$order_sql = "req_created DESC";
       
   141 	} 
       
   142 	
       
   143 	// pick relervant mySQl QUERY
       
   144 	if ($view_mode == "public")
       
   145 	{
       
   146 		$query = mysql_query("SELECT * FROM requests_tbl WHERE (uid = 1) ORDER BY " . $order_sql);
       
   147 	}
       
   148 	else if ($view_mode == "all")
       
   149 	{
       
   150 		$query = mysql_query("SELECT * FROM requests_tbl WHERE (uid = '$currID' OR uid = 1) ORDER BY " . $order_sql);
       
   151 	}
       
   152 	else 
       
   153 	{
       
   154 		$query = mysql_query("SELECT * FROM requests_tbl WHERE (uid = '$currID') ORDER BY " . $order_sql);
       
   155 	}
       
   156 	
       
   157 	
       
   158 	// Construct search information message
       
   159 	if ($view_mode == "public") 
       
   160 	{
       
   161 		$search_info = "There are " . mysql_num_rows($query) . " saved public searches.";
       
   162 	} else if ($view_mode == "private") {
       
   163 		$search_info = "You have " . mysql_num_rows($query) . " saved private searches.";
       
   164 	} else {
       
   165 		$search_info = "There are " . mysql_num_rows($query) . " saved searches.";
       
   166 	}
       
   167 ?>
       
   168 
       
   169 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
   170 <html xmlns="http://www.w3.org/1999/xhtml">
       
   171 <head>
       
   172 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
   173 <title>N4C - Welcome. <?php print $search_info; ?></title>
       
   174 <link href="files/n4c_main.css" rel="stylesheet" type="text/css" media="screen" />
       
   175 </head>
       
   176 
       
   177 <body>
       
   178 <div id="container">
       
   179     <div id="header">
       
   180         <h1><span>N4C</span></h1>
       
   181     	<p id="credits">HTML Requester v. 1.00</p>
       
   182     </div>
       
   183     <?php 
       
   184 		// show status messages based on user interaction
       
   185 		if ($status == "del") 
       
   186 		{
       
   187 			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>";
       
   188 		} else if ($status == "add")
       
   189 		{
       
   190 			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>";
       
   191 		} else if ($status == "urlerror") 
       
   192 		{
       
   193 			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>";
       
   194 		}
       
   195 	?>
       
   196     <div id="search">
       
   197    	  <h2>Open a new search:</h2>
       
   198         <form action="add_search.php" method="get" enctype="multipart/form-data" name="search" target="_parent">
       
   199         <input name="search_term" type="text" value="http://www.yourwebsite.com" size="21" />
       
   200         <?php
       
   201 			// 
       
   202 		  	if ($currID != 1)
       
   203 			{
       
   204 				print "<select name='search_type' size='1'>";
       
   205           		print "<option value='public' selected>Public</option>";
       
   206             	print "<option value='private'>Private</option></select>";	
       
   207 			}
       
   208 		  ?>
       
   209           <input id="search_button" name="submit" type="submit" value="Search" />
       
   210         </form>
       
   211     </div>
       
   212     <div id="results">
       
   213     	<h2><?php print $search_info; ?></h2>
       
   214 		<?php
       
   215             // customise view links based on view
       
   216             
       
   217 			print "<div id='view_options'><form action='details.php' method='get' enctype='multipart/form-data' name='search' target='_parent'>";
       
   218 			
       
   219 			if ($currID != 1)
       
   220             {
       
   221                 print "<select name='view_mode' size='1'>";
       
   222                 print "<option value='private' selected>Private searches</option>";
       
   223                 print "<option value='public'>Public searches</option>";
       
   224                 print "<option value='all'>All searches</option></select> ";
       
   225             }
       
   226 			
       
   227 			print "<select name='view_order' size='1'>";
       
   228             print "<option value='date_desc' selected>Newest &gt; Oldest</option>";
       
   229             print "<option value='date_asc'>Oldest &gt; Newest</option>";
       
   230             print "<option value='avail'>Available &gt; Pending</option> ";
       
   231 			print "<option value='pend'>Pending &gt; Available</option></select> ";
       
   232             print "<input name='submit' type='submit' value='Update view' /></form></div>";
       
   233         ?>
       
   234 		<?php
       
   235             //display current search requests
       
   236             $search_count = 1;
       
   237 			
       
   238 			while ($db_field = mysql_fetch_assoc($query)) 
       
   239 			{		 
       
   240 				if ($db_field['uid'] != 1 and $view_mode == "all")
       
   241 				{
       
   242 					print "<div class='search_result_private'><span class='result_no'>" . $search_count . "</span> ";
       
   243 					print "<span class='search_txt'>Search:</span><span class='search_input'><em>" . $db_field['req_val'] . "</em></span> ";
       
   244 				} else {
       
   245 					print "<div class='search_result'><span class='result_no'>" . $search_count . "</span> ";
       
   246 					print "<span class='search_txt'>Search:</span><span class='search_input'><em>" . $db_field['req_val'] . "</em></span> ";
       
   247 				}
       
   248 				
       
   249   				print "<span class='results_txt'>Status:</span>";
       
   250 				
       
   251 				if ($db_field['req_response'] == 0)
       
   252 				{
       
   253 					print "<span class='result_output_none'>Result pending</span>";
       
   254 					print "<a href='delete.php?req_id=" . $db_field['req_ID'] . "'><img src='images/x.gif' width='14' height='20' /></a>";
       
   255 				} else {
       
   256 //header ('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . dirname($db_fieldcache["req_ID"]));
       
   257 //strip_tags($text, '<p><a>')
       
   258                                       print "<span class='result_output'><a href='". 'router_stage/' . $db_field['req_ID'] . '/' . remove_http($db_field['req_val']) ."' target='_blank'>View website</a></span>";
       
   259 //					print "<span class='result_output'><a href='" . $db_field['req_val'] ."' target='_blank'>View website</a></span>";
       
   260 					print "<a href='delete.php?req_id=" . $db_field['req_ID'] . "'><img src='images/x.gif' width='14' height='20' /></a>";
       
   261 				}
       
   262                 print "<div class='clear'></div></div>";
       
   263 				
       
   264 				$search_count++;
       
   265             }
       
   266             
       
   267             // if no requests, display message
       
   268             if (mysql_num_rows($query) < 1) 
       
   269             {
       
   270                 print '<p>You have no previous searches please use the search form to create a new search.</p>';
       
   271             }
       
   272             
       
   273             mysql_close();
       
   274         ?>
       
   275     </div>
       
   276 </div>
       
   277 </body>
       
   278 </html>