root/trunk/gregarius/plugins.php

Revision 1486, 6.5 kB (checked in by sdcosta, 2 years ago)

Fixed a problem with arrays of escaped strings not going in / coming out
correctly in the plugins config section.

  • 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');
29
30/**
31 * (private)
32 * Returns a reference to the hooks array, which holds
33 * references of installed plugin's callback functions
34 */
35function & __getHooksArray() {
36    static $__rss_hooks;
37    if ($__rss_hooks == null) {
38        $__rss_hooks = array();
39    }
40    return ($__rss_hooks);
41}
42
43/**
44 * (private)
45 * Returns a reference to the filename of the plugin
46 * being currently loaded
47 */
48function & __getLoadingPlugin() {
49    static $__rss_loading_plugin;
50    return ($__rss_loading_plugin);
51}
52
53function set_loading_plugin($loading) {
54    $static_var =& __getLoadingPlugin();
55    $static_var = $loading;
56}
57
58/**
59 * Allows a plugin to register itself for the
60 * given hook
61 */
62function rss_set_hook($hook,$fnct) {
63    $hooks =& __getHooksArray();
64    $loading_filename =& __getLoadingPlugin();
65
66    if (!array_key_exists($loading_filename, $hooks)) {
67        $hooks[$loading_filename]=array();
68    }
69    $filehooks =& $hooks[$loading_filename];
70   
71    if (array_key_exists($hook, $filehooks)) {
72        $filehooks[$hook][] = $fnct;
73    } else {
74        $filehooks[$hook]=array($fnct);
75    }
76}
77
78/**
79 * Performs callbacks for the given hook,
80 * based on the plugins registered functions
81 */
82function rss_plugin_hook($hook, $data, $plugin_filename=null) {
83    $hooks =& __getHooksArray();
84    foreach($hooks as $this_plugin_name => $filehooks) {
85        if (!isset($plugin_filename) || $this_plugin_name == $plugin_filename) {
86            if (array_key_exists($hook, $filehooks)) {
87                foreach($filehooks[$hook] as $fnct) {
88                    if (function_exists($fnct)) {
89                        _pf("calling plugin func $fnct in '$this_plugin_name' for $hook ...");
90                        $data = call_user_func($fnct,$data);
91                        _pf("done");
92                    }
93                }
94            }
95        }
96    }
97    return $data;
98}
99
100
101/***** Plugin options ******/
102
103/**
104 * Wrapper functions for plugins
105 *
106 * Notes: Make sure that $value and $default are not escaped strings/arrays of strings,
107 *        otherwise they will not come out of the database correctly because of rss_real_escape_string
108 */
109function rss_plugins_add_option($key, $value, $type = "string", $default = "", $desc= "", $export = NULL) {
110    if (!$key) {
111        return false;
112    }
113    $pKey = "plugins." . rss_real_escape_string($key);
114
115    if (is_array($value) || $type == 'array') {
116        $value = serialize($value);
117    }
118    $value = rss_real_escape_string($value);
119    $default = $default? $default: $value;
120    $ret =  rss_query("replace into " . getTable("config")
121                      . " (key_,value_,default_,type_,desc_,export_) VALUES ("
122                      . "'$pKey','$value','$default','$type','$desc','$export')" );
123    configInvalidate();
124    return $ret;
125
126}
127
128function rss_plugins_get_option($key) {
129    if (!$key) {
130        return false;
131    }
132    return getConfig("plugins.".rss_real_escape_string($key), false);
133
134}
135
136function rss_plugins_delete_option($key) {
137    if (!$key) {
138        return;
139    }
140    $pKey = "plugins." . rss_real_escape_string($key);
141    $ret = rss_query("delete from " . getTable("config") . " where key_='$pKey'");
142    configInvalidate();
143    return $ret;
144
145}
146
147function rss_plugins_set_item_state($itemId, $bit_mask, $set
148                                    , $sqlwhere = "", $entire_db = false) {
149    $retvalue = false;
150    if($itemId || $entire_db) { // Check to see if itemId is set or if we are allowed to fsck up the entire db
151        // the bitmask has a one in the spot (field(s)) we want to change.
152        if($set
153          ) { // Set the value to the field to 1
154            $sql = "update " .getTable("item") ." set unread = unread | ".  $bit_mask;
155        }
156        else { // set the value of the field to 0
157            $sql = "update " .getTable("item") ." set unread = unread & ". ~ $bit_mask;
158        }
159
160        if($itemId) {
161            if (is_array($itemId)) {
162                $sql .= " where id  in (" . implode(',',$itemId) .")";
163            } else { // assume it is a number or a string
164                $sql .= " where id=" .$itemId;
165            }
166        } else {
167            $sql .= " where 1";
168        }
169
170        if($sqlwhere) {
171            $sql .= " and " . $sqlwhere;
172        }
173
174        $retvalue =  rss_query($sql);
175        rss_invalidate_cache();
176
177    } else {
178        $retvalue = false;
179    }
180    return $retvalue;
181}
182
183function rss_plugins_get_plugins_http_path() {
184    //returns http://example.com/rss/plugins/
185    return guessTransportProto().$_SERVER['HTTP_HOST'] . getPath() . RSS_PLUGINS_DIR . "/";
186}
187
188/**
189 * loads the specified plugin file
190 */
191function rss_load_plugin( $plugin_filename )
192{
193        set_loading_plugin($plugin_filename);
194        if( file_exists(rss_home_dir() . RSS_PLUGINS_DIR.'/'.$plugin_filename ) ) {
195                rss_require(RSS_PLUGINS_DIR.'/'.$plugin_filename);
196        }
197        set_loading_plugin('');
198}
199
200/**
201 * loads the active plugins from the config, instantiates
202 * them
203 */
204foreach(getConfig('rss.config.plugins') as $pf) {
205    rss_load_plugin($pf);
206}
207
208/*
209 * autoload plugins specific to a theme without the
210 * need to add them to config
211 * all plugins def must be inside a unique file called plugins.php that
212 * do all the works
213 */
214
215$mythemeplugin=getThemePath(GREGARIUS_HOME)."plugins.php";
216if (file_exists($mythemeplugin)) {
217    require_once($mythemeplugin);
218}
219?>
Note: See TracBrowser for help on using the browser.