]> git.wh0rd.org - tt-rss.git/commitdiff
implement plugin routing masks, add example plugin
authorAndrew Dolgov <fox@fakecake.org>
Sun, 23 Dec 2012 19:05:51 +0000 (23:05 +0400)
committerAndrew Dolgov <fox@fakecake.org>
Sun, 23 Dec 2012 19:05:51 +0000 (23:05 +0400)
backend.php
classes/handler.php
classes/ihandler.php [new file with mode: 0644]
classes/pluginhost.php
include/functions.php
plugins/example_routing/example_routing.php [new file with mode: 0644]
public.php

index 8aaf1001657c573c9dd3dabec24405674ccd2bdf..2cac8232b7f7a1aa3b93d7884f35856787597c4b 100644 (file)
 
        $op = str_replace("-", "_", $op);
 
-       if (class_exists($op)) {
-               $handler = new $op($link, $_REQUEST);
+       global $pluginhost;
+       $override = $pluginhost->lookup_handler($op, $method);
 
-               if ($handler && is_subclass_of($handler, 'Handler')) {
+       if (class_exists($op) || $override) {
+
+               if ($override) {
+                       $handler = $override;
+               } else {
+                       $handler = new $op($link, $_REQUEST);
+               }
+
+               if ($handler && implements_interface($handler, 'IHandler')) {
                        if (validate_csrf($csrf_token) || $handler->csrf_ignore($method)) {
                                if ($handler->before($method)) {
                                        if ($method && method_exists($handler, $method)) {
index e00b36aa34994a981fdb3706fc91ffdda37a8dca..68b16eac199f608149ade09ae3e4c51a0ebb5569 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-class Handler {
+class Handler implements IHandler {
        protected $link;
        protected $args;
 
diff --git a/classes/ihandler.php b/classes/ihandler.php
new file mode 100644 (file)
index 0000000..e3c8a53
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+interface IHandler {
+       function csrf_ignore($method);
+       function before($method);
+       function after();
+}
+?>
index d7926fa4e0bf62f5b3cd1224016fedaeed91bc18..b28d2511d7423c391d2d649e77fba41bce0a2295 100644 (file)
@@ -3,6 +3,7 @@ class PluginHost {
        private $link;
        private $hooks = array();
        private $plugins = array();
+       private $handlers = array();
 
        const HOOK_ARTICLE_BUTTON = 1;
        const HOOK_ARTICLE_FILTER = 2;
@@ -62,7 +63,7 @@ class PluginHost {
 
                foreach ($plugins as $class) {
                        $class = trim($class);
-                       $class_file = str_replace("_", "/", strtolower(basename($class)));
+                       $class_file = strtolower(basename($class));
                        $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
 
                        if (file_exists($file)) require_once $file;
@@ -75,5 +76,33 @@ class PluginHost {
                }
        }
 
+       function add_handler($handler, $method, $sender) {
+               $handler = strtolower($handler);
+               $method = strtolower($method);
+
+               if (!is_array($this->handlers[$handler])) {
+                       $this->handlers[$handler] = array();
+               }
+
+               $this->handlers[$handler][$method] = $sender;
+       }
+
+       function del_handler($handler, $method) {
+               $handler = strtolower($handler);
+               $method = strtolower($method);
+
+               unset($this->handlers[$handler][$method]);
+       }
+
+       function lookup_handler($handler, $method) {
+               $handler = strtolower($handler);
+               $method = strtolower($method);
+
+               if (is_array($this->handlers[$handler])) {
+                       return $this->handlers[$handler][$method];
+               }
+
+               return false;
+       }
 }
 ?>
index a4caf9fde306e9464b5d6dbec96d9c4dc7453c3d..b338bde5bc5eb468c43d99db34a61c51a68268c7 100644 (file)
                return $rc;
        }
 
+       function implements_interface($class, $interface) {
+               return in_array($interface, class_implements($class));
+       }
+
 ?>
diff --git a/plugins/example_routing/example_routing.php b/plugins/example_routing/example_routing.php
new file mode 100644 (file)
index 0000000..a5c4e61
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+class Example_Routing extends Plugin implements IHandler {
+
+       // Demonstrates adding a custom handler and method:
+       // backend.php?op=test&method=example
+       // and masking a system builtin public method:
+       // public.php?op=getUnread
+
+       // Plugin class must implelement IHandler interface and has
+       // a public method of same name as being registered.
+       //
+       // Any system method may be masked by plugins.
+
+       private $link;
+       private $host;
+
+       function __construct($host) {
+               $this->link = $host->get_link();
+               $this->host = $host;
+
+               $host->add_handler("test", "example", $this);
+               $host->add_handler("public", "getunread", $this);
+       }
+
+       function getunread() {
+               print rand(0,100); # yeah right
+       }
+
+       function example() {
+               print "example method called";
+       }
+
+       function csrf_ignore($method) {
+               return true;
+       }
+
+       function before($method) {
+               return true;
+       }
+
+       function after() {
+               return true;
+       }
+
+}
+?>
index 59e0ef2e3696082acbc9cb9ab2f017a60817747d..4cf7653f99a376223e099bb4cab4a6a1f4203cf7 100644 (file)
 
        $method = $_REQUEST["op"];
 
-       $handler = new Handler_Public($link, $_REQUEST);
+       global $pluginhost;
+       $override = $pluginhost->lookup_handler("public", $method);
 
-       if ($handler->before($method)) {
+       if ($override) {
+               $handler = $override;
+       } else {
+               $handler = new Handler_Public($link, $_REQUEST);
+       }
+
+       if (implements_interface($handler, "IHandler") && $handler->before($method)) {
                if ($method && method_exists($handler, $method)) {
                        $handler->$method();
                } else if (method_exists($handler, 'index')) {