2012-07-28 01:34:45 +02:00
|
|
|
<?php
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
// Probe for a language include with constants. Still include defines later on, if some constants were missing
|
|
|
|
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
|
|
|
|
if (file_exists($probelang)) {
|
|
|
|
include $probelang;
|
|
|
|
}
|
2012-08-16 23:35:02 +02:00
|
|
|
include dirname(__FILE__) . '/lang_en.inc.php';
|
|
|
|
include dirname(__FILE__) . '/version.inc.php';
|
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
class serendipity_plugin_spamblock_bee extends serendipity_plugin {
|
|
|
|
var $title = PLUGIN_SPAMBLOCK_BEE_TITLE;
|
2015-09-03 11:43:49 +02:00
|
|
|
var $cache_file = null;
|
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
function introspect(&$propbag) {
|
|
|
|
$this->title = $this->get_config('title', $this->title);
|
|
|
|
|
|
|
|
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_TITLE);
|
|
|
|
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_DESC);
|
|
|
|
$propbag->add('stackable', false);
|
2012-08-19 17:15:20 +02:00
|
|
|
$propbag->add('author', 'Grischa Brockhaus, Janek Bevendorff');
|
2012-07-28 01:34:45 +02:00
|
|
|
$propbag->add('requirements', array(
|
|
|
|
'serendipity' => '0.8',
|
|
|
|
'smarty' => '2.6.7',
|
|
|
|
'php' => '4.1.0'
|
|
|
|
));
|
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
$propbag->add('version', PLUGIN_SPAMBLOCK_BEE_VERSION); // setup via version.inc.php
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
$propbag->add('groups', array('STATISTICS'));
|
2012-07-28 01:34:45 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
$configuration = array('title', 'db_search_pattern', 'days', 'loggedin_only', 'cachemin');
|
2012-07-28 01:34:45 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
$propbag->add('configuration', $configuration );
|
2012-07-28 01:34:45 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
function introspect_config_item($name, &$propbag) {
|
|
|
|
global $serendipity;
|
|
|
|
|
|
|
|
switch($name) {
|
|
|
|
case 'title':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', TITLE);
|
|
|
|
$propbag->add('description', TITLE_FOR_NUGGET);
|
|
|
|
$propbag->add('default', PLUGIN_SPAMBLOCK_BEE_TITLE);
|
|
|
|
break;
|
|
|
|
case 'days':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_DAYS);
|
|
|
|
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_DAYS_DESC);
|
|
|
|
$propbag->add('default','1,7,30');
|
2015-09-03 11:43:49 +02:00
|
|
|
break;
|
2012-07-28 01:34:45 +02:00
|
|
|
case 'db_search_pattern':
|
|
|
|
$propbag->add('type', 'text');
|
|
|
|
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_DBSEARCHES);
|
|
|
|
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_DBSEARCHES_DESC);
|
2012-08-14 23:59:18 +02:00
|
|
|
$propbag->add('rows', 4);
|
2015-09-03 11:43:49 +02:00
|
|
|
$propbag->add('default',
|
2012-08-19 17:46:02 +02:00
|
|
|
'Honeypot:BEE Honeypot%
|
2012-07-28 01:34:45 +02:00
|
|
|
HiddenCaptcha:BEE HiddenCaptcha%
|
|
|
|
Bayes:%Bayes%'
|
|
|
|
);
|
|
|
|
break;
|
2012-08-16 23:35:02 +02:00
|
|
|
case 'cachemin':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_CACHEMINS);
|
|
|
|
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_CACHEMINS_DESC);
|
2015-09-03 11:43:49 +02:00
|
|
|
$propbag->add('default', '10'); // 10min
|
|
|
|
break;
|
2012-07-28 01:34:45 +02:00
|
|
|
case 'loggedin_only':
|
|
|
|
$propbag->add('type', 'boolean');
|
|
|
|
$propbag->add('name', PLUGIN_SPAMBLOCK_BEE_LOGGEDIN);
|
|
|
|
$propbag->add('description', PLUGIN_SPAMBLOCK_BEE_LOGGEDIN_DESC);
|
2012-08-19 17:14:26 +02:00
|
|
|
$propbag->add('default', true);
|
2012-07-28 01:34:45 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
function generate_content(&$title) {
|
|
|
|
global $serendipity;
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-07-28 01:34:45 +02:00
|
|
|
if (serendipity_db_bool($this->get_config('loggedin_only', TRUE))) {
|
|
|
|
// show content if logged on only
|
|
|
|
if ($_SESSION['serendipityAuthedUser'] != true) return;
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
$title = $this->get_config('title', $this->title);
|
|
|
|
$statsCached = $this->loadCachedStats();
|
|
|
|
$stats = array();
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
if (!empty($statsCached)) {
|
|
|
|
$stats = $statsCached;
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
$days = explode(',', $this->get_config('days'));
|
|
|
|
$searches = preg_split('/(?:\r?\n|\r)/', $this->get_config('db_search_pattern'));
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
$todayAtMidnight = mktime(0,0,0, date("n"), date("j"), date("Y"));
|
|
|
|
$timestampDay = 60 * 60 * 24;
|
|
|
|
$statsString = '';
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
foreach ($days as $day) {
|
|
|
|
if (!isset($stats[$day])) {
|
|
|
|
$stats[$day] = array();
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
$timestamp = $todayAtMidnight - ($timestampDay * (trim($day) -1));
|
|
|
|
if ($day==1) {
|
|
|
|
$statsString .= '<b>' . PLUGIN_SPAMBLOCK_BEE_TODAY . '</b> <br>';
|
|
|
|
} else {
|
|
|
|
$statsString .= '<b>' . sprintf(PLUGIN_SPAMBLOCK_BEE_LAST_X_DAYS, $day) . '</b><br>';
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
foreach ($searches as $search) {
|
|
|
|
$singleSearch = explode(':', $search, 2);
|
|
|
|
$singleSearch[0] = trim($singleSearch[0]);
|
|
|
|
$singleSearch[1] = trim($singleSearch[1]);
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
if (empty($statsCached)) {
|
|
|
|
$sql = "SELECT COUNT(*) as total FROM {$serendipity['dbPrefix']}spamblocklog WHERE reason like '%s' and timestamp>%d;";
|
|
|
|
$singleSql = sprintf($sql, serendipity_db_escape_string($singleSearch[1]), $timestamp);
|
|
|
|
$result = serendipity_db_query($singleSql, true);
|
2012-08-16 23:35:02 +02:00
|
|
|
if (!empty($result['total'])) {
|
2012-08-19 17:14:26 +02:00
|
|
|
$stats[$day][$singleSearch[1]] = $result['total'];
|
|
|
|
} else {
|
|
|
|
$stats[$day][$singleSearch[1]] = 0;
|
2012-08-16 23:35:02 +02:00
|
|
|
}
|
2012-07-28 01:34:45 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
$searchResult = isset($stats[$day][$singleSearch[1]]) ? $stats[$day][$singleSearch[1]] : 0;
|
2012-08-19 17:55:45 +02:00
|
|
|
if ($searchResult) {
|
|
|
|
$statsString .= "{$singleSearch[0]}: {$searchResult}<br>";
|
|
|
|
}
|
2012-07-28 01:34:45 +02:00
|
|
|
}
|
2012-08-19 17:14:26 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
if (empty($statsCached)) {
|
2012-08-16 23:35:02 +02:00
|
|
|
$this->cacheStats($stats);
|
2012-07-28 01:34:45 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-19 17:14:26 +02:00
|
|
|
echo $statsString;
|
2012-07-28 01:34:45 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
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);
|
2012-08-19 17:14:26 +02:00
|
|
|
return unserialize($stats);
|
2012-08-16 23:35:02 +02:00
|
|
|
}
|
2012-08-19 17:14:26 +02:00
|
|
|
return array();
|
2012-08-16 23:35:02 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
function cacheStats($stats) {
|
2012-08-19 17:14:26 +02:00
|
|
|
$stats = serialize($stats);
|
2012-08-16 23:35:02 +02:00
|
|
|
$cacheFile = $this->getCacheFilename();
|
|
|
|
$fh = fopen($cacheFile, 'w');
|
|
|
|
fputs($fh, $stats, strlen($stats));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-16 23:35:02 +02:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
2012-08-19 17:14:26 +02:00
|
|
|
return $this->cache_file;
|
2012-08-16 23:35:02 +02:00
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
2012-08-16 23:38:47 +02:00
|
|
|
function cleanup() {
|
|
|
|
$cacheFile = $this->getCacheFilename();
|
|
|
|
if (file_exists($cacheFile)) {
|
|
|
|
unlink($cacheFile);
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 11:43:49 +02:00
|
|
|
|
|
|
|
}
|