Browse Source

rework class system to use subdirectories
add placeholder plugin/hook system

Andrew Dolgov 7 years ago
parent
commit
369dbc19d6

+ 2 - 0
backend.php

@@ -77,6 +77,8 @@
 		return;
 	}
 
+	$plugins = new Plugins($link);
+
 	$purge_intervals = array(
 		0  => __("Use default"),
 		-1 => __("Never purge"),

+ 1 - 1
classes/article.php

@@ -1,5 +1,5 @@
 <?php
-class Article extends Protected_Handler {
+class Article extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("redirect");

classes/auth_base.php → classes/auth/base.php


classes/auth_internal.php → classes/auth/internal.php


classes/auth_remote.php → classes/auth/remote.php


+ 1 - 1
classes/plugin_button.php

@@ -1,5 +1,5 @@
 <?php
-class Plugin_Button {
+class Button {
 
 	protected $link;
 

+ 1 - 1
classes/mail_button.php

@@ -1,5 +1,5 @@
 <?php
-class Mail_Button extends Plugin_Button {
+class Button_Mail extends Button {
 	function render($article_id) {
 		return "<img src=\"".theme_image($link, 'images/art-email.png')."\"
 					class='tagsPic' style=\"cursor : pointer\"

+ 1 - 1
classes/note_button.php

@@ -1,5 +1,5 @@
 <?php
-class Note_Button extends Plugin_Button {
+class Button_Note extends Button {
 	function render($article_id) {
 		return "<img src=\"".theme_image($this->link, "images/art-pub-note.png")."\"
 				style=\"cursor : pointer\" style=\"cursor : pointer\"

+ 1 - 1
classes/share_button.php

@@ -1,5 +1,5 @@
 <?php
-class Share_Button extends Plugin_Button {
+class Button_Share extends Button {
 	function render($article_id, $line) {
 		return "<img src=\"".theme_image($this->link, 'images/art-share.png')."\"
 			class='tagsPic' style=\"cursor : pointer\"

+ 1 - 1
classes/tweet_button.php

@@ -1,5 +1,5 @@
 <?php
-class Tweet_Button extends Plugin_Button {
+class Button_Tweet extends Button {
 	function render($article_id) {
 		$rv = "<img src=\"".theme_image($this->link, 'images/art-tweet.png')."\"
 			class='tagsPic' style=\"cursor : pointer\"

+ 16 - 2
classes/feeds.php

@@ -1,5 +1,5 @@
 <?php
-class Feeds extends Protected_Handler {
+class Feeds extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index");
@@ -121,6 +121,8 @@ class Feeds extends Protected_Handler {
 					$next_unread_feed, $offset, $vgr_last_feed = false,
 					$override_order = false, $include_children = false) {
 
+		global $plugins;
+
 		$disable_cache = false;
 
 		$reply = array();
@@ -220,10 +222,12 @@ class Feeds extends Protected_Handler {
 
 		$headlines_count = db_num_rows($result);
 
+		$plugins->hook('headlines_before', $reply);
+
 		if (get_pref($this->link, 'COMBINED_DISPLAY_MODE')) {
 			$button_plugins = array();
 			foreach (explode(",", ARTICLE_BUTTON_PLUGINS) as $p) {
-				$pclass = trim("${p}_button");
+				$pclass = trim("button_${p}");
 
 				if (class_exists($pclass)) {
 					$plugin = new $pclass($link);
@@ -245,6 +249,12 @@ class Feeds extends Protected_Handler {
 
 			while ($line = db_fetch_assoc($result)) {
 
+				if (get_pref($this->link, 'COMBINED_DISPLAY_MODE')) {
+					$plugins->hook('cdm_article_before', $line);
+				} else {
+					$plugins->hook('headlines_row', $line);
+				}
+
 				$class = ($lnum % 2) ? "even" : "odd";
 
 				$id = $line["id"];
@@ -673,11 +683,15 @@ class Feeds extends Protected_Handler {
 
 					$reply['content'] .= "</div>";
 
+					$plugins->hook('cdm_article_after', $reply['content']);
+
 				}
 
 				++$lnum;
 			}
 
+			$plugins->hook('headlines_after', $reply);
+
 			if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
 
 		} else {

+ 1 - 1
classes/protected_handler.php

@@ -1,5 +1,5 @@
 <?php
-class Protected_Handler extends Handler {
+class Handler_Protected extends Handler {
 
 	function before($method) {
 		return parent::before($method) && $_SESSION['uid'];

+ 1 - 1
classes/public_handler.php

@@ -1,5 +1,5 @@
 <?php
-class Public_Handler extends Handler {
+class Handler_Public extends Handler {
 
 	private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
 		$limit, $search, $search_mode, $match_on, $view_mode = false) {

+ 1 - 1
classes/opml.php

@@ -1,5 +1,5 @@
 <?php
-class Opml extends Protected_Handler {
+class Opml extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("export", "import");

+ 21 - 0
classes/plugin.php

@@ -0,0 +1,21 @@
+<?php
+class Plugin {
+	protected $link;
+	protected $handler;
+
+	function __construct($link, $handler) {
+		$this->link = $link;
+		$this->handler = $handler;
+		$this->initialize();
+	}
+
+	function initialize() {
+
+
+	}
+
+	function add_listener($hook) {
+		$this->handler->add_listener($hook, $this);
+	}
+}
+?>

+ 11 - 0
classes/plugin/example.php

@@ -0,0 +1,11 @@
+<?
+	class Plugin_Example extends Plugin {
+		function initialize() {
+			$this->add_listener('article_before');
+		}
+
+		function article_before(&$line) {
+			$line["title"] = "EXAMPLE/REPLACED:" . $line["title"];
+		}
+	}
+?>

+ 44 - 0
classes/plugins.php

@@ -0,0 +1,44 @@
+<?php
+class Plugins {
+	protected $link;
+	protected $plugins;
+	protected $listeners;
+
+	function __construct($link) {
+		$this->link = $link;
+		$this->listeners = array();
+		$this->load_plugins();
+	}
+
+	function load_plugins() {
+		if (defined('_ENABLE_PLUGINS')) {
+			$plugins = explode(",", _ENABLE_PLUGINS);
+
+			foreach ($plugins as $p) {
+				$plugin_class = "plugin_$p";
+				if (class_exists($plugin_class)) {
+					$plugin = new $plugin_class($this->link, $this);
+				}
+			}
+		}
+	}
+
+	function add_listener($hook_name, $plugin) {
+		if (!is_array($this->listeners[$hook_name]))
+			$this->listeners[$hook_name] = array();
+
+		array_push($this->listeners[$hook_name], $plugin);
+	}
+
+	function hook($hook_name, &$params) {
+		if (is_array($this->listeners[$hook_name])) {
+			foreach ($this->listeners[$hook_name] as $p) {
+				if (method_exists($p, $hook_name)) {
+					$p->$hook_name($params);
+				}
+			}
+		}
+	}
+
+}
+?>

+ 1 - 1
classes/pref_feeds.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Feeds extends Protected_Handler {
+class Pref_Feeds extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index", "getfeedtree", "add", "editcats", "editfeed",

+ 1 - 1
classes/pref_filters.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Filters extends Protected_Handler {
+class Pref_Filters extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index", "getfiltertree", "edit");

+ 1 - 1
classes/pref_instances.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Instances extends Protected_Handler {
+class Pref_Instances extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index", "edit");

+ 1 - 1
classes/pref_labels.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Labels extends Protected_Handler {
+class Pref_Labels extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index", "getlabeltree", "edit");

+ 1 - 1
classes/pref_prefs.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Prefs extends Protected_Handler {
+class Pref_Prefs extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("index");

+ 1 - 1
classes/pref_users.php

@@ -1,5 +1,5 @@
 <?php
-class Pref_Users extends Protected_Handler {
+class Pref_Users extends Handler_Protected {
 		function before($method) {
 			if (parent::before($method)) {
 				if ($_SESSION["access_level"] < 10) {

+ 2 - 2
classes/rpc.php

@@ -1,5 +1,5 @@
 <?php
-class RPC extends Protected_Handler {
+class RPC extends Handler_Protected {
 
 	function csrf_ignore($method) {
 		$csrf_ignored = array("sanitycheck", "buttonplugin", "exportget");
@@ -766,7 +766,7 @@ class RPC extends Protected_Handler {
 	}
 
 	function buttonPlugin() {
-		$pclass = basename($_REQUEST['plugin']) . "_button";
+		$pclass = "button_" . basename($_REQUEST['plugin']);
 		$method = $_REQUEST['plugin_method'];
 
 		if (class_exists($pclass)) {

+ 10 - 2
include/functions.php

@@ -3,7 +3,10 @@
 	define('SCHEMA_VERSION', 94);
 
 	function __autoload($class) {
-		$file = dirname(__FILE__)."/../classes/".strtolower(basename($class)).".php";
+		$class_file = str_replace("_", "/", strtolower(basename($class)));
+
+		$file = dirname(__FILE__)."/../classes/$class_file.php";
+
 		if (file_exists($file)) {
 			require $file;
 		}
@@ -3194,6 +3197,7 @@
 	}
 
 	function format_article($link, $id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
+		global $plugins;
 
 		if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
@@ -3256,6 +3260,8 @@
 
 			$line = db_fetch_assoc($result);
 
+			$plugins->hook('article_before', $line);
+
 			if ($line["icon_url"]) {
 				$feed_icon = "<img src=\"" . $line["icon_url"] . "\">";
 			} else {
@@ -3359,7 +3365,7 @@
 				$button_plugins = explode(",", ARTICLE_BUTTON_PLUGINS);
 
 				foreach ($button_plugins as $p) {
-					$pclass = trim("${p}_button");
+					$pclass = trim("button_${p}");
 
 					if (class_exists($pclass)) {
 						$plugin = new $pclass($link);
@@ -3468,6 +3474,8 @@
 			$rv['content'] .= "</body></html>";
 		}
 
+		$plugins->hook('article_after', $rv);
+
 		return $rv;
 
 	}

+ 5 - 0
include/rssfuncs.php

@@ -212,6 +212,8 @@
 	function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false,
 		$override_url = false) {
 
+		global $plugins;
+
 		require_once "lib/simplepie/simplepie.inc";
 		require_once "lib/magpierss/rss_fetch.inc";
 		require_once 'lib/magpierss/rss_utils.inc';
@@ -557,6 +559,9 @@
 			}
 
 			foreach ($iterator as $item) {
+				$hook_params = array("item" => &$item, "feed" => $feed);
+
+				$plugins->hook('rss_update_item', $hook_params);
 
 				if ($_REQUEST['xdebug'] == 2) {
 					print_r($item);

+ 1 - 1
public.php

@@ -40,7 +40,7 @@
 
 	$method = $_REQUEST["op"];
 
-	$handler = new Public_Handler($link, $_REQUEST);
+	$handler = new Handler_Public($link, $_REQUEST);
 
 	if ($handler->before($method)) {
 		if ($method && method_exists($handler, $method)) {

+ 2 - 0
update.php

@@ -56,6 +56,8 @@
 
 	init_connection($link);
 
+	$plugins = new Plugins($link);
+
 	if (in_array("-feeds", $op)) {
 		// Update all feeds needing a update.
 		update_daemon_common($link);

+ 2 - 0
update_daemon2.php

@@ -189,6 +189,8 @@
 
 					if (!init_connection($link)) return;
 
+					$plugins = new Plugins($link);
+
 					// We disable stamp file, since it is of no use in a multiprocess update.
 					// not really, tho for the time being -fox
 					if (!make_stampfile('update_daemon.stamp')) {