Welcome to MAPS 2.0
[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 ? 0 :
244     "<a href=/maps/bin/detail.cgi?type=returned;date=$today>$returned</a>";
245   $whitelist_link = $whitelist == 0 ? 0 :
246     "<a href=/maps/bin/detail.cgi?type=whitelist;date=$today>$whitelist</a>";
247   $blacklist_link = $blacklist == 0 ? 0 :
248     "<a href=/maps/bin/detail.cgi?type=blacklist;date=$today>$blacklist</a>";
249   $registered_link = $registered == 0 ? 0 :
250     "<a href=/maps/bin/detail.cgi?type=registered;date=$today>$registered</a>";
251   $mailloop_link = $mailloop == 0 ? 0 :
252     "<a href=/maps/bin/detail.cgi?type=mailloop;date=$today>$mailloop</a>";
253   $nulllist_link = $nulllist == 0 ? 0 :
254     "<a href=/maps/bin/detail.cgi?type=nulllist;date=$today>$nulllist</a>";
255
256 print <<<EOT
257 <table cellpadding="2" border="0" align="center" cellspacing="0">
258   <tr align="right">
259     <td align="right" class="smalllabel">Processed</td>
260     <td align="right" class="smallnumber">$processed</td>
261     <td align="right" class="smallnumber">n/a</td>
262   </tr>
263   <tr align="right">
264     <td class="smalllabel">Returned</td>
265     <td class=smallnumber>$returned_link
266     <td class="smallnumber">$returned_pct%</td>
267   </tr>
268   <tr align="right">
269     <td class="smalllabel">Whitelist</td>
270     <td class="smallnumber">$whitelist_link
271     <td class="smallnumber">$whitelist_pct%</td>
272   </tr>
273   <tr align="right">
274     <td class="smalllabel">Blacklist</td>
275     <td class="smallnumber">$blacklist_link
276     <td class="smallnumber">$blacklist_pct%</td>
277   </tr>
278   <tr align="right">
279     <td class="smalllabel">Registered</td>
280     <td class="smallnumber">$registered_link
281     <td class="smallnumber">n/a</td>
282   </tr>
283   <tr align="right">
284     <td class="smalllabel">Mailloop</td>
285     <td class="smallnumber">$mailloop_link
286     <td class="smallnumber">n/a</td>
287   </tr>
288   <tr align="right">
289     <td class="smalllabel">Nulllist</td>
290     <td class="smallnumber">$nulllist_link
291     <td class="smallnumber">$nulllist_pct%</td>
292   </tr>
293 </table>
294 </div>
295 EOT;
296 } // displayquickstats
297
298 function getquickstats($date) {
299   global $Types;
300
301   $dates = GetStats(1, $date);
302
303   foreach ($Types as $type) {
304     if (isset ($dates[$date]["processed"])) {
305       $dates[$date]["processed"] += $dates[$date][$type];
306     } else {
307       $dates[$date]["processed"] = $dates[$date][$type];
308     } // if
309   } # foreach
310
311   return $dates;
312 } // getquickstats
313
314 function NavigationBar($userid) {
315   print "<div id=leftbar>";
316
317   if (!isset ($userid) || $userid == "") {
318     print <<<END
319     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
320     <div class="username">Welcome to MAPS</div>
321     <div class="menu">
322     <a href="/maps/doc/">What is MAPS?</a><br>
323     <a href="/maps/doc/SPAM.php">What is SPAM?</a><br>
324     <a href="/maps/doc/Requirements.php">Requirements</a><br>
325     <a href="/maps/SignupForm.html">Signup</a><br>
326     <a href="/maps/doc/Using.php">Using MAPS</a><br>
327     <a href="/maps/doc/">Help</a><br>
328     </div>
329 END;
330   } else {
331     $Userid = ucfirst($userid);
332     print <<<END
333     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
334     <div class="username">Welcome $Userid</div>
335     <div class="menu">
336     <a href="/maps/">Home</a><br>
337     <a href="/maps/bin/stats.cgi">Statistics</a><br>
338     <a href="/maps/bin/editprofile.cgi">Profile</a><br>
339     <a href="/maps/php/Reports.php">Reports</a><br>
340     <a href="/maps/php/list.php?type=white">White</a><br>
341     <a href="/maps/php/list.php?type=black">Black</a><br>
342     <a href="/maps/php/list.php?type=null">Null</a><br>
343     <a href="/maps/doc/">Help</a><br>
344     <a href="/maps/adm/">Admin</a><br>
345     <a href="/maps/?logout=yes">Logout</a>
346     </div>
347 END;
348
349     displayquickstats();
350
351     print <<<END
352   <div class="search">
353   <form method="get" action="/maps/bin/search.cgi" name="search">
354     Search Sender/Subject
355     <input type="text" class="searchfield" id="searchfield" name="str"
356      size="20" maxlength="255"  value="" onclick="document.search.str.value='';">
357   </form>
358   </div>
359 END;
360
361     print <<<END
362   <div class="search">
363   <form "method"=post action="javascript://" name="address"
364    onsubmit="checkaddress(this);">
365     Check Email Address
366     <input type="text" class="searchfield" id="searchfield" name="email"
367      size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
368   </form>
369   <p></p>
370   </div>
371 END;
372   } // if
373
374   print "</div>";
375 } # NavigationBar
376
377 function GetUserLines() {
378   global $userid, $db;
379
380   $lines = 10;
381
382   $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
383
384   $result = mysqli_query($db, $statement)
385     or DBError("GetUserLines: Unable to execute query: ", $statement);
386
387   $row = mysqli_fetch_array ($result);
388
389   if (isset ($row["value"])) {
390     $lines = $row["value"];
391   } // if
392
393   return $lines;
394 } // GetUserLines
395
396 function DisplayList($type, $next, $lines) {
397   global $userid;
398   global $total;
399   global $last;
400   global $db;
401
402   $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
403
404   $result = mysqli_query($db, $statement)
405     or DBError("DisplayList: Unable to execute query: ", $statement);
406
407   for ($i = 0; $i < $lines; $i++) {
408     $row = mysqli_fetch_array ($result);
409
410     if (!isset ($row ["sequence"])) {
411       break;
412     } // if
413
414     $sequence  = $row["sequence"];
415     $username  = $row["pattern"]   == "" ? "&nbsp;" : $row["pattern"];
416     $domain    = $row["domain"]    == "" ? "&nbsp;" : $row["domain"];
417     $hit_count = $row["hit_count"] == "" ? "&nbsp;" : $row["hit_count"];
418     $last_hit  = $row["last_hit"]  == "" ? "&nbsp;" : $row["last_hit"];
419     $retention = $row["retention"] == "" ? "&nbsp;" : $row["retention"];
420     $comments  = $row["comment"]   == "" ? "&nbsp;" : $row["comment"];
421
422     // Remove time from last hit
423     $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
424
425     // Reformat last_hit
426     $last_hit = substr ($last_hit, 5, 2) . "/" .
427                 substr ($last_hit, 8, 2) . "/" .
428                 substr ($last_hit, 0, 4);
429     $leftclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
430       "tablebottomleft" : "tableleftdata";
431     $dataclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
432       "tablebottomdata"  : "tabledata";
433     $rightclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
434       "tablebottomright" : "tablerightdata";
435
436     print "<td class=$leftclass align=center>"  . $sequence  . "</td>";
437     print "<td class=$dataclass align=center><input type=checkbox name=action" . $sequence . " value=on></td>\n";
438     print "<td class=$dataclass align=right>"   . $username  . "</td>";
439     print "<td class=$dataclass align=center>@</td>";
440     print "<td class=$dataclass align=left><a href=\"http://$domain\" target=_blank>$domain</a></td>";
441     print "<td class=$dataclass align=right>"   . $hit_count . "</td>";
442     print "<td class=$dataclass align=center>"  . $last_hit  . "</td>";
443     print "<td class=$dataclass align=right>"   . $retention . "</td>";
444     print "<td class=$rightclass align=left>"   . $comments  . "</td>";
445     print "</tr>";
446   } // for
447 } // DisplayList
448
449 function MAPSHeader() {
450   print <<<END
451   <meta name="author" content="Andrew DeFaria <Andre@DeFaria.com>">
452   <meta name="MAPS" "Mail Authorization and Permission System">
453   <meta name="keywords" content="Eliminate SPAM, Permission based email, SPAM filtering system">
454   <meta http-equiv=Refresh content="900">
455   <link rel="icon" href="/maps/MAPS.png" type="image/png">
456   <link rel="SHORTCUT ICON" href="/maps/favicon.ico">
457   <link rel="stylesheet" type="text/css" href="/maps/css/MAPSStyle.css"/>
458   <script language="JavaScript1.2" src="/maps/JavaScript/MAPSUtils.js"
459    type="text/javascript"></script>
460   <script language="JavaScript1.2" src="/maps/JavaScript/CheckAddress.js"
461    type="text/javascript"></script>
462 END;
463 } // MAPSHeader
464
465 function ListDomains($top = 10) {
466   global $userid, $db;
467
468   // Generate a list of the top 10 spammers by domain
469   $statement = "select count(sender) as nbr, ";
470   // Must extract domain from sender...
471   $statement = $statement . "substring(sender, locate(\"@\",sender, 1)+1) as domain ";
472   // From email for the current userid...
473   $statement = $statement . "from email where userid=\"$userid\" ";
474   // Group things by domain but order them descending on nbr...
475   $statement = $statement . "group by domain order by nbr desc";
476
477   // Do the query
478   $result = mysqli_query($db, $statement)
479     or DBError("ListDomains: Unable to execute query: ", $statement);
480
481   print <<<END
482   <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
483     <tr>
484       <th class="tableleftend">Mark</th>
485       <th class="tableheader">Ranking</th>
486       <th class="tableheader">Domain</th>
487       <th class="tablerightend">Returns</th>
488     </tr>
489 END;
490
491   // Get results
492   for ($i = 0; $i < $top; $i++) {
493     $row = mysqli_fetch_array ($result);
494     $domain = $row["domain"];
495     $nbr    = $row["nbr"];
496
497     print "<tr>";
498     $ranking = $i + 1;
499     if ($i < $top - 1) {
500       print "<td class=tableleftdata align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
501       print "<td align=center class=tabledata>" . $ranking . "</td>";
502       print "<td class=tabledata><a href=\"http://$domain\">$domain</as></td>";
503       print "<input type=hidden name=email$i value=\"@$domain\">";
504       print "<td align=center class=tablerightdata>$nbr</td>";
505     } else {
506       print "<td class=tablebottomleft align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
507       print "<td align=center class=tablebottomdata>" . $ranking . "</td>";
508       print "<td class=tablebottomdata><a href=\"http://$domain\">$domain</a></td>";
509       print "<input type=hidden name=email$i value=\"@$domain\">";
510       print "<td align=center class=tablebottomright>$nbr</td>";
511     } // if
512     print "</tr>";
513   } // for
514
515   print <<<END
516   <tr>
517     <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);" />
518     </td>
519   </tr>
520 <table>
521 END;
522 } // ListDomains
523
524 function Space() {
525   global $userid, $db;
526
527   // Tally up space used by $userid
528   $space = 0;
529
530   $statement = "select * from email where userid = \"$userid\"";
531
532   $result = mysqli_query($db, $statement)
533     or DBError("Space: Unable to execute query: ", $statement);
534
535   while ($row = mysqli_fetch_array ($result)) {
536     $msg_space =
537       strlen($row["userid"])    +
538       strlen($row["sender"])    +
539       strlen($row["subject"])   +
540       strlen($row["timestamp"]) +
541       strlen($row["data"]);
542     $space += $msg_space;
543   } // while
544
545   mysqli_free_result($result);
546
547   return $space;
548 } // Space
549 ?>