root/branches/multiuser/plugins.php

Revision 1759, 6.5 kB (checked in by mdodoo, 2 years ago)

Lots of things are still not working, but I think this code is better than what was currently committed ([1639]
should have been reverted, and the person who committed should have had their SVN access pulled, for example).
Created this by grabbing the trunk code and then manually inserting the MU branch's changes in. This is probably
not usable in an actual installation (no support for creating new user accounts yet, for example), but patches are
welcome.

Not sure why I write so much here - I am not sure anyone other than my fellow devs actually read them...

  • Property svn:eol-style set to native
  • Property svn:eolstyle set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:      mbonetti at gmail dot com
24# Web page:    http://gregarius.net/
25#
26###############################################################################
27
28require_once('init.php');
29require_once('cls/config.php');
30
31/**
32 * (private)
33 * Returns a reference to the hooks array, which holds
34 * references of installed plugin's callback functions
35 */
36function & __getHooksArray() {
37    static $__rss_hooks;
38    if ($__rss_hooks == null) {
39        $__rss_hooks = array();
40    }
41    return ($__rss_hooks);
42}
43
44/**
45 * (private)
46 * Returns a reference to the filename of the plugin
47 * being currently loaded
48 */
49function & __getLoadingPlugin() {
50    static $__rss_loading_plugin;
51    return ($__rss_loading_plugin);
52}
53
54function set_loading_plugin($loading) {
55    $static_var =& __getLoadingPlugin();
56    $static_var = $loading;
57}
58
59/**
60 * Allows a plugin to register itself for the
61 * given hook
62 */
63function rss_set_hook($hook,$fnct) {
64    $hooks =& __getHooksArray();
65    $loading_filename =& __getLoadingPlugin();
66
67    if (!array_key_exists($loading_filename, $hooks)) {
68        $hooks[$loading_filename]=array();
69    }
70    $filehooks =& $hooks[$loading_filename];
71   
72    if (array_key_exists($hook, $filehooks)) {
73        $filehooks[$hook][] = $fnct;
74    } else {
75        $filehooks[$hook]=array($fnct);
76    }
77}
78
79/**
80 * Performs callbacks for the given hook,
81 * based on the plugins registered functions
82 */
83function rss_plugin_hook($hook, $data, $plugin_filename=null) {
84    $hooks =& __getHooksArray();
85    foreach($hooks as $this_plugin_name => $filehooks) {
86        if (!isset($plugin_filename) || $this_plugin_name == $plugin_filename) {
87            if (array_key_exists($hook, $filehooks)) {
88                foreach($filehooks[$hook] as $fnct) {
89                    if (function_exists($fnct)) {
90                        _pf("calling plugin func $fnct in '$this_plugin_name' for $hook ...");
91                        $data = call_user_func($fnct,$data);
92                        _pf("done");
93                    }
94                }
95            }
96        }
97    }
98    return $data;
99}
100
101
102/***** Plugin options ******/
103
104/**
105 * Wrapper functions for plugins
106 *
107 * Notes: Make sure that $value and $default are not escaped strings/arrays of strings,
108 *        otherwise they will not come out of the database correctly because of rss_real_escape_string
109 */
110function rss_plugins_add_option($key, $value, $type = "string", $default = "", $desc= "", $export = NULL) {
111    if (!$key) {
112        return false;
113    }
114    $pKey = "plugins." . rss_real_escape_string($key);
115
116    if (is_array($value) || $type == 'array') {
117        $value = serialize($value);
118    }
119    $value = rss_real_escape_string($value);
120    $default = $default? $default: $value;
121    $ret =  rss_query("replace into " . getTable("config")
122                      . " (key_,value_,default_,type_,desc_,export_) VALUES ("
123                      . "'$pKey','$value','$default','$type','$desc','$export')" );
124    configInvalidate();
125    return $ret;
126
127}
128
129function rss_plugins_get_option($key) {
130    if (!$key) {
131        return false;
132    }
133    return getConfig("plugins.".rss_real_escape_string($key), false);
134
135}
136
137function rss_plugins_delete_option($key) {
138    if (!$key) {
139        return;
140    }
141    $pKey = "plugins." . rss_real_escape_string($key);
142    $ret = rss_query("delete from " . getTable("config") . " where key_='$pKey'");
143    configInvalidate();
144    return $ret;
145
146}
147
148function rss_plugins_set_item_state($itemId, $bit_mask, $set
149                                    , $sqlwhere = "", $entire_db = false) {
150    $retvalue = false;
151    if($itemId || $entire_db) { // Check to see if itemId is set or if we are allowed to fsck up the entire db
152        // the bitmask has a one in the spot (field(s)) we want to change.
153        if($set
154          ) { // Set the value to the field to 1
155            $sql = "update " .getTable("item") ." set unread = unread | ".  $bit_mask;
156        }
157        else { // set the value of the field to 0
158            $sql = "update " .getTable("item") ." set unread = unread & ". ~ $bit_mask;
159        }
160
161        if($itemId) {
162            if (is_array($itemId)) {
163                $sql .= " where id  in (" . implode(',',$itemId) .")";
164            } else { // assume it is a number or a string
165                $sql .= " where id=" .$itemId;
166            }
167        } else {
168            $sql .= " where 1";
169        }
170
171        if($sqlwhere) {
172            $sql .= " and " . $sqlwhere;
173        }
174
175        $retvalue =  rss_query($sql);
176        rss_invalidate_cache();
177
178    } else {
179        $retvalue = false;
180    }
181    return $retvalue;
182}
183
184function rss_plugins_get_plugins_http_path() {
185    //returns http://example.com/rss/plugins/
186    return guessTransportProto().$_SERVER['HTTP_HOST'] . getPath() . RSS_PLUGINS_DIR . "/";
187}
188
189/**
190 * loads the specified plugin file
191 */
192function rss_load_plugin( $plugin_filename )
193{
194        set_loading_plugin($plugin_filename);
195        if( file_exists(rss_home_dir() . RSS_PLUGINS_DIR.'/'.$plugin_filename ) ) {
196                rss_require(RSS_PLUGINS_DIR.'/'.$plugin_filename);
197        }
198        set_loading_plugin('');
199}
200
201/**
202 * loads the active plugins from the config, instantiates
203 * them
204 */
205foreach(getConfig('rss.config.plugins') as $pf) {
206    rss_load_plugin($pf);
207}
208
209/*
210 * autoload plugins specific to a theme without the
211 * need to add them to config
212 * all plugins def must be inside a unique file called plugins.php that
213 * do all the works
214 */
215
216$mythemeplugin=getThemePath(GREGARIUS_HOME)."plugins.php";
217if (file_exists($mythemeplugin)) {
218    require_once($mythemeplugin);
219}
220?>
Note: See TracBrowser for help on using the browser.