2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/**
|
|
|
|
* Name: Admin Controls
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Various things to make admins' lives easier
|
|
|
|
* Documentation:
|
|
|
|
* <p>Lowercase all tags:
|
|
|
|
* <br>Set all tags to lowercase for consistency
|
|
|
|
* <p>Recount tag use:
|
|
|
|
* <br>If the counts of images per tag get messed up somehow, this will reset them
|
|
|
|
* <p>Purge unused tags:
|
|
|
|
* <br>Get rid of all the tags that don't have any images associated with
|
|
|
|
* them (normally they were created as typos or spam); this is mostly for
|
|
|
|
* neatness, the performance gain is tiny...
|
|
|
|
* <p>Convert to InnoDB:
|
|
|
|
* <br>Convert your database tables to InnoDB, thus allowing shimmie to
|
|
|
|
* take advantage of useful InnoDB-only features (this should be done
|
|
|
|
* automatically, this button only exists as a backup). This only applies
|
|
|
|
* to MySQL -- all other databases come with useful features enabled
|
|
|
|
* as standard.
|
|
|
|
* <p>Database dump:
|
|
|
|
* <br>Download the contents of the database in plain text format, useful
|
|
|
|
* for backups.
|
|
|
|
*/
|
|
|
|
|
2009-07-21 03:18:40 +00:00
|
|
|
/**
|
2007-04-16 11:58:25 +00:00
|
|
|
* Sent when the admin page is ready to be added to
|
|
|
|
*/
|
|
|
|
class AdminBuildingEvent extends Event {
|
2007-07-19 12:08:42 +00:00
|
|
|
var $page;
|
2012-02-08 11:24:25 +00:00
|
|
|
public function AdminBuildingEvent(Page $page) {
|
2007-07-19 12:08:42 +00:00
|
|
|
$this->page = $page;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:08:42 +00:00
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class AdminPage extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2012-01-30 03:22:41 +00:00
|
|
|
global $page, $user;
|
2007-07-19 12:08:42 +00:00
|
|
|
|
2012-01-30 03:22:41 +00:00
|
|
|
if($event->page_matches("admin")) {
|
2009-05-11 14:04:33 +00:00
|
|
|
if(!$user->is_admin()) {
|
2012-02-15 08:59:24 +00:00
|
|
|
$this->theme->display_permission_denied();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-01-05 11:07:13 +00:00
|
|
|
send_event(new AdminBuildingEvent($page));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 03:22:41 +00:00
|
|
|
if($event->page_matches("admin_utils")) {
|
2010-05-28 13:26:46 +00:00
|
|
|
if($user->is_admin() && $user->check_auth_token()) {
|
2009-05-08 11:43:45 +00:00
|
|
|
log_info("admin", "Util: {$_POST['action']}");
|
2008-04-11 05:36:36 +00:00
|
|
|
set_time_limit(0);
|
2008-05-22 20:41:46 +00:00
|
|
|
$redirect = false;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-11 05:36:36 +00:00
|
|
|
switch($_POST['action']) {
|
2010-12-23 14:00:50 +00:00
|
|
|
case 'delete by query':
|
|
|
|
$this->delete_by_query($_POST['query']);
|
|
|
|
$redirect = true;
|
|
|
|
break;
|
2008-04-11 05:36:36 +00:00
|
|
|
case 'lowercase all tags':
|
|
|
|
$this->lowercase_all_tags();
|
2008-05-22 20:41:46 +00:00
|
|
|
$redirect = true;
|
2008-04-11 05:36:36 +00:00
|
|
|
break;
|
|
|
|
case 'recount tag use':
|
|
|
|
$this->recount_tag_use();
|
2008-05-22 20:41:46 +00:00
|
|
|
$redirect = true;
|
2008-04-11 05:36:36 +00:00
|
|
|
break;
|
|
|
|
case 'purge unused tags':
|
|
|
|
$this->purge_unused_tags();
|
2008-05-22 20:41:46 +00:00
|
|
|
$redirect = true;
|
|
|
|
break;
|
2009-12-30 08:48:40 +00:00
|
|
|
case 'convert to innodb':
|
|
|
|
$this->convert_to_innodb();
|
|
|
|
$redirect = true;
|
|
|
|
break;
|
2008-05-22 20:41:46 +00:00
|
|
|
case 'database dump':
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->dbdump($page);
|
2008-04-11 05:36:36 +00:00
|
|
|
break;
|
2012-02-01 19:16:55 +00:00
|
|
|
case 'reset image ids':
|
|
|
|
$this->reset_imageids();
|
|
|
|
$redirect = true;
|
|
|
|
break;
|
2012-02-06 06:52:04 +00:00
|
|
|
case 'image dump':
|
|
|
|
$this->imgdump($page);
|
|
|
|
break;
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2008-05-22 20:41:46 +00:00
|
|
|
if($redirect) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("admin"));
|
2008-05-22 20:41:46 +00:00
|
|
|
}
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-30 03:22:41 +00:00
|
|
|
}
|
2008-04-11 05:36:36 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event) {
|
2012-01-30 03:22:41 +00:00
|
|
|
global $page;
|
|
|
|
$this->theme->display_page($page);
|
|
|
|
$this->theme->display_form($page);
|
|
|
|
}
|
2007-05-08 21:01:51 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
|
2012-01-30 03:22:41 +00:00
|
|
|
global $user;
|
|
|
|
if($user->is_admin()) {
|
|
|
|
$event->add_link("Board Admin", make_link("admin"));
|
2007-05-08 21:01:51 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2008-04-11 05:36:36 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
private function delete_by_query(/*array(string)*/ $query) {
|
2010-12-23 14:00:50 +00:00
|
|
|
global $page, $user;
|
|
|
|
assert(strlen($query) > 1);
|
|
|
|
foreach(Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 05:36:36 +00:00
|
|
|
private function lowercase_all_tags() {
|
|
|
|
global $database;
|
|
|
|
$database->execute("UPDATE tags SET tag=lower(tag)");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function recount_tag_use() {
|
|
|
|
global $database;
|
2009-07-28 00:19:40 +00:00
|
|
|
$database->Execute("
|
|
|
|
UPDATE tags
|
|
|
|
SET count = COALESCE(
|
2009-10-08 13:22:18 +00:00
|
|
|
(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id),
|
2009-07-28 00:19:40 +00:00
|
|
|
0
|
|
|
|
)");
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function purge_unused_tags() {
|
|
|
|
global $database;
|
|
|
|
$this->recount_tag_use();
|
|
|
|
$database->Execute("DELETE FROM tags WHERE count=0");
|
|
|
|
}
|
|
|
|
|
2012-02-10 04:04:37 +00:00
|
|
|
private function dbdump(Page $page) {
|
2008-05-22 20:41:46 +00:00
|
|
|
$matches = array();
|
2012-03-02 16:17:13 +00:00
|
|
|
preg_match("#^(?P<proto>\w+)\:(?:user=(?P<user>\w+)(?:;|$)|password=(?P<password>\w+)(?:;|$)|host=(?P<host>[\w\.\-]+)(?:;|$)|dbname=(?P<dbname>[\w_]+)(?:;|$))+#", DATABASE_DSN, $matches);
|
|
|
|
$software = $matches['proto'];
|
|
|
|
$username = $matches['user'];
|
|
|
|
$password = $matches['password'];
|
|
|
|
$hostname = $matches['host'];
|
|
|
|
$database = $matches['dbname'];
|
2008-05-22 20:41:46 +00:00
|
|
|
|
2012-02-10 04:04:37 +00:00
|
|
|
// TODO: Support more than just MySQL..
|
2008-05-22 20:41:46 +00:00
|
|
|
switch($software) {
|
|
|
|
case 'mysql':
|
|
|
|
$cmd = "mysqldump -h$hostname -u$username -p$password $database";
|
|
|
|
break;
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-05-22 20:41:46 +00:00
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_type("application/x-unknown");
|
|
|
|
$page->set_filename('shimmie-'.date('Ymd').'.sql');
|
|
|
|
$page->set_data(shell_exec($cmd));
|
|
|
|
}
|
|
|
|
|
2008-04-11 05:36:36 +00:00
|
|
|
private function check_for_orphanned_images() {
|
|
|
|
$orphans = array();
|
|
|
|
foreach(glob("images/*") as $dir) {
|
|
|
|
foreach(glob("$dir/*") as $file) {
|
|
|
|
$hash = str_replace("$dir/", "", $file);
|
|
|
|
if(!$this->db_has_hash($hash)) {
|
|
|
|
$orphans[] = $hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 08:48:40 +00:00
|
|
|
|
2011-01-01 19:00:30 +00:00
|
|
|
/*
|
2009-12-30 08:48:40 +00:00
|
|
|
private function convert_to_innodb() {
|
|
|
|
global $database;
|
|
|
|
if($database->engine->name == "mysql") {
|
|
|
|
$tables = $database->db->MetaTables();
|
|
|
|
foreach($tables as $table) {
|
|
|
|
log_info("upgrade", "converting $table to innodb");
|
|
|
|
$database->execute("ALTER TABLE $table TYPE=INNODB");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-01 19:00:30 +00:00
|
|
|
*/
|
2012-02-01 19:16:55 +00:00
|
|
|
|
|
|
|
private function reset_imageids() {
|
|
|
|
global $database;
|
|
|
|
//This might be a bit laggy on boards with lots of images (?)
|
|
|
|
//Seems to work fine with 1.2k~ images though.
|
|
|
|
$i = 0;
|
|
|
|
$image = $database->get_all("SELECT * FROM images ORDER BY images.id ASC");
|
|
|
|
/*$score_log = $database->get_all("SELECT message FROM score_log");*/
|
|
|
|
foreach($image as $img){
|
|
|
|
$xid = $img[0];
|
|
|
|
$i = $i + 1;
|
|
|
|
$table = array( //Might be missing some tables?
|
|
|
|
"image_tags", "tag_histories", "image_reports", "comments", "user_favorites", "tag_histories",
|
|
|
|
"numeric_score_votes", "pool_images", "slext_progress_cache", "notes");
|
|
|
|
|
|
|
|
$sql =
|
|
|
|
"SET FOREIGN_KEY_CHECKS=0;
|
|
|
|
UPDATE images
|
|
|
|
SET id=".$i.
|
|
|
|
" WHERE id=".$xid.";"; //id for images
|
|
|
|
|
|
|
|
foreach($table as $tbl){
|
|
|
|
$sql .= "
|
|
|
|
UPDATE ".$tbl."
|
|
|
|
SET image_id=".$i."
|
|
|
|
WHERE image_id=".$xid.";";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*foreach($score_log as $sl){
|
|
|
|
//This seems like a bad idea.
|
|
|
|
//TODO: Might be better for log_info to have an $id option (which would then affix the id to the table?)
|
|
|
|
preg_replace(".Image \\#[0-9]+.", "Image #".$i, $sl);
|
|
|
|
}*/
|
|
|
|
$sql .= " SET FOREIGN_KEY_CHECKS=1;";
|
|
|
|
$database->execute($sql);
|
|
|
|
}
|
|
|
|
$count = (count($image)) + 1;
|
|
|
|
$database->execute("ALTER TABLE images AUTO_INCREMENT=".$count);
|
|
|
|
}
|
2012-02-06 06:52:04 +00:00
|
|
|
|
2012-02-10 04:04:37 +00:00
|
|
|
private function imgdump(Page $page) {
|
2012-02-06 06:52:04 +00:00
|
|
|
global $database;
|
|
|
|
$zip = new ZipArchive;
|
|
|
|
$images = $database->get_all("SELECT * FROM images");
|
|
|
|
$filename = 'imgdump-'.date('Ymd').'.zip';
|
|
|
|
|
|
|
|
if($zip->open($filename, 1 ? ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE){
|
|
|
|
foreach($images as $img){
|
|
|
|
$hash = $img["hash"];
|
|
|
|
preg_match("^[A-Za-z0-9]{2}^", $hash, $matches);
|
|
|
|
$img_loc = "images/".$matches[0]."/".$hash;
|
|
|
|
if(file_exists($img_loc)){
|
|
|
|
$zip->addFile($img_loc, $hash.".".$img["ext"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
}
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link($filename)); //Fairly sure there is better way to do this..
|
|
|
|
//TODO: Delete file after downloaded?
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
?>
|