rework class system to use subdirectories

add placeholder plugin/hook system
This commit is contained in:
Andrew Dolgov 2012-08-17 14:20:55 +04:00
parent 3d2c9f5adf
commit 369dbc19d6
29 changed files with 131 additions and 22 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

21
classes/plugin.php Normal file
View File

@ -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);
}
}
?>

View File

@ -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
classes/plugins.php Normal file
View File

@ -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);
}
}
}
}
}
?>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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