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");
68 function SetContext($new_userid) {
71 $userid = $new_userid;
74 function Encrypt($password, $userid) {
77 $statement = "select encode(\"$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);
114 // Return -1 if user doesn't exist
115 if ($dbpassword == -1) {
119 // Return -2 if password does not match
120 if ($password != $dbpassword) {
123 setcookie("MAPSUser", $userid, time()+60*60*24*30, "/maps");
129 function CountList ($type) {
132 $statement = "select count(*) as count from list where type=\"$type\" and userid=\"$userid\"";
134 $result = mysqli_query($db, $statement)
135 or DBError("CountList: Unable to count list: ", $statement);
137 // How many rows are there?
138 $row = mysqli_fetch_array($result);
140 return $row["count"];
143 function FindList($type, $next, $lines) {
148 $statement = "select * from list where type=\"$type\" and userid=\"$userid\" order by sequence limit $next, $lines";
150 $result = mysqli_query($db, $statement)
151 or DBError ("FindList: Unable to execute query: ", $statement);
153 $count = mysqli_num_rows($result);
155 return array($count, $result);
158 function Today2SQLDatetime() {
159 return date ("Y-m-d H:i:s");
160 } // Today2SQLDatetime
162 function countem($table, $condition) {
165 $statement = "select count(distinct sender) as count from $table where $condition";
167 $result = mysqli_query($db, $statement)
168 or DBError("countem: Unable to perform query: ", $statement);
170 // How many rows are there?
171 $row = mysqli_fetch_array($result);
173 return $row["count"];
176 function countlog($condition="") {
179 if ($condition != "") {
180 return countem("log", "userid=\"$userid\" and " . $condition);
182 return countem("log", "userid=\"$userid\"");
186 function SubtractDays($date, $nbr_days) {
190 function GetStats($nbr_days, $date = "") {
194 $date = Today2SQLDatetime();
197 while ($nbr_days > 0) {
198 $ymd = substr($date, 0, 10);
199 $sod = $ymd . " 00:00:00";
200 $eod = $ymd . " 23:59:59";
202 foreach ($Types as $type) {
203 $condition = "type=\"$type\" and (timestamp > \"$sod\" and timestamp < \"$eod\")";
204 $stats[$type] = countlog($condition);
207 $dates[$ymd] = &$stats;
209 $date = SubtractDays($date, 1);
216 function displayquickstats() {
217 $today = substr (Today2SQLDatetime(), 0, 10);
218 $dates = getquickstats($today);
219 $current_time = date("g:i:s a");
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>";
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, ".", "");
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>";
256 <table cellpadding="2" border="0" align="center" cellspacing="0">
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>
263 <td class="smalllabel">Returned</td>
264 <td class=smallnumber>$returned_link
265 <td class="smallnumber">$returned_pct%</td>
268 <td class="smalllabel">Whitelist</td>
269 <td class="smallnumber">$whitelist_link
270 <td class="smallnumber">$whitelist_pct%</td>
273 <td class="smalllabel">Blacklist</td>
274 <td class="smallnumber">$blacklist_link
275 <td class="smallnumber">$blacklist_pct%</td>
278 <td class="smalllabel">Registered</td>
279 <td class="smallnumber">$registered_link
280 <td class="smallnumber">n/a</td>
283 <td class="smalllabel">Mailloop</td>
284 <td class="smallnumber">$mailloop_link
285 <td class="smallnumber">n/a</td>
288 <td class="smalllabel">Nulllist</td>
289 <td class="smallnumber">$nulllist_link
290 <td class="smallnumber">$nulllist_pct%</td>
295 } // displayquickstats
297 function getquickstats($date) {
300 $dates = GetStats(1, $date);
302 foreach ($Types as $type) {
303 if (isset ($dates[$date]["processed"])) {
304 $dates[$date]["processed"] += $dates[$date][$type];
306 $dates[$date]["processed"] = $dates[$date][$type];
313 function NavigationBar($userid) {
314 print "<div id=leftbar>";
316 if (!isset ($userid) || $userid == "") {
318 <div class="username">Welcome to MAPS</div>
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>
329 $Userid = ucfirst($userid);
331 <div class="username">Welcome $Userid</div>
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>
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='';">
359 <form "method"=post action="javascript://" name="address"
360 onsubmit="checkaddress(this);">
362 <input type="text" class="searchfield" id="searchfield" name="email"
363 size="20" maxlength="255" value="" onclick="document.address.email.value = '';">
372 function GetUserLines() {
377 $statement = "select value from useropts where userid=\"$userid\" and name=\"Page\"";
379 $result = mysqli_query($db, $statement)
380 or DBError("GetUserLines: Unable to execute query: ", $statement);
382 $row = mysqli_fetch_array ($result);
384 if (isset ($row["value"])) {
385 $lines = $row["value"];
391 function DisplayList($type, $next, $lines) {
397 $statement = "select * from list where userid=\"$userid\" and type=\"$type\" order by sequence limit $next, $lines";
399 $result = mysqli_query($db, $statement)
400 or DBError("DisplayList: Unable to execute query: ", $statement);
402 for ($i = 0; $i < $lines; $i++) {
403 $row = mysqli_fetch_array ($result);
405 if (!isset ($row ["sequence"])) {
409 $sequence = $row["sequence"];
410 $username = $row["pattern"] == "" ? " " : $row["pattern"];
411 $domain = $row["domain"] == "" ? " " : $row["domain"];
412 $hit_count = $row["hit_count"] == "" ? " " : $row["hit_count"];
413 $last_hit = $row["last_hit"] == "" ? " " : $row["last_hit"];
414 $comments = $row["comment"] == "" ? " " : $row["comment"];
416 // Remove time from last hit
417 $last_hit = substr($last_hit, 0, (strlen($last_hit) - strpos($last_hit, " ")) + 1);
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";
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>";
442 function MAPSHeader() {
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>
458 function ListDomains($top = 10) {
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";
471 $result = mysqli_query($db, $statement)
472 or DBError("ListDomains: Unable to execute query: ", $statement);
475 <table border="0" cellspacing="0" cellpadding="4" align="center" name="domainlist">
477 <th class="tableleftend">Mark</th>
478 <th class="tableheader">Ranking</th>
479 <th class="tableheader">Domain</th>
480 <th class="tablerightend">Returns</th>
485 for ($i = 0; $i < $top; $i++) {
486 $row = mysqli_fetch_array ($result);
487 $domain = $row["domain"];
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>";
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>";
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);" />
520 // Tally up space used by $userid
523 $statement = "select * from email where userid = \"$userid\"";
525 $result = mysqli_query($db, $statement)
526 or DBError("Space: Unable to execute query: ", $statement);
528 while ($row = mysqli_fetch_array ($result)) {
530 strlen($row["userid"]) +
531 strlen($row["sender"]) +
532 strlen($row["subject"]) +
533 strlen($row["timestamp"]) +
534 strlen($row["data"]);
535 $space += $msg_space;
538 mysqli_free_result($result);