summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Save <savetheinternet@tinyboard.org>2012-03-19 02:41:06 +1100
committerMichael Save <savetheinternet@tinyboard.org>2012-03-19 02:41:06 +1100
commit9d5818636008f99e425465c42a808a36489f4052 (patch)
treee2c35a312c7e9a894c8c00aa2e3d54e60f686770
parentab1db9dd8a8480c7c5cd69c11abe281ddc6d97ca (diff)
New feature: "events". Will eventually replace/extend "themes".
-rw-r--r--inc/config.php17
-rw-r--r--inc/events.php40
-rw-r--r--inc/functions.php8
-rw-r--r--post.php6
4 files changed, 70 insertions, 1 deletions
diff --git a/inc/config.php b/inc/config.php
index ae0a36c5..afc4ceaa 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -956,6 +956,23 @@
/*
* ====================
+ * Events (PHP 5.3.0+)
+ * ====================
+ */
+
+ // event_handler('post', function($post) {
+ // // do something
+ // });
+
+ // event_handler('post', function($post) {
+ // // do something else
+ //
+ // // return an error (reject post)
+ // return 'Sorry, you cannot post that!';
+ // });
+
+/*
+ * ====================
* Other/uncategorized
* ====================
*/
diff --git a/inc/events.php b/inc/events.php
new file mode 100644
index 00000000..a5b9129d
--- /dev/null
+++ b/inc/events.php
@@ -0,0 +1,40 @@
+<?php
+
+function event() {
+ global $events;
+
+ $args = func_get_args();
+
+ $event = $args[0];
+ $args = array_splice($args, 1);
+
+ rebuildThemes($event);
+
+ if(!isset($events[$event]))
+ return false;
+
+ foreach($events[$event] as $callback) {
+ if(!is_callable($callback))
+ error('Event handler for ' . $event . ' is not callable!');
+ if($error = call_user_func_array($callback, $args))
+ return $error;
+ }
+
+ return false;
+}
+
+function event_handler($event, $callback) {
+ global $events;
+
+ if(!isset($events[$event]))
+ $events[$event] = Array();
+
+ $events[$event][] = $callback;
+}
+
+function reset_events() {
+ global $events;
+
+ $events = Array();
+}
+
diff --git a/inc/functions.php b/inc/functions.php
index 25ff89b5..d62c3a20 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -5,6 +5,7 @@
exit;
}
+ require 'inc/events.php';
require 'contrib/gettext/gettext.inc';
register_shutdown_function('fatal_error_handler');
@@ -14,6 +15,13 @@
function loadConfig() {
global $board, $config, $__ip, $debug, $__version;
+ reset_events();
+
+ if(!defined('PHP_VERSION_ID')) {
+ $version = explode('.', PHP_VERSION);
+ define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
+ }
+
if(!isset($_SERVER['REMOTE_ADDR']))
$_SERVER['REMOTE_ADDR'] = '0.0.0.0';
diff --git a/post.php b/post.php
index 3b309085..0066d538 100644
--- a/post.php
+++ b/post.php
@@ -595,6 +595,11 @@
$post['thumb'] = substr_replace($post['thumb'], '', 0, mb_strlen($board['dir'] . $config['dir']['thumb']));
}
+ if($error = event('post', $post)) {
+ undoImage($post);
+ error($error);
+ }
+
$id = post($post, $OP);
if(isset($post['tracked_cites'])) {
@@ -643,7 +648,6 @@
if($config['syslog'])
_syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $OP?$id:$post['thread']) . (!$OP ? '#' . $id : ''));
- rebuildThemes('post');
header('Location: ' . $redirect, true, $config['redirect_http']);
} else {
if(!file_exists($config['has_installed'])) {