2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-07-21 06:36:12 +00:00
|
|
|
* \page eande Events and Extensions
|
2014-02-22 20:42:09 +00:00
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* An event is a little blob of data saying "something happened", possibly
|
|
|
|
* "something happened, here's the specific data". Events are sent with the
|
|
|
|
* send_event() function. Since events can store data, they can be used to
|
|
|
|
* return data to the extension which sent them, for example:
|
2014-02-22 20:42:09 +00:00
|
|
|
*
|
2009-07-21 03:18:40 +00:00
|
|
|
* \code
|
|
|
|
* $tfe = new TextFormattingEvent($original_text);
|
|
|
|
* send_event($tfe);
|
|
|
|
* $formatted_text = $tfe->formatted;
|
|
|
|
* \endcode
|
2014-02-22 20:42:09 +00:00
|
|
|
*
|
2012-02-08 12:07:01 +00:00
|
|
|
* An extension is something which is capable of reacting to events.
|
2009-07-21 06:36:12 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* \page hello The Hello World Extension
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* // ext/hello/main.php
|
2012-03-05 13:56:36 +00:00
|
|
|
* public class HelloEvent extends Event {
|
|
|
|
* public function __construct($username) {
|
|
|
|
* $this->username = $username;
|
|
|
|
* }
|
|
|
|
* }
|
2014-02-22 20:42:09 +00:00
|
|
|
*
|
2012-02-08 12:07:01 +00:00
|
|
|
* public class Hello extends Extension {
|
2012-03-05 18:42:05 +00:00
|
|
|
* public function onPageRequest(PageRequestEvent $event) { // Every time a page request is sent
|
|
|
|
* global $user; // Look at the global "currently logged in user" object
|
|
|
|
* send_event(new HelloEvent($user->name)); // Broadcast a signal saying hello to that user
|
2012-03-05 13:56:36 +00:00
|
|
|
* }
|
2012-03-05 18:42:05 +00:00
|
|
|
* public function onHello(HelloEvent $event) { // When the "Hello" signal is recieved
|
|
|
|
* $this->theme->display_hello($event->username); // Display a message on the web page
|
2009-07-21 06:36:12 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // ext/hello/theme.php
|
|
|
|
* public class HelloTheme extends Themelet {
|
2012-03-05 13:58:04 +00:00
|
|
|
* public function display_hello($username) {
|
2012-03-05 13:56:36 +00:00
|
|
|
* global $page;
|
2012-03-05 18:42:05 +00:00
|
|
|
* $h_user = html_escape($username); // Escape the data before adding it to the page
|
|
|
|
* $block = new Block("Hello!", "Hello there $h_user"); // HTML-safe variables start with "h_"
|
|
|
|
* $page->add_block($block); // Add the block to the page
|
2009-07-21 06:36:12 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // ext/hello/test.php
|
2010-01-12 15:01:34 +00:00
|
|
|
* public class HelloTest extends SCoreWebTestCase {
|
2012-03-05 13:58:04 +00:00
|
|
|
* public function testHello() {
|
2012-03-05 18:42:05 +00:00
|
|
|
* $this->get_page("post/list"); // View a page, any page
|
|
|
|
* $this->assert_text("Hello there"); // Check that the specified text is in that page
|
2009-07-21 06:36:12 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // themes/mytheme/hello.theme.php
|
2012-03-05 18:42:05 +00:00
|
|
|
* public class CustomHelloTheme extends HelloTheme { // CustomHelloTheme overrides HelloTheme
|
|
|
|
* public function display_hello($username) { // the display_hello() function is customised
|
|
|
|
* global $page;
|
|
|
|
* $h_user = html_escape($username);
|
2009-07-21 06:36:12 +00:00
|
|
|
* $page->add_block(new Block(
|
|
|
|
* "Hello!",
|
|
|
|
* "Hello there $h_user, look at my snazzy custom theme!"
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-05-15 08:52:55 +00:00
|
|
|
* send_event(BlahEvent()) -> onBlah($event)
|
|
|
|
*
|
|
|
|
* Also loads the theme object into $this->theme if available
|
|
|
|
*
|
2012-02-08 12:07:01 +00:00
|
|
|
* The original concept came from Artanis's Extension extension
|
2009-05-15 08:52:55 +00:00
|
|
|
* --> http://github.com/Artanis/simple-extension/tree/master
|
|
|
|
* Then re-implemented by Shish after he broke the forum and couldn't
|
|
|
|
* find the thread where the original was posted >_<
|
2009-05-11 21:08:32 +00:00
|
|
|
*/
|
2012-02-08 12:07:01 +00:00
|
|
|
abstract class Extension {
|
2012-03-05 13:56:36 +00:00
|
|
|
/** this theme's Themelet object */
|
2014-04-24 08:36:05 +00:00
|
|
|
public $theme;
|
2012-03-05 13:56:36 +00:00
|
|
|
|
|
|
|
/** @private */
|
2009-05-11 21:08:32 +00:00
|
|
|
var $_child;
|
|
|
|
|
2012-02-01 17:00:44 +00:00
|
|
|
// in PHP5.3, late static bindings can take care of this; __CLASS__
|
|
|
|
// used here will refer to the subclass
|
|
|
|
// http://php.net/manual/en/language.oop5.late-static-bindings.php
|
2012-03-05 13:56:36 +00:00
|
|
|
/** @private */
|
2012-02-02 13:58:48 +00:00
|
|
|
public function i_am(Extension $child) {
|
2009-05-11 21:08:32 +00:00
|
|
|
$this->_child = $child;
|
2012-03-05 14:21:41 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = $this->get_theme_object($child, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Find the theme object for a given extension.
|
|
|
|
*
|
|
|
|
* @param Extension $class
|
|
|
|
* @param bool $fatal
|
|
|
|
* @return bool
|
2012-03-05 14:21:41 +00:00
|
|
|
*/
|
|
|
|
private function get_theme_object(Extension $class, $fatal=true) {
|
|
|
|
$base = get_class($class);
|
|
|
|
if(class_exists('Custom'.$base.'Theme')) {
|
|
|
|
$class = 'Custom'.$base.'Theme';
|
|
|
|
return new $class();
|
|
|
|
}
|
|
|
|
elseif ($fatal || class_exists($base.'Theme')) {
|
|
|
|
$class = $base.'Theme';
|
|
|
|
return new $class();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-11 21:08:32 +00:00
|
|
|
}
|
|
|
|
|
2012-03-05 13:56:36 +00:00
|
|
|
/**
|
|
|
|
* Override this to change the priority of the extension,
|
2014-04-27 19:33:57 +00:00
|
|
|
* lower numbered ones will recieve events first.
|
|
|
|
*
|
|
|
|
* @return int
|
2012-03-05 13:56:36 +00:00
|
|
|
*/
|
2012-01-27 18:16:46 +00:00
|
|
|
public function get_priority() {
|
|
|
|
return 50;
|
|
|
|
}
|
2009-05-11 21:08:32 +00:00
|
|
|
}
|
2008-08-23 12:05:24 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2008-08-23 12:05:24 +00:00
|
|
|
* Several extensions have this in common, make a common API
|
|
|
|
*/
|
2012-02-08 12:07:01 +00:00
|
|
|
abstract class FormatterExtension extends Extension {
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param TextFormattingEvent $event
|
|
|
|
*/
|
2012-02-02 16:08:49 +00:00
|
|
|
public function onTextFormatting(TextFormattingEvent $event) {
|
2012-01-27 18:16:46 +00:00
|
|
|
$event->formatted = $this->format($event->formatted);
|
|
|
|
$event->stripped = $this->strip($event->stripped);
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
abstract public function format(/*string*/ $text);
|
2014-04-27 19:33:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
abstract public function strip(/*string*/ $text);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-07-16 19:20:53 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-07-19 03:48:25 +00:00
|
|
|
* This too is a common class of extension with many methods in common,
|
|
|
|
* so we have a base class to extend from
|
|
|
|
*/
|
2012-02-08 12:07:01 +00:00
|
|
|
abstract class DataHandlerExtension extends Extension {
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param DataUploadEvent $event
|
|
|
|
* @throws UploadException
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
2014-04-06 19:47:01 +00:00
|
|
|
$supported_ext = $this->supported_ext($event->type);
|
|
|
|
$check_contents = $this->check_contents($event->tmpname);
|
2014-03-30 12:26:48 +00:00
|
|
|
if($supported_ext && $check_contents) {
|
2009-07-16 19:20:53 +00:00
|
|
|
if(!move_upload_to_archive($event)) return;
|
|
|
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
2011-08-26 01:35:59 +00:00
|
|
|
|
2011-08-25 00:53:53 +00:00
|
|
|
/* Check if we are replacing an image */
|
2012-01-27 18:16:46 +00:00
|
|
|
if(array_key_exists('replace', $event->metadata) && isset($event->metadata['replace'])) {
|
2011-08-26 01:35:59 +00:00
|
|
|
/* hax: This seems like such a dirty way to do this.. */
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
/* Validate things */
|
|
|
|
$image_id = int_escape($event->metadata['replace']);
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
/* Check to make sure the image exists. */
|
|
|
|
$existing = Image::by_id($image_id);
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
if(is_null($existing)) {
|
|
|
|
throw new UploadException("Image to replace does not exist!");
|
|
|
|
}
|
|
|
|
if ($existing->hash === $event->metadata['hash']) {
|
|
|
|
throw new UploadException("The uploaded image is the same as the one to replace.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// even more hax..
|
|
|
|
$event->metadata['tags'] = $existing->get_tag_list();
|
|
|
|
$image = $this->create_image_from_data(warehouse_path("images", $event->metadata['hash']), $event->metadata);
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
if(is_null($image)) {
|
|
|
|
throw new UploadException("Data handler failed to create image object from data");
|
|
|
|
}
|
|
|
|
|
2011-08-25 00:53:53 +00:00
|
|
|
$ire = new ImageReplaceEvent($image_id, $image);
|
|
|
|
send_event($ire);
|
|
|
|
$event->image_id = $image_id;
|
2011-08-26 01:35:59 +00:00
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
else {
|
2011-08-26 01:35:59 +00:00
|
|
|
$image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata);
|
|
|
|
if(is_null($image)) {
|
|
|
|
throw new UploadException("Data handler failed to create image object from data");
|
|
|
|
}
|
2012-08-18 18:45:39 +00:00
|
|
|
$iae = new ImageAdditionEvent($image);
|
2011-08-25 00:53:53 +00:00
|
|
|
send_event($iae);
|
|
|
|
$event->image_id = $iae->image->id;
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-12-23 21:27:38 +00:00
|
|
|
// Rating Stuff.
|
2011-12-24 20:18:00 +00:00
|
|
|
if(!empty($event->metadata['rating'])){
|
2011-12-23 21:27:38 +00:00
|
|
|
$rating = $event->metadata['rating'];
|
2012-08-18 18:45:39 +00:00
|
|
|
send_event(new RatingSetEvent($image, $rating));
|
2011-12-23 21:27:38 +00:00
|
|
|
}
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2011-12-24 20:18:00 +00:00
|
|
|
// Locked Stuff.
|
|
|
|
if(!empty($event->metadata['locked'])){
|
|
|
|
$locked = $event->metadata['locked'];
|
|
|
|
send_event(new LockSetEvent($image, !empty($locked)));
|
|
|
|
}
|
2011-08-25 00:53:53 +00:00
|
|
|
}
|
2009-07-16 19:20:53 +00:00
|
|
|
}
|
2014-01-01 17:25:28 +00:00
|
|
|
elseif($supported_ext && !$check_contents){
|
|
|
|
throw new UploadException("Invalid or corrupted file");
|
2014-01-01 00:41:11 +00:00
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
}
|
2009-07-16 19:20:53 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param ThumbnailGenerationEvent $event
|
|
|
|
*/
|
2012-02-05 05:06:55 +00:00
|
|
|
public function onThumbnailGeneration(ThumbnailGenerationEvent $event) {
|
2012-01-27 18:16:46 +00:00
|
|
|
if($this->supported_ext($event->type)) {
|
2012-01-12 01:54:27 +00:00
|
|
|
if (method_exists($this, 'create_thumb_force') && $event->force == true) {
|
|
|
|
$this->create_thumb_force($event->hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->create_thumb($event->hash);
|
|
|
|
}
|
2009-07-16 19:20:53 +00:00
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
}
|
2009-07-16 19:20:53 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param DisplayingImageEvent $event
|
|
|
|
*/
|
2012-02-02 13:58:48 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
2012-01-27 18:16:46 +00:00
|
|
|
global $page;
|
|
|
|
if($this->supported_ext($event->image->ext)) {
|
2009-07-16 19:20:53 +00:00
|
|
|
$this->theme->display_image($page, $event->image);
|
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
}
|
2010-08-06 23:24:31 +00:00
|
|
|
|
2014-04-26 02:33:57 +00:00
|
|
|
/*
|
2012-02-02 13:58:48 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2012-01-27 18:16:46 +00:00
|
|
|
$sb = $this->setup();
|
|
|
|
if($sb) $event->panel->add_block($sb);
|
2009-07-16 19:20:53 +00:00
|
|
|
}
|
|
|
|
|
2010-08-06 23:24:31 +00:00
|
|
|
protected function setup() {}
|
2014-04-26 02:33:57 +00:00
|
|
|
*/
|
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param string $ext
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-07-16 19:20:53 +00:00
|
|
|
abstract protected function supported_ext($ext);
|
2014-04-27 19:33:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $tmpname
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-07-16 19:20:53 +00:00
|
|
|
abstract protected function check_contents($tmpname);
|
2014-04-27 19:33:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $metadata
|
|
|
|
* @return Image|null
|
|
|
|
*/
|
2009-07-16 19:20:53 +00:00
|
|
|
abstract protected function create_image_from_data($filename, $metadata);
|
2014-04-27 19:33:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $hash
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-07-16 19:20:53 +00:00
|
|
|
abstract protected function create_thumb($hash);
|
|
|
|
}
|
2014-04-24 23:01:47 +00:00
|
|
|
|