[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_config/src/Controller/ -> ApplicationController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_config
   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\Config\Administrator\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\BaseController;
  16  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  17  use Joomla\CMS\Response\JsonResponse;
  18  use Joomla\CMS\Router\Route;
  19  use Joomla\CMS\Session\Session;
  20  use Joomla\Input\Input;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Controller for global configuration
  28   *
  29   * @since  1.5
  30   */
  31  class ApplicationController extends BaseController
  32  {
  33      /**
  34       * Constructor.
  35       *
  36       * @param   array                $config   An optional associative array of configuration settings.
  37       * Recognized key values include 'name', 'default_task', 'model_path', and
  38       * 'view_path' (this list is not meant to be comprehensive).
  39       * @param   MVCFactoryInterface  $factory  The factory.
  40       * @param   CMSApplication       $app      The Application for the dispatcher
  41       * @param   Input                $input    Input
  42       *
  43       * @since   3.0
  44       */
  45      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  46      {
  47          parent::__construct($config, $factory, $app, $input);
  48  
  49          // Map the apply task to the save method.
  50          $this->registerTask('apply', 'save');
  51      }
  52  
  53      /**
  54       * Cancel operation.
  55       *
  56       * @return  void
  57       *
  58       * @since   3.0.0
  59       */
  60      public function cancel()
  61      {
  62          $this->setRedirect(Route::_('index.php?option=com_cpanel'));
  63      }
  64  
  65      /**
  66       * Saves the form
  67       *
  68       * @return  void|boolean  Void on success. Boolean false on fail.
  69       *
  70       * @since  4.0.0
  71       */
  72      public function save()
  73      {
  74          // Check for request forgeries.
  75          $this->checkToken();
  76  
  77          // Check if the user is authorized to do this.
  78          if (!$this->app->getIdentity()->authorise('core.admin')) {
  79              $this->setRedirect('index.php', Text::_('JERROR_ALERTNOAUTHOR'), 'error');
  80  
  81              return false;
  82          }
  83  
  84          $this->app->setUserState('com_config.config.global.data', null);
  85  
  86          /** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
  87          $model = $this->getModel('Application', 'Administrator');
  88  
  89          $data  = $this->input->post->get('jform', array(), 'array');
  90  
  91          // Complete data array if needed
  92          $oldData = $model->getData();
  93          $data = array_replace($oldData, $data);
  94  
  95          // Get request type
  96          $saveFormat = $this->app->getDocument()->getType();
  97  
  98          // Handle service requests
  99          if ($saveFormat == 'json') {
 100              $form = $model->getForm();
 101              $return = $model->validate($form, $data);
 102  
 103              if ($return === false) {
 104                  $this->app->setHeader('Status', 422, true);
 105  
 106                  return false;
 107              }
 108  
 109              return $model->save($return);
 110          }
 111  
 112          // Must load after serving service-requests
 113          $form = $model->getForm();
 114  
 115          // Validate the posted data.
 116          $return = $model->validate($form, $data);
 117  
 118          // Check for validation errors.
 119          if ($return === false) {
 120              // Get the validation messages.
 121              $errors = $model->getErrors();
 122  
 123              // Push up to three validation messages out to the user.
 124              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 125                  if ($errors[$i] instanceof \Exception) {
 126                      $this->app->enqueueMessage($errors[$i]->getMessage(), 'warning');
 127                  } else {
 128                      $this->app->enqueueMessage($errors[$i], 'warning');
 129                  }
 130              }
 131  
 132              // Save the posted data in the session.
 133              $this->app->setUserState('com_config.config.global.data', $data);
 134  
 135              // Redirect back to the edit screen.
 136              $this->setRedirect(Route::_('index.php?option=com_config', false));
 137  
 138              return false;
 139          }
 140  
 141          // Validate database connection data.
 142          $data   = $return;
 143          $return = $model->validateDbConnection($data);
 144  
 145          // Check for validation errors.
 146          if ($return === false) {
 147              /*
 148               * The validateDbConnection method enqueued all messages for us.
 149               */
 150  
 151              // Save the posted data in the session.
 152              $this->app->setUserState('com_config.config.global.data', $data);
 153  
 154              // Redirect back to the edit screen.
 155              $this->setRedirect(Route::_('index.php?option=com_config', false));
 156  
 157              return false;
 158          }
 159  
 160          // Save the validated data in the session.
 161          $this->app->setUserState('com_config.config.global.data', $return);
 162  
 163          // Attempt to save the configuration.
 164          $data   = $return;
 165          $return = $model->save($data);
 166  
 167          // Check the return value.
 168          if ($return === false) {
 169              /*
 170               * The save method enqueued all messages for us, so we just need to redirect back.
 171               */
 172  
 173              // Save failed, go back to the screen and display a notice.
 174              $this->setRedirect(Route::_('index.php?option=com_config', false));
 175  
 176              return false;
 177          }
 178  
 179          // Set the success message.
 180          $this->app->enqueueMessage(Text::_('COM_CONFIG_SAVE_SUCCESS'), 'message');
 181  
 182          // Set the redirect based on the task.
 183          switch ($this->input->getCmd('task')) {
 184              case 'apply':
 185                  $this->setRedirect(Route::_('index.php?option=com_config', false));
 186                  break;
 187  
 188              case 'save':
 189              default:
 190                  $this->setRedirect(Route::_('index.php', false));
 191                  break;
 192          }
 193      }
 194  
 195      /**
 196       * Method to remove root in global configuration.
 197       *
 198       * @return  boolean
 199       *
 200       * @since   3.2
 201       */
 202      public function removeroot()
 203      {
 204          // Check for request forgeries.
 205          if (!Session::checkToken('get')) {
 206              $this->setRedirect('index.php', Text::_('JINVALID_TOKEN'), 'error');
 207  
 208              return false;
 209          }
 210  
 211          // Check if the user is authorized to do this.
 212          if (!$this->app->getIdentity()->authorise('core.admin')) {
 213              $this->setRedirect('index.php', Text::_('JERROR_ALERTNOAUTHOR'), 'error');
 214  
 215              return false;
 216          }
 217  
 218          // Initialise model.
 219  
 220          /** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
 221          $model = $this->getModel('Application', 'Administrator');
 222  
 223          // Attempt to save the configuration and remove root.
 224          try {
 225              $model->removeroot();
 226          } catch (\RuntimeException $e) {
 227              // Save failed, go back to the screen and display a notice.
 228              $this->setRedirect('index.php', Text::_('JERROR_SAVE_FAILED', $e->getMessage()), 'error');
 229  
 230              return false;
 231          }
 232  
 233          // Set the redirect based on the task.
 234          $this->setRedirect(Route::_('index.php'), Text::_('COM_CONFIG_SAVE_SUCCESS'));
 235  
 236          return true;
 237      }
 238  
 239      /**
 240       * Method to send the test mail.
 241       *
 242       * @return  void
 243       *
 244       * @since   3.5
 245       */
 246      public function sendtestmail()
 247      {
 248          // Send json mime type.
 249          $this->app->mimeType = 'application/json';
 250          $this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
 251          $this->app->sendHeaders();
 252  
 253          // Check if user token is valid.
 254          if (!Session::checkToken()) {
 255              $this->app->enqueueMessage(Text::_('JINVALID_TOKEN'), 'error');
 256              echo new JsonResponse();
 257              $this->app->close();
 258          }
 259  
 260          // Check if the user is authorized to do this.
 261          if (!$this->app->getIdentity()->authorise('core.admin')) {
 262              $this->app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
 263              echo new JsonResponse();
 264              $this->app->close();
 265          }
 266  
 267          /** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
 268          $model = $this->getModel('Application', 'Administrator');
 269  
 270          echo new JsonResponse($model->sendTestMail());
 271  
 272          $this->app->close();
 273      }
 274  
 275      /**
 276       * Method to GET permission value and give it to the model for storing in the database.
 277       *
 278       * @return  void
 279       *
 280       * @since   3.5
 281       */
 282      public function store()
 283      {
 284          // Send json mime type.
 285          $this->app->mimeType = 'application/json';
 286          $this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
 287          $this->app->sendHeaders();
 288  
 289          // Check if user token is valid.
 290          if (!Session::checkToken('get')) {
 291              $this->app->enqueueMessage(Text::_('JINVALID_TOKEN'), 'error');
 292              echo new JsonResponse();
 293              $this->app->close();
 294          }
 295  
 296          /** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
 297          $model = $this->getModel('Application', 'Administrator');
 298          echo new JsonResponse($model->storePermissions());
 299          $this->app->close();
 300      }
 301  }


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