merge some useful parts of SCore

git-svn-id: file:///home/shish/svn/shimmie2/trunk@1002 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-08-23 12:05:24 +00:00
parent a9c8d7c6ec
commit 1e4d7d1938
8 changed files with 123 additions and 64 deletions

View File

@ -15,7 +15,7 @@ class Block {
var $section; var $section;
var $position; var $position;
public function Block($header, $body, $section="main", $position=50) { public function __construct($header, $body, $section="main", $position=50) {
$this->header = $header; $this->header = $header;
$this->body = $body; $this->body = $body;
$this->section = $section; $this->section = $section;
@ -29,11 +29,8 @@ class Block {
* because "new NavBlock()" is easier than "new Block('Navigation', ..." * because "new NavBlock()" is easier than "new Block('Navigation', ..."
*/ */
class NavBlock extends Block { class NavBlock extends Block {
public function NavBlock() { public function __construct() {
$this->header = "Navigation"; parent::__construct("Navigation", "<a href='".make_link()."'>Index</a>", "left", 0);
$this->body = "<a href='".make_link()."'>Index</a>";
$this->section = "left";
$this->position = 0;
} }
} }
?> ?>

View File

@ -191,6 +191,22 @@ class Database {
return $result; return $result;
} }
public function get_row($query, $args=array()) {
$result = $this->db->GetRow($query, $args);
if($result === False) {
print "SQL Error: " . $this->db->ErrorMsg();
print "<br>Query: $query";
print "<br>Args: "; print_r($args);
exit;
}
if(count($result) == 0) {
return null;
}
else {
return $result;
}
}
public function upgrade_schema($filename) { public function upgrade_schema($filename) {
$this->install_schema($filename); $this->install_schema($filename);
} }

View File

@ -3,12 +3,11 @@
* Event: * Event:
* generic parent class * generic parent class
*/ */
class Event { abstract class Event {
var $vetoed = false, $veto_reason = null; var $context;
public function veto($reason="") { public function __construct(RequestContext $context) {
$this->vetoed = true; $this->context = $context;
$this->veto_reason = $reason;
} }
} }
@ -114,13 +113,7 @@ class ImageDeletionEvent extends Event {
* InitExtEvent: * InitExtEvent:
* A wake-up call for extensions * A wake-up call for extensions
*/ */
class InitExtEvent extends Event { class InitExtEvent extends Event {}
var $context;
public function InitExtEvent($context) {
$this->context = $context;
}
}
/* /*
@ -148,6 +141,23 @@ class PageRequestEvent extends Event {
$this->user = $context->user; $this->user = $context->user;
} }
public function page_matches($name) {
$parts = explode("/", $name);
if(count($parts) > count($this->args)) {
return false;
}
for($i=0; $i<count($parts); $i++) {
if($parts[$i] != $this->args[$i]) {
return false;
}
}
return true;
}
public function get_arg($n) { public function get_arg($n) {
return isset($this->args[$n]) ? $this->args[$n] : null; return isset($this->args[$n]) ? $this->args[$n] : null;
} }

View File

@ -0,0 +1,4 @@
<?php
class PermissionDeniedException extends Exception {}
?>

View File

@ -2,7 +2,23 @@
/* /*
* A generic extension class, for subclassing * A generic extension class, for subclassing
*/ */
class Extension { interface Extension {
public function receive_event($event) {} public function receive_event(Event $event);
}
/*
* Several extensions have this in common, make a common API
*/
abstract class FormatterExtension implements Extension {
public function receive_event(Event $event) {
if($event instanceof TextFormattingEvent) {
$event->formatted = $this->format($event->formatted);
$event->stripped = $this->strip($event->stripped);
}
}
abstract public function format($text);
abstract public function strip($text);
} }
?> ?>

View File

@ -3,25 +3,73 @@
* An object representing a row in the "users" table. * An object representing a row in the "users" table.
*/ */
class User { class User {
var $config;
var $database;
var $id; var $id;
var $name; var $name;
var $email; var $email;
var $join_date; var $join_date;
var $days_old; var $days_old;
var $admin; var $admin;
public function User($row) { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Initialisation *
* *
* User objects shouldn't be created directly, they should be *
* fetched from the database like so: *
* *
* $user = User::by_name($config, $database, "bob"); *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public function User(Config $config, Database $database, $row) {
$this->config = $config;
$this->database = $database;
$this->id = int_escape($row['id']); $this->id = int_escape($row['id']);
$this->name = $row['name']; $this->name = $row['name'];
$this->email = $row['email']; $this->email = $row['email'];
$this->join_date = $row['joindate']; $this->join_date = $row['joindate'];
$this->days_old = $row['days_old']; $this->days_old = 0; // $row['days_old'];
$this->admin = ($row['admin'] == 'Y'); $this->admin = ($row['admin'] == 'Y');
} }
public static function by_session(Config $config, Database $database, $name, $session) {
$row = $database->get_row(
"SELECT * FROM user WHERE name = ? AND md5(concat(pass, ?)) = ?",
array($name, get_session_ip($config), $session)
);
return is_null($row) ? null : new User($config, $database, $row);
}
public static function by_id(Config $config, Database $database, $id) {
assert(is_numeric($id));
$row = $database->get_row("SELECT * FROM user WHERE id = ?", array($id));
return is_null($row) ? null : new User($config, $database, $row);
}
public static function by_name(Config $config, Database $database, $name) {
assert(is_string($name));
$row = $database->get_row("SELECT * FROM user WHERE name = ?", array($name));
return is_null($row) ? null : new User($config, $database, $row);
}
public static function by_name_and_hash(Config $config, Database $database, $name, $hash) {
assert(is_string($name));
assert(is_string($hash));
assert(strlen($hash) == 32);
$row = $database->get_row("SELECT * FROM user WHERE name = ? AND pass = ?", array($name, $hash));
return is_null($row) ? null : new User($config, $database, $row);
}
/*
* useful user object functions start here
*/
public function is_anonymous() { public function is_anonymous() {
global $config; return ($this->id == $this->config->get_int('anon_id'));
return ($this->id == $config->get_int('anon_id'));
} }
public function is_admin() { public function is_admin() {
@ -29,17 +77,14 @@ class User {
} }
public function set_admin($admin) { public function set_admin($admin) {
global $database; assert(is_bool($admin));
$yn = $admin ? 'Y' : 'N'; $yn = $admin ? 'Y' : 'N';
$database->Execute("UPDATE users SET admin=? WHERE id=?", array($yn, $this->id)); $this->database->Execute("UPDATE users SET admin=? WHERE id=?", array($yn, $this->id));
} }
public function set_password($password) { public function set_password($password) {
global $database;
$hash = md5(strtolower($this->name) . $password); $hash = md5(strtolower($this->name) . $password);
$database->Execute("UPDATE users SET pass=? WHERE id=?", array($hash, $this->id)); $this->database->Execute("UPDATE users SET pass=? WHERE id=?", array($hash, $this->id));
} }
public function get_days_old() { public function get_days_old() {

View File

@ -501,7 +501,6 @@ function send_event($event) {
ksort($my_event_listeners); ksort($my_event_listeners);
foreach($my_event_listeners as $listener) { foreach($my_event_listeners as $listener) {
$listener->receive_event($event); $listener->receive_event($event);
if($event->vetoed) break;
} }
$_event_count++; $_event_count++;
} }
@ -526,31 +525,6 @@ function _get_query_parts() {
$path = substr($path, 1); $path = substr($path, 1);
} }
/*
* Split post/list/fate//stay_night/1
* into post list fate/stay_night 1
*/
/*
$parts = array();
$n = 0;
$lastsplit = 0;
while($n<=strlen($path)) {
if(
$n == strlen($path) ||
(
$path[$n] == '/' &&
($n < strlen($path) && $path[$n+1] != '/')
&& ($n > 0 && $path[$n-1] != '/')
)
) {
$part = substr($path, $lastsplit, $n-$lastsplit);
$part = str_replace('//', '/', $part);
$parts[] = $part;
$lastsplit = $n+1;
}
$n++;
}
*/
$path = str_replace('/', '%%', $path); $path = str_replace('/', '%%', $path);
$path = str_replace('%%%%', '/', $path); $path = str_replace('%%%%', '/', $path);
$parts = split('%%', $path); $parts = split('%%', $path);
@ -579,20 +553,17 @@ function _get_page_request($context) {
return new PageRequestEvent($context, $page_name, $args); return new PageRequestEvent($context, $page_name, $args);
} }
function _get_user() { function _get_user($config, $database) {
global $database;
global $config;
$user = null; $user = null;
if(isset($_COOKIE["shm_user"]) && isset($_COOKIE["shm_session"])) { if(isset($_COOKIE["shm_user"]) && isset($_COOKIE["shm_session"])) {
$tmp_user = $database->get_user_session($_COOKIE["shm_user"], $_COOKIE["shm_session"]); $tmp_user = User::by_session($config, $database, $_COOKIE["shm_user"], $_COOKIE["shm_session"]);
if(!is_null($tmp_user)) { if(!is_null($tmp_user)) {
$user = $tmp_user; $user = $tmp_user;
} }
} }
if(is_null($user)) { if(is_null($user)) {
$user = $database->get_user_by_id($config->get_int("anon_id", 0)); $user = User::by_id($config, $database, $config->get_int("anon_id", 0));
} }
assert(!is_null($user)); assert(!is_null($user));
return $user; return $user;

View File

@ -48,7 +48,7 @@ if($custom_themelets) {
// start the page generation waterfall // start the page generation waterfall
$page = new Page(); $page = new Page();
$user = _get_user(); $user = _get_user($config, $database);
$context = new RequestContext(); $context = new RequestContext();
$context->page = $page; $context->page = $page;
$context->user = $user; $context->user = $user;