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