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 {
|
|
|
|
var $oldtag;
|
|
|
|
var $newtag;
|
|
|
|
|
|
|
|
public function AddAliasEvent($oldtag, $newtag) {
|
|
|
|
$this->oldtag = $oldtag;
|
|
|
|
$this->newtag = $newtag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 14:01:59 +00:00
|
|
|
class AddAliasException extends SCoreException {}
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class AliasEditor implements Extension {
|
2007-06-30 01:19:11 +00:00
|
|
|
var $theme;
|
2007-07-05 21:30:37 +00:00
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2008-09-06 16:59:02 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
if(($event instanceof PageRequestEvent) && $event->page_matches("alias")) {
|
2007-06-04 03:31:52 +00:00
|
|
|
if($event->get_arg(0) == "add") {
|
2009-05-11 14:04:33 +00:00
|
|
|
if($user->is_admin()) {
|
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) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_error($page, "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") {
|
2009-05-11 14:04:33 +00:00
|
|
|
if($user->is_admin()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(isset($_POST['oldtag'])) {
|
2007-05-23 22:19:12 +00:00
|
|
|
$database->Execute("DELETE FROM aliases WHERE oldtag=?", array($_POST['oldtag']));
|
2009-12-30 07:59:40 +00:00
|
|
|
log_info("alias_editor", "Deleted alias for ".$_POST['oldtag']);
|
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);
|
|
|
|
|
|
|
|
$alias = $database->db->GetAssoc(
|
2010-02-01 16:19:16 +00:00
|
|
|
"SELECT oldtag, newtag FROM aliases ORDER BY newtag ASC OFFSET ? LIMIT ?",
|
2009-08-25 01:36:00 +00:00
|
|
|
array($page_number * $alias_per_page, $alias_per_page)
|
|
|
|
);
|
|
|
|
|
|
|
|
$total_pages = ceil($database->db->GetOne("SELECT COUNT(*) FROM aliases") / $alias_per_page);
|
|
|
|
|
|
|
|
$this->theme->display_aliases($page, $alias, $user->is_admin(), $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");
|
|
|
|
$page->set_type("text/plain");
|
|
|
|
$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") {
|
2009-05-11 14:04:33 +00:00
|
|
|
if($user->is_admin()) {
|
2008-02-16 03:58:05 +00:00
|
|
|
print_r($_FILES);
|
|
|
|
if(count($_FILES) > 0) {
|
|
|
|
global $database;
|
|
|
|
$tmp = $_FILES['alias_file']['tmp_name'];
|
|
|
|
$contents = file_get_contents($tmp);
|
|
|
|
$this->add_alias_csv($database, $contents);
|
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 {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_error($page, "No File Specified", "You have to upload a file");
|
2008-02-16 03:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_error($page, "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
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof AddAliasEvent) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2008-08-12 00:04:10 +00:00
|
|
|
$pair = array($event->oldtag, $event->newtag);
|
|
|
|
if($database->db->GetRow("SELECT * FROM aliases WHERE oldtag=? AND lower(newtag)=lower(?)", $pair)) {
|
2009-01-04 14:01:59 +00:00
|
|
|
throw new AddAliasException("That alias already exists");
|
2008-08-12 00:04:10 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$database->Execute("INSERT INTO aliases(oldtag, newtag) VALUES(?, ?)", $pair);
|
2009-12-30 07:59:40 +00:00
|
|
|
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}");
|
2008-08-12 00:04:10 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof UserBlockBuildingEvent) {
|
2009-05-11 14:04:33 +00:00
|
|
|
if($user->is_admin()) {
|
2007-06-04 02:35:28 +00:00
|
|
|
$event->add_link("Alias Editor", make_link("alias/list"));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-10-18 02:35:01 +00:00
|
|
|
|
|
|
|
private function get_alias_csv($database) {
|
|
|
|
$csv = "";
|
|
|
|
$aliases = $database->db->GetAssoc("SELECT oldtag, newtag FROM aliases");
|
|
|
|
foreach($aliases as $old => $new) {
|
|
|
|
$csv .= "$old,$new\n";
|
|
|
|
}
|
|
|
|
return $csv;
|
|
|
|
}
|
2008-02-16 03:58:05 +00:00
|
|
|
|
|
|
|
private function add_alias_csv($database, $csv) {
|
|
|
|
foreach(explode("\n", $csv) as $line) {
|
|
|
|
$parts = explode(",", $line);
|
|
|
|
if(count($parts) == 2) {
|
|
|
|
$database->execute("INSERT INTO aliases(oldtag, newtag) VALUES(?, ?)", $parts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
add_event_listener(new AliasEditor());
|
|
|
|
?>
|