Fixed quickstats
[clearscm.git] / maps / php / MAPS.php
index b718935..6ccc76d 100755 (executable)
@@ -34,9 +34,13 @@ $Types = array (
   "nulllist"
 );
 
+$db;
+
 function DBError($msg, $statement) {
-  $errno  = mysql_errno();
-  $errmsg = mysql_error();
+  global $db;
+
+  $errno  = mysqli_errno($db);
+  $errmsg = mysqli_error($db);
   print "$msg<br>Error # $errno $errmsg";
   print "<br>SQL Statement: $statement";
 
@@ -44,13 +48,23 @@ function DBError($msg, $statement) {
 } // DBError
 
 function OpenDB() {
-  $db = mysql_connect("localhost", "mapsadmin", "mapsadmin")
+  global $db;
+
+  $db = mysqli_connect("127.0.0.1", "maps", "spam")
     or DBError("OpenDB: Unable to connect to database server", "Connect");
 
-  mysql_select_db("MAPS")
+  mysqli_select_db($db, "MAPS")
     or DBError("OpenDB: Unable to select MAPS database", "adefaria_maps");
 } // OpenDB
 
+function CloseDB() {
+  global $db;
+
+  if (isset ($db)) {
+    mysqli_close($db);
+  } // if
+} // CloseDB
+
 function SetContext($new_userid) {
   global $userid;
 
@@ -58,24 +72,28 @@ function SetContext($new_userid) {
 } // SetContext
 
 function Encrypt($password, $userid) {
-  $statement = "select encode(\"$password\",\"$userid\")";
+  global $db;
+
+  $statement = "select hex(aes_encrypt(\"$password\",\"$userid\"))";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("Encrypt: Unable to execute statement", $statement);
 
   // Get return value, which should be the encoded password
-  $row = mysql_fetch_array($result);
+  $row = mysqli_fetch_array($result);
 
   return $row[0];
 } // Encrypt
 
 function UserExists($userid) {
+  global $db;
+
   $statement = "select userid, password from user where userid = \"$userid\"";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError ("UserExists: Unable to execute statement", $statement);
 
-  $row = mysql_fetch_array($result);
+  $row = mysqli_fetch_array($result);
 
   $dbuserid   = $row["userid"];
   $dbpassword = $row["password"];
@@ -92,6 +110,7 @@ function Login($userid, $password) {
 
   // Check if user exists
   $dbpassword = UserExists($userid);
+  print "dbpassword = $dbpassword<br>";
 
   // Return -1 if user doesn't exist
   if ($dbpassword == -1) {
@@ -109,15 +128,15 @@ function Login($userid, $password) {
 } // Login
 
 function CountList ($type) {
-  global $userid;
+  global $userid, $db;
 
   $statement = "select count(*) as count from list where type=\"$type\" and userid=\"$userid\"";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("CountList: Unable to count list: ", $statement);
 
   // How many rows are there?
-  $row = mysql_fetch_array($result);
+  $row = mysqli_fetch_array($result);
 
   return $row["count"];
 } // CountList
@@ -129,10 +148,10 @@ function FindList($type, $next, $lines) {
 
   $statement = "select * from list where type=\"$type\" and userid=\"$userid\" order by sequence limit $next, $lines";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError ("FindList: Unable to execute query: ", $statement);
 
-  $count = mysql_num_rows($result);
+  $count = mysqli_num_rows($result);
 
   return array($count, $result);
 } // FindList
@@ -142,13 +161,15 @@ function Today2SQLDatetime() {
 } // Today2SQLDatetime
 
 function countem($table, $condition) {
+  global $db;
+
   $statement = "select count(distinct sender) as count from $table where $condition";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("countem: Unable to perform query: ", $statement);
 
   // How many rows are there?
-  $row = mysql_fetch_array($result);
+  $row = mysqli_fetch_array($result);
 
   return $row["count"];
 } // countem
@@ -196,12 +217,12 @@ function GetStats($nbr_days, $date = "") {
 function displayquickstats() {
   $today = substr (Today2SQLDatetime(), 0, 10);
   $dates = getquickstats($today);
-  $current_time = date("g:i a");
+  $current_time = date("g:i:s a");
 
   // Start quickstats
-  print "<div class=quickstats>";
-  print "<h4 align=center class=header>Today's Activity</h4>";
-  print "<p align=center><b>as of $current_time</b></p>";
+  print "<div class=\"quickstats\">";
+  print "<h4 align=\"center\" class=\"todaysactivity\">Today's Activity</h4>";
+  print "<p align=\"center\"><b>as of $current_time</b></p>";
 
   $processed     = $dates[$today]["processed"];
   $returned      = $dates[$today]["returned"];
@@ -219,20 +240,21 @@ function displayquickstats() {
   $nulllist_pct  = $processed == 0 ? 0 :
     number_format ($nulllist / $processed * 100, 1, ".", "");
 
-  $returned_link = $returned == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=returned;date=$today>$returned</a>";
-  $whitelist_link = $whitelist == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=whitelist;date=$today>$whitelist</a>";
-  $blacklist_link = $blacklist == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=blacklist;date=$today>$blacklist</a>";
-  $registered_link = $registered == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=registered;date=$today>$registered</a>";
-  $mailloop_link = $mailloop == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=mailloop;date=$today>$mailloop</a>";
-  $nulllist_link = $nulllist == 0 ? 0 :
-    "<a href=/maps/bin/detail.cgi?type=nulllist;date=$today>$nulllist</a>";
+  $returned_link = $returned == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=returned;date=$today\">";
+  $whitelist_link = $whitelist == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=whitelist;date=$today\">";
+  $blacklist_link = $blacklist == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=blacklist;date=$today\">";
+  $registered_link = $registered == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=registered;date=$today\">";
+  $mailloop_link = $mailloop == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=mailloop;date=$today>\"";
+  $nulllist_link = $nulllist == 0 ? '' :
+    "<a href=\"/maps/bin/detail.cgi?type=nulllist;date=$today\">";
 
 print <<<EOT
+<div id="quickwrap">
 <table cellpadding="2" border="0" align="center" cellspacing="0">
   <tr align="right">
     <td align="right" class="smalllabel">Processed</td>
@@ -240,37 +262,38 @@ print <<<EOT
     <td align="right" class="smallnumber">n/a</td>
   </tr>
   <tr align="right">
-    <td class="smalllabel">Returned</td>
-    <td class=smallnumber>$returned_link
+    <td class="link">${nulllist_link}Nulllist</a></td>
+    <td class="smallnumber">$nulllist</td>
+    <td class="smallnumber">$nulllist_pct%</td>
+  </tr>
+  <tr align="right">
+    <td class="link">${returned_link}Returned</a></td>
+    <td class=smallnumber>$returned</td>
     <td class="smallnumber">$returned_pct%</td>
   </tr>
   <tr align="right">
-    <td class="smalllabel">Whitelist</td>
-    <td class="smallnumber">$whitelist_link
+    <td class="link">${whitelist_link}Whitelist</a></td>
+    <td class="smallnumber">$whitelist</td>
     <td class="smallnumber">$whitelist_pct%</td>
   </tr>
   <tr align="right">
-    <td class="smalllabel">Blacklist</td>
-    <td class="smallnumber">$blacklist_link
+    <td class="link">${blacklist_link}Blacklist</a></td>
+    <td class="smallnumber">$blacklist</td>
     <td class="smallnumber">$blacklist_pct%</td>
   </tr>
   <tr align="right">
-    <td class="smalllabel">Registered</td>
-    <td class="smallnumber">$registered_link
+    <td class="link">${registered_link}Registered</a></td>
+    <td class="smallnumber">$registered</td>
     <td class="smallnumber">n/a</td>
   </tr>
   <tr align="right">
-    <td class="smalllabel">Mailloop</td>
-    <td class="smallnumber">$mailloop_link
+    <td class="link">${mailloop_link}Mailloop</a></td>
+    <td class="smallnumber">$mailloop</td>
     <td class="smallnumber">n/a</td>
   </tr>
-  <tr align="right">
-    <td class="smalllabel">Nulllist</td>
-    <td class="smallnumber">$nulllist_link
-    <td class="smallnumber">$nulllist_pct%</td>
-  </tr>
 </table>
 </div>
+</div>
 EOT;
 } // displayquickstats
 
@@ -295,7 +318,8 @@ function NavigationBar($userid) {
 
   if (!isset ($userid) || $userid == "") {
     print <<<END
-  <div class="username">Welcome to MAPS</div>
+    <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
+    <div class="username">Welcome to MAPS</div>
     <div class="menu">
     <a href="/maps/doc/">What is MAPS?</a><br>
     <a href="/maps/doc/SPAM.php">What is SPAM?</a><br>
@@ -308,20 +332,24 @@ END;
   } else {
     $Userid = ucfirst($userid);
     print <<<END
-  <div class="username">Welcome $Userid</div>
+    <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
+    <div class="username">Welcome $Userid</div>
     <div class="menu">
-    <a href="/maps/">MAPS Home</a><br>
+    <a href="/maps/">Home</a><br>
     <a href="/maps/bin/stats.cgi">Statistics</a><br>
-    <a href="/maps/bin/editprofile.cgi">Edit Profile</a><br>
+    <a href="/maps/bin/editprofile.cgi">Profile</a><br>
     <a href="/maps/php/Reports.php">Reports</a><br>
-    <a href="/maps/php/list.php?type=white">White List</a><br>
-    <a href="/maps/php/list.php?type=black">Black List</a><br>
-    <a href="/maps/php/list.php?type=null">Null List</a><br>
+    <a href="/maps/php/list.php?type=white">White</a><br>
+    <a href="/maps/php/list.php?type=black">Black</a><br>
+    <a href="/maps/php/list.php?type=null">Null</a><br>
     <a href="/maps/doc/">Help</a><br>
-    <a href="/maps/adm/">MAPS Admin</a><br>
+    <a href="/maps/adm/">Admin</a><br>
     <a href="/maps/?logout=yes">Logout</a>
     </div>
 END;
+
+    displayquickstats();
+
     print <<<END
   <div class="search">
   <form method="get" action="/maps/bin/search.cgi" name="search">
@@ -332,8 +360,6 @@ END;
   </div>
 END;
 
-    displayquickstats();
-
     print <<<END
   <div class="search">
   <form "method"=post action="javascript://" name="address"
@@ -342,6 +368,7 @@ END;
     <input type="text" class="searchfield" id="searchfield" name="email"
      size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
   </form>
+  <p></p>
   </div>
 END;
   } // if
@@ -350,16 +377,16 @@ END;
 } # NavigationBar
 
 function GetUserLines() {
-  global $userid;
+  global $userid, $db;
 
   $lines = 10;
 
   $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("GetUserLines: Unable to execute query: ", $statement);
 
-  $row = mysql_fetch_array ($result);
+  $row = mysqli_fetch_array ($result);
 
   if (isset ($row["value"])) {
     $lines = $row["value"];
@@ -372,16 +399,17 @@ function DisplayList($type, $next, $lines) {
   global $userid;
   global $total;
   global $last;
+  global $db;
 
   $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("DisplayList: Unable to execute query: ", $statement);
 
   for ($i = 0; $i < $lines; $i++) {
-    $row = mysql_fetch_array ($result);
+    $row = mysqli_fetch_array($result);
 
-    if (!isset ($row ["sequence"])) {
+    if (!isset ($row["sequence"])) {
       break;
     } // if
 
@@ -390,6 +418,7 @@ function DisplayList($type, $next, $lines) {
     $domain    = $row["domain"]    == "" ? "&nbsp;" : $row["domain"];
     $hit_count = $row["hit_count"] == "" ? "&nbsp;" : $row["hit_count"];
     $last_hit  = $row["last_hit"]  == "" ? "&nbsp;" : $row["last_hit"];
+    $retention = $row["retention"] == "" ? "&nbsp;" : $row["retention"];
     $comments  = $row["comment"]   == "" ? "&nbsp;" : $row["comment"];
 
     // Remove time from last hit
@@ -406,13 +435,13 @@ function DisplayList($type, $next, $lines) {
     $rightclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
       "tablebottomright" : "tablerightdata";
 
-    print "<td class=$leftclass align=center>"  . $sequence  . "</td>";
-    print "<td class=$dataclass align=center><input type=checkbox name=action" . $sequence . " value=on></td>\n";
+    print "<td class=$leftclass align=right>"   . $sequence  . "<input type=checkbox name=action" . $sequence . " value=on></td>\n";
     print "<td class=$dataclass align=right>"   . $username  . "</td>";
     print "<td class=$dataclass align=center>@</td>";
     print "<td class=$dataclass align=left><a href=\"http://$domain\" target=_blank>$domain</a></td>";
     print "<td class=$dataclass align=right>"   . $hit_count . "</td>";
     print "<td class=$dataclass align=center>"  . $last_hit  . "</td>";
+    print "<td class=$dataclass align=right>"   . $retention . "</td>";
     print "<td class=$rightclass align=left>"   . $comments  . "</td>";
     print "</tr>";
   } // for
@@ -435,7 +464,7 @@ END;
 } // MAPSHeader
 
 function ListDomains($top = 10) {
-  global $userid;
+  global $userid, $db;
 
   // Generate a list of the top 10 spammers by domain
   $statement = "select count(sender) as nbr, ";
@@ -447,7 +476,7 @@ function ListDomains($top = 10) {
   $statement = $statement . "group by domain order by nbr desc";
 
   // Do the query
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("ListDomains: Unable to execute query: ", $statement);
 
   print <<<END
@@ -462,7 +491,7 @@ END;
 
   // Get results
   for ($i = 0; $i < $top; $i++) {
-    $row = mysql_fetch_array ($result);
+    $row = mysqli_fetch_array ($result);
     $domain = $row["domain"];
     $nbr    = $row["nbr"];
 
@@ -471,13 +500,13 @@ END;
     if ($i < $top - 1) {
       print "<td class=tableleftdata align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
       print "<td align=center class=tabledata>" . $ranking . "</td>";
-      print "<td class=tabledata>$domain</td>";
+      print "<td class=tabledata><a href=\"http://$domain\">$domain</as></td>";
       print "<input type=hidden name=email$i value=\"@$domain\">";
       print "<td align=center class=tablerightdata>$nbr</td>";
     } else {
       print "<td class=tablebottomleft align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
       print "<td align=center class=tablebottomdata>" . $ranking . "</td>";
-      print "<td class=tablebottomdata>$domain</td>";
+      print "<td class=tablebottomdata><a href=\"http://$domain\">$domain</a></td>";
       print "<input type=hidden name=email$i value=\"@$domain\">";
       print "<td align=center class=tablebottomright>$nbr</td>";
     } // if
@@ -486,7 +515,7 @@ END;
 
   print <<<END
   <tr>
-    <td align=center colspan=4><input type="submit" name="action" value="Nulllist Marked" onclick="return CheckAtLeast1Checked (document.domains);" /><input type="submit" name="action" value="Reset Marks" onclick="return ClearAll (document.domains);" />
+    <td align=center colspan=4><input type="submit" name="action" value="Nulllist" onclick="return CheckAtLeast1Checked (document.domains);" /><input type="submit" name="action" value="Reset" onclick="return ClearAll (document.domains);" />
     </td>
   </tr>
 <table>
@@ -494,26 +523,28 @@ END;
 } // ListDomains
 
 function Space() {
-  global $userid;
+  global $userid, $db;
 
   // Tally up space used by $userid
   $space = 0;
 
   $statement = "select * from email where userid = \"$userid\"";
 
-  $result = mysql_query($statement)
+  $result = mysqli_query($db, $statement)
     or DBError("Space: Unable to execute query: ", $statement);
 
-  while ($row = mysql_fetch_array ($result)) {
+  while ($row = mysqli_fetch_array ($result)) {
     $msg_space =
       strlen($row["userid"])    +
       strlen($row["sender"])    +
       strlen($row["subject"])   +
       strlen($row["timestamp"]) +
       strlen($row["data"]);
-    $space = $space + $msg_space;
+    $space += $msg_space;
   } // while
 
+  mysqli_free_result($result);
+
   return $space;
 } // Space
 ?>