spamBee: sidebar plugin report caching.

This commit is contained in:
Grischa Brockhaus 2012-08-16 23:35:02 +02:00
parent 50e13c189a
commit 52fbef7487
6 changed files with 97 additions and 37 deletions

View file

@ -82,3 +82,5 @@
@define('PLUGIN_SPAMBLOCK_BEE_DBSEARCHES_DESC', 'Dieses plugin durchsucht die spamblocklog Tabelle. Hier kannst Du Suchen für Deine AntiSpam Maßnamen einstellen, über die gezählt werden soll. In jede Zeile kommt eine Einstellung, Format ist DeinNameFürAntiSpamArt:DbSuche. Beispiel: "BayesPlugin:%Bayes%" wird alle Einträge zählen, die "Bayes" irgendwo in ihrem Titel haben und sie als "BayesPlugin" im Report darstellen.');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN', 'Nur für eingeloggte Benutzer');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN_DESC', 'Wenn Du dies anschaltest, dann können nur eingeloggte Benutzer des Blogs die Statistik in der Seitenleiste sehen.');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS', 'Reporting Cache');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS_DESC', 'Das Erzeugen des Reports erzeugt Last auf er Datenbank, deshalb sollte der Report gecache werden. Hier wird eingestellt, wie viele Minuten mindestens gewartet werden soll, bevor ein neuer Report erstellt wird.');

View file

@ -82,3 +82,5 @@
@define('PLUGIN_SPAMBLOCK_BEE_DBSEARCHES_DESC', 'Dieses plugin durchsucht die spamblocklog Tabelle. Hier kannst Du Suchen für Deine AntiSpam Maßnamen einstellen, über die gezählt werden soll. In jede Zeile kommt eine Einstellung, Format ist DeinNameFürAntiSpamArt:DbSuche. Beispiel: "BayesPlugin:%Bayes%" wird alle Einträge zählen, die "Bayes" irgendwo in ihrem Titel haben und sie als "BayesPlugin" im Report darstellen.');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN', 'Nur für eingeloggte Benutzer');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN_DESC', 'Wenn Du dies anschaltest, dann können nur eingeloggte Benutzer des Blogs die Statistik in der Seitenleiste sehen.');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS', 'Reporting Cache');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS_DESC', 'Das Erzeugen des Reports erzeugt Last auf er Datenbank, deshalb sollte der Report gecache werden. Hier wird eingestellt, wie viele Minuten mindestens gewartet werden soll, bevor ein neuer Report erstellt wird.');

View file

@ -84,4 +84,6 @@
@define('PLUGIN_SPAMBLOCK_BEE_DBSEARCHES_DESC', 'This plugin scans the spamblocklog table to generate its reports. You can enter searches you want to do here. One line is one search. The line should look like YourNameForTheBlock:SqlSearch. For instance "BayesPlugin:%Bayes%" will count all entries having "Bayes" anywhere in there title and report them as "BayesPlugin" in the sidebar.');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN', 'Only for logged in users');
@define('PLUGIN_SPAMBLOCK_BEE_LOGGEDIN_DESC', 'If enabled, the sidebar will be visible only for logged in users (authors) of your blog.');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS', 'Cache the report');
@define('PLUGIN_SPAMBLOCK_BEE_CACHEMINS_DESC', 'Creating the report uses the database, so you shoul not create it with each pagecall but let it cache. Here you set up the wait time (in minutes) before a new statistic is created.');

View file

@ -10,8 +10,8 @@ $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $sere
if (file_exists($probelang)) {
include $probelang;
}
include dirname(__FILE__) . '/lang_en.inc.php';
include dirname(__FILE__) . '/version.inc.php';
require_once dirname(__FILE__) . '/json/json.php4.include.php';
@define('PLUGIN_EVENT_SPAMBLOCK_BEE_DEBUG', FALSE);
@ -106,7 +106,7 @@ class serendipity_event_spamblock_bee extends serendipity_event
'php' => '4.1.0'
));
$propbag->add('version', '1.2.4');
$propbag->add('version', PLUGIN_SPAMBLOCK_BEE_VERSION); // setup via version.inc.php
$propbag->add('event_hooks', array(
'frontend_comment' => true,
@ -379,7 +379,7 @@ class serendipity_event_spamblock_bee extends serendipity_event
$phone = $serendipity['POST']['phone'];
if ($this->useHoneyPot && (!empty($phone) || $phone == '0') ) {
if (mb_strlen($phone) > 40) {
$phone = mb_substr($phone, 0, 40) . '';
$phone = mb_substr($phone, 0, 40) . '..';
}
$this->spamlog($eventData['id'], 'REJECTED', "BEE Honeypot [" . $phone . "]", $addData);
$eventData = array('allow_comments' => false);
@ -427,7 +427,7 @@ class serendipity_event_spamblock_bee extends serendipity_event
if (!$isCorrect) {
if (mb_strlen($answer) > 40) {
$answer = mb_substr($answer, 0, 40) . '';
$answer = mb_substr($answer, 0, 40) . '..';
}
$this->processComment($this->hiddenCaptchaHandle, $eventData, $addData, PLUGIN_EVENT_SPAMBLOCK_BEE_ERROR_HCAPTCHA, "BEE HiddenCaptcha [ $correctAnswer[answer] != $answer ]");
return $isCorrect;
@ -1099,4 +1099,4 @@ class serendipity_event_spamblock_bee extends serendipity_event
break;
}
}
}
}

View file

@ -4,9 +4,14 @@ $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $sere
if (file_exists($probelang)) {
include $probelang;
}
include dirname(__FILE__) . '/lang_en.inc.php';
include dirname(__FILE__) . '/version.inc.php';
class serendipity_plugin_spamblock_bee extends serendipity_plugin {
var $title = PLUGIN_SPAMBLOCK_BEE_TITLE;
var $cache_file = null;
function introspect(&$propbag) {
$this->title = $this->get_config('title', $this->title);
@ -20,13 +25,13 @@ class serendipity_plugin_spamblock_bee extends serendipity_plugin {
'php' => '4.1.0'
));
$propbag->add('version', '1.01');
$propbag->add('version', PLUGIN_SPAMBLOCK_BEE_VERSION); // setup via version.inc.php
$propbag->add('groups', array('STATISTICS'));
$propbag->add('groups', array('STATISTICS'));
$configuration = array('title', 'db_search_pattern', 'days', 'loggedin_only', 'cachemin');
$configuration = array('title', 'db_search_pattern', 'days', 'loggedin_only');
$propbag->add('configuration', $configuration );
$propbag->add('configuration', $configuration );
}
function introspect_config_item($name, &$propbag) {
@ -51,11 +56,17 @@ class serendipity_plugin_spamblock_bee extends serendipity_plugin {
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_DBSEARCHES_DESC);
$propbag->add('rows', 4);
$propbag->add('default',
'Honepod:BEE Honeypot%
'Honepot:BEE Honeypot%
HiddenCaptcha:BEE HiddenCaptcha%
Bayes:%Bayes%'
);
break;
case 'cachemin':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_CACHEMINS);
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_CACHEMINS_DESC);
$propbag->add('default','10'); // 10min
break;
case 'loggedin_only':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_LOGGEDIN);
@ -76,34 +87,75 @@ Bayes:%Bayes%'
if ($_SESSION['serendipityAuthedUser'] != true) return;
}
$title = $this->get_config('title', $this->title);
$sql = "SELECT COUNT(*) as total FROM {$serendipity['dbPrefix']}spamblocklog WHERE reason like '%s' and timestamp>%d;";
$days = explode(',', $this->get_config('days'));
$searches = explode("\n", $this->get_config('db_search_pattern'));
$title = $this->get_config('title', $this->title);
$todayAtMidnight = mktime(0,0,0, date("n"), date("j"), date("Y"));
//echo "today: $todayAtMidnight<br>";
$timestampDay = 60 * 60 * 24;
//echo "diffDay: $timestampDay<br>";
foreach ($days as $day) {
$timestamp = $todayAtMidnight - ($timestampDay * (trim($day) -1));
if ($day==1) {
echo "<b>Heute:</b> <br>";
}
else {
echo "<b>Die letzen $day Tage:</b> <br>";
}
//echo "ts: $timestamp<br>";
foreach ($searches as $search) {
$singleSearch = explode(':', $search,2);
$singleSql = sprintf($sql, trim($singleSearch[1]), $timestamp);
//echo "$singleSql<br>";
$result = serendipity_db_query($singleSql,TRUE);
if (!empty($result['total'])) {
echo "{$singleSearch[0]}: {$result['total']}<br/>";
$stats = $this->loadCachedStats();
if (empty($stats)) {
$stats = "";
$sql = "SELECT COUNT(*) as total FROM {$serendipity['dbPrefix']}spamblocklog WHERE reason like '%s' and timestamp>%d;";
$days = explode(',', $this->get_config('days'));
$searches = explode("\n", $this->get_config('db_search_pattern'));
$todayAtMidnight = mktime(0,0,0, date("n"), date("j"), date("Y"));
//echo "today: $todayAtMidnight<br>";
$timestampDay = 60 * 60 * 24;
//echo "diffDay: $timestampDay<br>";
foreach ($days as $day) {
$timestamp = $todayAtMidnight - ($timestampDay * (trim($day) -1));
if ($day==1) {
$stats .= "<b>Heute:</b> <br>";
}
else {
$stats .= "<b>Die letzen $day Tage:</b> <br>";
}
//echo "ts: $timestamp<br>";
foreach ($searches as $search) {
$singleSearch = explode(':', $search,2);
$singleSql = sprintf($sql, trim($singleSearch[1]), $timestamp);
$result = serendipity_db_query($singleSql,TRUE);
if (!empty($result['total'])) {
$stats .= "{$singleSearch[0]}: {$result['total']}<br/>";
}
}
}
$this->cacheStats($stats);
}
echo $stats;
}
function loadCachedStats() {
$cacheFile = $this->getCacheFilename();
$cachesecs = $this->get_config('cachemin', '10') * 60;
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cachesecs)) {
$stats = "";
$fh = fopen($cacheFile,'r');
while (!feof($fh)){
$stats .= fgets($fh);
}
fflush($fh);
fclose($fh);
return $stats;
}
return null;
}
function cacheStats($stats) {
$cacheFile = $this->getCacheFilename();
$fh = fopen($cacheFile, 'w');
fputs($fh, $stats, strlen($stats));
fclose($fh);
}
/**
* Returns the cache file name
*/
function getCacheFilename(){
global $serendipity;
if ($this->cache_file === null) {
$this->cache_file = $serendipity['serendipityPath'] . PATH_SMARTY_COMPILE . '/serendipity_plugin_spamblog_bee';
}
return $this->cache_file;
}
}

View file

@ -0,0 +1,2 @@
<?php
@define('PLUGIN_SPAMBLOCK_BEE_VERSION', '1.2.3');