This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/event.class.php

158 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2009-07-19 07:38:13 +00:00
/**
* @package SCore
*/
/**
* Generic parent class for all events.
*
* An event is anything that can be passed around via send_event($blah)
*/
abstract class Event {
public function __construct() {}
}
2009-07-19 07:38:13 +00:00
/**
* A wake-up call for extensions
*/
class InitExtEvent extends Event {}
2009-07-19 07:38:13 +00:00
/**
* A signal that a page has been requested.
*
* User requests /view/42 -> an event is generated with $args = array("view",
* "42"); when an event handler asks $event->page_matches("view"), it returns
* true and ignores the matched part, such that $event->count_args() = 1 and
* $event->get_arg(0) = "42"
*/
class PageRequestEvent extends Event {
var $args;
var $arg_count;
var $part_count;
public function __construct($args) {
$this->args = $args;
$this->arg_count = count($args);
}
2009-07-19 07:38:13 +00:00
/**
* Test if the requested path matches a given pattern.
*
* If it matches, store the remaining path elements in $args
*/
public function page_matches($name) {
$parts = explode("/", $name);
$this->part_count = count($parts);
2009-01-04 19:18:37 +00:00
if($this->part_count > $this->arg_count) {
return false;
}
for($i=0; $i<$this->part_count; $i++) {
if($parts[$i] != $this->args[$i]) {
return false;
}
}
return true;
}
public function get_arg($n) {
$offset = $this->part_count + $n;
if($offset >= 0 && $offset < $this->arg_count) {
return $this->args[$offset];
}
else {
return null;
}
}
public function count_args() {
return $this->arg_count - $this->part_count;
}
}
2009-07-19 07:38:13 +00:00
/**
* A signal that some text needs formatting, the event carries
* both the text and the result
*/
class TextFormattingEvent extends Event {
2009-07-19 07:38:13 +00:00
/**
* For reference
*/
var $original;
2009-07-19 07:38:13 +00:00
/**
* with formatting applied
*/
var $formatted;
2009-07-19 07:38:13 +00:00
/**
* with formatting removed
*/
var $stripped;
2009-07-19 07:38:13 +00:00
public function __construct($text) {
$h_text = html_escape(trim($text));
$this->original = $h_text;
$this->formatted = $h_text;
$this->stripped = $h_text;
}
}
2009-05-08 10:52:29 +00:00
2009-07-19 07:38:13 +00:00
/**
* A signal that something needs logging
2009-05-08 10:52:29 +00:00
*/
class LogEvent extends Event {
2009-07-19 07:38:13 +00:00
/**
* a category, normally the extension name
*
* @var string
*/
2009-05-08 10:52:29 +00:00
var $section;
2009-07-19 07:38:13 +00:00
/**
* See python...
*
* @var int
*/
2009-05-08 10:52:29 +00:00
var $priority = 0;
2009-07-19 07:38:13 +00:00
/**
* Free text to be logged
*
* @var text
*/
2009-05-08 10:52:29 +00:00
var $message;
2009-07-19 07:38:13 +00:00
/**
* The time that the event was created
*
* @var int
*/
2009-05-08 10:52:29 +00:00
var $time;
public function __construct($section, $priority, $message) {
2009-05-08 10:52:29 +00:00
$this->section = $section;
$this->priority = $priority;
$this->message = $message;
$this->time = time();
// this should be an extension
2009-05-11 19:53:55 +00:00
if(defined("X-HALFASSED-LOGGING")) {
global $user;
$ftime = date("Y-m-d H:i:s", $this->time);
$username = $user->name;
$ip = $_SERVER['REMOTE_ADDR'];
$fp = fopen("shimmie.log", "a");
fputs($fp, "$ftime\t$section/$priority\t$username/$ip\t$message\n");
fclose($fp);
}
2009-05-08 10:52:29 +00:00
}
}
?>