[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_media/src/Controller/ -> PluginController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_media
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Media\Administrator\Controller;
  12  
  13  use Joomla\CMS\MVC\Controller\BaseController;
  14  use Joomla\CMS\Plugin\PluginHelper;
  15  use Joomla\CMS\Router\Route;
  16  use Joomla\Component\Media\Administrator\Event\OAuthCallbackEvent;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Plugin Controller for OAuth2.0 callbacks
  24   *
  25   * This controller handles OAuth2 Callbacks
  26   *
  27   * @since  4.0.0
  28   */
  29  class PluginController extends BaseController
  30  {
  31      /**
  32       * Handles an OAuth Callback request for a specified plugin.
  33       *
  34       * URLs containing [sitename]/administrator/index.php?option=com_media&task=plugin.oauthcallback
  35       *  &plugin=[plugin_name]
  36       *
  37       * will be handled by this endpoint.
  38       * It will select the plugin specified by plugin_name and pass all the data received from the provider
  39       *
  40       * @return void
  41       *
  42       * @since  4.0.0
  43       */
  44      public function oauthcallback()
  45      {
  46          try {
  47              // Load plugin names
  48              $pluginName = $this->input->getString('plugin', null);
  49              $plugins    = PluginHelper::getPlugin('filesystem');
  50  
  51              // If plugin name was not found in parameters redirect back to control panel
  52              if (!$pluginName || !$this->containsPlugin($plugins, $pluginName)) {
  53                  throw new \Exception('Plugin not found!');
  54              }
  55  
  56              // Check if the plugin is disabled, if so redirect to control panel
  57              if (!PluginHelper::isEnabled('filesystem', $pluginName)) {
  58                  throw new \Exception('Plugin ' . $pluginName . ' is disabled.');
  59              }
  60  
  61              // Only import our required plugin, not entire group
  62              PluginHelper::importPlugin('filesystem', $pluginName);
  63  
  64              // Event parameters
  65              $eventParameters = ['context' => $pluginName, 'input' => $this->input];
  66              $event           = new OAuthCallbackEvent('onFileSystemOAuthCallback', $eventParameters);
  67  
  68              // Get results from event
  69              $eventResults = (array) $this->app->triggerEvent('onFileSystemOAuthCallback', $event);
  70  
  71              // If event was not triggered in the selected Plugin, raise a warning and fallback to Control Panel
  72              if (!$eventResults) {
  73                  throw new \Exception(
  74                      'Plugin ' . $pluginName . ' should have implemented onFileSystemOAuthCallback method'
  75                  );
  76              }
  77  
  78              $action  = $eventResults['action'] ?? null;
  79  
  80              // If there are any messages display them
  81              if (isset($eventResults['message'])) {
  82                  $message     = $eventResults['message'];
  83                  $messageType = ($eventResults['message_type'] ?? '');
  84  
  85                  $this->app->enqueueMessage($message, $messageType);
  86              }
  87  
  88              /**
  89               * Execute actions defined by the plugin
  90               * Supported actions
  91               *  - close         : Closes the current window, use this only for windows opened by javascript
  92               *  - redirect      : Redirect to a URI defined in 'redirect_uri' parameter, if not fallback to control panel
  93               *  - media-manager : Redirect to Media Manager
  94               *  - control-panel : Redirect to Control Panel
  95               */
  96              switch ($action) {
  97                  /**
  98                   * Close a window opened by developer
  99                   * Use this for close New Windows opened for OAuth Process
 100                   */
 101                  case 'close':
 102                      $this->setRedirect(Route::_('index.php?option=com_media&view=plugin&action=close', false));
 103                      break;
 104  
 105                  // Redirect browser to any page specified by the user
 106                  case 'redirect':
 107                      if (!isset($eventResults['redirect_uri'])) {
 108                          throw new \Exception("Redirect URI must be set in the plugin");
 109                      }
 110  
 111                      $this->setRedirect($eventResults['redirect_uri']);
 112                      break;
 113  
 114                  // Redirect browser to Control Panel
 115                  case 'control-panel':
 116                      $this->setRedirect(Route::_('index.php', false));
 117                      break;
 118  
 119                  // Redirect browser to Media Manager
 120                  case 'media-manager':
 121                  default:
 122                      $this->setRedirect(Route::_('index.php?option=com_media&view=media', false));
 123              }
 124          } catch (\Exception $e) {
 125              // Display any error
 126              $this->app->enqueueMessage($e->getMessage(), 'error');
 127              $this->setRedirect(Route::_('index.php', false));
 128          }
 129  
 130          // Redirect
 131          $this->redirect();
 132      }
 133  
 134      /**
 135       * Check whether a plugin exists in given plugin array.
 136       *
 137       * @param   array   $plugins     Array of plugin names
 138       * @param   string  $pluginName  Plugin name to look up
 139       *
 140       * @return bool
 141       *
 142       * @since  4.0.0
 143       */
 144      private function containsPlugin($plugins, $pluginName)
 145      {
 146          foreach ($plugins as $plugin) {
 147              if ($plugin->name == $pluginName) {
 148                  return true;
 149              }
 150          }
 151  
 152          return false;
 153      }
 154  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer