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("127.0.0.1", "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");
68 function SetContext($new_userid) {
71 $userid = $new_userid;
74 function Encrypt($password, $userid) {
77 $statement = "select hex(aes_encrypt(\"$password\",\"$userid\"))";
79 $result = mysqli_query($db, $statement)
80 or DBError("Encrypt: Unable to execute statement", $statement);
82 // Get return value, which should be the encoded password
83 $row = mysqli_fetch_array($result);
88 function UserExists($userid) {
91 $statement = "select userid, password from user where userid = \"$userid\"";
93 $result = mysqli_query($db, $statement)
94 or DBError ("UserExists: Unable to execute statement", $statement);
96 $row = mysqli_fetch_array($result);
98 $dbuserid = $row["userid"];
99 $dbpassword = $row["password"];
101 if ($dbuserid != $userid) {
108 function Login($userid, $password) {
109 $password = Encrypt($password, $userid);
111 // Check if user exists
112 $dbpassword = UserExists($userid);
113 print "dbpassword = $dbpassword<br>";
115 // Return -1 if user doesn't exist
116 if ($dbpassword == -1) {
120 // Return -2 if password does not match
121 if ($password != $dbpassword) {
124 setcookie("MAPSUser", $userid, time()+60*60*24*30, "/maps");
130 function CountList ($type) {
133 $statement = "select count(*) as count from list where type=\"$type\" and userid=\"$userid\"";
135 $result = mysqli_query($db, $statement)
136 or DBError("CountList: Unable to count list: ", $statement);
138 // How many rows are there?
139 $row = mysqli_fetch_array($result);
141 return $row["count"];
144 function FindList($type, $next, $lines) {
149 $statement = "select * from list where type=\"$type\" and userid=\"$userid\" order by sequence limit $next, $lines";
151 $result = mysqli_query($db, $statement)
152 or DBError ("FindList: Unable to execute query: ", $statement);
154 $count = mysqli_num_rows($result);
156 return array($count, $result);
159 function Today2SQLDatetime() {
160 return date ("Y-m-d H:i:s");
161 } // Today2SQLDatetime
163 function countem($table, $condition) {
166 $statement = "select count(distinct sender) as count from $table where $condition";
168 $result = mysqli_query($db, $statement)
169 or DBError("countem: Unable to perform query: ", $statement);
171 // How many rows are there?
172 $row = mysqli_fetch_array($result);
174 return $row["count"];
177 function countlog($condition="") {
180 if ($condition != "") {
181 return countem("log", "userid=\"$userid\" and " . $condition);
183 return countem("log", "userid=\"$userid\"");
187 function SubtractDays($date, $nbr_days) {
191 function GetStats($nbr_days, $date = "") {
195 $date = Today2SQLDatetime();
198 while ($nbr_days > 0) {
199 $ymd = substr($date, 0, 10);
200 $sod = $ymd . " 00:00:00";
201 $eod = $ymd . " 23:59:59";
203 foreach ($Types as $type) {
204 $condition = "type=\"$type\" and (timestamp > \"$sod\" and timestamp < \"$eod\")";
205 $stats[$type] = countlog($condition);
208 $dates[$ymd] = &$stats;
210 $date = SubtractDays($date, 1);
217 function displayquickstats() {
218 $today = substr (Today2SQLDatetime(), 0, 10);
219 $dates = getquickstats($today);
220 $current_time = date("g:i:s a");
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>";
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, ".", "");
243 $returned_link = $returned == 0 ? '' :
244 "<a href=\"/maps/bin/detail.cgi?type=returned;date=$today\">";
245 $whitelist_link = $whitelist == 0 ? '' :
246 "<a href=\"/maps/bin/detail.cgi?type=whitelist;date=$today\">";
247 $blacklist_link = $blacklist == 0 ? '' :
248 "<a href=\"/maps/bin/detail.cgi?type=blacklist;date=$today\">";
249 $registered_link = $registered == 0 ? '' :
250 "<a href=\"/maps/bin/detail.cgi?type=registered;date=$today\">";
251 $mailloop_link = $mailloop == 0 ? '' :
252 "<a href=\"/maps/bin/detail.cgi?type=mailloop;date=$today>\"";
253 $nulllist_link = $nulllist == 0 ? '' :
254 "<a href=\"/maps/bin/detail.cgi?type=nulllist;date=$today\">";
258 <table cellpadding="2" border="0" align="center" cellspacing="0">
260 <td align="right" class="smalllabel">Processed</td>
261 <td align="right" class="smallnumber">$processed</td>
262 <td align="right" class="smallnumber">n/a</td>
265 <td class="link">${nulllist_link}Nulllist</a></td>
266 <td class="smallnumber">$nulllist</td>
267 <td class="smallnumber">$nulllist_pct%</td>
270 <td class="link">${returned_link}Returned</a></td>
271 <td class=smallnumber>$returned</td>
272 <td class="smallnumber">$returned_pct%</td>
275 <td class="link">${whitelist_link}Whitelist</a></td>
276 <td class="smallnumber">$whitelist</td>
277 <td class="smallnumber">$whitelist_pct%</td>
280 <td class="link">${blacklist_link}Blacklist</a></td>
281 <td class="smallnumber">$blacklist</td>
282 <td class="smallnumber">$blacklist_pct%</td>
285 <td class="link">${registered_link}Registered</a></td>
286 <td class="smallnumber">$registered</td>
287 <td class="smallnumber">n/a</td>
290 <td class="link">${mailloop_link}Mailloop</a></td>
291 <td class="smallnumber">$mailloop</td>
292 <td class="smallnumber">n/a</td>
298 } // displayquickstats
300 function getquickstats($date) {
303 $dates = GetStats(1, $date);
305 foreach ($Types as $type) {
306 if (isset ($dates[$date]["processed"])) {
307 $dates[$date]["processed"] += $dates[$date][$type];
309 $dates[$date]["processed"] = $dates[$date][$type];
316 function NavigationBar($userid) {
317 print "<div id=leftbar>";
319 if (!isset ($userid) || $userid == "") {
321 <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
322 <div class="username">Welcome to MAPS</div>
324 <a href="/maps/doc/">What is MAPS?</a><br>
325 <a href="/maps/doc/SPAM.php">What is SPAM?</a><br>
326 <a href="/maps/doc/Requirements.php">Requirements</a><br>
327 <a href="/maps/SignupForm.html">Signup</a><br>
328 <a href="/maps/doc/Using.php">Using MAPS</a><br>
329 <a href="/maps/doc/">Help</a><br>
333 $Userid = ucfirst($userid);
335 <h2 align='center'><font style="color: white">MAPS 2.0</font></h2>
336 <div class="username">Welcome $Userid</div>
338 <a href="/maps/">Home</a><br>
339 <a href="/maps/bin/stats.cgi">Statistics</a><br>
340 <a href="/maps/bin/editprofile.cgi">Profile</a><br>
341 <a href="/maps/php/Reports.php">Reports</a><br>
342 <a href="/maps/php/list.php?type=white">White</a><br>
343 <a href="/maps/php/list.php?type=black">Black</a><br>
344 <a href="/maps/php/list.php?type=null">Null</a><br>
345 <a href="/maps/doc/">Help</a><br>
346 <a href="/maps/adm/">Admin</a><br>
347 <a href="/maps/?logout=yes">Logout</a>
355 <form method="get" action="/maps/bin/search.cgi" name="search">
356 Search Sender/Subject
357 <input type="text" class="searchfield" id="searchfield" name="str"
358 size="20" maxlength="255" value="" onclick="document.search.str.value='';">
365 <form "method"=post action="javascript://" name="address"
366 onsubmit="checkaddress(this);">
368 <input type="text" class="searchfield" id="searchfield" name="email"
369 size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
379 function GetUserLines() {
384 $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
386 $result = mysqli_query($db, $statement)
387 or DBError("GetUserLines: Unable to execute query: ", $statement);
389 $row = mysqli_fetch_array ($result);
391 if (isset ($row["value"])) {
392 $lines = $row["value"];
398 function DisplayList($type, $next, $lines) {
404 $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
406 $result = mysqli_query($db, $statement)
407 or DBError("DisplayList: Unable to execute query: ", $statement);
409 for ($i = 0; $i < $lines; $i++) {
410 $row = mysqli_fetch_array($result);
412 if (!isset ($row["sequence"])) {
416 $sequence = $row["sequence"];
417 $username = $row["pattern"] == "" ? " " : $row["pattern"];
418 $domain = $row["domain"] == "" ? " " : $row["domain"];
419 $hit_count = $row["hit_count"] == "" ? " " : $row["hit_count"];
420 $last_hit = $row["last_hit"] == "" ? " " : $row["last_hit"];
421 $retention = $row["retention"] == "" ? " " : $row["retention"];
422 $comments = $row["comment"] == "" ? " " : $row["comment"];
424 // Remove time from last hit
425 $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
428 $last_hit = substr ($last_hit, 5, 2) . "/" .
429 substr ($last_hit, 8, 2) . "/" .
430 substr ($last_hit, 0, 4);
431 $leftclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
432 "tablebottomleft" : "tableleftdata";
433 $dataclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
434 "tablebottomdata" : "tabledata";
435 $rightclass = ($i == $lines || $sequence == $total || $sequence == $last) ?
436 "tablebottomright" : "tablerightdata";
438 print "<td class=$leftclass align=right>" . $sequence . "<input type=checkbox name=action" . $sequence . " value=on></td>\n";
439 print "<td class=$dataclass align=right>" . $username . "</td>";
440 print "<td class=$dataclass align=center>@</td>";
441 print "<td class=$dataclass align=left><a href=\"http://$domain\" target=_blank>$domain</a></td>";
442 print "<td class=$dataclass align=right>" . $hit_count . "</td>";
443 print "<td class=$dataclass align=center>" . $last_hit . "</td>";
444 print "<td class=$dataclass align=right>" . $retention . "</td>";
445 print "<td class=$rightclass align=left>" . $comments . "</td>";
450 function MAPSHeader() {
452 <meta name="author" content="Andrew DeFaria <Andre@DeFaria.com>">
453 <meta name="MAPS" "Mail Authorization and Permission System">
454 <meta name="keywords" content="Eliminate SPAM, Permission based email, SPAM filtering system">
455 <meta http-equiv=Refresh content="900">
456 <link rel="icon" href="/maps/MAPS.png" type="image/png">
457 <link rel="SHORTCUT ICON" href="/maps/favicon.ico">
458 <link rel="stylesheet" type="text/css" href="/maps/css/MAPSStyle.css"/>
459 <script language="JavaScript1.2" src="/maps/JavaScript/MAPSUtils.js"
460 type="text/javascript"></script>
461 <script language="JavaScript1.2" src="/maps/JavaScript/CheckAddress.js"
462 type="text/javascript"></script>
466 function ListDomains($top = 10) {
469 // Generate a list of the top 10 spammers by domain
470 $statement = "select count(sender) as nbr, ";
471 // Must extract domain from sender...
472 $statement = $statement . "substring(sender, locate(\"@\",sender, 1)+1) as domain ";
473 // From email for the current userid...
474 $statement = $statement . "from email where userid=\"$userid\" ";
475 // Group things by domain but order them descending on nbr...
476 $statement = $statement . "group by domain order by nbr desc";
479 $result = mysqli_query($db, $statement)
480 or DBError("ListDomains: Unable to execute query: ", $statement);
483 <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
485 <th class="tableleftend">Mark</th>
486 <th class="tableheader">Ranking</th>
487 <th class="tableheader">Domain</th>
488 <th class="tablerightend">Returns</th>
493 for ($i = 0; $i < $top; $i++) {
494 $row = mysqli_fetch_array ($result);
495 $domain = $row["domain"];
501 print "<td class=tableleftdata align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
502 print "<td align=center class=tabledata>" . $ranking . "</td>";
503 print "<td class=tabledata><a href=\"http://$domain\">$domain</as></td>";
504 print "<input type=hidden name=email$i value=\"@$domain\">";
505 print "<td align=center class=tablerightdata>$nbr</td>";
507 print "<td class=tablebottomleft align=center><input type=checkbox name=action" . $i . " value=on></td>\n";
508 print "<td align=center class=tablebottomdata>" . $ranking . "</td>";
509 print "<td class=tablebottomdata><a href=\"http://$domain\">$domain</a></td>";
510 print "<input type=hidden name=email$i value=\"@$domain\">";
511 print "<td align=center class=tablebottomright>$nbr</td>";
518 <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);" />
528 // Tally up space used by $userid
531 $statement = "select * from email where userid = \"$userid\"";
533 $result = mysqli_query($db, $statement)
534 or DBError("Space: Unable to execute query: ", $statement);
536 while ($row = mysqli_fetch_array ($result)) {
538 strlen($row["userid"]) +
539 strlen($row["sender"]) +
540 strlen($row["subject"]) +
541 strlen($row["timestamp"]) +
542 strlen($row["data"]);
543 $space += $msg_space;
546 mysqli_free_result($result);