- md5
- d9a14da9221470acd2dd8a9f85adbfc9
- sha1
- af3e2e2850173a7c88b4c1061c3a3d51ee47648c
- sha256
- 86caf62a9ecd118d0729417af13948fc794fcc4db28c9ceaa32fb2fbb292f6f9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
<?php
/**
* @package Joomla.Administrator
* @subpackage com_privacy
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Privacy\Administrator\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Language;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\User;
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
use Joomla\Component\Privacy\Administrator\Export\Domain;
use Joomla\Component\Privacy\Administrator\Helper\PrivacyHelper;
use Joomla\Component\Privacy\Administrator\Table\RequestTable;
use PHPMailer\PHPMailer\Exception as phpmailerException;
/**
* Export model class.
*
* @since 3.9.0
*/
class ExportModel extends BaseDatabaseModel
{
/**
* Create the export document for an information request.
*
* @param integer $id The request ID to process
*
* @return Domain[]|boolean A SimpleXMLElement object for a successful export or boolean false on an error
*
* @since 3.9.0
*/
public function collectDataForExportRequest($id = null)
{
$id = !empty($id) ? $id : (int) $this->getState($this->getName() . '.request_id');
if (!$id)
{
$this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_ID_REQUIRED_FOR_EXPORT'));
return false;
}
/** @var RequestTable $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
if ($table->request_type !== 'export')
{
$this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_TYPE_NOT_EXPORT'));
return false;
}
if ($table->status != 1)
{
$this->setError(Text::_('COM_PRIVACY_ERROR_CANNOT_EXPORT_UNCONFIRMED_REQUEST'));
return false;
}
// If there is a user account associated with the email address, load it here for use in the plugins
$db = $this->getDbo();
$userId = (int) $db->setQuery(
$db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__users'))
->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
->bind(':email', $table->email)
->setLimit(1)
)->loadResult();
$user = $userId ? User::getInstance($userId) : null;
// Log the export
$this->logExport($table);
PluginHelper::importPlugin('privacy');
$pluginResults = Factory::getApplication()->triggerEvent('onPrivacyExportRequest', [$table, $user]);
$domains = [];
foreach ($pluginResults as $pluginDomains)
{
$domains = array_merge($domains, $pluginDomains);
}
return $domains;
}
/**
* Email the data export to the user.
*
* @param integer $id The request ID to process
*
* @return boolean
*
* @since 3.9.0
*/
public function emailDataExport($id = null)
{
$id = !empty($id) ? $id : (int) $this->getState($this->getName() . '.request_id');
if (!$id)
{
$this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_ID_REQUIRED_FOR_EXPORT'));
return false;
}
$exportData = $this->collectDataForExportRequest($id);
if ($exportData === false)
{
// Error is already set, we just need to bail
return false;
}
/** @var RequestTable $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
if ($table->request_type !== 'export')
{
$this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_TYPE_NOT_EXPORT'));
return false;
}
if ($table->status != 1)
{
$this->setError(Text::_('COM_PRIVACY_ERROR_CANNOT_EXPORT_UNCONFIRMED_REQUEST'));
return false;
}
// Log the email
$this->logExportEmailed($table);
/*
* If there is an associated user account, we will attempt to send this email in the user's preferred language.
* Because of this, it is expected that Language::_() is directly called and that the Text class is NOT used
* for translating all messages.
*
* Error messages will still be displayed to the administrator, so those messages should continue to use the Text class.
*/
$lang = Factory::getLanguage();
$db = $this->getDbo();
$userId = (int) $db->setQuery(
$db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__users'))
->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
->bind(':email', $table->email),
0,
1
)->loadResult();
if ($userId)
{
$receiver = User::getInstance($userId);
/*
* We don't know if the user has admin access, so we will check if they have an admin language in their parameters,
* falling back to the site language, falling back to the currently active language
*/
$langCode = $receiver->getParam('admin_language', '');
if (!$langCode)
{
$langCode = $receiver->getParam('language', $lang->getTag());
}
$lang = Language::getInstance($langCode, $lang->getDebug());
}
// Ensure the right language files have been loaded
$lang->load('com_privacy', JPATH_ADMINISTRATOR)
|| $lang->load('com_privacy', JPATH_ADMINISTRATOR . '/components/com_privacy');
// The mailer can be set to either throw Exceptions or return boolean false, account for both
try
{
$app = Factory::getApplication();
$substitutions = [
'[SITENAME]' => $app->get('sitename'),
'[URL]' => Uri::root(),
'\\n' => "\n",
];
$emailSubject = $lang->_('COM_PRIVACY_EMAIL_DATA_EXPORT_COMPLETED_SUBJECT');
$emailBody = $lang->_('COM_PRIVACY_EMAIL_DATA_EXPORT_COMPLETED_BODY');
foreach ($substitutions as $k => $v)
{
$emailSubject = str_replace($k, $v, $emailSubject);
$emailBody = str_replace($k, $v, $emailBody);
}
$mailer = Factory::getMailer();
$mailer->setSubject($emailSubject);
$mailer->setBody($emailBody);
$mailer->addRecipient($table->email);
$mailer->addStringAttachment(
PrivacyHelper::renderDataAsXml($exportData),
'user-data_' . Uri::getInstance()->toString(['host']) . '.xml'
);
if ($mailer->Send() === false)
{
$this->setError($mailer->ErrorInfo);
return false;
}
return true;
}
catch (phpmailerException $exception)
{
$this->setError($exception->getMessage());
return false;
}
return true;
}
/**
* Method to get a table object, load it if necessary.
*
* @param string $name The table name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $options Configuration array for model. Optional.
*
* @return Table A Table object
*
* @throws \Exception
* @since 3.9.0
*/
public function getTable($name = 'Request', $prefix = 'Administrator', $options = [])
{
return parent::getTable($name, $prefix, $options);
}
/**
* Log the data export to the action log system.
*
* @param RequestTable $request The request record being processed
*
* @return void
*
* @since 3.9.0
*/
public function logExport(RequestTable $request)
{
$user = Factory::getUser();
$message = [
'action' => 'export',
'id' => $request->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $request->id,
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
];
$this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_EXPORT', 'com_privacy.request', $user->id);
}
/**
* Log the data export email to the action log system.
*
* @param RequestTable $request The request record being processed
*
* @return void
*
* @since 3.9.0
*/
public function logExportEmailed(RequestTable $request)
{
$user = Factory::getUser();
$message = [
'action' => 'export_emailed',
'id' => $request->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $request->id,
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
];
$this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_EXPORT_EMAILED', 'com_privacy.request', $user->id);
}
/**
* Method to auto-populate the model state.
*
* @return void
*
* @since 3.9.0
*/
protected function populateState()
{
// Get the pk of the record from the request.
$this->setState($this->getName() . '.request_id', Factory::getApplication()->input->getUint('id'));
// Load the parameters.
$this->setState('params', ComponentHelper::getParams('com_privacy'));
}
/**
* Method to fetch an instance of the action log model.
*
* @return ActionlogModel
*
* @since 4.0.0
*/
private function getActionlogModel(): ActionlogModel
{
return Factory::getApplication()->bootComponent('com_actionlogs')
->getMVCFactory()->createModel('Actionlog', 'Administrator', ['ignore_request' => true]);
}
}
The same content is in 5 releases, Joomla 4.0.0-beta to 4.0.0-beta5. See them all, or every version of this file.