This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/admin/main.php

197 lines
5.2 KiB
PHP
Raw Normal View History

<?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:
2012-03-19 17:48:42 +00:00
* Various moderate-level tools for admins; for advanced, obscure, and
* possibly dangerous tools see the shimmie2-utils script set
2010-01-05 10:11:53 +00:00
* <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, and remove any unused tags
2010-01-05 10:11:53 +00:00
* <p>Database dump:
* <br>Download the contents of the database in plain text format, useful
* for backups.
2012-03-19 17:48:42 +00:00
* <p>Image dump:
* <br>Download all the images as a .zip file
2010-01-05 10:11:53 +00:00
*/
2009-07-21 03:18:40 +00:00
/**
* Sent when the admin page is ready to be added to
*/
class AdminBuildingEvent extends Event {
var $page;
public function AdminBuildingEvent(Page $page) {
$this->page = $page;
}
}
2012-03-10 12:57:13 +00:00
class AdminActionEvent extends Event {
var $action;
var $redirect = true;
public function __construct(/*string*/ $action) {
$this->action = $action;
}
}
class AdminPage extends Extension {
public function onPageRequest(PageRequestEvent $event) {
2012-01-30 03:22:41 +00:00
global $page, $user;
2012-01-30 03:22:41 +00:00
if($event->page_matches("admin")) {
2012-03-30 19:54:33 +00:00
if(!$user->can("manage_admintools")) {
2012-02-15 08:59:24 +00:00
$this->theme->display_permission_denied();
}
else {
2012-03-10 12:57:13 +00:00
if($event->count_args() == 0) {
send_event(new AdminBuildingEvent($page));
}
2012-03-10 12:57:13 +00:00
else {
$action = $event->get_arg(0);
$aae = new AdminActionEvent($action);
2012-03-10 12:57:13 +00:00
if($user->check_auth_token()) {
log_info("admin", "Util: $action");
set_time_limit(0);
send_event($aae);
}
if($aae->redirect) {
$page->set_mode("redirect");
$page->set_redirect(make_link("admin"));
}
}
}
}
2012-01-30 03:22:41 +00:00
}
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_page();
$this->theme->display_form();
2012-01-30 03:22:41 +00:00
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2012-01-30 03:22:41 +00:00
global $user;
2012-03-30 19:54:33 +00:00
if($user->can("manage_admintools")) {
2012-01-30 03:22:41 +00:00
$event->add_link("Board Admin", make_link("admin"));
}
}
2012-03-10 12:57:13 +00:00
public function onAdminAction(AdminActionEvent $event) {
$action = $event->action;
if(method_exists($this, $action)) {
2012-03-10 12:57:13 +00:00
$event->redirect = $this->$action();
}
}
public function onPostListBuilding(PostListBuildingEvent $event) {
global $user;
2012-03-30 19:54:33 +00:00
if($user->can("manage_admintools") && !empty($event->search_terms)) {
$this->theme->display_dbq(implode(" ", $event->search_terms));
}
}
2012-03-10 12:57:13 +00:00
private function delete_by_query() {
2010-12-23 14:00:50 +00:00
global $page, $user;
2012-03-10 12:57:13 +00:00
$query = $_POST['query'];
2010-12-23 14:00:50 +00:00
assert(strlen($query) > 1);
2012-03-10 12:57:13 +00:00
2012-03-15 19:30:02 +00:00
log_warning("admin", "Mass deleting: $query");
2012-06-10 03:21:03 +00:00
$count = 0;
2010-12-23 14:00:50 +00:00
foreach(Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
send_event(new ImageDeletionEvent($image));
2012-06-10 03:21:03 +00:00
$count++;
2010-12-23 14:00:50 +00:00
}
2012-06-10 03:21:03 +00:00
log_debug("admin", "Deleted $count images", true);
2012-03-10 12:57:13 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link("post/list"));
return false;
2010-12-23 14:00:50 +00:00
}
private function lowercase_all_tags() {
global $database;
$database->execute("UPDATE tags SET tag=lower(tag)");
2012-06-10 03:21:03 +00:00
log_warning("admin", "Set all tags to lowercase", true);
2012-03-10 12:57:13 +00:00
return true;
}
private function recount_tag_use() {
global $database;
2009-07-28 00:19:40 +00:00
$database->Execute("
UPDATE tags
SET count = COALESCE(
(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
2012-03-10 12:57:13 +00:00
)
");
$database->Execute("DELETE FROM tags WHERE count=0");
2012-06-10 03:21:03 +00:00
log_warning("admin", "Re-counted tags", true);
2012-03-10 12:57:13 +00:00
return true;
}
2012-03-11 14:02:00 +00:00
private function database_dump() {
2012-03-10 12:57:13 +00:00
global $page;
$matches = array();
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'];
switch($software) {
case 'mysql':
$cmd = "mysqldump -h$hostname -u$username -p$password $database";
break;
2012-03-11 14:02:00 +00:00
case 'pgsql':
putenv("PGPASSWORD=$password");
$cmd = "pg_dump -h $hostname -U $username $database";
break;
case 'sqlite':
$cmd = "sqlite3 $database .dump";
break;
}
2009-01-04 19:18:37 +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));
2012-03-10 12:57:13 +00:00
return false;
}
2012-03-10 12:57:13 +00:00
private function download_all_images() {
global $database, $page;
2012-02-06 06:52:04 +00:00
$zip = new ZipArchive;
$images = $database->get_all("SELECT * FROM images");
2012-03-30 17:21:35 +00:00
$filename = data_path('imgdump-'.date('Ymd').'.zip');
2012-02-06 06:52:04 +00:00
2012-03-10 12:57:13 +00:00
if($zip->open($filename, 1 ? ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE) === TRUE){
2012-02-06 06:52:04 +00:00
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();
}
2012-03-10 12:57:13 +00:00
2012-02-06 06:52:04 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link($filename)); //Fairly sure there is better way to do this..
//TODO: Delete file after downloaded?
2012-03-10 12:57:13 +00:00
return false; // we do want a redirect, but a manual one
2012-02-06 06:52:04 +00:00
}
}
?>