Removed /usr/local from CDPATH
[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
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=\"todaysactivity\">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 ? '' :
243     "<a href=\"/maps/bin/detail.cgi?type=returned;date=$today\">";
244   $whitelist_link = $whitelist == 0 ? '' :
245     "<a href=\"/maps/bin/detail.cgi?type=whitelist;date=$today\">";
246   $blacklist_link = $blacklist == 0 ? '' :
247     "<a href=\"/maps/bin/detail.cgi?type=blacklist;date=$today\">";
248   $registered_link = $registered == 0 ? '' :
249     "<a href=\"/maps/bin/detail.cgi?type=registered;date=$today\">";
250   $mailloop_link = $mailloop == 0 ? '' :
251     "<a href=\"/maps/bin/detail.cgi?type=mailloop;date=$today>\"";
252   $nulllist_link = $nulllist == 0 ? '' :
253     "<a href=\"/maps/bin/detail.cgi?type=nulllist;date=$today\">";
254
255 print <<<EOT
256 <div id="quickstats">
257 <table cellpadding="2" border="0" align="center" cellspacing="0">
258   <tr align="right">
259     <td align="left" 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">${nulllist_link}Nulllist</a></td>
265     <td class="smallnumber">$nulllist</td>
266     <td class="smallnumber">$nulllist_pct%</td>
267   </tr>
268   <tr align="right">
269     <td class="smalllabel">${returned_link}Returned</a></td>
270     <td class=smallnumber>$returned</td>
271     <td class="smallnumber">$returned_pct%</td>
272   </tr>
273   <tr align="right">
274     <td class="smalllabel">${whitelist_link}Whitelist</a></td>
275     <td class="smallnumber">$whitelist</td>
276     <td class="smallnumber">$whitelist_pct%</td>
277   </tr>
278   <tr align="right">
279     <td class="smalllabel">${blacklist_link}Blacklist</a></td>
280     <td class="smallnumber">$blacklist</td>
281     <td class="smallnumber">$blacklist_pct%</td>
282   </tr>
283   <tr align="right">
284     <td class="smalllabel">${registered_link}Registered</a></td>
285     <td class="smallnumber">$registered</td>
286     <td class="smallnumber">n/a</td>
287   </tr>
288   <tr align="right">
289     <td class="smalllabel">${mailloop_link}Mailloop</a></td>
290     <td class="smallnumber">$mailloop</td>
291     <td class="smallnumber">n/a</td>
292   </tr>
293 </table>
294 </div>
295 </div>
296 EOT;
297 } // displayquickstats
298
299 function getquickstats($date) {
300   global $Types;
301
302   $dates = GetStats(1, $date);
303
304   foreach ($Types as $type) {
305     if (isset ($dates[$date]["processed"])) {
306       $dates[$date]["processed"] += $dates[$date][$type];
307     } else {
308       $dates[$date]["processed"] = $dates[$date][$type];
309     } // if
310   } # foreach
311
312   return $dates;
313 } // getquickstats
314
315 function NavigationBar($userid) {
316   print "<div id=leftbar>";
317
318   if (!isset ($userid) || $userid == "") {
319     print <<<END
320     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
321     <div class="username">Welcome to MAPS</div>
322     <div class="menu">
323     <a href="/maps/doc/">What is MAPS?</a><br>
324     <a href="/maps/doc/SPAM.php">What is SPAM?</a><br>
325     <a href="/maps/doc/Requirements.php">Requirements</a><br>
326     <a href="/maps/SignupForm.html">Signup</a><br>
327     <a href="/maps/doc/Using.php">Using MAPS</a><br>
328     <a href="/maps/doc/">Help</a><br>
329     </div>
330 END;
331   } else {
332     $Userid = ucfirst($userid);
333     print <<<END
334     <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
335     <div class="username">Welcome $Userid</div>
336     <div class="menu">
337     <a href="/maps/">Home</a><br>
338     <a href="/maps/bin/stats.cgi">Statistics</a><br>
339     <a href="/maps/bin/editprofile.cgi">Profile</a><br>
340     <a href="/maps/php/Reports.php">Reports</a><br>
341     <a href="/maps/php/list.php?type=white">White</a><br>
342     <a href="/maps/php/list.php?type=black">Black</a><br>
343     <a href="/maps/php/list.php?type=null">Null</a><br>
344     <a href="/maps/doc/">Help</a><br>
345     <a href="/maps/adm/">Admin</a><br>
346     <a href="/maps/?logout=yes">Logout</a>
347     </div>
348 END;
349
350     displayquickstats();
351
352     print <<<END
353   <div class="search">
354   <form method="get" action="/maps/bin/search.cgi" name="search">
355     Search Sender/Subject
356     <input type="text" class="searchfield" id="searchfield" name="str"
357      size="20" maxlength="255"  value="" onclick="document.search.str.value='';">
358   </form>
359   </div>
360 END;
361
362     print <<<END
363   <div class="search">
364   <form "method"=post action="javascript://" name="address"
365    onsubmit="checkaddress(this);">
366     Check Email Address
367     <input type="text" class="searchfield" id="searchfield" name="email"
368      size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
369   </form>
370   <p></p>
371   </div>
372 END;
373   } // if
374
375   print "</div>";
376 } # NavigationBar
377
378 function GetUserLines() {
379   global $userid, $db;
380
381   $lines = 10;
382
383   $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
384
385   $result = mysqli_query($db, $statement)
386     or DBError("GetUserLines: Unable to execute query: ", $statement);
387
388   $row = mysqli_fetch_array ($result);
389
390   if (isset ($row["value"])) {
391     $lines = $row["value"];
392   } // if
393
394   return $lines;
395 } // GetUserLines
396
397 function DisplayList($type, $next, $lines) {
398   global $userid;
399   global $total;
400   global $last;
401   global $db;
402
403   $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
404
405   $result = mysqli_query($db, $statement)
406     or DBError("DisplayList: Unable to execute query: ", $statement);
407
408   for ($i = 0; $i < $lines; $i++) {
409     $row = mysqli_fetch_array($result);
410
411     if (!isset ($row["sequence"])) {
412       break;
413     } // if
414
415     $sequence  = $row["sequence"];
416     $username  = $row["pattern"]   == "" ? "&nbsp;" : $row["pattern"];
417     $domain    = $row["domain"]    == "" ? "&nbsp;" : $row["domain"];
418     $hit_count = $row["hit_count"] == "" ? "&nbsp;" : $row["hit_count"];
419     $last_hit  = $row["last_hit"]  == "" ? "&nbsp;" : $row["last_hit"];
420     $retention = $row["retention"] == "" ? "&nbsp;" : $row["retention"];
421     $comments  = $row["comment"]   == "" ? "&nbsp;" : $row["comment"];
422
423     // Remove time from last hit
424     $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
425
426     // Reformat last_hit
427     $last_hit = substr ($last_hit, 5, 2) . "/" .
428                 substr ($last_hit, 8, 2) . "/" .
429                 substr ($last_hit, 0, 4);
430     $leftclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
431       "tablebottomleft" : "tableleftdata";
432     $dataclass  = ($i == $lines || $sequence == $total || $sequence == $last) ?
433       "tablebottomdata"  : "tabledata";
434     $rightclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
435       "tablebottomright" : "tablerightdata";
436
437     print "<td class=$leftclass align=right>"   . $sequence  . "<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 "<div id=highlightrow>";
482   print <<<END
483   <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
484     <tr>
485       <th class="tableleftend">&nbsp;</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 align=center class=tableleftdata>" . $ranking . "<input type=checkbox name=action" . $i . " value=on></td>\n";
501       print "<td class=tabledata><a href=\"http://$domain\">$domain</as></td>";
502       print "<input type=hidden name=email$i value=\"@$domain\">";
503       print "<td align=center class=tablerightdata>$nbr</td>";
504     } else {
505       print "<td align=center class=tablebottomleft>" . $ranking . "<input type=checkbox name=action" . $i . " value=on></td>\n";
506       print "<td class=tablebottomdata><a href=\"http://$domain\">$domain</a></td>";
507       print "<input type=hidden name=email$i value=\"@$domain\">";
508       print "<td align=center class=tablebottomright>$nbr</td>";
509     } // if
510     print "</tr>";
511   } // for
512
513   print <<<END
514   <tr>
515     <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);" />
516     </td>
517   </tr>
518 <table>
519 </div>
520 END;
521 } // ListDomains
522
523 function Space() {
524   global $userid, $db;
525
526   // Tally up space used by $userid
527   $space = 0;
528
529   $statement = "select * from email where userid = \"$userid\"";
530
531   $result = mysqli_query($db, $statement)
532     or DBError("Space: Unable to execute query: ", $statement);
533
534   while ($row = mysqli_fetch_array ($result)) {
535     $msg_space =
536       strlen($row["userid"])    +
537       strlen($row["sender"])    +
538       strlen($row["subject"])   +
539       strlen($row["timestamp"]) +
540       strlen($row["data"]);
541     $space += $msg_space;
542   } // while
543
544   mysqli_free_result($result);
545
546   return $space;
547 } // Space
548 ?>