OSDN Git Service

*** empty log message ***
[nucleus-jp/nucleus-plugins.git] / trunk / NP_SpamBayes / spambayes / index.php
1 <?php
2         // vim: tabstop=2:shiftwidth=2
3         //
4         // Nucleus Admin section;
5         // Created by Xiffy
6         //
7         $strRel = '../../../';
8         include($strRel . 'config.php');
9         
10         include($DIR_LIBS . 'PLUGINADMIN.php');
11         
12         if ($blogid) {$isblogadmin = $member->isBlogAdmin($blogid);}
13         else $isblogadmin = 0;
14         
15         if (!($member->isAdmin() || $isblogadmin)) {
16                 $oPluginAdmin = new PluginAdmin('SpamBayes');
17                 $pbl_config = array();
18                 $oPluginAdmin->start();
19                 echo "<p>"._ERROR_DISALLOWED."</p>";
20                 $oPluginAdmin->end();
21                 exit;
22         }
23         
24         // Actions
25         $action = requestVar('page');
26         $aActionsNotToCheck = array(
27                 '',
28         );
29         if (!in_array($action, $aActionsNotToCheck)) {
30                 if (!$manager->checkTicket()) doError(_ERROR_BADTICKET);
31         }
32         
33         if (isset($_GET['page'])) {$action = $_GET['page'];}
34         if (isset($_POST['page'])) {$action = $_POST['page'];}
35         
36         // Okay; we are allowed. let's go
37         // create the admin area page
38         $oPluginAdmin = new PluginAdmin('SpamBayes');
39         $oPluginAdmin->start();
40         
41         if( !$oPluginAdmin->plugin->getOption('appid') ){
42                 echo '<h2>Plugin Error!</h2>';
43                 echo '<h3>Yahoo!Japan Application ID is not set.</h3>';
44                 $oPluginAdmin->end();
45                 exit;
46         }
47         
48         $action = requestVar('page');
49         if ($action == 'clearlog') {
50                 $filter     = requestVar('filter')     ? requestVar('filter')     : 'all';
51                 $filtertype = requestVar('filtertype') ? requestVar('filtertype') : 'all';
52                 $ipp        = requestVar('ipp')            ? requestVar('ipp')        : 10;
53                 $keyword        = requestVar('keyword');
54                 $oPluginAdmin->plugin->spambayes->nbs->clearlog($filter, $filtertype, $keyword, $ipp);
55                 $action = 'log';
56                 // reset values to no filter; otherwise the view will be empty
57                 if ($_REQUEST['amount'] <> 'cp' ) {
58                         $_REQUEST['filter']     = 'all';
59                         $_REQUEST['filtertype'] = 'all';
60                         $_REQUEST['keyword']    = '';
61                 }
62         }
63         
64         sb_nucmenu($oPluginAdmin->plugin->getOption('enableTrainall'),$oPluginAdmin->plugin->getOption('enableLogging'));
65         
66         switch ($action) {
67                 case 'update':
68                         $oPluginAdmin->plugin->spambayes->updateProbabilities();
69                         break;
70                 case 'trainall':
71                         sb_trainall();
72                         break;
73                 case 'trainnew':
74                         sb_trainnew();
75                         break;
76                 case 'train':
77                         sb_train();
78                         sb_trainform();
79                         break;
80                 case 'untrain':
81                         sb_untrain();
82                         break;
83                 case 'trainlog':
84                         sb_trainlog();
85                         sb_log();
86                         break;
87                 case 'untrainall':
88                         sb_untrainall();
89                         break;
90                 case 'test':
91                         sb_test();
92                         sb_testform();
93                         break;
94                 case 'log':
95                         sb_log();
96                         break;
97                 case 'explain':
98                         sb_explain();
99                         sb_log();
100                         break;
101                 case 'batch':
102                         sb_batch();
103                         sb_log();
104                         break;
105                 case 'promote':
106                         sb_promote();
107                         sb_log();
108                         break;
109                         
110                 case 'trainspamtb':
111                         sb_trainspamtb();
112                         break;
113                 case 'trainspamtbnew':
114                         sb_trainspamtbnew();
115                         break;
116                 case 'traintb':
117                         sb_traintb();
118                         break;
119                 case 'traintbnew':
120                         sb_traintbnew();
121                         break;
122                 case 'trainblocked':
123                         sb_trainblocked();
124                         break;
125                 case 'trainblockednew':
126                         sb_trainblockednew();
127                         break;
128         }
129         
130         $cats = $oPluginAdmin->plugin->spambayes->nbs->getCategories();
131         $i = 0;
132         $keys = array_keys($cats);
133         echo '<fieldset><legend>Baysian DB statistics</legend><table>';
134         echo '<tr><th>category</th><th>probability</th><th>wordcount</th></tr>';
135         foreach($cats as $category) {
136                 echo "<tr><td><b>$keys[$i]</b></td>";
137                 foreach($category as $key => $value) {
138                         echo '<td>'.$value.'</td>';
139                 }
140                 echo '</tr>';
141                 $i++;
142         }
143         echo '</table></fieldset>';
144         
145         function getPluginOption($name) {
146                 global $pbl_config;
147                 return $pbl_config[$name];
148         }
149         function getPlugid() {
150                 global $oPluginAdmin;
151                 return $oPluginAdmin->plugin->plugid;
152         }
153         
154         function sb_train() {
155                 global $oPluginAdmin;
156                 if (requestVar('catcode') > '' && requestVar('expression') > '') {
157                         $docid = $oPluginAdmin->plugin->spambayes->nbs->nextdocid();
158                         $oPluginAdmin->plugin->spambayes->train($docid,requestVar('catcode'),requestVar('expression'));
159                         $oPluginAdmin->plugin->spambayes->updateProbabilities();
160                 }
161         }
162         
163         function sb_trainlog() {
164                 global $oPluginAdmin;
165                 if (requestVar('catcode') > '' && requestVar('id') > 0) {
166                         $query = 'select content from '.$oPluginAdmin->plugin->table_log.' where id = '.intval(requestVar('id'));
167                         $res = sql_query($query);
168                         $arr = mysql_fetch_array($res);
169                         if ($arr['content']) {
170                                 $docid = $oPluginAdmin->plugin->spambayes->nbs->nextdocid();
171                                 $oPluginAdmin->plugin->spambayes->train($docid,requestVar('catcode'),$arr['content']);
172                                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
173                                 echo '<h3>document added to the database as: '.requestVar('catcode').'</h3>';
174                         } else {
175                                 echo 'An error occured';
176                         }
177                 }
178         }
179         
180         function sb_test() {
181                 global $oPluginAdmin;
182                 $expression = requestVar('expression');
183                 if ($expression > '') {
184                         $score = $oPluginAdmin->plugin->spambayes->categorize($expression);
185                         if ((float)$score['spam'] > (float)$oPluginAdmin->plugin->getOption('probability')) {
186                                 echo '<h2>Testresult: Spam! [score:'.$score['spam'].']</h2>';
187                         } else {
188                                 echo '<h2>Testresult: Ham! [score:'.$score['ham'].']</h2>';
189                         }
190                         echo '<fieldset style="width:90%;"><legend>Tested:</legend>';
191                         echo htmlspecialchars($expression,ENT_QUOTES);
192                         echo '</fieldset>';
193                 }
194         }
195         
196         function sb_trainall() {
197                 global $oPluginAdmin;
198                 // now train spam bayes with all current comments as ham!!
199                 $res = sql_query("select * from ".sql_table('comment'));
200                 while ($arr = mysql_fetch_array($res)) {
201                         $oPluginAdmin->plugin->spambayes->train($arr['cnumber'], 'ham', $arr['cbody'].' '.$arr['chost'].' '.$arr['cip']);
202                 }
203                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
204         }
205         
206         function sb_traintb() {
207                 global $oPluginAdmin;
208                 // now train spam bayes with all current trackbacks as ham!!
209                 $res = sql_query("select * from ".sql_table('plugin_tb').' where block = 0');
210                 while ($arr = mysql_fetch_array($res)) {
211                         $oPluginAdmin->plugin->spambayes->train($arr['id']+100000000, 'ham', $arr['title'].' '.$arr['excerpt'].' '.$arr['blog_name'].' '.$arr['url']);
212                 }
213                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
214         }
215         
216         function sb_traintbnew() {
217                 global $oPluginAdmin;
218                 // now train spam bayes with all current trackbacks as ham!!
219                 $res = sql_query("select * from ".sql_table('plugin_tb').' where block = 0');
220                 while ($arr = mysql_fetch_array($res)) {
221                         $oPluginAdmin->plugin->spambayes->trainnew($arr['id']+100000000, 'ham', $arr['title'].' '.$arr['excerpt'].' '.$arr['blog_name'].' '.$arr['url']);
222                 }
223                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
224         }
225
226         function sb_trainspamtb() {
227                 global $oPluginAdmin;
228                 // now train spam bayes with all blocked trackbacks as spam!!
229                 $res = sql_query("select * from ".sql_table('plugin_tb').' where block = 1');
230                 while ($arr = mysql_fetch_array($res)) {
231                         $oPluginAdmin->plugin->spambayes->train($arr['id']+100000000, 'spam', $arr['title'].' '.$arr['excerpt'].' '.$arr['blog_name'].' '.$arr['url']);
232                 }
233                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
234         }
235         
236         function sb_trainspamtbnew() {
237                 global $oPluginAdmin;
238                 // now train spam bayes with all blocked trackbacks as spam!!
239                 $res = sql_query("select * from ".sql_table('plugin_tb').' where block = 1');
240                 while ($arr = mysql_fetch_array($res)) {
241                         $oPluginAdmin->plugin->spambayes->trainnew($arr['id']+100000000, 'spam', $arr['title'].' '.$arr['excerpt'].' '.$arr['blog_name'].' '.$arr['url']);
242                 }
243                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
244         }
245         
246         function sb_trainnew() {
247                 global $oPluginAdmin;
248                 // now train spam bayes with all current comments as ham!!
249                 $res = sql_query("select * from ".sql_table('comment'));
250                 while ($arr = mysql_fetch_array($res)) {
251                         $oPluginAdmin->plugin->spambayes->trainnew($arr['cnumber'], 'ham', $arr['cbody'].' '.$arr['chost'].' '.$arr['cip']);
252                 }
253                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
254         }
255         
256         function sb_trainblocked() {
257                 global $oPluginAdmin;
258                 // now train spam bayes with all current comments as ham!!
259                 $res = sql_query("select * from ".sql_table('plug_cc_pending')." where processed = 1");
260                 while ($arr = mysql_fetch_array($res)) {
261                         $oPluginAdmin->plugin->spambayes->train($arr['id']+200000000, 'spam', $arr['cbody'].' '.$arr['chost'].' '.$arr['cip']);
262                 }
263                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
264         }
265         
266         function sb_trainblockednew() {
267                 global $oPluginAdmin;
268                 // now train spam bayes with all current comments as ham!!
269                 $res = sql_query("select * from ".sql_table('plug_cc_pending')." where processed = 1");
270                 while ($arr = mysql_fetch_array($res)) {
271                         $oPluginAdmin->plugin->spambayes->trainnew($arr['id']+200000000, 'spam', $arr['cbody'].' '.$arr['chost'].' '.$arr['cip']);
272                 }
273                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
274         }
275         
276         function sb_untrainall() {
277                 global $oPluginAdmin;
278                 // now untrain spam bayes with all current comments as ham!!
279                 $res = sql_query("select * from ".sql_table('comment'));
280                 while ($arr = mysql_fetch_array($res)) {
281                         $oPluginAdmin->plugin->spambayes->untrain($arr['cnumber']);
282                 }
283                 $oPluginAdmin->plugin->spambayes->updateProbabilities();
284         }
285         
286         function sb_untrain() {
287                 global $oPluginAdmin, $manager;
288                 if (requestVar('ref') > 0) {
289                         $oPluginAdmin->plugin->spambayes->untrain(requestVar('ref'));
290                         $oPluginAdmin->plugin->spambayes->updateProbabilities();
291                         echo '<h3>document untrained</h3>';
292                 }
293                 // build document table ...
294                 $startpos = requestVar('startpos') ? requestVar('startpos') : 0;
295                 $filterform = '<td></td>';
296                 $total = $oPluginAdmin->plugin->spambayes->nbs->countreftable();
297         
298                 $pager = buildpager($startpos, $total, $filter, $filtertype, $filterform,'untrain', $keyword, 10);
299                 $res = $oPluginAdmin->plugin->spambayes->nbs->getreftable($startpos);
300         
301                 echo '<h2>Spam Bayesian: Training data ['.$total.'] </h2>';
302                 echo '<table>';
303                 echo $pager;
304                 echo '<tr><th>Type</th><th>content</th><th>action</th></tr>';
305         
306                 while ($arr = mysql_fetch_array($res)) {
307                         echo '<tr><td>'.$arr['catcode'].'</td><td>'.htmlspecialchars($arr['content'],ENT_QUOTES).'</td><td><a href="'.htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF').'?page=untrain&ref='.$arr['ref']),ENT_QUOTES).'">untrain</a></td></tr>';
308                 }
309                 echo $pager;
310                 echo '</table>';
311         }
312         
313         function sb_explain(){
314                 global $oPluginAdmin;
315                 $id = requestVar('id');
316                 echo '<h2>Explain: Scorelog unweighed results (sorted on ham scores)</h2>';
317                 $arr = $oPluginAdmin->plugin->spambayes->nbs->getLogevent($id);
318         
319                 $oPluginAdmin->plugin->spambayes->explain($arr['content']);
320         }
321         
322         function sb_promote(){
323                 global $oPluginAdmin;
324                 $id = requestVar('id');
325                 echo '<h2>Promoting to blog: '.$id.'</h2>';
326                 $arr = $oPluginAdmin->plugin->spambayes->nbs->getLogevent($id);
327                 $itemid = explode('itemid:', $arr['log']);
328                 $itemid = $itemid[1];
329                 echo 'itemid: '.$itemid.'<br />';
330                 $blogid = getBlogIDFromItemID($itemid);
331                 $comment = explode('^^',$arr['content']);
332         
333                 $body           = addslashes($comment[0]);
334                 $host           = addslashes($comment[1]);
335                 $name           = addslashes($comment[2]);
336                 $url            = addslashes($comment[3]);
337                 $ip                     = addslashes($comment[4]);
338                 $memberid       = 0;
339                 $timestamp      = $arr['logtime'];
340         
341         
342                 $query = 'INSERT INTO '.sql_table('comment').' (CUSER, CMAIL, CMEMBER, CBODY, CITEM, CTIME, CHOST, CIP, CBLOG) '
343                                    . "VALUES ('$name', '$url', $memberid, '$body', $itemid, '$timestamp', '$host', '$ip', '$blogid')";
344                 sql_query($query);
345                 echo '<b>comment added</b><br />';
346                 echo '-- end promote --';
347         }
348         
349         function sb_batch() {
350                 global $oPluginAdmin;
351                 $logids = requestIntArray('batch');
352                 $action = requestVar('batchaction');
353                 //debug: var_dump($logids);
354                 if ($logids) foreach ($logids as $id) {
355                         switch ($action) {
356                                 case 'tspam':
357                                 case 'tham':
358                                         $ar = $oPluginAdmin->plugin->spambayes->nbs->getLogevent($id);
359                                         $docid = $oPluginAdmin->plugin->spambayes->nbs->nextdocid();
360                                         $cat = substr($action,1);
361                                         $oPluginAdmin->plugin->spambayes->train($docid,$cat,$ar['content']);
362                                         echo 'train '.$cat.': '.$id.'<br />';
363                                         break;
364                                 case 'delete':
365                                         echo 'delete: '.$id.'<br />';
366                                         $oPluginAdmin->plugin->spambayes->nbs->removeLogevent($id);
367                         }
368                 }
369         
370                 echo '--end of batch--';
371         }
372         
373         function sb_nucmenu($trainall, $logging) {
374                 global $oPluginAdmin, $manager;
375                 ?>
376         
377         <!-- sorry, it's stronger then me :-) this javascript less popup's are styled using: http://meyerweb.com/eric/css/edge/popups/demo.html -->
378         <style type="text/css">
379                                 .adminmenu span {
380                                         display:none;
381                                 }
382                                 .adminmenu a:hover span {
383                                         display:block;
384                                         position: absolute;
385                                         text-decoration: none;
386                                         top: 100px;
387                                         left: 350px;
388                                         width: 225px;
389                                         background-color:#ffff7d;
390                                         padding: 10px;
391                                         font-weight: normal;
392                                         font-size: 14px;
393                                         border: 1px solid black;
394                                         z-index: 100;
395                                 }
396                                 .adminmenu a:hover {
397                                         background-color: #ffff7d;
398                                 }
399                         </style>
400                 <?php
401                 $total = $oPluginAdmin->plugin->spambayes->nbs->countlogtable('all');
402                 echo "<h2>SpamBayes menu</h2>\n";
403                 echo "<ul class=\"adminmenu\">\n";
404                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=train"),ENT_QUOTES)."\">Spam Bayes training<span>Use this to train the Spam Bayesian filter with either 'ham' (not spam) or 'spam' messages. Your Bayessian filter needs both type of messages. The filter will become better with each message submitted.</span></a></li>\n";
405                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=untrain"),ENT_QUOTES)."\">Spam Bayes untraining<span>Use this to remove references to a earlier trained document.</span></a></li>\n";
406                 if ($logging == 'yes') {
407                         echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=log"),ENT_QUOTES)."\">Spam Bayes log ($total)<span>This page shows you the logging of Spam Bayes. You can browse through all 'ham' and 'spam' messages and train the filter with them if you like. (Especially usefull when SpamBayes got it wrong).</span></a></li>\n";
408                 }
409                 if ($trainall == 'yes') {
410                         echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainall"),ENT_QUOTES)."\">Train HAM (not spam) with all comments<span>Use this to train the Spam Bayesian filter with all your comments as 'ham' (not spam). This can take a while but you don't have to do anything. Just sit back and relax. Once you've run this option it's save to remove it from the menu. (See options)</span></a></li>\n";
411                         //echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainblocked"),ENT_QUOTES)."\">Train spam with all blocked comments</a></li>\n";
412                         echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=traintb"),ENT_QUOTES)."\">Train ham with all trackbacks.</a></li>\n";
413                         echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainspamtb"),ENT_QUOTES)."\">Train spam with all blocked trackbacks.</a></li>\n";
414                         echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=untrainall"),ENT_QUOTES)."\">Remove all comments from the HAM (not spam).<span>Use this to untrain the Spam Bayesian filter. This can take a while but you don't have to do anything. Just sit back and relax. Use only if you think earlier training went wrong.</span></a></li>\n";
415                 }
416                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainnew"),ENT_QUOTES)."\">Train HAM (not spam) with all NEW comments<span>Use this to train the Spam Bayesian filter with all your yet untrained comments as 'ham' (not spam). This can take a while but you don't have to do anything. Just sit back and relax. You can use this option as much as you like. Only untrained comments will be added.</span></a></li>\n";
417                 //echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainblockednew"),ENT_QUOTES)."\">Train spam with all NEW blocked comments</a></li>\n";
418                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=traintbnew"),ENT_QUOTES)."\">Train HAM (not spam) with all NEW trackbacks.</a></li>\n";
419                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=trainspamtbnew"),ENT_QUOTES)."\">Train spam with all NEW blocked trackbacks.</a></li>\n";
420                 //echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=update"),ENT_QUOTES)."\">Update probabilities<span>After some training, you must use this to finalise</span></a></li>\n";
421                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?page=test"),ENT_QUOTES)."\">Spam Bayes Test<span>Use this to test if a certain message would be considered 'ham' (not spam) or 'spam' message</span></a></li>\n";
422                 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(dirname(serverVar('PHP_SELF'))."/../../index.php?action=pluginoptions&plugid=".getPlugid()),ENT_QUOTES)."\">Spam Bayes options<span>This will take you to the plugins options page. This menu is NOT available on that page. Sorry for this. Use the quickmenu option to show a quicklink to the admin page!</span></a></li>\n";
423                 echo "</ul>\n";
424         }
425         
426         function sb_log() {
427                 global $oPluginAdmin, $manager;
428                 $ticket = $manager->_generateTicket();
429                 
430                 $startpos   = requestVar('startpos')   ? requestVar('startpos')   : 0;
431                 $filter     = requestVar('filter')     ? requestVar('filter')     : 'all';
432                 $filtertype = requestVar('filtertype') ? requestVar('filtertype') : 'all';
433                 $ipp        = requestVar('ipp')        ? requestVar('ipp')        : 10;
434                 $keyword    = requestVar('keyword');
435                 $filterform = buildfilterform($filter,$filtertype,$keyword,$ipp);
436         
437                 $total      = $oPluginAdmin->plugin->spambayes->nbs->countlogtable($filter, $filtertype, $keyword);
438                 if ($filter == 'all') {
439                         $htotal = $oPluginAdmin->plugin->spambayes->nbs->countlogtable('ham',$filtertype, $keyword);
440                         $stotal = $oPluginAdmin->plugin->spambayes->nbs->countlogtable('spam',$filtertype, $keyword);
441                         echo '<h2>Spam Bayesian: Log [total events: '.$total.' (ham: '.$htotal.' spam: '.$stotal.') ]</h2>';
442                 } else {
443                         echo '<h2>Spam Bayesian: Log [total '.$filter.' events: '.$total.']</h2>';
444                 }
445         
446                 $res = $oPluginAdmin->plugin->spambayes->nbs->getlogtable($startpos,$filter, $filtertype, $keyword, $ipp);
447                 $pager = buildpager($startpos, $total, $filter, $filtertype, $filterform,'log', $keyword, $ipp);
448                 if ($total % $ipp == 0) {
449                         $ap = intval(floor($total / $ipp));
450                 } else {
451                         $ap = intval(floor($total / $ipp)) + 1;
452                 }
453                 $cp = intval($startpos + $ipp) / $ipp;
454                 echo '<table>';
455                 echo '<tr><th colspan="2">Page '.$cp.' of '.$ap.'</th><td colspan="2">Browse: <form style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="text" size="3" name="ipp" value="'.$ipp.'" /> items per page. <input type="submit" value="Go" /><input type="hidden" name="amount" value="cp" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="hidden" name="page" value="log" /></form>';
456                 echo '<span style="text-align:right" class="batchoperations">';
457                 if ($filter <> 'all') {
458                         echo ' type: <b>'.$filter.'</b>';
459                 }
460                 if ($filtertype <> 'all') {
461                         echo ' event: <b>'.$filtertype.'</b>';
462                 }
463                 if ($keyword > '') {
464                         echo ' keyword: <b>'.$keyword.'</b>';
465                 }
466                 echo '</span></td></tr>';
467                 echo $pager;
468                 $extraaction = '&filter='.$filter.'&filtertype='.urlencode($filtertype).'&startpos='.$startpos.'&keyword='.$keyword.'&ipp='.$ipp.'&ticket='.$ticket;
469                 echo '<tr><th>Date</th><th>event</th><th>content</th><th>action</th></tr><form method="post"><input type="hidden" name="ticket" value="'.$ticket.'" />';
470                 $i = 0;
471                 while ($arr = mysql_fetch_array($res)) {
472                         echo '<tr onmouseover="focusRow(this);" onmouseout="blurRow(this);"><td>'.$arr['logtime'].'<br /><b>'.$arr['catcode'].'</b></td><td>'.$arr['log'].'</td><td><input id="batch'.$i.'" name="batch['.$i.']" value="'.$arr['id'].'" type="checkbox"><label for="batch'.$i.'">'.htmlspecialchars(str_replace('^^', ' ',$arr['content']),ENT_QUOTES).'</label></td>';
473                         echo '<td><a href="'.htmlspecialchars(serverVar('PHP_SELF').'?page=trainlog&catcode=ham&id='.$arr['id'].$extraaction,ENT_QUOTES).'"><nobr>train ham</nobr></a>';
474                         echo ' <a href="'.htmlspecialchars(serverVar('PHP_SELF').'?page=trainlog&catcode=spam&id='.$arr['id'].$extraaction,ENT_QUOTES).'"><nobr>train spam</nobr></a>';
475                         echo '<br /><a href="'.htmlspecialchars(serverVar('PHP_SELF').'?page=explain&id='.$arr['id'].$extraaction,ENT_QUOTES).'"><nobr>explain</nobr></a>';
476                         if (strstr($arr['log'], 'itemid:')) {
477                                 echo '<br /><br /><a style="color:red" href="'.htmlspecialchars(serverVar('PHP_SELF').'?page=promote&id='.$arr['id'].$extraaction,ENT_QUOTES).'"><nobr>publish</nobr></a>';
478                         }
479                         echo '</td>';
480                         echo '</tr>';
481                         $i++;
482                 }
483                 if (mysql_num_rows($res) == 0) {
484                         echo '<tr><td colspan="4"><b>Eventlog is empty</b></td></tr>';
485                 }
486                 echo '<tr><td colspan="4"><div class="batchoperations">with selected:<select name="batchaction">';
487                 echo '<option value="tspam">Train spam</option>';
488                 echo '<option value="tham">Train ham</option>';
489                 echo '<option value="delete">Delete</option></select><input name="page" value="batch" type="hidden">';
490                 echo '<input type="hidden" name="ipp" value="'.$ipp.'"/><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" />';
491                 echo '<input value="Execute" type="submit">(
492                                  <a href="" onclick="if (event && event.preventDefault) event.preventDefault(); return batchSelectAll(1); ">select all</a> -
493                                  <a href="" onclick="if (event && event.preventDefault) event.preventDefault(); return batchSelectAll(0); ">deselect all</a>
494                                 )
495                         </div></td></tr></form>';
496                 echo '<tr><td colspan="4"><div class="batchoperations"><form action="" method="get" style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="hidden" name="ipp" value="'.$ipp.'"/><input type="hidden" name="page" value="clearlog" /><input type="hidden" name="amount" value="cp" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="submit" value="Clear first '.$ipp.'" /></form> <form action="" method="get" style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="hidden" name="ipp" value="'.$ipp.'"/><input type="hidden" name="page" value="clearlog" /><input type="hidden" name="amount" value="cf" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="submit" value="Clear current filtered logs" /></form> <form action="" method="get" style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="hidden" name="page" value="clearlog" /><input type="submit" value="Clear complete log" /></form></div></td></tr>';
497                 echo '<tr><th colspan="2">Page '.$cp.' of '.$ap.'</th><td colspan="2">Browse: <form style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="text" size="3" name="ipp" value="'.$ipp.'" /> items per page. <input type="submit" value="Go" /><input type="hidden" name="amount" value="cp" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="hidden" name="page" value="log" /></form></td></tr>';
498                 echo $pager;
499                 echo '</table>';
500         }
501         
502         function sb_trainform() {
503                 global $manager;
504                 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
505                 echo $manager->addTicketHidden();
506                 echo "<input type=\"hidden\" name=\"page\" value=\"train\" />\n";
507                 echo "<select name=\"catcode\"><option value=\"ham\">Ham (not spam)</option><option value=\"spam\" selected=\"1\">Spam</option></select><br />";
508                 echo "<textarea class=\"sb_textinput\" cols=\"60\" rows=\"6\" name=\"expression\" ></textarea><br />";
509                 echo "<input type=\"submit\" value=\"Train\" />\n";
510                 echo "</form>\n";
511         }
512         
513         function sb_testform() {
514                 global $manager;
515                 echo "<h2>Enter a message that needs to be tested against Spam Bayes</h2>";
516                 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
517                 echo $manager->addTicketHidden();
518                 echo "<input type=\"hidden\" name=\"page\" value=\"test\" />\n";
519                 echo "<textarea class=\"sb_textinput\" cols=\"60\" rows=\"6\" name=\"expression\" ></textarea><br />";
520                 echo "<input type=\"submit\" value=\"Test this!\" />\n";
521                 echo "</form>\n";
522         }
523         
524         function buildpager($startpos, $total, $filter, $filtertype, $filterform, $action, $keyword, $ipp) {
525                 global $manager;
526                 $ticket = $manager->_generateTicket();
527                 
528                 $pager = '<tr>';
529                 if ($startpos >= $ipp) {
530                         $pager .= '<td><form action="" method="get" style="display:inline"><input type="hidden" name="page" value="'.$action.'" />';
531                         $pager .= '<input type="hidden" value="'.($startpos - $ipp).'" name="startpos" /><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="submit" value="Previous page" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="hidden" name="ipp" value="'.$ipp.'"/></form></td>'.$filterform;
532                 } else {
533                         $pager .= '<td></td>'.$filterform;
534                 }
535                 if (($total - $ipp) > $startpos) {
536                         $pager .= '<td><form action="" method="get" style="display:inline"><input type="hidden" name="page" value="'.$action.'" />';
537                         $pager .= '<input type="hidden" value="'.($startpos + $ipp).'" name="startpos" /><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="submit" value="Next page" /><input type="hidden" name="filter" value="'.$filter.'" /><input type="hidden" name="filtertype" value="'.$filtertype.'" /><input type="hidden" name="keyword" value="'.$keyword.'" /><input type="hidden" name="ipp" value="'.$ipp.'"/></form></td>';
538                 } else {
539                         $pager .= '<td></td>';
540                 }
541                 $pager .= '</tr>';
542                 return $pager;
543         }
544         
545         function buildfilterform($filter,$filtertype, $keyword, $ipp) {
546                 global $oPluginAdmin, $manager;
547                 $ticket = $manager->_generateTicket();
548
549                 $selected   = $filter == 'all' ? 'selected':'';
550                 $filterform = '<td colspan="2"><form style="display:inline">Show: <select name="filter"><option value="all" '.$selected.'>All</option>';
551                 $selected   = $filter == 'ham' ? 'selected':'';
552                 $filterform .= '<option value="ham" '.$selected.'>Ham (not spam)</option>';
553                 $selected   = $filter == 'spam' ? 'selected':'';
554                 $filterform .= '<option value="spam" '.$selected.'>Spam</option></select> <input type="hidden" name="page" value="log"/><input type="hidden" name="ipp" value="'.$ipp.'"/>';
555         
556                 $logtypes   = $oPluginAdmin->plugin->spambayes->nbs->getlogtypes();
557                 $selected   = $filtertype == 'all' ? 'selected':'';
558                 $filterform .= '<select name="filtertype"><option value="all" '.$selected.'>All events</option>';
559                 foreach($logtypes as $logtype) {
560                         $selected = $filtertype == $logtype ? 'selected' : '';
561                         $show = explode(' ',$logtype);
562                         $show = $show[0] == 'event' ? $show[1] : $show[0];
563                         $filterform .= '<option value="'.$logtype.'" '.$selected.'>'.$show.'</option>';
564                 }
565                 $filterform .= '</select><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="submit" value="Apply filter" /></form>';
566                 $filterform .= '&nbsp;|&nbsp;<form style="display:inline"><input type="hidden" name="ticket" value="'.$ticket.'" /><input type="hidden" name="page" value="log"/><input type="hidden" name="filter" value="'.$filter.'"/><input type="hidden" name="filtertype" value="'.$filtertype.'"/><input type="hidden" name="ipp" value="'.$ipp.'"/><input type="text" name="keyword" value="'.$keyword.'" /><input type="submit" value="search" /></form>';
567                 $filterform .= '</td>';
568                 return $filterform;
569         }
570         $oPluginAdmin->end();