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