[1578] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 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 | |
---|
[3136] | 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 | } |
---|
[8263] | 122 | $args = func_get_args(); |
---|
[1578] | 123 | |
---|
| 124 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 125 | { |
---|
[3136] | 126 | foreach($handlers as $handler) |
---|
[1578] | 127 | { |
---|
[3136] | 128 | $function_name = $handler['function']; |
---|
| 129 | $accepted_args = $handler['accepted_args']; |
---|
[8263] | 130 | $args[1] = $data; |
---|
| 131 | $data = call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); |
---|
[1578] | 132 | } |
---|
| 133 | } |
---|
[3136] | 134 | trigger_action('trigger', |
---|
| 135 | array('type'=>'post_event', 'event'=>$event, 'data'=>$data) ); |
---|
[1590] | 136 | return $data; |
---|
| 137 | } |
---|
[1578] | 138 | |
---|
[1590] | 139 | function trigger_action($event, $data=null) |
---|
| 140 | { |
---|
| 141 | global $pwg_event_handlers; |
---|
[3136] | 142 | if ( isset($pwg_event_handlers['trigger']) and $event!='trigger' ) |
---|
[1590] | 143 | {// special case for debugging - avoid recursive calls |
---|
[3136] | 144 | trigger_action('trigger', |
---|
| 145 | array('type'=>'action', 'event'=>$event, 'data'=>$data) ); |
---|
[1578] | 146 | } |
---|
| 147 | |
---|
[1590] | 148 | if ( !isset($pwg_event_handlers[$event]) ) |
---|
| 149 | { |
---|
| 150 | return; |
---|
| 151 | } |
---|
[8263] | 152 | $args = func_get_args(); |
---|
[1578] | 153 | |
---|
[1590] | 154 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 155 | { |
---|
[3136] | 156 | foreach($handlers as $handler) |
---|
[1590] | 157 | { |
---|
[3136] | 158 | $function_name = $handler['function']; |
---|
| 159 | $accepted_args = $handler['accepted_args']; |
---|
[1578] | 160 | |
---|
[8263] | 161 | call_user_func_array($function_name, array_slice($args,1,$accepted_args) ); |
---|
[1590] | 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
[1578] | 165 | |
---|
[1705] | 166 | /** Saves some data with the associated plugim id. It can be retrieved later ( |
---|
| 167 | * during this script lifetime) using get_plugin_data |
---|
| 168 | * @param string plugin_id |
---|
| 169 | * @param mixed data |
---|
| 170 | * returns true on success, false otherwise |
---|
| 171 | */ |
---|
| 172 | function set_plugin_data($plugin_id, &$data) |
---|
| 173 | { |
---|
| 174 | global $pwg_loaded_plugins; |
---|
| 175 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
| 176 | { |
---|
| 177 | $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data; |
---|
| 178 | return true; |
---|
| 179 | } |
---|
| 180 | return false; |
---|
| 181 | } |
---|
[1590] | 182 | |
---|
[1705] | 183 | /** Retrieves plugin data saved previously with set_plugin_data |
---|
| 184 | * @param string plugin_id |
---|
| 185 | */ |
---|
| 186 | function &get_plugin_data($plugin_id) |
---|
| 187 | { |
---|
| 188 | global $pwg_loaded_plugins; |
---|
| 189 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
| 190 | { |
---|
| 191 | return $pwg_loaded_plugins[$plugin_id]['plugin_data']; |
---|
| 192 | } |
---|
| 193 | return null; |
---|
| 194 | } |
---|
| 195 | |
---|
[1584] | 196 | /* Returns an array of plugins defined in the database |
---|
| 197 | * @param string $state optional filter on this state |
---|
| 198 | * @param string $id optional returns only data about given plugin |
---|
| 199 | */ |
---|
| 200 | function get_db_plugins($state='', $id='') |
---|
[1578] | 201 | { |
---|
[1584] | 202 | $query = ' |
---|
| 203 | SELECT * FROM '.PLUGINS_TABLE; |
---|
[3136] | 204 | $clauses = array(); |
---|
| 205 | if (!empty($state)) |
---|
[1578] | 206 | { |
---|
[4367] | 207 | $clauses[] = 'state=\''.$state.'\''; |
---|
[3136] | 208 | } |
---|
| 209 | if (!empty($id)) |
---|
| 210 | { |
---|
| 211 | $clauses[] = 'id="'.$id.'"'; |
---|
| 212 | } |
---|
| 213 | if (count($clauses)) |
---|
| 214 | { |
---|
[1584] | 215 | $query .= ' |
---|
[3136] | 216 | WHERE '. implode(' AND ', $clauses); |
---|
[1578] | 217 | } |
---|
[1584] | 218 | |
---|
| 219 | $result = pwg_query($query); |
---|
| 220 | $plugins = array(); |
---|
[4325] | 221 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1578] | 222 | { |
---|
[1584] | 223 | array_push($plugins, $row); |
---|
[1578] | 224 | } |
---|
[1584] | 225 | return $plugins; |
---|
[1578] | 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
[1604] | 229 | function load_plugin($plugin) |
---|
| 230 | { |
---|
| 231 | $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; |
---|
| 232 | if ( file_exists($file_name) ) |
---|
| 233 | { |
---|
[1705] | 234 | global $pwg_loaded_plugins; |
---|
| 235 | $pwg_loaded_plugins[ $plugin['id'] ] = $plugin; |
---|
[1604] | 236 | include_once( $file_name ); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
[1584] | 240 | /*loads all the plugins on startup*/ |
---|
[1578] | 241 | function load_plugins() |
---|
| 242 | { |
---|
[1705] | 243 | global $conf, $pwg_loaded_plugins; |
---|
| 244 | $pwg_loaded_plugins = array(); |
---|
[1616] | 245 | if ($conf['enable_plugins']) |
---|
[1584] | 246 | { |
---|
[1616] | 247 | $plugins = get_db_plugins('active'); |
---|
| 248 | foreach( $plugins as $plugin) |
---|
| 249 | {// include main from a function to avoid using same function context |
---|
| 250 | load_plugin($plugin); |
---|
| 251 | } |
---|
| 252 | trigger_action('plugins_loaded'); |
---|
[1584] | 253 | } |
---|
[1578] | 254 | } |
---|
| 255 | ?> |
---|