2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/**
|
|
|
|
* Name: Alias Editor
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Edit the alias list
|
|
|
|
* Documentation:
|
2010-01-05 13:13:11 +00:00
|
|
|
* The list is visible at <a href="$site/alias/list">/alias/list</a>; only
|
|
|
|
* site admins can edit it, other people can view and download it
|
2010-01-05 10:11:53 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
class AddAliasEvent extends Event {
|
2014-04-28 06:43:49 +00:00
|
|
|
/** @var string */
|
|
|
|
public $oldtag;
|
|
|
|
/** @var string */
|
|
|
|
public $newtag;
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $oldtag, string $newtag) {
|
2013-07-06 09:41:19 +00:00
|
|
|
$this->oldtag = trim($oldtag);
|
|
|
|
$this->newtag = trim($newtag);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 14:01:59 +00:00
|
|
|
class AddAliasException extends SCoreException {}
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class AliasEditor extends Extension {
|
2010-05-15 11:17:32 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2010-05-15 11:17:32 +00:00
|
|
|
if($event->page_matches("alias")) {
|
2007-06-04 03:31:52 +00:00
|
|
|
if($event->get_arg(0) == "add") {
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("manage_alias_list")) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(isset($_POST['oldtag']) && isset($_POST['newtag'])) {
|
2009-01-04 14:01:59 +00:00
|
|
|
try {
|
|
|
|
$aae = new AddAliasEvent($_POST['oldtag'], $_POST['newtag']);
|
|
|
|
send_event($aae);
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("alias/list"));
|
2008-08-12 00:04:10 +00:00
|
|
|
}
|
2009-01-04 14:01:59 +00:00
|
|
|
catch(AddAliasException $ex) {
|
2012-02-16 15:37:05 +00:00
|
|
|
$this->theme->display_error(500, "Error adding alias", $ex->getMessage());
|
2009-01-04 14:01:59 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-04 03:31:52 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "remove") {
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("manage_alias_list")) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(isset($_POST['oldtag'])) {
|
2011-02-22 22:35:07 +00:00
|
|
|
$database->execute("DELETE FROM aliases WHERE oldtag=:oldtag", array("oldtag" => $_POST['oldtag']));
|
2012-06-10 03:21:03 +00:00
|
|
|
log_info("alias_editor", "Deleted alias for ".$_POST['oldtag'], true);
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("alias/list"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-04 03:31:52 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "list") {
|
2009-08-25 01:36:00 +00:00
|
|
|
$page_number = $event->get_arg(1);
|
|
|
|
if(is_null($page_number) || !is_numeric($page_number)) {
|
|
|
|
$page_number = 0;
|
|
|
|
}
|
|
|
|
else if ($page_number <= 0) {
|
|
|
|
$page_number = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page_number--;
|
|
|
|
}
|
|
|
|
|
|
|
|
$alias_per_page = $config->get_int('alias_items_per_page', 30);
|
|
|
|
|
2011-01-01 16:28:04 +00:00
|
|
|
$query = "SELECT oldtag, newtag FROM aliases ORDER BY newtag ASC LIMIT :limit OFFSET :offset";
|
|
|
|
$alias = $database->get_pairs($query,
|
|
|
|
array("limit"=>$alias_per_page, "offset"=>$page_number * $alias_per_page)
|
2009-08-25 01:36:00 +00:00
|
|
|
);
|
|
|
|
|
2011-01-01 16:28:04 +00:00
|
|
|
$total_pages = ceil($database->get_one("SELECT COUNT(*) FROM aliases") / $alias_per_page);
|
2009-08-25 01:36:00 +00:00
|
|
|
|
2012-02-07 15:15:18 +00:00
|
|
|
$this->theme->display_aliases($alias, $page_number + 1, $total_pages);
|
2007-06-04 03:31:52 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "export") {
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("data");
|
2015-03-20 22:16:20 +00:00
|
|
|
$page->set_type("text/csv");
|
|
|
|
$page->set_filename("aliases.csv");
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_data($this->get_alias_csv($database));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2008-02-16 03:58:05 +00:00
|
|
|
else if($event->get_arg(0) == "import") {
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("manage_alias_list")) {
|
2008-02-16 03:58:05 +00:00
|
|
|
if(count($_FILES) > 0) {
|
|
|
|
$tmp = $_FILES['alias_file']['tmp_name'];
|
|
|
|
$contents = file_get_contents($tmp);
|
|
|
|
$this->add_alias_csv($database, $contents);
|
2012-06-10 03:21:03 +00:00
|
|
|
log_info("alias_editor", "Imported aliases from file", true); # FIXME: how many?
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("alias/list"));
|
2008-02-16 03:58:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-02-16 15:37:05 +00:00
|
|
|
$this->theme->display_error(400, "No File Specified", "You have to upload a file");
|
2008-02-16 03:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-02-16 15:37:05 +00:00
|
|
|
$this->theme->display_error(401, "Admins Only", "Only admins can edit the alias list");
|
2008-02-16 03:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2010-05-15 11:17:32 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2010-05-15 11:17:32 +00:00
|
|
|
public function onAddAlias(AddAliasEvent $event) {
|
|
|
|
global $database;
|
2011-02-22 22:35:07 +00:00
|
|
|
$pair = array("oldtag" => $event->oldtag, "newtag" => $event->newtag);
|
|
|
|
if($database->get_row("SELECT * FROM aliases WHERE oldtag=:oldtag AND lower(newtag)=lower(:newtag)", $pair)) {
|
2010-05-15 11:17:32 +00:00
|
|
|
throw new AddAliasException("That alias already exists");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2013-03-07 03:23:35 +00:00
|
|
|
else if($database->get_row("SELECT * FROM aliases WHERE oldtag=:newtag", array("newtag" => $event->newtag))) {
|
2013-03-03 18:32:56 +00:00
|
|
|
throw new AddAliasException("{$event->newtag} is itself an alias");
|
|
|
|
}
|
2010-05-15 11:17:32 +00:00
|
|
|
else {
|
2011-02-22 22:35:07 +00:00
|
|
|
$database->execute("INSERT INTO aliases(oldtag, newtag) VALUES(:oldtag, :newtag)", $pair);
|
2012-06-10 03:21:03 +00:00
|
|
|
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}", true);
|
2010-05-15 11:17:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2010-05-15 11:17:32 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
|
|
|
|
global $user;
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("manage_alias_list")) {
|
2010-05-15 11:17:32 +00:00
|
|
|
$event->add_link("Alias Editor", make_link("alias/list"));
|
2007-06-04 02:35:28 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-10-18 02:35:01 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
private function get_alias_csv(Database $database): string {
|
2007-10-18 02:35:01 +00:00
|
|
|
$csv = "";
|
2012-03-25 02:32:32 +00:00
|
|
|
$aliases = $database->get_pairs("SELECT oldtag, newtag FROM aliases ORDER BY newtag");
|
2007-10-18 02:35:01 +00:00
|
|
|
foreach($aliases as $old => $new) {
|
2015-01-30 03:53:58 +00:00
|
|
|
$csv .= "\"$old\",\"$new\"\n";
|
2007-10-18 02:35:01 +00:00
|
|
|
}
|
|
|
|
return $csv;
|
|
|
|
}
|
2008-02-16 03:58:05 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
private function add_alias_csv(Database $database, string $csv) {
|
2010-03-27 04:01:29 +00:00
|
|
|
$csv = str_replace("\r", "\n", $csv);
|
2008-02-16 03:58:05 +00:00
|
|
|
foreach(explode("\n", $csv) as $line) {
|
2015-01-30 03:53:58 +00:00
|
|
|
$parts = str_getcsv($line);
|
2008-02-16 03:58:05 +00:00
|
|
|
if(count($parts) == 2) {
|
2015-03-20 22:45:33 +00:00
|
|
|
try {
|
|
|
|
$aae = new AddAliasEvent($parts[0], $parts[1]);
|
|
|
|
send_event($aae);
|
|
|
|
}
|
|
|
|
catch(AddAliasException $ex) {
|
|
|
|
$this->theme->display_error(500, "Error adding alias", $ex->getMessage());
|
2013-07-05 22:33:31 +00:00
|
|
|
}
|
2008-02-16 03:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-30 15:40:14 +00:00
|
|
|
|
2014-04-28 06:43:49 +00:00
|
|
|
/**
|
|
|
|
* Get the priority for this extension.
|
|
|
|
*
|
|
|
|
* Add alias *after* mass tag editing, else the MTE will
|
|
|
|
* search for the images and be redirected to the alias,
|
|
|
|
* missing out the images tagged with the old tag.
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_priority(): int {return 60;}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2014-04-25 02:34:45 +00:00
|
|
|
|