[1578] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 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 | |
---|
[25601] | 24 | /** |
---|
| 25 | * @package functions\plugins |
---|
| 26 | */ |
---|
[1578] | 27 | |
---|
[25601] | 28 | |
---|
| 29 | /** base directory of plugins */ |
---|
[1699] | 30 | define('PHPWG_PLUGINS_PATH', PHPWG_ROOT_PATH.'plugins/'); |
---|
[25601] | 31 | /** default priority for plugins handlers */ |
---|
[1590] | 32 | define('EVENT_HANDLER_PRIORITY_NEUTRAL', 50); |
---|
| 33 | |
---|
[25406] | 34 | |
---|
| 35 | /** |
---|
[25601] | 36 | * Used to declare maintenance methods of a plugin. |
---|
[25406] | 37 | */ |
---|
[27221] | 38 | class PluginMaintain |
---|
[25406] | 39 | { |
---|
[25601] | 40 | /** @var string $plugin_id */ |
---|
[25406] | 41 | protected $plugin_id; |
---|
[25601] | 42 | |
---|
| 43 | /** |
---|
| 44 | * @param string $id |
---|
| 45 | */ |
---|
[25406] | 46 | function __construct($id) |
---|
| 47 | { |
---|
| 48 | $this->plugin_id = $id; |
---|
| 49 | } |
---|
[25601] | 50 | |
---|
| 51 | /** |
---|
| 52 | * @param string $plugin_version |
---|
[25658] | 53 | * @param array &$errors - used to return error messages |
---|
[25601] | 54 | */ |
---|
[27221] | 55 | function install($plugin_version, &$errors=array()) {} |
---|
[25601] | 56 | |
---|
| 57 | /** |
---|
| 58 | * @param string $plugin_version |
---|
[25658] | 59 | * @param array &$errors - used to return error messages |
---|
[25601] | 60 | */ |
---|
[27221] | 61 | function activate($plugin_version, &$errors=array()) {} |
---|
[25601] | 62 | |
---|
[27221] | 63 | function deactivate() {} |
---|
[25601] | 64 | |
---|
[27221] | 65 | function uninstall() {} |
---|
[25601] | 66 | |
---|
| 67 | /** |
---|
| 68 | * Tests if the plugin needs to be updated and call an update function |
---|
| 69 | * |
---|
| 70 | * @param string $version version exposed by the plugin (potentially new) |
---|
| 71 | * @param string $on_update name of a method to call when an update is needed |
---|
[25406] | 72 | * it receives the previous version as first parameter |
---|
| 73 | */ |
---|
| 74 | function autoUpdate($version, $on_update=null) |
---|
| 75 | { |
---|
| 76 | global $pwg_loaded_plugins; |
---|
| 77 | |
---|
| 78 | $current_version = $pwg_loaded_plugins[$this->plugin_id]['version']; |
---|
| 79 | |
---|
| 80 | if ( $version == 'auto' or $current_version == 'auto' |
---|
[26591] | 81 | or safe_version_compare($current_version, $version, '<') |
---|
[25406] | 82 | ) |
---|
| 83 | { |
---|
| 84 | if (!empty($on_update)) |
---|
| 85 | { |
---|
| 86 | call_user_func(array(&$this, $on_update), $current_version); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if ($version != 'auto') |
---|
| 90 | { |
---|
| 91 | $query = ' |
---|
| 92 | UPDATE '. PLUGINS_TABLE .' |
---|
| 93 | SET version = "'. $version .'" |
---|
| 94 | WHERE id = "'. $this->plugin_id .'" |
---|
| 95 | ;'; |
---|
| 96 | pwg_query($query); |
---|
| 97 | |
---|
| 98 | $pwg_loaded_plugins[$this->plugin_id]['version'] = $version; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
[25601] | 105 | * Used to declare maintenance methods of a theme. |
---|
[25406] | 106 | */ |
---|
[27221] | 107 | class ThemeMaintain |
---|
[25406] | 108 | { |
---|
[25601] | 109 | /** @var string $theme_id */ |
---|
[25406] | 110 | protected $theme_id; |
---|
[25601] | 111 | |
---|
| 112 | /** |
---|
| 113 | * @param string $id |
---|
| 114 | */ |
---|
[25406] | 115 | function __construct($id) |
---|
| 116 | { |
---|
| 117 | $this->theme_id = $id; |
---|
| 118 | } |
---|
[25601] | 119 | |
---|
| 120 | /** |
---|
| 121 | * @param string $theme_version |
---|
[25658] | 122 | * @param array &$errors - used to return error messages |
---|
[25601] | 123 | */ |
---|
[27221] | 124 | function activate($theme_version, &$errors=array()) {} |
---|
[25601] | 125 | |
---|
[27221] | 126 | function deactivate() {} |
---|
[25601] | 127 | |
---|
[27221] | 128 | function delete() {} |
---|
[25406] | 129 | |
---|
[25601] | 130 | /** |
---|
| 131 | * Tests if the theme needs to be updated and call an update function |
---|
| 132 | * |
---|
| 133 | * @param string $version version exposed by the theme (potentially new) |
---|
| 134 | * @param string $on_update name of a method to call when an update is needed |
---|
[25406] | 135 | * it receives the previous version as first parameter |
---|
| 136 | */ |
---|
| 137 | function autoUpdate($version, $on_update=null) |
---|
| 138 | { |
---|
| 139 | $query = ' |
---|
| 140 | SELECT version |
---|
| 141 | FROM '. THEMES_TABLE .' |
---|
| 142 | WHERE id = "'. $this->theme_id .'" |
---|
| 143 | ;'; |
---|
| 144 | list($current_version) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 145 | |
---|
| 146 | if ( $version == 'auto' or $current_version == 'auto' |
---|
[26591] | 147 | or safe_version_compare($current_version, $version, '<') |
---|
[25406] | 148 | ) |
---|
| 149 | { |
---|
| 150 | if (!empty($on_update)) |
---|
| 151 | { |
---|
| 152 | call_user_func(array(&$this, $on_update), $current_version); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if ($version != 'auto') |
---|
| 156 | { |
---|
| 157 | $query = ' |
---|
| 158 | UPDATE '. THEMES_TABLE .' |
---|
| 159 | SET version = "'. $version .'" |
---|
| 160 | WHERE id = "'. $this->theme_id .'" |
---|
| 161 | ;'; |
---|
| 162 | pwg_query($query); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[25601] | 169 | /** |
---|
| 170 | * Register an event handler. |
---|
| 171 | * |
---|
[1578] | 172 | * @param string $event the name of the event to listen to |
---|
[25601] | 173 | * @param Callable $func the callback function |
---|
| 174 | * @param int $priority greater priority will be executed at last |
---|
[27238] | 175 | * @param string $include_path file to include before executing the callback |
---|
| 176 | * @return bool false is handler already exists |
---|
[25601] | 177 | */ |
---|
[1590] | 178 | function add_event_handler($event, $func, |
---|
[27238] | 179 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL, $include_path=null) |
---|
[1578] | 180 | { |
---|
| 181 | global $pwg_event_handlers; |
---|
| 182 | |
---|
[27238] | 183 | if (isset($pwg_event_handlers[$event][$priority])) |
---|
[1578] | 184 | { |
---|
[27238] | 185 | foreach ($pwg_event_handlers[$event][$priority] as $handler) |
---|
[1578] | 186 | { |
---|
[27238] | 187 | if ($handler['function'] == $func) |
---|
[1578] | 188 | { |
---|
[1604] | 189 | return false; |
---|
[1578] | 190 | } |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
[27238] | 194 | $pwg_event_handlers[$event][$priority][] = array( |
---|
| 195 | 'function' => $func, |
---|
| 196 | 'include_path' => is_string($include_path) ? $include_path : null, |
---|
| 197 | ); |
---|
| 198 | |
---|
| 199 | ksort($pwg_event_handlers[$event]); |
---|
[1578] | 200 | return true; |
---|
| 201 | } |
---|
| 202 | |
---|
[25601] | 203 | /** |
---|
| 204 | * Removes an event handler. |
---|
| 205 | * @see add_event_handler() |
---|
| 206 | * |
---|
| 207 | * @param string $event |
---|
| 208 | * @param Callable $func |
---|
| 209 | * @param int $priority |
---|
| 210 | */ |
---|
[1590] | 211 | function remove_event_handler($event, $func, |
---|
| 212 | $priority=EVENT_HANDLER_PRIORITY_NEUTRAL) |
---|
| 213 | { |
---|
| 214 | global $pwg_event_handlers; |
---|
[1578] | 215 | |
---|
[27238] | 216 | if (!isset($pwg_event_handlers[$event][$priority])) |
---|
[1590] | 217 | { |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | for ($i=0; $i<count($pwg_event_handlers[$event][$priority]); $i++) |
---|
| 221 | { |
---|
| 222 | if ($pwg_event_handlers[$event][$priority][$i]['function']==$func) |
---|
| 223 | { |
---|
| 224 | unset($pwg_event_handlers[$event][$priority][$i]); |
---|
| 225 | $pwg_event_handlers[$event][$priority] = |
---|
| 226 | array_values($pwg_event_handlers[$event][$priority]); |
---|
| 227 | |
---|
[27238] | 228 | if (empty($pwg_event_handlers[$event][$priority])) |
---|
[1590] | 229 | { |
---|
[27238] | 230 | unset($pwg_event_handlers[$event][$priority]); |
---|
| 231 | if (empty($pwg_event_handlers[$event])) |
---|
[1590] | 232 | { |
---|
[27238] | 233 | unset($pwg_event_handlers[$event]); |
---|
[1590] | 234 | } |
---|
| 235 | } |
---|
| 236 | return true; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | return false; |
---|
| 240 | } |
---|
| 241 | |
---|
[25601] | 242 | /** |
---|
| 243 | * Triggers a modifier event and calls all registered event handlers. |
---|
[25602] | 244 | * trigger_change() is used as a modifier: it allows to transmit _$data_ |
---|
[25601] | 245 | * through all handlers, thus each handler MUST return a value, |
---|
| 246 | * optional _$args_ are not transmitted. |
---|
| 247 | * |
---|
[25602] | 248 | * @since 2.6 |
---|
| 249 | * |
---|
[25601] | 250 | * @param string $event |
---|
| 251 | * @param mixed $data data to transmit to all handlers |
---|
| 252 | * @param mixed $args,... optional arguments |
---|
| 253 | * @return mixed $data |
---|
| 254 | */ |
---|
[25602] | 255 | function trigger_change($event, $data=null) |
---|
| 256 | { |
---|
[1578] | 257 | global $pwg_event_handlers; |
---|
[1590] | 258 | |
---|
[27238] | 259 | if (isset($pwg_event_handlers['trigger'])) |
---|
| 260 | {// debugging |
---|
[28587] | 261 | trigger_notify('trigger', |
---|
[27238] | 262 | array('type'=>'event', 'event'=>$event, 'data'=>$data) |
---|
| 263 | ); |
---|
[3136] | 264 | } |
---|
[1578] | 265 | |
---|
[27238] | 266 | if (!isset($pwg_event_handlers[$event])) |
---|
[1578] | 267 | { |
---|
| 268 | return $data; |
---|
| 269 | } |
---|
[8263] | 270 | $args = func_get_args(); |
---|
[27335] | 271 | array_shift($args); |
---|
[1578] | 272 | |
---|
| 273 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 274 | { |
---|
[27238] | 275 | foreach ($handlers as $handler) |
---|
[1578] | 276 | { |
---|
[27335] | 277 | $args[0] = $data; |
---|
[27238] | 278 | |
---|
| 279 | if (!empty($handler['include_path'])) |
---|
| 280 | { |
---|
| 281 | include_once($handler['include_path']); |
---|
| 282 | } |
---|
| 283 | |
---|
[27335] | 284 | $data = call_user_func_array($handler['function'], $args); |
---|
[1578] | 285 | } |
---|
| 286 | } |
---|
[27238] | 287 | |
---|
| 288 | if (isset($pwg_event_handlers['trigger'])) |
---|
| 289 | {// debugging |
---|
[28587] | 290 | trigger_notify('trigger', |
---|
[27238] | 291 | array('type'=>'post_event', 'event'=>$event, 'data'=>$data) |
---|
| 292 | ); |
---|
| 293 | } |
---|
| 294 | |
---|
[1590] | 295 | return $data; |
---|
| 296 | } |
---|
[1578] | 297 | |
---|
[25601] | 298 | /** |
---|
[25602] | 299 | * Triggers a notifier event and calls all registered event handlers. |
---|
| 300 | * trigger_notify() is only used as a notifier, no modification of data is possible |
---|
[25601] | 301 | * |
---|
[25602] | 302 | * @since 2.6 |
---|
| 303 | * |
---|
[25601] | 304 | * @param string $event |
---|
| 305 | * @param mixed $args,... optional arguments |
---|
| 306 | */ |
---|
[25602] | 307 | function trigger_notify($event) |
---|
| 308 | { |
---|
[1590] | 309 | global $pwg_event_handlers; |
---|
[27238] | 310 | |
---|
| 311 | if (isset($pwg_event_handlers['trigger']) and $event!='trigger') |
---|
| 312 | {// debugging - avoid recursive calls |
---|
[28587] | 313 | trigger_notify('trigger', |
---|
[27238] | 314 | array('type'=>'action', 'event'=>$event, 'data'=>null) |
---|
| 315 | ); |
---|
[1578] | 316 | } |
---|
| 317 | |
---|
[27238] | 318 | if (!isset($pwg_event_handlers[$event])) |
---|
[1590] | 319 | { |
---|
| 320 | return; |
---|
| 321 | } |
---|
[8263] | 322 | $args = func_get_args(); |
---|
[27335] | 323 | array_shift($args); |
---|
[1578] | 324 | |
---|
[1590] | 325 | foreach ($pwg_event_handlers[$event] as $priority => $handlers) |
---|
| 326 | { |
---|
[27238] | 327 | foreach ($handlers as $handler) |
---|
[1590] | 328 | { |
---|
[27238] | 329 | if (!empty($handler['include_path'])) |
---|
| 330 | { |
---|
| 331 | include_once($handler['include_path']); |
---|
| 332 | } |
---|
[1578] | 333 | |
---|
[27335] | 334 | call_user_func_array($handler['function'], $args); |
---|
[1590] | 335 | } |
---|
| 336 | } |
---|
| 337 | } |
---|
[1578] | 338 | |
---|
[25601] | 339 | /** |
---|
| 340 | * Saves some data with the associated plugin id, data are only available |
---|
| 341 | * during script lifetime. |
---|
| 342 | * @depracted 2.6 |
---|
| 343 | * |
---|
| 344 | * @param string $plugin_id |
---|
[25658] | 345 | * @param mixed &$data |
---|
[25601] | 346 | * @return bool |
---|
[1705] | 347 | */ |
---|
| 348 | function set_plugin_data($plugin_id, &$data) |
---|
| 349 | { |
---|
| 350 | global $pwg_loaded_plugins; |
---|
| 351 | if ( isset($pwg_loaded_plugins[$plugin_id]) ) |
---|
| 352 | { |
---|
| 353 | $pwg_loaded_plugins[$plugin_id]['plugin_data'] = &$data; |
---|
| 354 | return true; |
---|
| 355 | } |
---|
| 356 | return false; |
---|
| 357 | } |
---|
[1590] | 358 | |
---|
[25601] | 359 | /** |
---|
| 360 | * Retrieves plugin data saved previously with set_plugin_data. |
---|
| 361 | * @see set_plugin_data() |
---|
| 362 | * @depracted 2.6 |
---|
| 363 | * |
---|
| 364 | * @param string $plugin_id |
---|
| 365 | * @return mixed |
---|
[1705] | 366 | */ |
---|
| 367 | function &get_plugin_data($plugin_id) |
---|
| 368 | { |
---|
| 369 | global $pwg_loaded_plugins; |
---|
[25601] | 370 | if ( isset($pwg_loaded_plugins[$plugin_id]['plugin_data']) ) |
---|
[1705] | 371 | { |
---|
| 372 | return $pwg_loaded_plugins[$plugin_id]['plugin_data']; |
---|
| 373 | } |
---|
| 374 | return null; |
---|
| 375 | } |
---|
| 376 | |
---|
[25601] | 377 | /** |
---|
| 378 | * Returns an array of plugins defined in the database. |
---|
| 379 | * |
---|
| 380 | * @param string $state optional filter |
---|
| 381 | * @param string $id returns only data about given plugin |
---|
| 382 | * @return array |
---|
| 383 | */ |
---|
[1584] | 384 | function get_db_plugins($state='', $id='') |
---|
[1578] | 385 | { |
---|
[1584] | 386 | $query = ' |
---|
| 387 | SELECT * FROM '.PLUGINS_TABLE; |
---|
[3136] | 388 | $clauses = array(); |
---|
| 389 | if (!empty($state)) |
---|
[1578] | 390 | { |
---|
[4367] | 391 | $clauses[] = 'state=\''.$state.'\''; |
---|
[3136] | 392 | } |
---|
| 393 | if (!empty($id)) |
---|
| 394 | { |
---|
| 395 | $clauses[] = 'id="'.$id.'"'; |
---|
| 396 | } |
---|
| 397 | if (count($clauses)) |
---|
| 398 | { |
---|
[27221] | 399 | $query .= ' |
---|
[3136] | 400 | WHERE '. implode(' AND ', $clauses); |
---|
[1578] | 401 | } |
---|
[27221] | 402 | |
---|
| 403 | return query2array($query); |
---|
[1578] | 404 | } |
---|
| 405 | |
---|
[25601] | 406 | /** |
---|
| 407 | * Loads a plugin, it includes the main.inc.php file and updates _$pwg_loaded_plugins_. |
---|
| 408 | * |
---|
| 409 | * @param string $plugin |
---|
| 410 | */ |
---|
[1604] | 411 | function load_plugin($plugin) |
---|
| 412 | { |
---|
| 413 | $file_name = PHPWG_PLUGINS_PATH.$plugin['id'].'/main.inc.php'; |
---|
| 414 | if ( file_exists($file_name) ) |
---|
| 415 | { |
---|
[1705] | 416 | global $pwg_loaded_plugins; |
---|
| 417 | $pwg_loaded_plugins[ $plugin['id'] ] = $plugin; |
---|
[1604] | 418 | include_once( $file_name ); |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
[25601] | 422 | /** |
---|
| 423 | * Loads all the registered plugins. |
---|
| 424 | */ |
---|
[1578] | 425 | function load_plugins() |
---|
| 426 | { |
---|
[1705] | 427 | global $conf, $pwg_loaded_plugins; |
---|
| 428 | $pwg_loaded_plugins = array(); |
---|
[1616] | 429 | if ($conf['enable_plugins']) |
---|
[1584] | 430 | { |
---|
[1616] | 431 | $plugins = get_db_plugins('active'); |
---|
| 432 | foreach( $plugins as $plugin) |
---|
| 433 | {// include main from a function to avoid using same function context |
---|
| 434 | load_plugin($plugin); |
---|
| 435 | } |
---|
[28587] | 436 | trigger_notify('plugins_loaded'); |
---|
[1584] | 437 | } |
---|
[1578] | 438 | } |
---|
[24160] | 439 | |
---|
[1578] | 440 | ?> |
---|