[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_privacy/src/Model/ -> ExportModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_privacy
   6   *
   7   * @copyright   (C) 2018 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\Privacy\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Language;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Mail\MailTemplate;
  18  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  19  use Joomla\CMS\Plugin\PluginHelper;
  20  use Joomla\CMS\Table\Table;
  21  use Joomla\CMS\Uri\Uri;
  22  use Joomla\CMS\User\User;
  23  use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
  24  use Joomla\Component\Privacy\Administrator\Export\Domain;
  25  use Joomla\Component\Privacy\Administrator\Helper\PrivacyHelper;
  26  use Joomla\Component\Privacy\Administrator\Table\RequestTable;
  27  use PHPMailer\PHPMailer\Exception as phpmailerException;
  28  
  29  // phpcs:disable PSR1.Files.SideEffects
  30  \defined('_JEXEC') or die;
  31  // phpcs:enable PSR1.Files.SideEffects
  32  
  33  /**
  34   * Export model class.
  35   *
  36   * @since  3.9.0
  37   */
  38  class ExportModel extends BaseDatabaseModel
  39  {
  40      /**
  41       * Create the export document for an information request.
  42       *
  43       * @param   integer  $id  The request ID to process
  44       *
  45       * @return  Domain[]|boolean  A SimpleXMLElement object for a successful export or boolean false on an error
  46       *
  47       * @since   3.9.0
  48       */
  49      public function collectDataForExportRequest($id = null)
  50      {
  51          $id = !empty($id) ? $id : (int) $this->getState($this->getName() . '.request_id');
  52  
  53          if (!$id) {
  54              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_ID_REQUIRED_FOR_EXPORT'));
  55  
  56              return false;
  57          }
  58  
  59          /** @var RequestTable $table */
  60          $table = $this->getTable();
  61  
  62          if (!$table->load($id)) {
  63              $this->setError($table->getError());
  64  
  65              return false;
  66          }
  67  
  68          if ($table->request_type !== 'export') {
  69              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_TYPE_NOT_EXPORT'));
  70  
  71              return false;
  72          }
  73  
  74          if ($table->status != 1) {
  75              $this->setError(Text::_('COM_PRIVACY_ERROR_CANNOT_EXPORT_UNCONFIRMED_REQUEST'));
  76  
  77              return false;
  78          }
  79  
  80          // If there is a user account associated with the email address, load it here for use in the plugins
  81          $db = $this->getDatabase();
  82  
  83          $userId = (int) $db->setQuery(
  84              $db->getQuery(true)
  85                  ->select($db->quoteName('id'))
  86                  ->from($db->quoteName('#__users'))
  87                  ->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
  88                  ->bind(':email', $table->email)
  89                  ->setLimit(1)
  90          )->loadResult();
  91  
  92          $user = $userId ? User::getInstance($userId) : null;
  93  
  94          // Log the export
  95          $this->logExport($table);
  96  
  97          PluginHelper::importPlugin('privacy');
  98  
  99          $pluginResults = Factory::getApplication()->triggerEvent('onPrivacyExportRequest', [$table, $user]);
 100  
 101          $domains = [];
 102  
 103          foreach ($pluginResults as $pluginDomains) {
 104              $domains = array_merge($domains, $pluginDomains);
 105          }
 106  
 107          return $domains;
 108      }
 109  
 110      /**
 111       * Email the data export to the user.
 112       *
 113       * @param   integer  $id  The request ID to process
 114       *
 115       * @return  boolean
 116       *
 117       * @since   3.9.0
 118       */
 119      public function emailDataExport($id = null)
 120      {
 121          $id = !empty($id) ? $id : (int) $this->getState($this->getName() . '.request_id');
 122  
 123          if (!$id) {
 124              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_ID_REQUIRED_FOR_EXPORT'));
 125  
 126              return false;
 127          }
 128  
 129          $exportData = $this->collectDataForExportRequest($id);
 130  
 131          if ($exportData === false) {
 132              // Error is already set, we just need to bail
 133              return false;
 134          }
 135  
 136          /** @var RequestTable $table */
 137          $table = $this->getTable();
 138  
 139          if (!$table->load($id)) {
 140              $this->setError($table->getError());
 141  
 142              return false;
 143          }
 144  
 145          if ($table->request_type !== 'export') {
 146              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_TYPE_NOT_EXPORT'));
 147  
 148              return false;
 149          }
 150  
 151          if ($table->status != 1) {
 152              $this->setError(Text::_('COM_PRIVACY_ERROR_CANNOT_EXPORT_UNCONFIRMED_REQUEST'));
 153  
 154              return false;
 155          }
 156  
 157          // Log the email
 158          $this->logExportEmailed($table);
 159  
 160          /*
 161           * If there is an associated user account, we will attempt to send this email in the user's preferred language.
 162           * Because of this, it is expected that Language::_() is directly called and that the Text class is NOT used
 163           * for translating all messages.
 164           *
 165           * Error messages will still be displayed to the administrator, so those messages should continue to use the Text class.
 166           */
 167  
 168          $lang = Factory::getLanguage();
 169  
 170          $db = $this->getDatabase();
 171  
 172          $userId = (int) $db->setQuery(
 173              $db->getQuery(true)
 174                  ->select($db->quoteName('id'))
 175                  ->from($db->quoteName('#__users'))
 176                  ->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
 177                  ->bind(':email', $table->email),
 178              0,
 179              1
 180          )->loadResult();
 181  
 182          if ($userId) {
 183              $receiver = User::getInstance($userId);
 184  
 185              /*
 186               * We don't know if the user has admin access, so we will check if they have an admin language in their parameters,
 187               * falling back to the site language, falling back to the currently active language
 188               */
 189  
 190              $langCode = $receiver->getParam('admin_language', '');
 191  
 192              if (!$langCode) {
 193                  $langCode = $receiver->getParam('language', $lang->getTag());
 194              }
 195  
 196              $lang = Language::getInstance($langCode, $lang->getDebug());
 197          }
 198  
 199          // Ensure the right language files have been loaded
 200          $lang->load('com_privacy', JPATH_ADMINISTRATOR)
 201              || $lang->load('com_privacy', JPATH_ADMINISTRATOR . '/components/com_privacy');
 202  
 203          // The mailer can be set to either throw Exceptions or return boolean false, account for both
 204          try {
 205              $app = Factory::getApplication();
 206              $mailer = new MailTemplate('com_privacy.userdataexport', $app->getLanguage()->getTag());
 207  
 208              $templateData = [
 209                  'sitename' => $app->get('sitename'),
 210                  'url'      => Uri::root(),
 211              ];
 212  
 213              $mailer->addRecipient($table->email);
 214              $mailer->addTemplateData($templateData);
 215              $mailer->addAttachment('user-data_' . Uri::getInstance()->toString(['host']) . '.xml', PrivacyHelper::renderDataAsXml($exportData));
 216  
 217              if ($mailer->send() === false) {
 218                  $this->setError($mailer->ErrorInfo);
 219  
 220                  return false;
 221              }
 222  
 223              return true;
 224          } catch (phpmailerException $exception) {
 225              $this->setError($exception->getMessage());
 226  
 227              return false;
 228          }
 229      }
 230  
 231      /**
 232       * Method to get a table object, load it if necessary.
 233       *
 234       * @param   string  $name     The table name. Optional.
 235       * @param   string  $prefix   The class prefix. Optional.
 236       * @param   array   $options  Configuration array for model. Optional.
 237       *
 238       * @return  Table  A Table object
 239       *
 240       * @throws  \Exception
 241       * @since   3.9.0
 242       */
 243      public function getTable($name = 'Request', $prefix = 'Administrator', $options = [])
 244      {
 245          return parent::getTable($name, $prefix, $options);
 246      }
 247  
 248      /**
 249       * Log the data export to the action log system.
 250       *
 251       * @param   RequestTable  $request  The request record being processed
 252       *
 253       * @return  void
 254       *
 255       * @since   3.9.0
 256       */
 257      public function logExport(RequestTable $request)
 258      {
 259          $user = Factory::getUser();
 260  
 261          $message = [
 262              'action'      => 'export',
 263              'id'          => $request->id,
 264              'itemlink'    => 'index.php?option=com_privacy&view=request&id=' . $request->id,
 265              'userid'      => $user->id,
 266              'username'    => $user->username,
 267              'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
 268          ];
 269  
 270          $this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_EXPORT', 'com_privacy.request', $user->id);
 271      }
 272  
 273      /**
 274       * Log the data export email to the action log system.
 275       *
 276       * @param   RequestTable  $request  The request record being processed
 277       *
 278       * @return  void
 279       *
 280       * @since   3.9.0
 281       */
 282      public function logExportEmailed(RequestTable $request)
 283      {
 284          $user = Factory::getUser();
 285  
 286          $message = [
 287              'action'      => 'export_emailed',
 288              'id'          => $request->id,
 289              'itemlink'    => 'index.php?option=com_privacy&view=request&id=' . $request->id,
 290              'userid'      => $user->id,
 291              'username'    => $user->username,
 292              'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
 293          ];
 294  
 295          $this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_EXPORT_EMAILED', 'com_privacy.request', $user->id);
 296      }
 297  
 298      /**
 299       * Method to auto-populate the model state.
 300       *
 301       * @return  void
 302       *
 303       * @since   3.9.0
 304       */
 305      protected function populateState()
 306      {
 307          // Get the pk of the record from the request.
 308          $this->setState($this->getName() . '.request_id', Factory::getApplication()->input->getUint('id'));
 309  
 310          // Load the parameters.
 311          $this->setState('params', ComponentHelper::getParams('com_privacy'));
 312      }
 313  
 314      /**
 315       * Method to fetch an instance of the action log model.
 316       *
 317       * @return  ActionlogModel
 318       *
 319       * @since   4.0.0
 320       */
 321      private function getActionlogModel(): ActionlogModel
 322      {
 323          return Factory::getApplication()->bootComponent('com_actionlogs')
 324              ->getMVCFactory()->createModel('Actionlog', 'Administrator', ['ignore_request' => true]);
 325      }
 326  }


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