[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/inc/plugins/ -> plugins.ctrl.php (source)

   1  <?php
   2  /**

   3   * This file implements the UI controller for plugins management.

   4   *

   5   * This file is part of the evoCore framework - {@link http://evocore.net/}

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}

   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.

  10   *

  11   * {@internal License choice

  12   * - If you have received this file as part of a package, please find the license.txt file in

  13   *   the same folder or the closest folder above for complete license terms.

  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  15   *   then you must choose one of the following licenses before using the file:

  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  18   * }}

  19   *

  20   * {@internal Open Source relicensing agreement:

  21   * Daniel HAHLER grants Francois PLANQUE the right to license

  22   * Daniel HAHLER's contributions to this file and the b2evolution project

  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  24   * }}

  25   *

  26   * @package admin

  27   *

  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  29   * @author fplanque: Francois PLANQUE.

  30   * @author blueyed: Daniel HAHLER

  31   *

  32   * @version $Id: plugins.ctrl.php,v 1.3 2007/08/21 22:32:31 blueyed Exp $

  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  
  37  $AdminUI->set_path( 'options', 'plugins' );
  38  
  39  $action = param_action( 'list' );
  40  
  41  $UserSettings->param_Request( 'plugins_disp_avail', 'plugins_disp_avail', 'integer', 0 );
  42  
  43  // Check permission to display:

  44  $current_User->check_perm( 'options', 'view', true );
  45  
  46  $admin_Plugins = get_Cache( 'Plugins_admin' );
  47  $admin_Plugins->restart();
  48  
  49  // Pre-walk list of plugins

  50  while( $loop_Plugin = & $admin_Plugins->get_next() )
  51  {
  52      if( $loop_Plugin->status == 'broken' && ! isset( $admin_Plugins->plugin_errors[$loop_Plugin->ID] ) )
  53      { // The plugin is not "broken" anymore (either the problem got fixed or it was "broken" from a canceled "install_db_schema" action)
  54          // TODO: set this to the previous status (dh)

  55          $Plugins->set_Plugin_status( $loop_Plugin, 'disabled' );
  56      }
  57  }
  58  
  59  
  60  /**

  61   * Helper function to do the action part of DB schema upgrades for "enable" and "install"

  62   * actions.

  63   *

  64   * @param Plugin

  65   * @return boolean True, if no changes needed or done; false if we should break out to display "install_db_schema" action payload.

  66   */
  67  function install_plugin_db_schema_action( & $Plugin )
  68  {
  69      global $action, $inc_path, $install_db_deltas, $DB, $Messages;
  70  
  71      $action = 'list';
  72      // Prepare vars for DB layout changes

  73      $install_db_deltas_confirm_md5 = param( 'install_db_deltas_confirm_md5' );
  74  
  75      $db_layout = $Plugin->GetDbLayout();
  76      $install_db_deltas = array(); // This holds changes to make, if any (just all queries)

  77  
  78      if( ! empty($db_layout) )
  79      { // The plugin has a DB layout attached
  80          load_class('_core/model/db/_upgrade.funcs.php');
  81  
  82          // Get the queries to make:

  83          foreach( db_delta($db_layout) as $table => $queries )
  84          {
  85              foreach( $queries as $query_info )
  86              {
  87                  foreach( $query_info['queries'] as $query )
  88                  { // subqueries for this query (usually one, but may include required other queries)
  89                      $install_db_deltas[] = $query;
  90                  }
  91              }
  92          }
  93  
  94          if( ! empty($install_db_deltas) )
  95          { // delta queries to make
  96              if( empty($install_db_deltas_confirm_md5) )
  97              { // delta queries have to be confirmed in payload
  98                  $action = 'install_db_schema';
  99                  return false;
 100              }
 101              elseif( $install_db_deltas_confirm_md5 == md5( implode('', $install_db_deltas) ) )
 102              { // Confirmed in first step:
 103                  foreach( $install_db_deltas as $query )
 104                  {
 105                      $DB->query( $query );
 106                  }
 107  
 108                  $Messages->add( T_('The database has been updated.'), 'success' );
 109              }
 110              else
 111              { // should not happen
 112                  $Messages->add( T_('The DB schema has been changed since confirmation.'), 'error' );
 113  
 114                  // delta queries have to be confirmed (again) in payload

 115                  $action = 'install_db_schema';
 116                  return false;
 117              }
 118          }
 119      }
 120      return true;
 121  }
 122  
 123  
 124  /*

 125   * Action Handling part I

 126   * Actions that delegate to other actions (other than list):

 127   */
 128  switch( $action )
 129  {
 130      case 'del_settings_set':
 131          // Delete a set from an array type setting:

 132          param( 'plugin_ID', 'integer', true );
 133          param( 'set_path' );
 134  
 135          $edit_Plugin = & $admin_Plugins->get_by_ID($plugin_ID);
 136  
 137          load_funcs('plugins/_plugin.funcs.php');
 138          _set_setting_by_path( $edit_Plugin, 'Settings', $set_path, NULL );
 139  
 140          #$edit_Plugin->Settings->dbupdate();

 141  
 142          $action = 'edit_settings';
 143  
 144          break;
 145  
 146      case 'add_settings_set': // delegates to edit_settings
 147          // Add a new set to an array type setting:

 148          param( 'plugin_ID', 'integer', true );
 149          param( 'set_path', 'string', '' );
 150  
 151          $edit_Plugin = & $admin_Plugins->get_by_ID($plugin_ID);
 152  
 153          load_funcs('plugins/_plugin.funcs.php');
 154          _set_setting_by_path( $edit_Plugin, 'Settings', $set_path, array() );
 155  
 156          #$edit_Plugin->Settings->dbupdate();

 157  
 158          $action = 'edit_settings';
 159  
 160          break;
 161  }
 162  
 163  
 164  /*

 165   * Action Handling part II

 166   */
 167  switch( $action )
 168  {
 169      case 'disable_plugin':
 170          // Disable a plugin, only if it is "enabled"

 171          $current_User->check_perm( 'options', 'edit', true );
 172  
 173          param( 'plugin_ID', 'integer', true );
 174  
 175          $action = 'list';
 176  
 177          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 178  
 179          if( empty($edit_Plugin) )
 180          {
 181              $Messages->add( sprintf( T_( 'The plugin with ID %d could not get instantiated.' ), $plugin_ID ), 'error' );
 182              break;
 183          }
 184          if( $edit_Plugin->status != 'enabled' )
 185          {
 186              $Messages->add( sprintf( T_( 'The plugin with ID %d is already disabled.' ), $plugin_ID ), 'note' );
 187              break;
 188          }
 189  
 190          // Check dependencies

 191          $msgs = $admin_Plugins->validate_dependencies( $edit_Plugin, 'disable' );
 192          if( ! empty( $msgs['error'] ) )
 193          {
 194              $Messages->add( T_( 'The plugin cannot be disabled because of the following dependencies:' ).' <ul><li>'.implode('</li><li>', $msgs['error']).'</li></ul>', 'error' );
 195              break;
 196          }
 197  
 198          // we call $Plugins(!) here: the Plugin gets disabled on the current page already and it should not get (un)registered on $admin_Plugins!

 199          $Plugins->set_Plugin_status( $edit_Plugin, 'disabled' ); // sets $edit_Plugin->status

 200  
 201          $Messages->add( /* TRANS: plugin name, class name and ID */ sprintf( T_('Disabled "%s" plugin (%s, #%d).'), $edit_Plugin->name, $edit_Plugin->classname, $edit_Plugin->ID ), 'success' );
 202  
 203          break;
 204  
 205  
 206      case 'enable_plugin':
 207          // Try to enable a plugin, only if it is in state "disabled" or "needs_config"

 208          $current_User->check_perm( 'options', 'edit', true );
 209  
 210          param( 'plugin_ID', 'integer', true );
 211  
 212          $action = 'list';
 213  
 214          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 215  
 216          if( empty($edit_Plugin) )
 217          {
 218              $Messages->add( sprintf( T_( 'The plugin with ID %d could not get instantiated.' ), $plugin_ID ), 'error' );
 219              break;
 220          }
 221          if( $edit_Plugin->status == 'enabled' )
 222          {
 223              $Messages->add( /* TRANS: plugin name, class name and ID */ sprintf( T_( 'The "%s" plugin (%s, #%d) is already enabled.' ), $edit_Plugin->name, $edit_Plugin->classname, $plugin_ID ), 'note' );
 224              break;
 225          }
 226          if( $edit_Plugin->status == 'broken' )
 227          {
 228              $Messages->add( sprintf( T_( 'The plugin is in a broken state. It cannot get enabled.' ), $plugin_ID ), 'error' );
 229              break;
 230          }
 231  
 232          // Check dependencies

 233          $msgs = $admin_Plugins->validate_dependencies( $edit_Plugin, 'enable' );
 234          if( ! empty( $msgs['error'] ) )
 235          {
 236              $Messages->add( T_( 'The plugin cannot be enabled because of the following dependencies:' ).' <ul><li>'.implode('</li><li>', $msgs['error']).'</li></ul>' );
 237              break;
 238          }
 239  
 240          if( ! install_plugin_db_schema_action( $edit_Plugin ) )
 241          {
 242              $next_action = 'enable_plugin';
 243              break;
 244          }
 245  
 246          // Try to enable plugin:

 247          $enable_return = $edit_Plugin->BeforeEnable();
 248          if( $enable_return === true )
 249          {
 250              // NOTE: we don't need to handle plug_version here, because it gets handled in Plugins::register() already.

 251  
 252              // Detect new events:

 253              $admin_Plugins->save_events( $edit_Plugin, array() );
 254  
 255              // we call $Plugins(!) here: the Plugin gets active on the current page already and it should not get (un)registered on $admin_Plugins!

 256              $Plugins->set_Plugin_status( $edit_Plugin, 'enabled' ); // sets $edit_Plugin->status

 257  
 258              $Messages->add( /* TRANS: plugin name, class name and ID */ sprintf( T_('Enabled "%s" plugin (%s, #%d).'), $edit_Plugin->name, $edit_Plugin->classname, $edit_Plugin->ID ), 'success' );
 259          }
 260          else
 261          {
 262              $Messages->add( T_('The plugin has not been enabled.').( empty($enable_return) ? '' : '<br />'.$enable_return ), 'error' );
 263          }
 264  
 265          break;
 266  
 267  
 268      case 'reload_plugins':
 269          // Register new events

 270          // Unregister obsolete events

 271          // Detect plugins with no code and try to have at least one plugin with the default code

 272          // Check permission:

 273          $current_User->check_perm( 'options', 'edit', true );
 274  
 275          $admin_Plugins->restart();
 276          $admin_Plugins->load_events();
 277          $changed = false;
 278          while( $loop_Plugin = & $admin_Plugins->get_next() )
 279          {
 280              // NOTE: we don't need to handle plug_version here, because it gets handled in Plugins::register() already.

 281  
 282              // Discover new events:

 283              if( $admin_Plugins->save_events( $loop_Plugin, array() ) )
 284              {
 285                  $changed = true;
 286              }
 287  
 288              // Detect plugins with no code and try to have at least one plugin with the default code:

 289              if( empty($loop_Plugin->code) )
 290              { // Instantiated Plugin has no code
 291                  $default_Plugin = & $admin_Plugins->register($loop_Plugin->classname);
 292  
 293                  if( ! empty($default_Plugin->code) // Plugin has default code
 294                      && ! $admin_Plugins->get_by_code( $default_Plugin->code ) ) // Default code is not in use (anymore)
 295                  { // Set the Plugin's code to the default one
 296                      if( $admin_Plugins->set_code( $loop_Plugin->ID, $default_Plugin->code ) )
 297                      {
 298                          $changed = true;
 299                      }
 300                  }
 301  
 302                  $admin_Plugins->unregister($default_Plugin, true);
 303              }
 304          }
 305  
 306          if( $changed )
 307          {
 308              $Messages->add( T_('Plugins have been reloaded.'), 'success' );
 309          }
 310          else
 311          {
 312              $Messages->add( T_('Plugins have not changed.'), 'note' );
 313          }
 314          $action = 'list';
 315          break;
 316  
 317  
 318      case 'install':
 319          // Install a plugin. This may be a two-step action, when DB changes have to be confirmed

 320          $action = 'list';
 321          // Check permission:

 322          $current_User->check_perm( 'options', 'edit', true );
 323  
 324          param( 'plugin', 'string', true );
 325  
 326          $edit_Plugin = & $admin_Plugins->install( $plugin, 'broken' ); // "broken" by default, gets adjusted later

 327  
 328          if( is_string($edit_Plugin) )
 329          {
 330              $Messages->add( $edit_Plugin, 'error' );
 331              break;
 332          }
 333  
 334  
 335      case 'install_db_schema':
 336          // we come here from the first step ("install")

 337          param( 'plugin_ID', 'integer', 0 );
 338  
 339          if( $plugin_ID )
 340          { // second step:
 341              $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 342  
 343              if( ! is_a($edit_Plugin, 'Plugin') )
 344              {
 345                  $Messages->add( sprintf( T_( 'The plugin with ID %d could not get instantiated.' ), $plugin_ID ), 'error' );
 346                  $action = 'list';
 347                  break;
 348              }
 349          }
 350          if( ! install_plugin_db_schema_action($edit_Plugin) )
 351          {
 352              $next_action = 'install_db_schema';
 353              break;
 354          }
 355  
 356          $msg = sprintf( T_('Installed plugin &laquo;%s&raquo;.'), $edit_Plugin->classname );
 357          if( ($edit_settings_icon = $edit_Plugin->get_edit_settings_link()) )
 358          {
 359              $msg .= ' '.$edit_settings_icon;
 360          }
 361          $Messages->add( $msg, 'success' );
 362  
 363          // Install completed:

 364          $r = $admin_Plugins->call_method( $edit_Plugin->ID, 'AfterInstall', $params = array() );
 365  
 366          // Try to enable plugin:

 367          $enable_return = $edit_Plugin->BeforeEnable();
 368          if( $enable_return === true )
 369          {
 370              $Plugins->set_Plugin_status( $edit_Plugin, 'enabled' );
 371          }
 372          else
 373          {
 374              $Messages->add( T_('The plugin has not been enabled.').( empty($enable_return) ? '' : '<br />'.$enable_return ), 'error' );
 375              $Plugins->set_Plugin_status( $edit_Plugin, 'disabled' ); // does not unregister it

 376          }
 377  
 378          if( ! empty( $edit_Plugin->install_dep_notes ) )
 379          { // Add notes from dependencies
 380              $Messages->add_messages( array( 'note' => $edit_Plugin->install_dep_notes ) );
 381          }
 382          break;
 383  
 384  
 385      case 'uninstall':
 386          // Check permission:

 387          $current_User->check_perm( 'options', 'edit', true );
 388          // Uninstall plugin:

 389          param( 'plugin_ID', 'integer', true );
 390          param( 'uninstall_confirmed_drop', 'integer', 0 );
 391  
 392          $action = 'list'; // leave 'uninstall' by default

 393  
 394          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 395  
 396          if( empty($edit_Plugin) )
 397          {
 398              $Messages->add( sprintf( T_( 'The plugin with ID %d could not get instantiated.' ), $plugin_ID ), 'error' );
 399              break;
 400          }
 401  
 402          // Check dependencies:

 403          $msgs = $admin_Plugins->validate_dependencies( $edit_Plugin, 'disable' );
 404          if( ! empty( $msgs['error'] ) )
 405          {
 406              $Messages->add( T_( 'The plugin cannot be uninstalled because of the following dependencies:' ).' <ul><li>'.implode('</li><li>', $msgs['error']).'</li></ul>', 'error' );
 407              break;
 408          }
 409          if( ! empty( $msgs['note'] ) )
 410          { // just notes:
 411              $Messages->add_messages( array( 'note' => $msgs['note'] ) );
 412          }
 413  
 414          // Ask plugin:

 415          $uninstall_ok = $admin_Plugins->call_method( $edit_Plugin->ID, 'BeforeUninstall', $params = array( 'unattended' => false ) );
 416  
 417          if( $uninstall_ok === false )
 418          { // Plugin said "NO":
 419              $Messages->add( sprintf( T_('Could not uninstall plugin #%d.'), $edit_Plugin->ID ), 'error' );
 420              break;
 421          }
 422  
 423          // See if we have (canonical) tables to drop:

 424          $uninstall_tables_to_drop = $DB->get_col( 'SHOW TABLES LIKE "'.$edit_Plugin->get_sql_table('%').'"' );
 425  
 426          if( $uninstall_ok === true )
 427          { // Plugin said "YES":
 428  
 429              if( $uninstall_tables_to_drop )
 430              { // There are tables with the prefix for this plugin:
 431                  if( $uninstall_confirmed_drop )
 432                  { // Drop tables:
 433                      $sql = 'DROP TABLE IF EXISTS '.implode( ', ', $uninstall_tables_to_drop );
 434                      $DB->query( $sql );
 435                      $Messages->add( T_('Dropped the table(s) of the plugin.'), 'success' );
 436                  }
 437                  else
 438                  {
 439                      $uninstall_ok = false;
 440                  }
 441              }
 442  
 443              if( $uninstall_ok )
 444              { // We either have no tables to drop or it has been confirmed:
 445                  $admin_Plugins->uninstall( $edit_Plugin->ID );
 446  
 447                  $Messages->add( /* %s = plugin's classname, %d = plugin's ID */
 448                      sprintf( T_('The &laquo;%s&raquo; plugin (#%d) has been uninstalled.'), $edit_Plugin->classname, $edit_Plugin->ID ), 'success' );
 449                  break;
 450              }
 451          }
 452  
 453          // $ok === NULL (or other): execute plugin event BeforeUninstallPayload() below

 454          // $ok === false: let the admin confirm DB table dropping below

 455          $action = 'uninstall';
 456  
 457          break;
 458  
 459  
 460      case 'update_settings':
 461          // Update plugin settings:

 462  
 463          // Check permission:

 464          $current_User->check_perm( 'options', 'edit', true );
 465  
 466          param( 'plugin_ID', 'integer', true );
 467  
 468          // Next default action:

 469          if( isset($actionArray['update_settings'])
 470              && is_array($actionArray['update_settings'])
 471              && isset($actionArray['update_settings']['review']) )
 472          { // "Save (and review)", next action will still be to edit
 473              $action = 'edit_settings';
 474          }
 475          else
 476          {    // Save only, next action: list
 477              $action = 'list';
 478          }
 479  
 480          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 481          if( empty($edit_Plugin) )
 482          {
 483              $Messages->add( sprintf( T_( 'The plugin with ID %d could not be instantiated.' ), $plugin_ID ), 'error' );
 484              $action = 'list';
 485              break;
 486          }
 487  
 488          // Params from/for form:

 489          param( 'edited_plugin_name' );
 490          param( 'edited_plugin_shortdesc' );
 491          param( 'edited_plugin_code' );
 492          param( 'edited_plugin_priority' );
 493          param( 'edited_plugin_apply_rendering' );
 494          param( 'edited_plugin_displayed_events', 'array', array() );
 495          param( 'edited_plugin_events', 'array', array() );
 496  
 497          $default_Plugin = & $admin_Plugins->register($edit_Plugin->classname);
 498  
 499          // Update plugin name:

 500          // (Only if changed to preserve initial localization feature and therefor also priorize NULL)

 501          if( $edit_Plugin->name != $edited_plugin_name )
 502          {
 503              $set_to = $edited_plugin_name == $default_Plugin->name ? NULL : $edited_plugin_name;
 504              $edit_Plugin->name = $edited_plugin_name;
 505              if( $DB->query( '
 506                  UPDATE T_plugins
 507                       SET plug_name = '.$DB->quote($set_to).'
 508                   WHERE plug_ID = '.$plugin_ID ) )
 509              {
 510                  $Messages->add( T_('Plugin name updated.'), 'success' );
 511              }
 512          }
 513  
 514          // Update plugin shortdesc:

 515          // (Only if changed to preserve initial localization feature and therefor also priorize NULL)

 516          if( $edit_Plugin->short_desc != $edited_plugin_shortdesc )
 517          {
 518              $set_to = $edited_plugin_shortdesc == $default_Plugin->short_desc ? NULL : $edited_plugin_shortdesc;
 519              $edit_Plugin->short_desc = $edited_plugin_shortdesc;
 520              if( $DB->query( '
 521                  UPDATE T_plugins
 522                       SET plug_shortdesc = '.$DB->quote($set_to).'
 523                   WHERE plug_ID = '.$plugin_ID ) )
 524              {
 525                  $Messages->add( T_('Plugin description updated.'), 'success' );
 526              }
 527          }
 528  
 529          // Plugin code

 530          $updated = $admin_Plugins->set_code( $edit_Plugin->ID, $edited_plugin_code );
 531          if( is_string( $updated ) )
 532          {
 533              param_error( 'edited_plugin_code', $updated );
 534              $action = 'edit_settings';
 535          }
 536          elseif( $updated === 1 )
 537          {
 538              $Messages->add( T_('Plugin code updated.'), 'success' );
 539          }
 540  
 541          // Plugin priority

 542          if( param_check_range( 'edited_plugin_priority', 0, 100, T_('Plugin priority must be numeric (0-100).'), true ) )
 543          {
 544              $updated = $admin_Plugins->set_priority( $edit_Plugin->ID, $edited_plugin_priority );
 545              if( $updated === 1 )
 546              {
 547                  $Messages->add( T_('Plugin priority updated.'), 'success' );
 548              }
 549          }
 550          else
 551          {
 552              $action = 'edit_settings';
 553          }
 554  
 555          // Plugin "apply_rendering":

 556          if( $admin_Plugins->set_apply_rendering( $edit_Plugin->ID, $edited_plugin_apply_rendering ) )
 557          {
 558              $Messages->add( T_('Plugin rendering updated.'), 'success' );
 559          }
 560  
 561          // Plugin specific settings:

 562          if( $edit_Plugin->Settings )
 563          {
 564              load_funcs('plugins/_plugin.funcs.php');
 565  
 566              // Loop through settings for this plugin:

 567              foreach( $edit_Plugin->GetDefaultSettings( $dummy = array('for_editing' => true) ) as $set_name => $set_meta )
 568              {
 569                  autoform_set_param_from_request( $set_name, $set_meta, $edit_Plugin, 'Settings' );
 570              }
 571  
 572              // Let the plugin handle custom fields:

 573              // fp> why can't we call the method directly with $edit_Plugin->PluginSettingsUpdateAction() ??

 574              $ok_to_update = $admin_Plugins->call_method( $edit_Plugin->ID, 'PluginSettingsUpdateAction', $tmp_params = array() );
 575  
 576              if( $ok_to_update === false )
 577              {    // fp> why do we reset here??
 578                  $edit_Plugin->Settings->reset();
 579              }
 580              elseif( $edit_Plugin->Settings->dbupdate() )
 581              {
 582                  $Messages->add( T_('Plugin settings have been updated.'), 'success' );
 583              }
 584          }
 585  
 586          // Plugin Events:

 587          $registered_events = $admin_Plugins->get_registered_events( $edit_Plugin );
 588  
 589          $enable_events = array();
 590          $disable_events = array();
 591          foreach( $edited_plugin_displayed_events as $l_event )
 592          {
 593              if( ! in_array( $l_event, $registered_events ) )
 594              { // unsupported event
 595                  continue;
 596              }
 597              if( isset($edited_plugin_events[$l_event]) && $edited_plugin_events[$l_event] )
 598              {
 599                  $enable_events[] = $l_event; // may be already there

 600              }
 601              else
 602              { // unset:
 603                  $disable_events[] = $l_event;
 604              }
 605          }
 606          if( $admin_Plugins->save_events( $edit_Plugin, $enable_events, $disable_events ) )
 607          {
 608              $Messages->add( T_('Plugin events have been updated.'), 'success' );
 609          }
 610  
 611          // Check if it can stay enabled, if it is

 612          if( $edit_Plugin->status == 'enabled' )
 613          {
 614              $enable_return = $edit_Plugin->BeforeEnable();
 615              if( $enable_return !== true )
 616              {
 617                  $Plugins->set_Plugin_status( $edit_Plugin, 'needs_config' );
 618                  $Messages->add( T_('The plugin has been disabled.').( empty($enable_return) ? '' : '<br />'.$enable_return ), 'error' );
 619              }
 620          }
 621  
 622          if( $Messages->count('error') )
 623          { // there were errors
 624              $action = 'edit_settings';
 625          }
 626          break;
 627  
 628  
 629      case 'edit_settings':
 630          // Check permission:

 631          $current_User->check_perm( 'options', 'view', true );
 632  
 633          // Edit plugin settings:

 634          param( 'plugin_ID', 'integer', true );
 635  
 636          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 637  
 638          if( ! $edit_Plugin )
 639          {
 640              $Debuglog->add( 'The plugin with ID '.$plugin_ID.' was not found.', array('plugins', 'error') );
 641              $action = 'list';
 642              break;
 643          }
 644  
 645          // Detect new events, so they get displayed correctly in the "Edit events" fieldset:

 646          $admin_Plugins->save_events( $edit_Plugin, array() );
 647  
 648          // Inform Plugin that it gets edited:

 649          $admin_Plugins->call_method( $edit_Plugin->ID, 'PluginSettingsEditAction', $tmp_params = array() );
 650  
 651          // Params for form:

 652          $edited_plugin_name = $edit_Plugin->name;
 653          $edited_plugin_shortdesc = $edit_Plugin->short_desc;
 654          $edited_plugin_code = $edit_Plugin->code;
 655          $edited_plugin_priority = $edit_Plugin->priority;
 656          $edited_plugin_apply_rendering = $edit_Plugin->apply_rendering;
 657  
 658          break;
 659  
 660  
 661      case 'default_settings': // Restore default settings
 662          // Check permission:

 663          $current_User->check_perm( 'options', 'edit', true );
 664  
 665          param( 'plugin_ID', 'integer', true );
 666  
 667          $edit_Plugin = & $admin_Plugins->get_by_ID( $plugin_ID );
 668          if( !$edit_Plugin )
 669          {
 670              $Debuglog->add( 'The plugin with ID '.$plugin_ID.' was not found.', array('plugins', 'error') );
 671              $action = 'list';
 672              break;
 673          }
 674  
 675          $default_Plugin = & $admin_Plugins->register($edit_Plugin->classname);
 676  
 677          // Params for/"from" form:

 678          $edited_plugin_name = $default_Plugin->name;
 679          $edited_plugin_shortdesc = $default_Plugin->short_desc;
 680          $edited_plugin_code = $default_Plugin->code;
 681          $edited_plugin_priority = $default_Plugin->priority;
 682          $edited_plugin_apply_rendering = $default_Plugin->apply_rendering;
 683  
 684          // Name and short desc:

 685          $DB->query( '
 686                  UPDATE T_plugins
 687                     SET plug_name = NULL,
 688                         plug_shortdesc = NULL
 689                   WHERE plug_ID = '.$plugin_ID );
 690  
 691          // Code:

 692          $updated = $admin_Plugins->set_code( $edit_Plugin->ID, $edited_plugin_code );
 693          if( is_string( $updated ) )
 694          { // error message
 695              param_error( 'edited_plugin_code', $updated );
 696              $action = 'edit_settings';
 697          }
 698          elseif( $updated === 1 )
 699          {
 700              $Messages->add( T_('Plugin code updated.'), 'success' );
 701          }
 702  
 703          // Priority:

 704          if( ! preg_match( '~^1?\d?\d$~', $edited_plugin_priority ) )
 705          {
 706              param_error( 'edited_plugin_priority', T_('Plugin priority must be numeric (0-100).') );
 707          }
 708          else
 709          {
 710              $updated = $admin_Plugins->set_priority( $edit_Plugin->ID, $edited_plugin_priority );
 711              if( $updated === 1 )
 712              {
 713                  $Messages->add( T_('Plugin priority updated.'), 'success' );
 714              }
 715          }
 716  
 717          // apply_rendering:

 718          if( $admin_Plugins->set_apply_rendering( $edit_Plugin->ID, $edited_plugin_apply_rendering ) )
 719          {
 720              $Messages->add( T_('Plugin rendering updated.'), 'success' );
 721          }
 722  
 723          // PluginSettings:

 724          if( $edit_Plugin->Settings )
 725          {
 726              if( $edit_Plugin->Settings->restore_defaults() )
 727              {
 728                  $Messages->add( T_('Restored default values.'), 'success' );
 729              }
 730              else
 731              {
 732                  $Messages->add( T_('Settings have not changed.'), 'note' );
 733              }
 734          }
 735  
 736          // Enable all events:

 737          if( $admin_Plugins->save_events( $edit_Plugin ) )
 738          {
 739              $Messages->add( T_('Plugin events have been updated.'), 'success' );
 740          }
 741  
 742          // Check if it can stay enabled, if it is

 743          if( $edit_Plugin->status == 'enabled' )
 744          {
 745              $enable_return = $edit_Plugin->BeforeEnable();
 746              if( $enable_return !== true )
 747              {
 748                  $Plugins->set_Plugin_status( $edit_Plugin, 'needs_config' );
 749                  $Messages->add( T_('The plugin has been disabled.').( empty($enable_return) ? '' : '<br />'.$enable_return ), 'error' );
 750              }
 751          }
 752  
 753          // blueyed>> IMHO it's good to see the new settings again. Perhaps we could use $action = 'list' for "Settings have not changed"?

 754          $action = 'edit_settings';
 755  
 756          break;
 757  
 758  
 759      case 'info':
 760      case 'disp_help':
 761      case 'disp_help_plain': // just the help, without any payload
 762          param( 'plugin_class', 'string', true );
 763  
 764          $edit_Plugin = & $admin_Plugins->register( $plugin_class );
 765  
 766          if( is_string($edit_Plugin) )
 767          {
 768              $Messages->add($edit_Plugin, 'error');
 769              $edit_Plugin = false;
 770              $action = 'list';
 771          }
 772          else
 773          {
 774              $admin_Plugins->unregister( $edit_Plugin );
 775          }
 776  
 777          break;
 778  
 779  }
 780  
 781  /*

 782  if( 1 || $Settings->get( 'plugins_disp_log_in_admin' ) )

 783  {

 784      $Messages->add_messages( $Debuglog->get_messages('plugins') );

 785  }

 786  */
 787  
 788  
 789  // Extend titlearea for some actions and add JS:

 790  switch( $action )
 791  {
 792      case 'edit_settings':
 793          $AdminUI->append_to_titlearea( '<a href="'.regenerate_url('', 'action=edit_settings&amp;plugin_ID='.$edit_Plugin->ID).'">'
 794              .sprintf( T_('Edit plugin &laquo;%s&raquo; (ID %d)'), $edit_Plugin->name, $edit_Plugin->ID ).'</a>' );
 795          break;
 796  
 797      case 'disp_help_plain': // just the help, without any payload
 798      case 'disp_help':
 799          if( ! ($help_file = $edit_Plugin->get_help_file()) )
 800          {
 801              $action = 'list';
 802              break;
 803          }
 804  
 805          if( $action == 'disp_help_plain' )
 806          { // display it now and exit:
 807              readfile($help_file);
 808              debug_info();
 809              exit();
 810          }
 811  
 812          $title = sprintf( T_('Help for plugin &laquo;%s&raquo;'), '<a href="admin.php?ctrl=plugins&amp;action=edit_settings&amp;plugin_ID='.$edit_Plugin->ID.'">'.$edit_Plugin->name.'</a>' );
 813          if( ! empty($edit_Plugin->help_url) )
 814          {
 815              $title .= ' '.action_icon( T_('External help page'), 'www', $edit_Plugin->help_url );
 816          }
 817          $AdminUI->append_to_titlearea( $title );
 818          break;
 819  }
 820  
 821  
 822  // Display load error from Plugins::register() (if any):

 823  if( isset($edit_Plugin) && is_object($edit_Plugin) && isset( $admin_Plugins->plugin_errors[$edit_Plugin->ID] )
 824          && ! empty($admin_Plugins->plugin_errors[$edit_Plugin->ID]['register']) )
 825  {
 826      $Messages->add( get_icon('warning').' '.$admin_Plugins->plugin_errors[$edit_Plugin->ID]['register'], 'error' );
 827  }
 828  
 829  
 830  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)

 831  $AdminUI->disp_html_head();
 832  
 833  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)

 834  $AdminUI->disp_body_top();
 835  
 836  // Begin payload block:

 837  $AdminUI->disp_payload_begin();
 838  
 839  switch( $action )
 840  {
 841      case 'disp_help':
 842          // Display plugin help:

 843          $help_file_body = implode( '', file($help_file) );
 844  
 845          // Try to extract the BODY part:

 846          if( preg_match( '~<body.*?>(.*)</body>~is', $help_file_body, $match ) )
 847          {
 848              $help_file_body = $match[1];
 849          }
 850  
 851          echo $help_file_body;
 852          unset($help_file_body);
 853          break;
 854  
 855  
 856      case 'install_db_schema':
 857          // Payload for 'install_db_schema' action if DB layout changes have to be confirmed:

 858          ?>
 859  
 860          <div class="panelinfo">
 861  
 862              <?php
 863              $Form = & new Form( NULL, 'install_db_deltas', 'get' );
 864              $Form->hidden_ctrl();
 865              $Form->hidden( 'action', $next_action );
 866              $Form->hidden( 'plugin_ID', $edit_Plugin->ID );
 867  
 868              $Form->global_icon( T_('Cancel installation!'), 'close', regenerate_url() );
 869  
 870              $Form->begin_form( 'fform', sprintf( /* TRANS: %d is ID, %d name */ T_('Finish setup for plugin #%d (%s)'), $edit_Plugin->ID, $edit_Plugin->name ) );
 871  
 872              echo '<p>'.T_('The plugin needs the following database changes.').'</p>';
 873  
 874              if( ! empty($install_db_deltas) )
 875              {
 876                  echo '<p>'.T_('The following database changes will be carried out. If you are not sure what this means, it will probably be alright.').'</p>';
 877  
 878                  echo '<ul>';
 879                  foreach( $install_db_deltas as $l_delta )
 880                  {
 881                      #echo '<li><code>'.nl2br($l_delta).'</code></li>';

 882                      echo '<li><pre>'.str_replace( "\t", '  ', $l_delta ).'</pre></li>';
 883                  }
 884                  echo '</ul>';
 885  
 886                  $Form->hidden( 'install_db_deltas_confirm_md5', md5(implode( '', $install_db_deltas )) );
 887              }
 888  
 889              $Form->submit( array( '', T_('Install!'), 'ActionButton' ) );
 890              $Form->end_form();
 891              ?>
 892  
 893          </div>
 894  
 895          <?php
 896          break;
 897  
 898  
 899      case 'uninstall': // We come here either if the plugin requested a call to BeforeUninstallPayload() or if there are tables to be dropped {{{
 900          ?>
 901  
 902          <div class="panelinfo">
 903  
 904              <?php
 905              $Form = & new Form( '', 'uninstall_plugin', 'post' );
 906              // We may need to use memorized params in the next page

 907              $Form->hiddens_by_key( get_memorized( 'action,plugin_ID') );
 908              $Form->hidden( 'action', 'uninstall' );
 909              $Form->hidden( 'plugin_ID', $edit_Plugin->ID );
 910              $Form->hidden( 'uninstall_confirmed_drop', 1 );
 911              $Form->global_icon( T_('Cancel uninstall!'), 'close', regenerate_url() );
 912  
 913              $Form->begin_form( 'fform', sprintf( /* TRANS: %d is ID, %d name */ T_('Uninstall plugin #%d (%s)'), $edit_Plugin->ID, $edit_Plugin->name ) );
 914  
 915              if( $uninstall_tables_to_drop )
 916              {
 917                  echo '<p>'.T_('Uninstalling this plugin will also delete its database tables:').'</p>'
 918                      .'<ul>'
 919                      .'<li>'
 920                      .implode( '</li><li>', $uninstall_tables_to_drop )
 921                      .'</li>'
 922                      .'</ul>';
 923              }
 924  
 925              if( $uninstall_ok === NULL )
 926              { // Plugin requested this:
 927                  $admin_Plugins->call_method( $edit_Plugin->ID, 'BeforeUninstallPayload', $params = array( 'Form' => & $Form ) );
 928              }
 929  
 930              echo '<p>'.T_('THIS CANNOT BE UNDONE!').'</p>';
 931  
 932              $Form->submit( array( '', T_('I am sure!'), 'DeleteButton' ) );
 933              $Form->end_form();
 934              ?>
 935  
 936          </div>
 937  
 938          <?php // }}}
 939          break;
 940  
 941  
 942      case 'edit_settings':
 943          $AdminUI->disp_view( 'plugins/views/_plugin_settings.form.php' );
 944          break;
 945  
 946  
 947      case 'info':
 948          // Display plugin info:

 949          load_funcs('plugins/_plugin.funcs.php');
 950  
 951          $Form = & new Form( $pagenow );
 952  
 953          if( $edit_Plugin->ID > 0 )
 954          { // Edit settings button (if installed):
 955              $Form->global_icon( T_('Edit plugin settings!'), 'edit', $admin_url.'?ctrl=plugins&amp;action=edit_settings&amp;plugin_ID='.$edit_Plugin->ID );
 956          }
 957  
 958          // Close button:

 959          $Form->global_icon( T_('Close info!'), 'close', regenerate_url() );
 960  
 961          $Form->begin_form('fform');
 962          $Form->hidden( 'ctrl', 'plugins' );
 963          $Form->begin_fieldset('Plugin info', array('class' => 'fieldset clear')); // "clear" to fix Konqueror (http://bugs.kde.org/show_bug.cgi?id=117509)

 964          $Form->info_field( T_('Name'), $edit_Plugin->name );
 965          $Form->info_field( T_('Code'),
 966                  ( empty($edit_Plugin->code) ? ' - ' : $edit_Plugin->code ),
 967                  array( 'note' => T_('This 32 character code uniquely identifies the functionality of this plugin. It is only necessary to call the plugin by code (SkinTag) or when using it as a Renderer.') ) );
 968          $Form->info_field( T_('Short desc'), $edit_Plugin->short_desc );
 969          $Form->info_field( T_('Long desc'), $edit_Plugin->long_desc );
 970          if( $edit_Plugin->ID > 0 )
 971          { // do not display ID for non registered Plugins
 972              $Form->info_field( T_('ID'), $edit_Plugin->ID );
 973          }
 974          $Form->info_field( T_('Version'), $edit_Plugin->version );
 975          $Form->info_field( T_('Classname'), $edit_Plugin->classname );
 976          $Form->info_field( T_('Class file'), rel_path_to_base($edit_Plugin->classfile_path ) );
 977  
 978          // Help icons (to homepage and README.html), if available:

 979          $help_icons = array();
 980          if( $help_www = $edit_Plugin->get_help_link('$help_url') )
 981          {
 982              $help_icons[] = $help_www;
 983          }
 984          if( $help_README = $edit_Plugin->get_help_link('$readme') )
 985          {
 986              $help_icons[] = $help_README;
 987          }
 988          if( ! empty($help_icons) )
 989          {
 990              $Form->info_field( T_('Help'), implode( ' ', $help_icons ) );
 991          }
 992  
 993          $Form->end_fieldset();
 994  
 995          if( $edit_Plugin->ID < 1 )
 996          { // add "Install NOW" submit button (if not already installed)
 997              $registrations = $admin_Plugins->count_regs($edit_Plugin->classname);
 998  
 999              if( ! isset( $edit_Plugin->number_of_installs )
1000                      || ( $admin_Plugins->count_regs($edit_Plugin->classname) < $edit_Plugin->number_of_installs ) )
1001              { // number of installations are not limited or not reached yet
1002                  $Form->hidden( 'action', 'install' );
1003                  $Form->hidden( 'plugin', $edit_Plugin->classname );
1004  
1005                  $Form->begin_fieldset( '', array( 'class'=>'fieldset center' ) );
1006                  $Form->submit( array( '', T_('Install NOW!'), 'ActionButton' ) );
1007                  $Form->end_fieldset();
1008              }
1009          }
1010  
1011          $Form->end_form();
1012          $action = '';
1013          break;
1014  
1015  }
1016  
1017  
1018  switch( $action )
1019  {
1020      case 'list':
1021          // Display VIEW:

1022          $AdminUI->disp_view( 'plugins/views/_plugin_list.view.php' );
1023          break;
1024  
1025      case 'list_available':
1026          // Display VIEW:

1027          $AdminUI->disp_view( 'plugins/views/_plugin_list_available.view.php' );
1028          break;
1029  }
1030  
1031  // End payload block:

1032  $AdminUI->disp_payload_end();
1033  
1034  // Display body bottom, debug info and close </html>:

1035  $AdminUI->disp_global_footer();
1036  
1037  /*

1038   * $Log: plugins.ctrl.php,v $

1039   * Revision 1.3  2007/08/21 22:32:31  blueyed

1040   * Use get_Cache() for singleton $Plugins_admin instance. This fixes at least the installation of flickr_plugin.

1041   *

1042   * Revision 1.2  2007/07/04 23:38:27  blueyed

1043   * Fixed unregistration of plugin instances to get the default code.

1044   *

1045   * Revision 1.1  2007/06/25 11:00:43  fplanque

1046   * MODULES (refactored MVC)

1047   *

1048   * Revision 1.79  2007/06/19 20:41:11  fplanque

1049   * renamed generic functions to autoform_*

1050   *

1051   * Revision 1.78  2007/06/19 18:47:27  fplanque

1052   * Nuked unnecessary Param (or I'm missing something badly :/)

1053   *

1054   * Revision 1.77  2007/04/26 00:11:14  fplanque

1055   * (c) 2007

1056   *

1057   * Revision 1.76  2007/01/14 08:21:01  blueyed

1058   * Optimized "info", "disp_help" and "disp_help_plain" actions by refering to them through classname, which makes Plugins::discover() unnecessary

1059   *

1060   * Revision 1.75  2007/01/14 05:53:14  blueyed

1061   * Optimized init of "info" action: do not discover available Plugins, if not necessary

1062   *

1063   * Revision 1.74  2007/01/10 21:41:00  blueyed

1064   * Step 2 is installing the DB changes

1065   *

1066   * Revision 1.73  2007/01/07 05:26:01  fplanque

1067   * doc

1068   *

1069   * Revision 1.72  2006/12/20 23:07:23  blueyed

1070   * Moved list of available plugins to separate sub-screen/form

1071   *

1072   * Revision 1.71  2006/12/04 22:34:30  blueyed

1073   * Use Plugins_admin::register() to instantiate $default_Plugin

1074   *

1075   * Revision 1.70  2006/12/01 20:41:37  blueyed

1076   * Moved Plugins::uninstall() to Plugins_admin class

1077   *

1078   * Revision 1.69  2006/12/01 20:01:38  blueyed

1079   * Moved Plugins::validate_dependencies() to Plugins_admin class

1080   *

1081   * Revision 1.68  2006/11/30 05:43:39  blueyed

1082   * Moved Plugins::discover() to Plugins_admin::discover(); Renamed Plugins_no_DB to Plugins_admin_no_DB (and deriving from Plugins_admin)

1083   *

1084   * Revision 1.67  2006/11/30 00:30:33  blueyed

1085   * Some minor memory optimizations regarding "Plugins" screen

1086   *

1087   * Revision 1.66  2006/11/26 23:40:34  blueyed

1088   * trans

1089   *

1090   * Revision 1.65  2006/11/26 01:42:09  fplanque

1091   * doc

1092   */
1093  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics