2007-05-23 22:53:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Simple Wiki
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Link: http://trac.shishnet.org/shimmie2/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: A simple wiki, for those who don't want the
|
|
|
|
* hugeness of mediawiki
|
|
|
|
*/
|
|
|
|
|
|
|
|
// WikiUpdateEvent {{{
|
|
|
|
class WikiUpdateEvent extends Event {
|
|
|
|
var $user;
|
2007-10-21 22:06:34 +00:00
|
|
|
var $wikipage;
|
2007-05-23 22:53:35 +00:00
|
|
|
|
2007-10-21 22:06:34 +00:00
|
|
|
public function WikiUpdateEvent($user, $wikipage) {
|
2007-05-23 22:53:35 +00:00
|
|
|
$this->user = $user;
|
2007-10-21 22:06:34 +00:00
|
|
|
$this->wikipage = $wikipage;
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2007-05-23 23:17:45 +00:00
|
|
|
// WikiPage {{{
|
|
|
|
class WikiPage {
|
2007-05-23 22:53:35 +00:00
|
|
|
var $id;
|
|
|
|
var $owner_id;
|
|
|
|
var $owner_ip;
|
|
|
|
var $date;
|
|
|
|
var $title;
|
|
|
|
var $revision;
|
2007-05-23 23:37:29 +00:00
|
|
|
var $locked;
|
2007-05-23 22:53:35 +00:00
|
|
|
var $body;
|
|
|
|
|
|
|
|
public function WikiPage($row=null) {
|
|
|
|
if(!is_null($row)) {
|
|
|
|
$this->id = $row['id'];
|
|
|
|
$this->owner_id = $row['owner_id'];
|
|
|
|
$this->owner_ip = $row['owner_ip'];
|
|
|
|
$this->date = $row['date'];
|
|
|
|
$this->title = $row['title'];
|
|
|
|
$this->revision = $row['revision'];
|
2007-05-23 23:37:29 +00:00
|
|
|
$this->locked = ($row['locked'] == 'Y');
|
2007-05-23 22:53:35 +00:00
|
|
|
$this->body = $row['body'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_owner() {
|
|
|
|
global $database;
|
|
|
|
return $database->get_user_by_id($this->owner_id);
|
|
|
|
}
|
|
|
|
|
2007-05-23 23:17:45 +00:00
|
|
|
public function is_locked() {
|
2007-05-23 23:37:29 +00:00
|
|
|
return $this->locked;
|
2007-05-23 23:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2007-05-23 22:53:35 +00:00
|
|
|
class Wiki extends Extension {
|
2007-07-19 12:51:12 +00:00
|
|
|
var $theme;
|
|
|
|
|
2007-05-23 22:53:35 +00:00
|
|
|
public function receive_event($event) {
|
2007-07-19 12:51:12 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object("wiki", "WikiTheme");
|
|
|
|
|
2007-05-23 22:53:35 +00:00
|
|
|
if(is_a($event, 'InitExtEvent')) {
|
2007-05-23 23:37:29 +00:00
|
|
|
$this->setup();
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2007-07-17 07:45:35 +00:00
|
|
|
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "wiki")) {
|
2007-05-23 22:53:35 +00:00
|
|
|
if(is_null($event->get_arg(0)) || strlen(trim($event->get_arg(0))) == 0) {
|
|
|
|
$title = "Index";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$title = $event->get_arg(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $this->get_page($title);
|
2007-07-19 12:51:12 +00:00
|
|
|
|
2007-05-23 22:53:35 +00:00
|
|
|
if(isset($_GET['save']) && $_GET['save'] == "on") {
|
|
|
|
$title = $_POST['title'];
|
2007-05-28 23:12:43 +00:00
|
|
|
$rev = int_escape($_POST['revision']);
|
2007-05-23 22:53:35 +00:00
|
|
|
$body = $_POST['body'];
|
2007-05-28 23:12:43 +00:00
|
|
|
$lock = isset($_POST['lock']) && ($_POST['lock'] == "on");
|
2007-05-23 22:53:35 +00:00
|
|
|
|
2007-05-23 23:17:45 +00:00
|
|
|
global $user;
|
|
|
|
if($this->can_edit($user, $this->get_page($title))) {
|
2007-10-28 17:20:25 +00:00
|
|
|
$wikipage = new WikiPage();
|
|
|
|
$wikipage->title = $title;
|
|
|
|
$wikipage->rev = $rev;
|
|
|
|
$wikipage->body = $body;
|
|
|
|
$wikipage->lock = $user->is_admin() ? $lock : false;
|
|
|
|
send_event(new WikiUpdateEvent($user, $wikipage));
|
2007-05-23 23:17:45 +00:00
|
|
|
|
|
|
|
$u_title = url_escape($title);
|
|
|
|
|
2007-07-19 12:51:12 +00:00
|
|
|
$event->page->set_mode("redirect");
|
|
|
|
$event->page->set_redirect(make_link("wiki/$u_title"));
|
2007-05-23 23:17:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-19 12:51:12 +00:00
|
|
|
$this->theme->display_error($event->page, "Denied", "You do not have permission to edit this page");
|
2007-05-23 23:17:45 +00:00
|
|
|
}
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
else if(is_null($content)) {
|
2007-05-28 23:12:43 +00:00
|
|
|
$default = $this->get_page("wiki:default");
|
2007-05-23 22:53:35 +00:00
|
|
|
$blank = new WikiPage();
|
|
|
|
$blank->title = $title;
|
2007-05-28 23:12:43 +00:00
|
|
|
if(!is_null($default) && !isset($_GET['edit'])) {
|
|
|
|
global $config;
|
|
|
|
$blank->body = $default->body;
|
|
|
|
$blank->owner_id = $config->get_int('anon_id');
|
|
|
|
$blank->date = $default->date;
|
2007-07-19 12:51:12 +00:00
|
|
|
$this->theme->display_page($event->page, $blank, $this->get_page("wiki:sidebar"));
|
2007-05-28 23:12:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-19 12:51:12 +00:00
|
|
|
$this->theme->display_page_editor($event->page, $blank);
|
2007-05-28 23:12:43 +00:00
|
|
|
}
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
else if(isset($_GET['edit']) && $_GET['edit'] == "on") {
|
2007-07-19 12:51:12 +00:00
|
|
|
$this->theme->display_page_editor($event->page, $content);
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-07-19 12:51:12 +00:00
|
|
|
$this->theme->display_page($event->page, $content, $this->get_page("wiki:sidebar"));
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_a($event, 'WikiUpdateEvent')) {
|
2007-10-28 17:20:25 +00:00
|
|
|
$this->set_page($event->user, $event->wikipage);
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
2007-05-23 23:17:45 +00:00
|
|
|
|
|
|
|
if(is_a($event, 'SetupBuildingEvent')) {
|
|
|
|
$sb = new SetupBlock("Wiki");
|
|
|
|
$sb->add_bool_option("wiki_edit_anon", "Allow anonymous edits: ");
|
|
|
|
$sb->add_bool_option("wiki_edit_user", "<br>Allow user edits: ");
|
2007-06-30 01:19:11 +00:00
|
|
|
$event->panel->add_block($sb);
|
2007-05-23 23:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:51:12 +00:00
|
|
|
|
2007-05-23 23:17:45 +00:00
|
|
|
private function can_edit($user, $page) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if(!is_null($page) && $page->is_locked() && !$user->is_admin()) return false;
|
|
|
|
if($config->get_bool("wiki_edit_anon", false) && $user->is_anonymous()) return true;
|
|
|
|
if($config->get_bool("wiki_edit_user", false) && !$user->is_anonymous()) return true;
|
|
|
|
if($user->is_admin()) return true;
|
|
|
|
return false;
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
2007-07-19 12:51:12 +00:00
|
|
|
|
2007-05-23 22:53:35 +00:00
|
|
|
// installer {{{
|
2007-05-23 23:37:29 +00:00
|
|
|
private function setup() {
|
2007-05-23 22:53:35 +00:00
|
|
|
global $database;
|
|
|
|
global $config;
|
2007-05-23 23:37:29 +00:00
|
|
|
|
2007-05-25 07:40:14 +00:00
|
|
|
if($config->get_int("ext_wiki_version", 0) < 1) {
|
2007-05-23 23:37:29 +00:00
|
|
|
$database->Execute("CREATE TABLE wiki_pages (
|
|
|
|
id int(11) NOT NULL auto_increment,
|
|
|
|
owner_id int(11) NOT NULL,
|
|
|
|
owner_ip char(15) NOT NULL,
|
|
|
|
date datetime default NULL,
|
|
|
|
title varchar(255) NOT NULL,
|
|
|
|
revision int(11) NOT NULL default 1,
|
|
|
|
body text NOT NULL,
|
|
|
|
PRIMARY KEY (id), UNIQUE (title, revision)
|
|
|
|
)");
|
|
|
|
$config->set_int("ext_wiki_version", 1);
|
|
|
|
}
|
|
|
|
if($config->get_int("ext_wiki_version") < 2) {
|
|
|
|
$database->Execute("ALTER TABLE wiki_pages ADD COLUMN
|
|
|
|
locked ENUM('Y', 'N') DEFAULT 'N' NOT NULL AFTER REVISION");
|
|
|
|
$config->set_int("ext_wiki_version", 2);
|
|
|
|
}
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// database {{{
|
|
|
|
private function get_page($title, $revision=-1) {
|
|
|
|
global $database;
|
|
|
|
$row = $database->db->GetRow("
|
|
|
|
SELECT *
|
|
|
|
FROM wiki_pages
|
|
|
|
WHERE title LIKE ?
|
|
|
|
ORDER BY revision DESC", array($title));
|
|
|
|
return ($row ? new WikiPage($row) : null);
|
|
|
|
}
|
2007-10-21 22:06:34 +00:00
|
|
|
|
|
|
|
// TODO: accept a WikiPage object
|
2007-10-28 17:20:25 +00:00
|
|
|
private function set_page($user, $wpage) {
|
2007-05-23 22:53:35 +00:00
|
|
|
global $database;
|
|
|
|
// FIXME: deal with collisions
|
|
|
|
$row = $database->Execute("
|
2007-05-23 23:37:29 +00:00
|
|
|
INSERT INTO wiki_pages(owner_id, owner_ip, date, title, revision, locked, body)
|
2007-10-28 17:20:25 +00:00
|
|
|
VALUES (?, ?, now(), ?, ?, ?, ?)", array($user->id, $_SERVER['REMOTE_ADDR'],
|
|
|
|
$wpage->title, $wpage->rev, $wpage->locked?'Y':'N', $wpage->body));
|
2007-05-23 22:53:35 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new Wiki());
|
|
|
|
?>
|