1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-25 16:00:22 +01:00
vichan/expire.php

153 lines
5.5 KiB
PHP
Raw Normal View History

2014-05-17 22:01:14 +02:00
<?php
include "inc/functions.php";
if (!(php_sapi_name() == "cli")) {
die('nope');
}
2014-11-09 01:57:43 +01:00
$protected = array('burgers', 'cow', 'wilno', 'cute', 'yoga');
2014-05-17 22:01:14 +02:00
$q = query("SELECT uri FROM boards");
$boards = $q->fetchAll(PDO::FETCH_COLUMN);
$now = new DateTime();
2014-12-03 09:21:11 +01:00
$ago = (new DateTime)->sub(new DateInterval('P7D'));
$mod_ago = (new DateTime)->sub(new DateInterval('P14D'));
2014-05-17 22:01:14 +02:00
// Find out the last activity for our board
$delete = array();
foreach($boards as $board) {
2014-09-13 18:36:16 +02:00
if (in_array($board, $protected)) {
continue;
}
2014-05-17 22:01:14 +02:00
// last post
$query = prepare(sprintf("SELECT MAX(time) AS time FROM posts_%s", $board));
$query->execute();
$row = $query->fetch();
//count posts
$query = prepare(sprintf("SELECT COUNT(id) AS count FROM posts_%s", $board));
$query->execute();
$count = $query->fetch();
$last_activity_date = new DateTime();
$last_mod_date = new DateTime();
$last_activity_date->setTimestamp($row['time']);
$query = query("SELECT id, username FROM mods WHERE boards = '$board'");
$mods = $query->fetchAll();
if ($mods) {
$mod = $mods[0]['id'];
2014-10-07 03:02:58 +02:00
$query = query("SELECT MAX(time) AS time FROM modlogs WHERE `mod` = BINARY $mod");
2014-05-17 22:01:14 +02:00
$a = $query->fetchAll(PDO::FETCH_COLUMN);
if ($a[0]) {
$last_mod_date->setTimestamp($a[0]);
if (!$row['time'])
$last_activity_date->setTimestamp($a[0]);
} else {// no one ever logged in, try board creation time
$query = query("SELECT UNIX_TIMESTAMP(time) AS time FROM board_create WHERE uri = '$board'");
$crt = $query->fetchAll(PDO::FETCH_COLUMN);
if (!empty($crt)) $last_activity_date->setTimestamp($crt[0]);
2014-05-17 22:01:14 +02:00
$last_mod_date = false;
}
}
if (($last_activity_date < $ago or ($last_mod_date and $last_mod_date < $mod_ago)) and (int)$count['count'] < 30 and isset($mods[0]['id'])) {
#if (($last_activity_date < $ago or ($last_mod_date and $last_mod_date < $mod_ago)) and isset($mods[0]['id'])) {
2014-06-10 19:57:21 +02:00
echo $board, ' ', $last_activity_date->format('Y-m-d H:i:s'), ' ', ($last_mod_date ? $last_mod_date->format('Y-m-d H:i:s') : 'false'), ' ', $count['count'], ' ', $mod, "\r\n";
2014-05-17 22:01:14 +02:00
$delete[] = array('board' => $board, 'last_activity' => $last_activity_date, 'last_mod' => $last_mod_date, 'mod' => isset($mods[0]['username']) ? $mods[0]['username'] : false, 'count' => $count['count']);
}
}
if ($argc > 1) {
$f = fopen('rip.txt', 'a');
2014-08-30 22:06:48 +02:00
fwrite($f, "--\r\n".date('c')."\r\n");
2014-05-17 22:01:14 +02:00
foreach($delete as $i => $d){
2015-01-14 21:15:33 +01:00
file_get_contents('http://8ch.net/listboards.php'); //I think this is bad
2014-07-21 18:54:16 +02:00
$s = "RIP /".$d['board']."/, created by ".($d['mod']?$d['mod']:'?')." and last active on ".$d['last_activity']->format('Y-m-d H:i:s.').($d['last_mod'] ? ' Mod last active on ' . $d['last_mod']->format('Y-m-d H:i:s.') : ' Mod never active.') . " Number of posts: {$d['count']}." . "\r\n";
2014-07-19 20:40:06 +02:00
echo $s;
2014-05-17 22:01:14 +02:00
fwrite($f, $s);
openBoard($d['board']);
$query = prepare('DELETE FROM ``boards`` WHERE `uri` = :uri');
$query->bindValue(':uri', $board['uri']);
$query->execute() or error(db_error($query));
if ($config['cache']['enabled']) {
cache::delete('board_' . $board['uri']);
cache::delete('all_boards');
}
// Delete posting table
$query = query(sprintf('DROP TABLE IF EXISTS ``posts_%s``', $board['uri'])) or error(db_error());
// Clear reports
$query = prepare('DELETE FROM ``reports`` WHERE `board` = :id');
$query->bindValue(':id', $board['uri'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
// Delete from table
$query = prepare('DELETE FROM ``boards`` WHERE `uri` = :uri');
$query->bindValue(':uri', $board['uri'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
$query = prepare("SELECT `board`, `post` FROM ``cites`` WHERE `target_board` = :board ORDER BY `board`");
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
if ($board['uri'] != $cite['board']) {
if (!isset($tmp_board))
$tmp_board = $board;
openBoard($cite['board']);
rebuildPost($cite['post']);
}
}
if (isset($tmp_board))
$board = $tmp_board;
$query = prepare('DELETE FROM ``cites`` WHERE `board` = :board OR `target_board` = :board');
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
$query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board');
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
// Remove board from users/permissions table
$query = query('SELECT `id`,`boards` FROM ``mods``') or error(db_error());
while ($user = $query->fetch(PDO::FETCH_ASSOC)) {
$user_boards = explode(',', $user['boards']);
if (in_array($board['uri'], $user_boards)) {
unset($user_boards[array_search($board['uri'], $user_boards)]);
$_query = prepare('UPDATE ``mods`` SET `boards` = :boards WHERE `id` = :id');
$_query->bindValue(':boards', implode(',', $user_boards));
$_query->bindValue(':id', $user['id']);
$_query->execute() or error(db_error($_query));
}
}
// Delete entire board directory
rrmdir($board['uri'] . '/');
2014-09-13 18:36:16 +02:00
rrmdir('static/banners/' . $board['uri']);
2014-09-26 00:53:56 +02:00
// HAAAAAX
if($config['dir']['img_root'] != '')
rrmdir($config['dir']['img_root'] . $board['uri']);
2014-05-29 16:41:20 +02:00
cache::delete('board_' . $board['uri']);
2014-05-17 22:01:14 +02:00
2014-06-18 00:58:53 +02:00
_syslog(LOG_NOTICE, "Board deleted: {$board['uri']}");
2014-05-17 22:01:14 +02:00
if ($d['mod']) {
$query = prepare('DELETE FROM ``mods`` WHERE `username` = BINARY :id');
2014-05-17 22:01:14 +02:00
$query->bindValue(':id', $d['mod']);
$query->execute() or error(db_error($query));
}
}
fclose($f);
}
2014-05-29 16:41:20 +02:00
cache::delete('all_boards_uri');
cache::delete('all_boards');
2014-05-17 22:01:14 +02:00
rebuildThemes('boards');
$query = query('DELETE FROM board_create WHERE uri NOT IN (SELECT uri FROM boards);') or error(db_error());