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