2 ////////////////////////////////////////////////////////////////////////////////
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 $
12 // (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
14 ////////////////////////////////////////////////////////////////////////////////
16 if (isset($_REQUEST["userid"])) {
17 $userid = $_REQUEST["userid"];
21 if (!isset($userid)) {
22 // No userid, see if we have a cookie for it
23 $userid=$_COOKIE["MAPSUser"];
39 function DBError($msg, $statement) {
42 $errno = mysqli_errno($db);
43 $errmsg = mysqli_error($db);
44 print "$msg<br>Error # $errno $errmsg";
45 print "<br>SQL Statement: $statement";
53 $db = mysqli_connect("localhost", "maps", "spam")
54 or DBError("OpenDB: Unable to connect to database server", "Connect");
56 mysqli_select_db($db, "MAPS")
57 or DBError("OpenDB: Unable to select MAPS database", "adefaria_maps");
60 function SetContext($new_userid) {
63 $userid = $new_userid;
66 function Encrypt($password, $userid) {
69 $statement = "select encode(\"$password\",\"$userid\")";
71 $result = mysqli_query($db, $statement)
72 or DBError("Encrypt: Unable to execute statement", $statement);
74 // Get return value, which should be the encoded password
75 $row = mysqli_fetch_array($result);
80 function UserExists($userid) {
83 $statement = "select userid, password from user where userid = \"$userid\"";
85 $result = mysqli_query($db, $statement)
86 or DBError ("UserExists: Unable to execute statement", $statement);
88 $row = mysqli_fetch_array($result);
90 $dbuserid = $row["userid"];
91 $dbpassword = $row["password"];
93 if ($dbuserid != $userid) {
100 function Login($userid, $password) {
101 $password = Encrypt($password, $userid);
103 // Check if user exists
104 $dbpassword = UserExists($userid);
106 // Return -1 if user doesn't exist
107 if ($dbpassword == -1) {
111 // Return -2 if password does not match
112 if ($password != $dbpassword) {
115 setcookie("MAPSUser", $userid, time()+60*60*24*30, "/maps");
121 function CountList ($type) {
124 $statement = "select count(*) as count from list where type=\"$type\" and userid=\"$userid\"";
126 $result = mysqli_query($db, $statement)
127 or DBError("CountList: Unable to count list: ", $statement);
129 // How many rows are there?
130 $row = mysqli_fetch_array($result);
132 return $row["count"];
135 function FindList($type, $next, $lines) {
140 $statement = "select * from list where type=\"$type\" and userid=\"$userid\" order by sequence limit $next, $lines";
142 $result = mysqli_query($db, $statement)
143 or DBError ("FindList: Unable to execute query: ", $statement);
145 $count = mysqli_num_rows($result);
147 return array($count, $result);
150 function Today2SQLDatetime() {
151 return date ("Y-m-d H:i:s");
152 } // Today2SQLDatetime
154 function countem($table, $condition) {
157 $statement = "select count(distinct sender) as count from $table where $condition";
159 $result = mysqli_query($db, $statement)
160 or DBError("countem: Unable to perform query: ", $statement);
162 // How many rows are there?
163 $row = mysqli_fetch_array($result);
165 return $row["count"];
168 function countlog($condition="") {
171 if ($condition != "") {
172 return countem("log", "userid=\"$userid\" and " . $condition);
174 return countem("log", "userid=\"$userid\"");
178 function SubtractDays($date, $nbr_days) {
182 function GetStats($nbr_days, $date = "") {
186 $date = Today2SQLDatetime();
189 while ($nbr_days > 0) {
190 $ymd = substr($date, 0, 10);
191 $sod = $ymd . " 00:00:00";
192 $eod = $ymd . " 23:59:59";
194 foreach ($Types as $type) {
195 $condition = "type=\"$type\" and (timestamp > \"$sod\" and timestamp < \"$eod\")";
196 $stats[$type] = countlog($condition);
199 $dates[$ymd] = &$stats;
201 $date = SubtractDays($date, 1);
208 function displayquickstats() {
209 $today = substr (Today2SQLDatetime(), 0, 10);
210 $dates = getquickstats($today);
211 $current_time = date("g:i:s a");
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>";
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, ".", "");
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>";
248 <table cellpadding="2" border="0" align="center" cellspacing="0">
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>
255 <td class="smalllabel">Returned</td>
256 <td class=smallnumber>$returned_link
257 <td class="smallnumber">$returned_pct%</td>
260 <td class="smalllabel">Whitelist</td>
261 <td class="smallnumber">$whitelist_link
262 <td class="smallnumber">$whitelist_pct%</td>
265 <td class="smalllabel">Blacklist</td>
266 <td class="smallnumber">$blacklist_link
267 <td class="smallnumber">$blacklist_pct%</td>
270 <td class="smalllabel">Registered</td>
271 <td class="smallnumber">$registered_link
272 <td class="smallnumber">n/a</td>
275 <td class="smalllabel">Mailloop</td>
276 <td class="smallnumber">$mailloop_link
277 <td class="smallnumber">n/a</td>
280 <td class="smalllabel">Nulllist</td>
281 <td class="smallnumber">$nulllist_link
282 <td class="smallnumber">$nulllist_pct%</td>
287 } // displayquickstats
289 function getquickstats($date) {
292 $dates = GetStats(1, $date);
294 foreach ($Types as $type) {
295 if (isset ($dates[$date]["processed"])) {
296 $dates[$date]["processed"] += $dates[$date][$type];
298 $dates[$date]["processed"] = $dates[$date][$type];
305 function NavigationBar($userid) {
306 print "<div id=leftbar>";
308 if (!isset ($userid) || $userid == "") {
310 <div class="username">Welcome to MAPS</div>
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>
321 $Userid = ucfirst($userid);
323 <div class="username">Welcome $Userid</div>
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>
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='';">
351 <form "method"=post action="javascript://" name="address"
352 onsubmit="checkaddress(this);">
354 <input type="text" class="searchfield" id="searchfield" name="email"
355 size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
364 function GetUserLines() {
369 $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
371 $result = mysqli_query($db, $statement)
372 or DBError("GetUserLines: Unable to execute query: ", $statement);
374 $row = mysqli_fetch_array ($result);
376 if (isset ($row["value"])) {
377 $lines = $row["value"];
383 function DisplayList($type, $next, $lines) {
389 $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
391 $result = mysqli_query($db, $statement)
392 or DBError("DisplayList: Unable to execute query: ", $statement);
394 for ($i = 0; $i < $lines; $i++) {
395 $row = mysqli_fetch_array ($result);
397 if (!isset ($row ["sequence"])) {
401 $sequence = $row["sequence"];
402 $username = $row["pattern"] == "" ? " " : $row["pattern"];
403 $domain = $row["domain"] == "" ? " " : $row["domain"];
404 $hit_count = $row["hit_count"] == "" ? " " : $row["hit_count"];
405 $last_hit = $row["last_hit"] == "" ? " " : $row["last_hit"];
406 $comments = $row["comment"] == "" ? " " : $row["comment"];
408 // Remove time from last hit
409 $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
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";
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>";
434 function MAPSHeader() {
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>
450 function ListDomains($top = 10) {
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";
463 $result = mysqli_query($db, $statement)
464 or DBError("ListDomains: Unable to execute query: ", $statement);
467 <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
469 <th class="tableleftend">Mark</th>
470 <th class="tableheader">Ranking</th>
471 <th class="tableheader">Domain</th>
472 <th class="tablerightend">Returns</th>
477 for ($i = 0; $i < $top; $i++) {
478 $row = mysqli_fetch_array ($result);
479 $domain = $row["domain"];
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>";
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>";
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);" />
512 // Tally up space used by $userid
515 $statement = "select * from email where userid = \"$userid\"";
517 $result = mysqli_query($db, $statement)
518 or DBError("Space: Unable to execute query: ", $statement);
520 while ($row = mysqli_fetch_array ($result)) {
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;