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:
|
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:
|
2012-03-19 17:43:37 +00:00
|
|
|
* <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
|
|
|
/**
|
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-03-10 12:57:13 +00:00
|
|
|
class AdminActionEvent extends Event {
|
|
|
|
var $action;
|
|
|
|
var $redirect = true;
|
|
|
|
public function __construct(/*string*/ $action) {
|
|
|
|
$this->action = $action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")) {
|
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();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-10 12:57:13 +00:00
|
|
|
if($event->count_args() == 0) {
|
|
|
|
send_event(new AdminBuildingEvent($page));
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
2012-03-10 12:57:13 +00:00
|
|
|
else {
|
|
|
|
$action = $event->get_arg(0);
|
|
|
|
$aae = new AdminActionEvent($action);
|
2008-04-11 05:36:36 +00:00
|
|
|
|
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"));
|
|
|
|
}
|
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-03-10 18:50:10 +00:00
|
|
|
$this->theme->display_page();
|
|
|
|
$this->theme->display_form();
|
2012-01-30 03:22:41 +00:00
|
|
|
}
|
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;
|
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"));
|
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-03-10 12:57:13 +00:00
|
|
|
public function onAdminAction(AdminActionEvent $event) {
|
|
|
|
$action = $event->action;
|
2012-03-10 18:50:10 +00:00
|
|
|
if(method_exists($this, $action)) {
|
2012-03-10 12:57:13 +00:00
|
|
|
$event->redirect = $this->$action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 18:50:10 +00:00
|
|
|
public function onPostListBuilding(PostListBuildingEvent $event) {
|
|
|
|
global $user;
|
2012-03-30 19:54:33 +00:00
|
|
|
if($user->can("manage_admintools") && !empty($event->search_terms)) {
|
2012-03-10 18:50:10 +00:00
|
|
|
$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
|
|
|
|
2012-03-10 18:50:10 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
return false;
|
2010-12-23 14:00:50 +00:00
|
|
|
}
|
|
|
|
|
2008-04-11 05:36:36 +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;
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-03-10 12:57:13 +00:00
|
|
|
)
|
|
|
|
");
|
2008-04-11 05:36:36 +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;
|
2008-04-11 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 17:43:37 +00:00
|
|
|
|
2012-03-11 14:02:00 +00:00
|
|
|
private function database_dump() {
|
2012-03-10 12:57:13 +00:00
|
|
|
global $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
|
|
|
|
|
|
|
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;
|
2008-05-22 20:41:46 +00:00
|
|
|
}
|
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));
|
2012-03-10 12:57:13 +00:00
|
|
|
|
|
|
|
return false;
|
2008-05-22 20:41:46 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
?>
|