Fixed quickstats
[clearscm.git] / maps / php / MAPS.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        $RCSFile$
5 // Revision:    $Revision: 1.1 $
6 // Description: Main PHP module to MAPS
7 // Author:      Andrew@DeFaria.com
8 // Created:     Fri Nov 29 14:17:21  2002
9 // Modified:    $Date: 2013/06/12 14:05:48 $
10 // Language:    PHP
11 //
12 // (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
13 //
14 ////////////////////////////////////////////////////////////////////////////////
15 // Get userid
16 if (isset($_REQUEST["userid"])) {
17   $userid = $_REQUEST["userid"];
18 } // if
19 $from_cookie = false;
20
21 if (!isset($userid)) {
22   // No userid, see if we have a cookie for it
23   $userid=$_COOKIE["MAPSUser"];
24   $from_cookie = true;
25 } // if
26
27 $lines = 10;
28 $Types = array (
29   "returned",
30   "whitelist",
31   "blacklist",
32   "registered",
33   "mailloop",
34   "nulllist"
35 );
36
37 $db;
38
39 function DBError($msg, $statement) {
40   global $db;
41
42   $errno  = mysqli_errno($db);
43   $errmsg = mysqli_error($db);
44   print "$msg<br>Error # $errno $errmsg";
45   print "<br>SQL Statement: $statement";
46
47   exit($errno);
48 } // DBError
49
50 function OpenDB() {
51   global $db;
52
53   $db = mysqli_connect("127.0.0.1", "maps", "spam")
54     or DBError("OpenDB: Unable to connect to database server", "Connect");
55
56   mysqli_select_db($db, "MAPS")
57     or DBError("OpenDB: Unable to select MAPS database", "adefaria_maps");
58 } // OpenDB
59
60 function CloseDB() {
61   global $db;
62
63   if (isset ($db)) {
64     mysqli_close($db);
65   } // if
66 } // CloseDB
67
68 function SetContext($new_userid) {
69   global $userid;
70
71   $userid = $new_userid;
72 } // SetContext
73
74 function Encrypt($password, $userid) {
75   global $db;
76
77   $statement = "select hex(aes_encrypt(\"$password\",\"$userid\"))";
78
79   $result = mysqli_query($db, $statement)
80     or DBError("Encrypt: Unable to execute statement", $statement);
81
82   // Get return value, which should be the encoded password
83   $row = mysqli_fetch_array($result);
84
85   return $row[0];
86 } // Encrypt
87
88 function UserExists($userid) {
89   global $db;
90
91   $statement = "select userid, password from user where userid = \"$userid\"";
92
93   $result = mysqli_query($db, $statement)
94     or DBError ("UserExists: Unable to execute statement", $statement);
95
96   $row = mysqli_fetch_array($result);
97
98   $dbuserid   = $row["userid"];
99   $dbpassword = $row["password"];
100
101   if ($dbuserid != $userid) {
102     return -1;
103   } else {
104     return $dbpassword;
105   } # if
106 } // UserExists
107
108 function Login($userid, $password) {
109   $password = Encrypt($password, $userid);
110
111   // Check if user exists
112   $dbpassword = UserExists($userid);
113   print "dbpassword = $dbpassword<br>";
114
115   // Return -1 if user doesn't exist
116   if ($dbpassword == -1) {
117     return -1;
118   } // if
119
120   // Return -2 if password does not match
121   if ($password != $dbpassword) {
122     return -2;
123   } else {
124     setcookie("MAPSUser", $userid, time()+60*60*24*30, "/maps");
125     SetContext($userid);
126     return 0;
127   } // if
128 } // Login
129
130 function CountList ($type) {
131   global $userid, $db;
132
133   $statement = "select count(*) as count from list where type=\"$type\" and userid=\"$userid\"";
134
135   $result = mysqli_query($db, $statement)
136     or DBError("CountList: Unable to count list: ", $statement);
137
138   // How many rows are there?
139   $row = mysqli_fetch_array($result);
140
141   return $row["count"];
142 } // CountList
143
144 function FindList($type, $next, $lines) {
145   global $db;
146   global $userid;
147   global $lines;
148
149   $statement = "select * from list where type=\"$type\" and userid=\"$userid\" order by sequence limit $next, $lines";
150
151   $result = mysqli_query($db, $statement)
152     or DBError ("FindList: Unable to execute query: ", $statement);
153
154   $count = mysqli_num_rows($result);
155
156   return array($count, $result);
157 } // FindList
158
159 function Today2SQLDatetime() {
160   return date ("Y-m-d H:i:s");
161 } // Today2SQLDatetime
162
163 function countem($table, $condition) {
164   global $db;
165
166   $statement = "select count(distinct sender) as count from $table where $condition";
167
168   $result = mysqli_query($db, $statement)
169     or DBError("countem: Unable to perform query: ", $statement);
170
171   // How many rows are there?
172   $row = mysqli_fetch_array($result);
173
174   return $row["count"];
175 } // countem
176
177 function countlog($condition="") {
178   global $userid;
179
180   if ($condition != "") {
181     return countem("log", "userid=\"$userid\" and " . $condition);
182   } else {
183     return countem("log", "userid=\"$userid\"");
184   } // if
185 } // countlog
186
187 function SubtractDays($date, $nbr_days) {
188
189 } // SubtractDays
190
191 function GetStats($nbr_days, $date = "") {
192   global $Types;
193
194   if ($date == "") {
195     $date = Today2SQLDatetime();
196   } // if
197
198   while ($nbr_days > 0) {
199     $ymd = substr($date, 0, 10);
200     $sod = $ymd . " 00:00:00";
201     $eod = $ymd . " 23:59:59";
202
203     foreach ($Types as $type) {
204       $condition = "type=\"$type\" and (timestamp > \"$sod\" and timestamp < \"$eod\")";
205       $stats[$type] = countlog($condition);
206     } # foreach
207
208     $dates[$ymd] = &$stats;
209
210     $date = SubtractDays($date, 1);
211     $nbr_days--;
212   } # while
213
214   return $dates;
215 } # GetStats
216
217 function displayquickstats() {
218   $today = substr (Today2SQLDatetime(), 0, 10);
219   $dates = getquickstats($today);
220   $current_time = date("g:i:s a");
221
222   // Start quickstats
223   print "<div class=\"quickstats\">";
224   print "<h4 align=\"center\" class=\"todaysactivity\">Today's Activity</h4>";
225   print "<p align=\"center\"><b>as of $current_time</b></p>";
226
227   $processed     = $dates[$today]["processed"];
228   $returned      = $dates[$today]["returned"];
229   $returned_pct  = $processed == 0 ? 0 :
230     number_format ($returned / $processed * 100, 1, ".", "");
231   $whitelist     = $dates[$today]["whitelist"];
232   $whitelist_pct = $processed == 0 ? 0 :
233     number_format ($whitelist / $processed * 100, 1, ".", "");
234   $blacklist     = $dates[$today]["blacklist"];
235   $blacklist_pct = $processed == 0 ? 0 :
236     number_format ($blacklist / $processed * 100, 1, ".", "");
237   $registered    = $dates[$today]["registered"];
238   $mailloop      = $dates[$today]["mailloop"];
239   $nulllist      = $dates[$today]["nulllist"];
240   $nulllist_pct  = $processed == 0 ? 0 :
241     number_format ($nulllist / $processed * 100, 1, ".", "");
242
243   $returned_link = $returned == 0 ? '' :
244     "<a href=\"/maps/bin/detail.cgi?type=returned;date=$today\">";
245   $whitelist_link = $whitelist == 0 ? '' :
246     "<a href=\"/maps/bin/detail.cgi?type=whitelist;date=$today\">";
247   $blacklist_link = $blacklist == 0 ? '' :
248     "<a href=\"/maps/bin/detail.cgi?type=blacklist;date=$today\">";
249   $registered_link = $registered == 0 ? '' :
250     "<a href=\"/maps/bin/detail.cgi?type=registered;date=$today\">";
251   $mailloop_link = $mailloop == 0 ? '' :
252     "<a href=\"/maps/bin/detail.cgi?type=mailloop;date=$today>\"";
253   $nulllist_link = $nulllist == 0 ? '' :
254     "<a href=\"/maps/bin/detail.cgi?type=nulllist;date=$today\">";
255
256 print <<<EOT
257 <div id="quickwrap">
258 <table cellpadding="2" border="0" align="center" cellspacing="0">
259   <tr align="right">
260     <td align="right" class="smalllabel">Processed</td>
261     <td align="right" class="smallnumber">$processed</td>
262     <td align="right" class="smallnumber">n/a</td>
263   </tr>
264   <tr align="right">
265     <td class="link">${nulllist_link}Nulllist</a></td>
266     <td class="smallnumber">$nulllist</td>
267     <td class="smallnumber">$nulllist_pct%</td>
268   </tr>
269   <tr align="right">
270     <td class="link">${returned_link}Returned</a></td>
271     <td class=smallnumber>$returned</td>
272     <td class="smallnumber">$returned_pct%</td>
273   </tr>
274   <tr align="right">
275     <td class="link">${whitelist_link}Whitelist</a></td>
276     <td class="smallnumber">$whitelist</td>
277     <td class="smallnumber">$whitelist_pct%</td>
278   </tr>
279   <tr align="right">
280     <td class="link">${blacklist_link}Blacklist</a></td>
281     <td class="smallnumber">$blacklist</td>
282     <td class="smallnumber">$blacklist_pct%</td>
283   </tr>
284   <tr align="right">
285     <td class="link">${registered_link}Registered</a></td>
286     <td class="smallnumber">$registered</td>
287     <td class="smallnumber">n/a</td>
288   </tr>
289   <tr align="right">
290     <td class="link">${mailloop_link}Mailloop</a></td>
291     <td class="smallnumber">$mailloop</td>
292     <td class="smallnumber">n/a</td>
293   </tr>
294 </table>
295 </div>
296 </div>
297 EOT;
298 } // displayquickstats
299
300 function getquickstats($date) {
301   global $Types;
302
303   $dates = GetStats(1, $date);
304
305   foreach ($Types as $type) {
306     if (isset ($dates[$date]["processed"])) {
307       $dates[$date]["processed"] += $dates[$date][$type];
308     } else {
309       $dates[$date]["processed"] = $dates[$date][$type];
310     } // if
311   } # foreach
312
313   return $dates;
314 } // getquickstats
315
316 function NavigationBar($userid) {
317   print "<div id=leftbar>";
318
319   if (!isset ($userid) || $userid == "") {
320     print <<<END
321     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
322     <div class="username">Welcome to MAPS</div>
323     <div class="menu">
324     <a href="/maps/doc/">What is MAPS?</a><br>
325     <a href="/maps/doc/SPAM.php">What is SPAM?</a><br>
326     <a href="/maps/doc/Requirements.php">Requirements</a><br>
327     <a href="/maps/SignupForm.html">Signup</a><br>
328     <a href="/maps/doc/Using.php">Using MAPS</a><br>
329     <a href="/maps/doc/">Help</a><br>
330     </div>
331 END;
332   } else {
333     $Userid = ucfirst($userid);
334     print <<<END
335     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
336     <div class="username">Welcome $Userid</div>
337     <div class="menu">
338     <a href="/maps/">Home</a><br>
339     <a href="/maps/bin/stats.cgi">Statistics</a><br>
340     <a href="/maps/bin/editprofile.cgi">Profile</a><br>
341     <a href="/maps/php/Reports.php">Reports</a><br>
342     <a href="/maps/php/list.php?type=white">White</a><br>
343     <a href="/maps/php/list.php?type=black">Black</a><br>
344     <a href="/maps/php/list.php?type=null">Null</a><br>
345     <a href="/maps/doc/">Help</a><br>
346     <a href="/maps/adm/">Admin</a><br>
347     <a href="/maps/?logout=yes">Logout</a>
348     </div>
349 END;
350
351     displayquickstats();
352
353     print <<<END
354   <div class="search">
355   <form method="get" action="/maps/bin/search.cgi" name="search">
356     Search Sender/Subject
357     <input type="text" class="searchfield" id="searchfield" name="str"
358      size="20" maxlength="255"  value="" onclick="document.search.str.value='';">
359   </form>
360   </div>
361 END;
362
363     print <<<END
364   <div class="search">
365   <form "method"=post action="javascript://" name="address"
366    onsubmit="checkaddress(this);">
367     Check Email Address
368     <input type="text" class="searchfield" id="searchfield" name="email"
369      size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
370   </form>
371   <p></p>
372   </div>
373 END;
374   } // if
375
376   print "</div>";
377 } # NavigationBar
378
379 function GetUserLines() {
380   global $userid, $db;
381
382   $lines = 10;
383
384   $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
385
386   $result = mysqli_query($db, $statement)
387     or DBError("GetUserLines: Unable to execute query: ", $statement);
388
389   $row = mysqli_fetch_array ($result);
390
391   if (isset ($row["value"])) {
392     $lines = $row["value"];
393   } // if
394
395   return $lines;
396 } // GetUserLines
397
398 function DisplayList($type, $next, $lines) {
399   global $userid;
400   global $total;
401   global $last;
402   global $db;
403
404   $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
405
406   $result = mysqli_query($db, $statement)
407     or DBError("DisplayList: Unable to execute query: ", $statement);
408
409   for ($i = 0; $i < $lines; $i++) {
410     $row = mysqli_fetch_array($result);
411
412     if (!isset ($row["sequence"])) {
413       break;
414     } // if
415
416     $sequence  = $row["sequence"];
417     $username  = $row["pattern"]   == "" ? "&nbsp;" : $row["pattern"];
418     $domain    = $row["domain"]    == "" ? "&nbsp;" : $row["domain"];
419     $hit_count = $row["hit_count"] == "" ? "&nbsp;" : $row["hit_count"];
420     $last_hit  = $row["last_hit"]  == "" ? "&nbsp;" : $row["last_hit"];
421     $retention = $row["retention"] == "" ? "&nbsp;" : $row["retention"];
422     $comments  = $row["comment"]   == "" ? "&nbsp;" : $row["comment"];
423
424     // Remove time from last hit
425     $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
426
427     // Reformat last_hit
428     $last_hit = substr ($last_hit, 5, 2) . "/" .
429                 substr ($last_hit, 8, 2) . "/" .
430                 substr ($last_hit, 0, 4);
431     $leftclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
432       "tablebottomleft" : "tableleftdata";
433     $dataclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
434       "tablebottomdata"  : "tabledata";
435     $rightclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
436       "tablebottomright" : "tablerightdata";
437
438     print "<td class=$leftclass align=right>"   . $sequence  . "<input type=checkbox name=action" . $sequence . " value=on></td>\n";
439     print "<td class=$dataclass align=right>"   . $username  . "</td>";
440     print "<td class=$dataclass align=center>@</td>";
441     print "<td class=$dataclass align=left><a href=\"http://$domain\" target=_blank>$domain</a></td>";
442     print "<td class=$dataclass align=right>"   . $hit_count . "</td>";
443     print "<td class=$dataclass align=center>"  . $last_hit  . "</td>";
444     print "<td class=$dataclass align=right>"   . $retention . "</td>";
445     print "<td class=$rightclass align=left>"   . $comments  . "</td>";
446     print "</tr>";
447   } // for
448 } // DisplayList
449
450 function MAPSHeader() {
451   print <<<END
452   <meta name="author" content="Andrew DeFaria <Andre@DeFaria.com>">
453   <meta name="MAPS" "Mail Authorization and Permission System">
454   <meta name="keywords" content="Eliminate SPAM, Permission based email, SPAM filtering system">
455   <meta http-equiv=Refresh content="900">
456   <link rel="icon" href="/maps/MAPS.png" type="image/png">
457   <link rel="SHORTCUT ICON" href="/maps/favicon.ico">
458   <link rel="stylesheet" type="text/css" href="/maps/css/MAPSStyle.css"/>
459   <script language="JavaScript1.2" src="/maps/JavaScript/MAPSUtils.js"
460    type="text/javascript"></script>
461   <script language="JavaScript1.2" src="/maps/JavaScript/CheckAddress.js"
462    type="text/javascript"></script>
463 END;
464 } // MAPSHeader
465
466 function ListDomains($top = 10) {
467   global $userid, $db;
468
469   // Generate a list of the top 10 spammers by domain
470   $statement = "select count(sender) as nbr, ";
471   // Must extract domain from sender...
472   $statement = $statement . "substring(sender, locate(\"@\",sender, 1)+1) as domain ";
473   // From email for the current userid...
474   $statement = $statement . "from email where userid=\"$userid\" ";
475   // Group things by domain but order them descending on nbr...
476   $statement = $statement . "group by domain order by nbr desc";
477
478   // Do the query
479   $result = mysqli_query($db, $statement)
480     or DBError("ListDomains: Unable to execute query: ", $statement);
481
482   print <<<END
483   <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
484     <tr>
485       <th class="tableleftend">Mark</th>
486       <th class="tableheader">Ranking</th>
487       <th class="tableheader">Domain</th>
488       <th class="tablerightend">Returns</th>
489     </tr>
490 END;
491
492   // Get results
493   for ($i = 0; $i < $top; $i++) {
494     $row = mysqli_fetch_array ($result);
495     $domain = $row["domain"];
496     $nbr    = $row["nbr"];
497
498     print "<tr>";
499     $ranking = $i + 1;
500     if ($i < $top - 1) {
501       print "<td class=tableleftdata align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
502       print "<td align=center class=tabledata>" . $ranking . "</td>";
503       print "<td class=tabledata><a href=\"http://$domain\">$domain</as></td>";
504       print "<input type=hidden name=email$i value=\"@$domain\">";
505       print "<td align=center class=tablerightdata>$nbr</td>";
506     } else {
507       print "<td class=tablebottomleft align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
508       print "<td align=center class=tablebottomdata>" . $ranking . "</td>";
509       print "<td class=tablebottomdata><a href=\"http://$domain\">$domain</a></td>";
510       print "<input type=hidden name=email$i value=\"@$domain\">";
511       print "<td align=center class=tablebottomright>$nbr</td>";
512     } // if
513     print "</tr>";
514   } // for
515
516   print <<<END
517   <tr>
518     <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);" />
519     </td>
520   </tr>
521 <table>
522 END;
523 } // ListDomains
524
525 function Space() {
526   global $userid, $db;
527
528   // Tally up space used by $userid
529   $space = 0;
530
531   $statement = "select * from email where userid = \"$userid\"";
532
533   $result = mysqli_query($db, $statement)
534     or DBError("Space: Unable to execute query: ", $statement);
535
536   while ($row = mysqli_fetch_array ($result)) {
537     $msg_space =
538       strlen($row["userid"])    +
539       strlen($row["sender"])    +
540       strlen($row["subject"])   +
541       strlen($row["timestamp"]) +
542       strlen($row["data"]);
543     $space += $msg_space;
544   } // while
545
546   mysqli_free_result($result);
547
548   return $space;
549 } // Space
550 ?>