add('name', PLUGIN_EVENT_DBCLEAN_NAME);
$propbag->add('description', PLUGIN_EVENT_DBCLEAN_DESC);
$propbag->add('stackable', false);
$propbag->add('author', 'Malte Paskuda, Matthias Mees');
$propbag->add('version', '0.2.6');
$propbag->add('requirements', array(
'serendipity' => '0.8'
));
$propbag->add('event_hooks', array(
'backend_sidebar_admin' => true,
'backend_sidebar_entries_event_display_dbclean' => true,
'external_plugin' => true
)
);
$propbag->add('groups', array('BACKEND_FEATURES'));
}
function generate_content(&$title) {
$title = $this->title;
}
/*function introspect_config_item($name, &$propbag) {
}*/
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'external_plugin':
switch ($eventData) {
case 'dbclean':
if (! (serendipity_checkPermission('siteConfiguration') || serendipity_checkPermission('blogConfiguration'))) {
return;
}
$days = $_REQUEST['days'];
if (is_numeric($days)) {
if (isset($_REQUEST['spamblocklog'])) {
$this->cleanDB('spamblocklog', $days);
}
if (isset($_REQUEST['spamblock_htaccess'])) {
$this->cleanDB('spamblock_htaccess', $days);
}
if (isset($_REQUEST['visitors'])) {
$this->cleanDB('visitors', $days);
}
if (isset($_REQUEST['referrers'])) {
$this->cleanDB('referrers', $days);
}
if (isset($_REQUEST['exits'])) {
$this->cleanDB('exits', $days);
}
}
#redirect the user back to the menu
echo '';
return true;
break;
}
return true;
break;
case 'backend_sidebar_admin':
if ($serendipity['version'][0] == '1') {
echo '
';
} else {
echo '' .PLUGIN_EVENT_DBCLEAN_NAME .'';
}
return true;
break;
case 'backend_sidebar_entries_event_display_dbclean':
$this->displayMenu();
return true;
break;
default:
return false;
}
} else {
return false;
}
}
function cleanDB($table, $days) {
global $serendipity;
set_time_limit(0);
if($table=='visitors') {
$sql = "DELETE
FROM {$serendipity['dbPrefix']}visitors
WHERE unix_timestamp(concat(day,' ',time)) <" . (time() - ($days*24*60*60));
serendipity_db_query($sql);
} else if ($table =='referrers') {
$sql = "DELETE
FROM {$serendipity['dbPrefix']}$table
WHERE day <" . (time() - ($days*24*60*60));
serendipity_db_query($sql);
} else if ($table =='exits') {
$sql = "DELETE
FROM {$serendipity['dbPrefix']}$table
WHERE day < '" . date( 'Y-m-d', (time() - ($days*24*60*60))) ."'";
serendipity_db_query($sql);
} else {
$sql = "DELETE
FROM {$serendipity['dbPrefix']}$table
WHERE timestamp < " . (time() - ($days*24*60*60));
serendipity_db_query($sql);
}
switch($serendipity['dbType']) {
case 'sqlite':
case 'sqlite3':
$sql = "VACUUM";
serendipity_db_query($sql);
break;
case 'pdo-postgres':
case 'postgres':
$sql = "VACUUM";
serendipity_db_query($sql);
break;
case 'mysql':
case 'mysqli':
$sql = "OPTIMIZE TABLE
{$serendipity['dbPrefix']}$table";
serendipity_db_query($sql);
break;
}
}
function displayMenu() {
if ($serendipity['version'][0] == '1') {
echo '';
}
echo '' . PLUGIN_EVENT_DBCLEAN_NAME_MENU . '
';
echo '';
}
function countElements($table, $timespan=false) {
global $serendipity;
if (! $timespan) {
$sql = "SELECT COUNT(*)
FROM {$serendipity['dbPrefix']}$table";
} else {
if ($table=='visitors') {
$sql = "SELECT COUNT(*)
FROM {$serendipity['dbPrefix']}visitors
WHERE unix_timestamp(concat(day,' ',time)) < $timespan";
} else if ($table =='referrers') {
$sql = "SELECT COUNT(*)
FROM {$serendipity['dbPrefix']}$table
WHERE day < $timespan";
} else {
$sql = "SELECT COUNT(*)
FROM {$serendipity['dbPrefix']}$table
WHERE timestamp < $timespan";
}
}
$count = serendipity_db_query($sql);
if(is_array($count)) {
if (is_array($count[0])) {
return $count[0][0];
}
}
return 0;
}
function debugMsg($msg) {
global $serendipity;
$this->debug_fp = @fopen ( $serendipity ['serendipityPath'] . 'templates_c/dbclean.log', 'a' );
if (! $this->debug_fp) {
return false;
}
if (empty ( $msg )) {
fwrite ( $this->debug_fp, "failure \n" );
} else {
fwrite ( $this->debug_fp, print_r ( $msg, true ) );
}
fclose ( $this->debug_fp );
}
}
/* vim: set sts=4 ts=4 expandtab : */
?>