<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head> <title>Feedback</title> </head>
<!-- $Id: interest.php 15 2004-12-30 02:25:12Z basile $ -->
<!-- Copyright 2004 Basile STARYNKEVITCH -->
<body>
<h1>Thanks for your feedback</h1>
<?php
// this interest.php file is
//   Copyright © 2004 Basile STARYNKEVITCH
//
// This Software is licensed under the GPL licensed Version 2, 
// please read http://www.gnu.org/copyleft/gpl.html

/* you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software
 * Foundation; either version 2 of the License.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

require 'interest_config.inc';
// the included file defines:
///  for opening the database
///  $int_db_name $int_db_host $int_db_user $int_db_passwd 
///  the name of the rate and access tables $int_tb_rate $int_tb_access
///  $int_webuser the user part of the email address
///  $int_webdomain the domain part of the email address
//// example
// $int_db_name="interest_db";
// $int_db_host="localhost";
// $int_db_user="phpuser";
// $int_db_passwd="secretpassword";
// $int_tb_rate="Tb_Rate";
// $int_tb_access="Tb_Access";
// $int_webuser="webmaster";
// $int_webdomain="exampledomain.org";

function query($query,$descr) {
  if (!($res=mysql_query($query))) {
    echo "<p>Failed mysql query:&nbsp; <tt>"
      . htmlspecialchars($query) . "</tt><br>\n";
    die("mysql query " .$descr . " failed: " . mysql_error());
  };
  return $res;
}
?>

<?php 
echo "<p>Please email any suggestions about this feedback page (giving its URL) to\n " 
     . "<a href='mailto:$int_webuser" 
     . ".NOSPAM@"
     . "$int_webdomain.invalid'> $int_webuser.NOSPAM <tt>at</tt> $int_webdomain.invalid</a>\n"
. " (but remove <tt>.NOSPAM</tt> and <tt>.invalid</tt> from the email address).</p>\n";
?>


<?php
// fetch data from the HTTP request
$clientip = $_SERVER['REMOTE_ADDR'];
$pagename = $_POST['page'];
$pagerev = $_POST['rev'];
$pagerate = $_POST['rate'];

// current time
$now=date("Y.M.d-h:i:s");


$link = mysql_connect($int_db_host, $int_db_user, $int_db_passwd)
     or die("Could not connect : " . mysql_error());  
 
mysql_select_db($int_db_name) 
     or die("Could not select database $int_db_name:" . mysql_error());

/// create the tables if they dont exist
query("CREATE TABLE IF NOT EXISTS $int_tb_rate("
      . " pagename VARCHAR(128) NOT NULL PRIMARY KEY, "
      . " url VARCHAR(200) NOT NULL, "
      . " r1 INT DEFAULT 0, r2 INT DEFAULT 0, r3 INT DEFAULT 0, r4 INT DEFAULT 0) "
      . "TYPE=InnoDB", 
      "rate table creation");

query("CREATE TABLE IF NOT EXISTS $int_tb_access("
	    . " clientip VARCHAR(24) NOT NULL, "
            . " pagename VARCHAR(128) NOT NULL, "
	    . " INDEX (clientip,pagename), "
	    . " tim TIMESTAMP NOT NULL) TYPE=InnoDB",
      "access table creation"); 


/// check data
if (!preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/", $clientip)) 
     die("invalid clientip '". addslashes($clientip) . "'");
  
if (!preg_match("/^[a-zA-Z0-9_]+$/", $pagename))
     die("invalid pagename '" . addslashes($pagename) . "'");

if (!preg_match("/^[1-4]$/", $pagerate))
     die("invalid pagerate '". addslashes($pagerate) . "'");

     
/// check that the page is known (by querying its rate count)
$res=query("SELECT COUNT(*) AS nbpage FROM $int_tb_rate WHERE pagename='$pagename'",
     "npage request");

(mysql_num_rows($res)>0) or die("unknown pagename '$pagename'");

mysql_free_result($res);

/// ask for previous feedback from same client for same page 

$res=query("SELECT tim<DATE_ADD(NOW(),INTERVAL -20 MINUTE) AS old, "
	   . " DATE_FORMAT(tim,'%Y/%M/%d@%H:%i:%S') AS tim "
	   . " FROM $int_tb_access WHERE clientip='$clientip' AND pagename='$pagename'",
	   "previous feedback");

     
     
$good=TRUE;

// tell about previous feedback
while ($row = mysql_fetch_array($res)) {
  if ($row[0]=="0") {
    $good=FALSE;
    $atim=$row[1];
    echo "<p>You already gave feedback about page <tt>$pagename</tt> on $atim from client $clientip.</p>";
  }
};

mysql_free_result($res);

// delete all access more than a week old
query("DELETE LOW_PRIORITY FROM $int_tb_access WHERE tim<DATE_ADD(NOW(),INTERVAL 8 DAY)",
      "forget old access");

if ($good) {

  query("INSERT INTO $int_tb_access SET clientip='$clientip', pagename='$pagename', tim=NOW()",
	"add current access");

  /// $rate is either 1 2 3 or 4 so r$rate is r1 r2 r3 or r4
  query("UPDATE $int_tb_rate SET r$rate=(r$rate+1) WHERE pagename='$pagename'",
	"update rate r$rate");

    echo "<p>You rated the page <tt>$pagename</tt> as $rate (1 is best, 4 is worst) " . 
      "from IP $clientip on $now</p>\n";

} else {
  echo "<p>Your feedback from IP $clientip is <em>ignored</em> on $now"
    . " <small>(maybe you share a browser or a proxy with someone else)</small> !</p>\n";
};

?>
<hr>
<?php
$res=query("SELECT r1,r2,r3,r4,url FROM $int_tb_rate WHERE pagename='$pagename'",
	   "rate fetching query");
while ($row = mysql_fetch_array($res)) {
  $r1=$row[0];  $r2=$row[1];  $r3=$row[2];  $r4=$row[3]; $url=$row[4];
  $sum = $r1 + $r2 + $r3 + $r4;
  if ($sum>0)
    $mean= round(($r1 + 2*$r2 + 3*$r3 + 4*$r4)/$sum,2);
  else { $sum=0.01; $mean=0; };

  echo "<p>the page (ref. <tt>$pagename</tt>";
  if ($url) 
    echo " at <a href='$url'>" . htmlspecialchars($url) . "</a>";
  
  echo ") has been rated $sum times (mean rate: $mean).<br>\n";
  echo "<em>rate 1</em> <small>(best)</small>: $r1 times&nbsp"
    . "=" . round(100.0*$r1/$sum,1) 
    . "%; \n";
  echo "<em>rate 2</em>: $r2 times&nbsp;"
    . "=" . round(100.0*$r2/$sum,1) 
    . "%; \n";
  echo "<em>rate 3</em>: $r3 times&nbsp;"
    . "=" . round(100.0*$r3/$sum,1) 
    . "%; \n";
  echo "<em>rate 4</em> <small>(worst)</small>: $r4 times&nbsp;"
    . "=" . round(100.0*$r4/$sum,1) 
    . "%; \n";
  echo "</p>\n";
};

mysql_free_result($res);
?>

<p>Don't give too much attention to the figures above. They might be inaccurate.
Consecutive feedbacks from the same browsing machine (or proxy?) are ignored if given too often.
</p>

<p>If you want, give additional feedback thru the form below:

<form action="mailfeedback.php" method="post">
<b>Your email:</b> <input type="text" name="email" size='64' maxlength='80'> 
<small>(required eg <tt> your.name1@your-domain.com </tt> )</small><br>

<?php 
   echo "<input type='hidden' name='page' value='feedback.$pagename'>\n"; 
?>

<b>Comment:</b> <textarea name="body" rows="15" cols="70"></TEXTAREA><br>
<input type="submit" value="Send." />&nbsp; 
<input type="reset" value="Clear." />
</body>
<!-- eof $Id: interest.php 15 2004-12-30 02:25:12Z basile $ -->
</html>
