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.php

298 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2009-07-19 07:38:13 +00:00
/**
* 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
/**
2009-07-21 03:18:40 +00:00
* A wake-up call for extensions. Upon recieving an InitExtEvent an extension
* should check that it's database tables are there and install them if not,
* and set any defaults with Config::set_default_int() and such.
*
* This event is sent before $user is set to anything
*/
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 array
*/
public $args;
/**
* @var int
*/
public $arg_count;
/**
* @var int
*/
public $part_count;
2017-09-19 17:55:43 +00:00
public function __construct(string $path) {
global $config;
// trim starting slashes
2013-06-13 11:20:27 +00:00
$path = ltrim($path, "/");
// if path is not specified, use the default front page
2013-05-14 00:40:01 +00:00
if(empty($path)) { /* empty is faster than strlen */
$path = $config->get_string('front_page');
}
// break the path into parts
$args = explode('/', $path);
// voodoo so that an arg can contain a slash; is
// this still needed?
if(strpos($path, "^") !== FALSE) {
$unescaped = array();
2012-06-24 06:13:53 +00:00
foreach($args as $part) {
$unescaped[] = _decaret($part);
}
$args = $unescaped;
}
$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
*/
2017-09-19 17:55:43 +00:00
public function page_matches(string $name): bool {
$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;
}
2012-02-08 01:13:58 +00:00
/**
* Get the n th argument of the page request (if it exists.)
*/
2019-05-28 16:31:20 +00:00
public function get_arg(int $n): ?string {
$offset = $this->part_count + $n;
if($offset >= 0 && $offset < $this->arg_count) {
return $this->args[$offset];
}
else {
return null;
}
}
2012-02-08 01:13:58 +00:00
/**
* Returns the number of arguments the page request has.
*/
2017-09-19 17:55:43 +00:00
public function count_args(): int {
return int_escape($this->arg_count - $this->part_count);
}
2010-04-26 01:35:53 +00:00
/*
* Many things use these functions
*/
2017-09-19 17:55:43 +00:00
public function get_search_terms(): array {
2010-04-26 01:35:53 +00:00
$search_terms = array();
2012-01-11 20:57:00 +00:00
if($this->count_args() === 2) {
$search_terms = Tag::explode($this->get_arg(0));
2010-04-26 01:35:53 +00:00
}
return $search_terms;
}
2017-09-19 17:55:43 +00:00
public function get_page_number(): int {
2010-04-26 01:35:53 +00:00
$page_number = 1;
2012-01-11 20:57:00 +00:00
if($this->count_args() === 1) {
2010-04-26 02:39:02 +00:00
$page_number = int_escape($this->get_arg(0));
2010-04-26 01:35:53 +00:00
}
2012-01-11 20:57:00 +00:00
else if($this->count_args() === 2) {
2010-04-26 02:39:02 +00:00
$page_number = int_escape($this->get_arg(1));
2010-04-26 01:35:53 +00:00
}
2012-01-11 20:57:00 +00:00
if($page_number === 0) $page_number = 1; // invalid -> 0
2010-04-26 01:35:53 +00:00
return $page_number;
}
2017-09-19 17:55:43 +00:00
public function get_page_size(): int {
2010-04-26 01:35:53 +00:00
global $config;
return $config->get_int('index_images');
2010-04-26 01:35:53 +00:00
}
}
/**
* Sent when index.php is called from the command line
*/
class CommandEvent extends Event {
/**
* @var string
*/
public $cmd = "help";
/**
* @var array
*/
public $args = array();
/**
2019-05-28 16:31:20 +00:00
* #param string[] $args
*/
2017-09-19 17:55:43 +00:00
public function __construct(array $args) {
global $user;
$opts = array();
$log_level = SCORE_LOG_WARNING;
2013-05-14 00:40:01 +00:00
$arg_count = count($args);
2014-02-22 20:42:09 +00:00
2013-05-14 00:40:01 +00:00
for($i=1; $i<$arg_count; $i++) {
switch($args[$i]) {
case '-u':
$user = User::by_name($args[++$i]);
if(is_null($user)) {
die("Unknown user");
}
break;
case '-q':
$log_level += 10;
break;
case '-v':
$log_level -= 10;
break;
default:
$opts[] = $args[$i];
break;
}
}
define("CLI_LOG_LEVEL", $log_level);
if(count($opts) > 0) {
$this->cmd = $opts[0];
$this->args = array_slice($opts, 1);
}
else {
print "\n";
print "Usage: php {$args[0]} [flags] [command]\n";
print "\n";
print "Flags:\n";
print " -u [username]\n";
print " Log in as the specified user\n";
print " -q / -v\n";
print " Be quieter / more verbose\n";
print " Scale is debug - info - warning - error - critical\n";
print " Default is to show warnings and above\n";
print " \n";
print "Currently known commands:\n";
}
}
}
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 string
2009-07-19 07:38:13 +00:00
*/
public $original;
2009-07-19 07:38:13 +00:00
/**
* with formatting applied
*
* @var string
2009-07-19 07:38:13 +00:00
*/
public $formatted;
2009-07-19 07:38:13 +00:00
/**
* with formatting removed
*
* @var string
2009-07-19 07:38:13 +00:00
*/
public $stripped;
2017-09-19 17:55:43 +00:00
public function __construct(string $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-07-19 07:38:13 +00:00
*/
public $section;
2009-07-19 07:38:13 +00:00
/**
* See python...
*
* @var int
2009-07-19 07:38:13 +00:00
*/
public $priority = 0;
2009-07-19 07:38:13 +00:00
/**
* Free text to be logged
*
* @var string
2009-07-19 07:38:13 +00:00
*/
public $message;
2009-07-19 07:38:13 +00:00
/**
* The time that the event was created
*
* @var int
2009-07-19 07:38:13 +00:00
*/
public $time;
2009-05-08 10:52:29 +00:00
2013-08-29 19:23:08 +00:00
/**
* Extra data to be held separate
2014-04-29 05:33:03 +00:00
*
* @var array
2013-08-29 19:23:08 +00:00
*/
public $args;
2013-08-29 19:23:08 +00:00
2017-09-19 17:55:43 +00:00
public function __construct(string $section, int $priority, string $message, array $args) {
2009-05-08 10:52:29 +00:00
$this->section = $section;
$this->priority = $priority;
$this->message = $message;
2013-08-29 19:23:08 +00:00
$this->args = $args;
2009-05-08 10:52:29 +00:00
$this->time = time();
}
}