2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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) {
|
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") {
|
2007-08-24 22:29:34 +00:00
|
|
|
if($event->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);
|
2008-08-12 00:04:10 +00:00
|
|
|
$event->page->set_mode("redirect");
|
|
|
|
$event->page->set_redirect(make_link("alias/list"));
|
|
|
|
}
|
2009-01-04 14:01:59 +00:00
|
|
|
catch(AddAliasException $ex) {
|
|
|
|
$this->theme->display_error($event->page, "Error adding alias", $ex->getMessage());
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-04 03:31:52 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "remove") {
|
2007-08-24 22:29:34 +00:00
|
|
|
if($event->user->is_admin()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(isset($_POST['oldtag'])) {
|
|
|
|
global $database;
|
2007-05-23 22:19:12 +00:00
|
|
|
$database->Execute("DELETE FROM aliases WHERE oldtag=?", array($_POST['oldtag']));
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2007-07-17 07:45:35 +00:00
|
|
|
$event->page->set_mode("redirect");
|
|
|
|
$event->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") {
|
2007-06-30 01:19:11 +00:00
|
|
|
global $database;
|
2009-01-04 19:18:37 +00:00
|
|
|
$this->theme->display_aliases($event->page,
|
2008-03-31 02:39:08 +00:00
|
|
|
$database->db->GetAssoc("SELECT oldtag, newtag FROM aliases ORDER BY newtag"),
|
|
|
|
$event->user->is_admin());
|
2007-06-04 03:31:52 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "export") {
|
2007-10-18 02:35:01 +00:00
|
|
|
global $database;
|
2007-07-17 07:45:35 +00:00
|
|
|
$event->page->set_mode("data");
|
|
|
|
$event->page->set_type("text/plain");
|
2007-10-18 02:35:01 +00:00
|
|
|
$event->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") {
|
|
|
|
if($event->user->is_admin()) {
|
|
|
|
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);
|
|
|
|
$event->page->set_mode("redirect");
|
|
|
|
$event->page->set_redirect(make_link("alias/list"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->theme->display_error($event->page, "No File Specified", "You have to upload a file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->theme->display_error($event->page, "Admins Only", "Only admins can edit the alias list");
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
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) {
|
2007-06-04 02:35:28 +00:00
|
|
|
if($event->user->is_admin()) {
|
|
|
|
$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());
|
|
|
|
?>
|