[1578] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[3046] | 5 | // | Copyright(C) 2008-2009 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1578] | 23 | |
---|
| 24 | /* |
---|
[2339] | 25 | Events and event handlers are the core of Piwigo plugin management. |
---|
[1578] | 26 | Plugins are addons that are found in plugins subdirectory. If activated, PWG |
---|
| 27 | will include the index.php of each plugin. |
---|
| 28 | Events are triggered by PWG core code. Plugins (or even PWG itself) can |
---|
| 29 | register their functions to handle these events. An event is identified by a |
---|
| 30 | string. |
---|
| 31 | */ |
---|
| 32 | |
---|
[1699] | 33 | define('PHPWG_PLUGINS_PATH', PHPWG_ROOT_PATH.'plugins/'); |
---|
[1578] | 34 | |
---|
[1590] | 35 | define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); |
---|
| 36 | |
---|
[1578] | 37 | /* Register a event handler. |
---|
| 38 | * @param string $event the name of the event to listen to |
---|
| 39 | * @param mixed $func the function that will handle the event |
---|
[1590] | 40 | * @param int $priority optional priority (greater priority will |
---|
| 41 | * be executed at last) |
---|
[1578] | 42 | */ |
---|
[1590] | 43 | function add_event_handler($event, $func, |
---|
| 44 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $accepted_args=1) |
---|
[1578] | 45 | { |
---|
| 46 | global $pwg_event_handlers; |
---|
| 47 | |
---|
[1590] | 48 | if ( isset($pwg_event_handlers[$event][$priority]) ) |
---|
[1578] | 49 | { |
---|
[1590] | 50 | foreach($pwg_event_handlers[$event][$priority] as $handler) |
---|
[1578] | 51 | { |
---|
| 52 | if ( $handler['function'] == $func ) |
---|
| 53 | { |
---|
[1604] | 54 | return false; |
---|
[1578] | 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
[1590] | 59 | $pwg_event_handlers[$event][$priority][] = |
---|
[1578] | 60 | array( |
---|
| 61 | 'function'=>$func, |
---|
| 62 | 'accepted_args'=>$accepted_args); |
---|
[1590] | 63 | ksort( $pwg_event_handlers[$event] ); |
---|
[1578] | 64 | return true; |
---|
| 65 | } |
---|
| 66 | |
---|
[1590] | 67 | /* Register a event handler. |
---|
| 68 | * @param string $event the name of the event to listen to |
---|
| 69 | * @param mixed $func the function that needs removal |
---|
| 70 | * @param int $priority optional priority (greater priority will |
---|
| 71 | * be executed at last) |
---|
| 72 | */ |
---|
| 73 | function remove_event_handler($event, $func, |
---|
| 74 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL) |
---|
| 75 | { |
---|
| 76 | global $pwg_event_handlers; |
---|
[1578] | 77 | |
---|
[1590] | 78 | if (!isset( $pwg_event_handlers[$event][$priority] ) ) |
---|
| 79 | { |
---|
| 80 | return false; |
---|
| 81 | } |
---|
| 82 | for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++) |
---|
| 83 | { |
---|
| 84 | if ($pwg_event_handlers[$event][$priority][$i]['function']==$func) |
---|
| 85 | { |
---|
| 86 | unset($pwg_event_handlers[$event][$priority][$i]); |
---|
| 87 | $pwg_event_handlers[$event][$priority] = |
---|
| 88 | array_values($pwg_event_handlers[$event][$priority]); |
---|
| 89 | |
---|
| 90 | if ( empty($pwg_event_handlers[$event][$priority]) ) |
---|
| 91 | { |
---|
| 92 | unset( $pwg_event_handlers[$event][$priority] ); |
---|
| 93 | if (empty( $pwg_event_handlers[$event] ) ) |
---|
| 94 | { |
---|
| 95 | unset( $pwg_event_handlers[$event] ); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | return true; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | |
---|
[1584] | 104 | /* Triggers an event and calls all registered event handlers |
---|
| 105 | * @param string $event name of the event |
---|
| 106 | * @param mixed $data data to pass to handlers |
---|
| 107 | */ |
---|
[1578] | 108 | function trigger_event($event, $data=null) |
---|
| 109 | { |
---|
| 110 | global $pwg_event_handlers; |
---|
[1590] | 111 | |
---|
[3137] | 112 | if ( isset($pwg_event_handlers['trigger']) ) |
---|
| 113 | {// just for debugging |
---|
| 114 | trigger_action('trigger', |
---|
| 115 | array('type'=>'event', 'event'=>$event, 'data'=>$data) ); |
---|
| 116 | } |
---|
[1578] | 117 | |
---|
| 118 | if ( !isset($pwg_event_handlers[$event]) ) |
---|
| 119 | { |
---|
| 120 | return $data; |
---|
| 121 | } |
---|
| 122 | $args = array_slice(func_get_args(), 2); |
---|
| 123 | |
---|
| 124 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 125 | { |
---|
[3137] | 126 | foreach($handlers as $handler) |
---|
[1578] | 127 | { |
---|
[3137] | 128 | $all_args = array_merge( array($data), $args); |
---|
| 129 | $function_name = $handler['function']; |
---|
| 130 | $accepted_args = $handler['accepted_args']; |
---|
[1578] | 131 | |
---|
[3137] | 132 | if ( $accepted_args == 1 ) |
---|
| 133 | $the_args = array($data); |
---|
| 134 | elseif ( $accepted_args > 1 ) |
---|
| 135 | $the_args = array_slice($all_args, 0, $accepted_args); |
---|
| 136 | elseif ( $accepted_args == 0 ) |
---|
| 137 | $the_args = NULL; |
---|
| 138 | else |
---|
| 139 | $the_args = $all_args; |
---|
[1578] | 140 | |
---|
[3137] | 141 | $data = call_user_func_array($function_name, $the_args); |
---|
[1578] | 142 | } |
---|
| 143 | } |
---|
[3137] | 144 | trigger_action('trigger', |
---|
| 145 | array('type'=>'post_event', 'event'=>$event, 'data'=>$data) ); |
---|
[1590] | 146 | return $data; |
---|
| 147 | } |
---|
[1578] | 148 | |
---|
[1590] | 149 | function trigger_action($event, $data=null) |
---|
| 150 | { |
---|
| 151 | global $pwg_event_handlers; |
---|
[3137] | 152 | if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' ) |
---|
[1590] | 153 | {// special case for debugging - avoid recursive calls |
---|
[3137] | 154 | trigger_action('trigger', |
---|
| 155 | array('type'=>'action', 'event'=>$event, 'data'=>$data) ); |
---|
[1578] | 156 | } |
---|
| 157 | |
---|
[1590] | 158 | if ( !isset($pwg_event_handlers[$event]) ) |
---|
| 159 | { |
---|
| 160 | return; |
---|
| 161 | } |
---|
| 162 | $args = array_slice(func_get_args(), 2); |
---|
[1578] | 163 | |
---|
[1590] | 164 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 165 | { |
---|
[3137] | 166 | foreach($handlers as $handler) |
---|
[1590] | 167 | { |
---|
[3137] | 168 | $all_args = array_merge( array($data), $args); |
---|
| 169 | $function_name = $handler['function']; |
---|
| 170 | $accepted_args = $handler['accepted_args']; |
---|
[1578] | 171 | |
---|
[3137] | 172 | if ( $accepted_args == 1 ) |
---|
| 173 | $the_args = array($data); |
---|
| 174 | elseif ( $accepted_args > 1 ) |
---|
| 175 | $the_args = array_slice($all_args, 0, $accepted_args); |
---|
| 176 | elseif ( $accepted_args == 0 ) |
---|
| 177 | $the_args = NULL; |
---|
| 178 | else |
---|
| 179 | $the_args = $all_args; |
---|
[1578] | 180 | |
---|
[3137] | 181 | call_user_func_array($function_name, $the_args); |
---|
[1590] | 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
[1578] | 185 | |
---|
[1705] | 186 | /** Saves some data with the associated plugim id. It can be retrieved later ( |
---|
| 187 | * during this script lifetime) using get_plugin_data |
---|
| 188 | * @param string plugin_id |
---|
| 189 | * @param mixed data |
---|
| 190 | * returns true on success, false otherwise |
---|
| 191 | */ |
---|
| 192 | function set_plugin_data($plugin_id, &$data) |
---|
| 193 | { |
---|
| 194 | global $pwg_loaded_plugins; |
---|
| 195 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
| 196 | { |
---|
| 197 | $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data; |
---|
| 198 | return true; |
---|
| 199 | } |
---|
| 200 | return false; |
---|
| 201 | } |
---|
[1590] | 202 | |
---|
[1705] | 203 | /** Retrieves plugin data saved previously with set_plugin_data |
---|
| 204 | * @param string plugin_id |
---|
| 205 | */ |
---|
| 206 | function &get_plugin_data($plugin_id) |
---|
| 207 | { |
---|
| 208 | global $pwg_loaded_plugins; |
---|
| 209 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
| 210 | { |
---|
| 211 | return $pwg_loaded_plugins[$plugin_id]['plugin_data']; |
---|
| 212 | } |
---|
| 213 | return null; |
---|
| 214 | } |
---|
| 215 | |
---|
[1584] | 216 | /* Returns an array of plugins defined in the database |
---|
| 217 | * @param string $state optional filter on this state |
---|
| 218 | * @param string $id optional returns only data about given plugin |
---|
| 219 | */ |
---|
| 220 | function get_db_plugins($state='', $id='') |
---|
[1578] | 221 | { |
---|
[1584] | 222 | $query = ' |
---|
| 223 | SELECT * FROM '.PLUGINS_TABLE; |
---|
[3137] | 224 | $clauses = array(); |
---|
| 225 | if (!empty($state)) |
---|
[1578] | 226 | { |
---|
[3137] | 227 | $clauses[] = 'state="'.$state.'"'; |
---|
| 228 | } |
---|
| 229 | if (!empty($id)) |
---|
| 230 | { |
---|
| 231 | $clauses[] = 'id="'.$id.'"'; |
---|
| 232 | } |
---|
| 233 | if (count($clauses)) |
---|
| 234 | { |
---|
[1584] | 235 | $query .= ' |
---|
[3137] | 236 | WHERE '. implode(' AND ', $clauses); |
---|
[1578] | 237 | } |
---|
[1584] | 238 | |
---|
| 239 | $result = pwg_query($query); |
---|
| 240 | $plugins = array(); |
---|
| 241 | while ($row = mysql_fetch_array($result)) |
---|
[1578] | 242 | { |
---|
[1584] | 243 | array_push($plugins, $row); |
---|
[1578] | 244 | } |
---|
[1584] | 245 | return $plugins; |
---|
[1578] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[1604] | 249 | function load_plugin($plugin) |
---|
| 250 | { |
---|
| 251 | $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; |
---|
| 252 | if ( file_exists($file_name) ) |
---|
| 253 | { |
---|
[1705] | 254 | global $pwg_loaded_plugins; |
---|
| 255 | $pwg_loaded_plugins[ $plugin['id'] ] = $plugin; |
---|
[1604] | 256 | include_once( $file_name ); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
[1584] | 260 | /*loads all the plugins on startup*/ |
---|
[1578] | 261 | function load_plugins() |
---|
| 262 | { |
---|
[1705] | 263 | global $conf, $pwg_loaded_plugins; |
---|
| 264 | $pwg_loaded_plugins = array(); |
---|
[1616] | 265 | if ($conf['enable_plugins']) |
---|
[1584] | 266 | { |
---|
[1616] | 267 | $plugins = get_db_plugins('active'); |
---|
| 268 | foreach( $plugins as $plugin) |
---|
| 269 | {// include main from a function to avoid using same function context |
---|
| 270 | load_plugin($plugin); |
---|
| 271 | } |
---|
| 272 | trigger_action('plugins_loaded'); |
---|
[1584] | 273 | } |
---|
[1578] | 274 | } |
---|
| 275 | ?> |
---|