request.php
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* @package Joomla.Administrator
* @subpackage com_privacy
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Router\Route;
/**
* Request item model class.
*
* @since 3.9.0
*/
class PrivacyModelRequest extends JModelAdmin
{
/**
* Clean the cache
*
* @param string $group The cache group
* @param integer $client_id The ID of the client
*
* @return void
*
* @since 3.9.0
*/
protected function cleanCache($group = 'com_privacy', $client_id = 1)
{
parent::cleanCache('com_privacy', 1);
}
/**
* Method for getting the form from the model.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return JForm|boolean A JForm object on success, false on failure
*
* @since 3.9.0
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_privacy.request', 'request', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* 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 JTable A JTable object
*
* @since 3.9.0
* @throws \Exception
*/
public function getTable($name = 'Request', $prefix = 'PrivacyTable', $options = array())
{
return parent::getTable($name, $prefix, $options);
}
/**
* Method to get the data that should be injected in the form.
*
* @return array The default data is an empty array.
*
* @since 3.9.0
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_privacy.edit.request.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
/**
* Log the completion of a request to the action log system.
*
* @param integer $id The ID of the request to process.
*
* @return boolean
*
* @since 3.9.0
*/
public function logRequestCompleted($id)
{
/** @var PrivacyTableRequest $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');
$user = JFactory::getUser();
$message = array(
'action' => 'request-completed',
'requesttype' => $table->request_type,
'subjectemail' => $table->email,
'id' => $table->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $table->id,
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
);
/** @var ActionlogsModelActionlog $model */
$model = JModelLegacy::getInstance('Actionlog', 'ActionlogsModel');
$model->addLog(array($message), 'COM_PRIVACY_ACTION_LOG_ADMIN_COMPLETED_REQUEST', 'com_privacy.request', $user->id);
return true;
}
/**
* Log the creation of a request to the action log system.
*
* @param integer $id The ID of the request to process.
*
* @return boolean
*
* @since 3.9.0
*/
public function logRequestCreated($id)
{
/** @var PrivacyTableRequest $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');
$user = JFactory::getUser();
$message = array(
'action' => 'request-created',
'requesttype' => $table->request_type,
'subjectemail' => $table->email,
'id' => $table->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $table->id,
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
);
/** @var ActionlogsModelActionlog $model */
$model = JModelLegacy::getInstance('Actionlog', 'ActionlogsModel');
$model->addLog(array($message), 'COM_PRIVACY_ACTION_LOG_ADMIN_CREATED_REQUEST', 'com_privacy.request', $user->id);
return true;
}
/**
* Log the invalidation of a request to the action log system.
*
* @param integer $id The ID of the request to process.
*
* @return boolean
*
* @since 3.9.0
*/
public function logRequestInvalidated($id)
{
/** @var PrivacyTableRequest $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');
$user = JFactory::getUser();
$message = array(
'action' => 'request-invalidated',
'requesttype' => $table->request_type,
'subjectemail' => $table->email,
'id' => $table->id,
'itemlink' => 'index.php?option=com_privacy&view=request&id=' . $table->id,
'userid' => $user->id,
'username' => $user->username,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
);
/** @var ActionlogsModelActionlog $model */
$model = JModelLegacy::getInstance('Actionlog', 'ActionlogsModel');
$model->addLog(array($message), 'COM_PRIVACY_ACTION_LOG_ADMIN_INVALIDATED_REQUEST', 'com_privacy.request', $user->id);
return true;
}
/**
* Notifies the user that an information request has been created by a site administrator.
*
* Because confirmation tokens are stored in the database as a hashed value, this method will generate a new confirmation token
* for the request.
*
* @param integer $id The ID of the request to process.
*
* @return boolean
*
* @since 3.9.0
*/
public function notifyUserAdminCreatedRequest($id)
{
/** @var PrivacyTableRequest $table */
$table = $this->getTable();
if (!$table->load($id))
{
$this->setError($table->getError());
return false;
}
/*
* 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 = JFactory::getLanguage();
$db = $this->getDbo();
$userId = (int) $db->setQuery(
$db->getQuery(true)
->select('id')
->from($db->quoteName('#__users'))
->where($db->quoteName('email') . ' = ' . $db->quote($table->email)),
0,
1
)->loadResult();
if ($userId)
{
$receiver = JUser::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 = JLanguage::getInstance($langCode, $lang->getDebug());
}
// Ensure the right language files have been loaded
$lang->load('com_privacy', JPATH_ADMINISTRATOR, null, false, true)
|| $lang->load('com_privacy', JPATH_ADMINISTRATOR . '/components/com_privacy', null, false, true);
// Regenerate the confirmation token
$token = JApplicationHelper::getHash(JUserHelper::genRandomPassword());
$hashedToken = JUserHelper::hashPassword($token);
$table->confirm_token = $hashedToken;
$table->confirm_token_created_at = JFactory::getDate()->toSql();
try
{
$table->store();
}
catch (JDatabaseException $exception)
{
$this->setError($exception->getMessage());
return false;
}
// The mailer can be set to either throw Exceptions or return boolean false, account for both
try
{
$app = JFactory::getApplication();
$linkMode = $app->get('force_ssl', 0) == 2 ? Route::TLS_FORCE : Route::TLS_IGNORE;
$substitutions = array(
'[SITENAME]' => $app->get('sitename'),
'[URL]' => JUri::root(),
'[TOKENURL]' => JRoute::link('site', 'index.php?option=com_privacy&view=confirm&confirm_token=' . $token, false, $linkMode, true),
'[FORMURL]' => JRoute::link('site', 'index.php?option=com_privacy&view=confirm', false, $linkMode, true),
'[TOKEN]' => $token,
'\\n' => "\n",
);
switch ($table->request_type)
{
case 'export':
$emailSubject = $lang->_('COM_PRIVACY_EMAIL_ADMIN_REQUEST_SUBJECT_EXPORT_REQUEST');
$emailBody = $lang->_('COM_PRIVACY_EMAIL_ADMIN_REQUEST_BODY_EXPORT_REQUEST');
break;
case 'remove':
$emailSubject = $lang->_('COM_PRIVACY_EMAIL_ADMIN_REQUEST_SUBJECT_REMOVE_REQUEST');
$emailBody = $lang->_('COM_PRIVACY_EMAIL_ADMIN_REQUEST_BODY_REMOVE_REQUEST');
break;
default:
$this->setError(JText::_('COM_PRIVACY_ERROR_UNKNOWN_REQUEST_TYPE'));
return false;
}
foreach ($substitutions as $k => $v)
{
$emailSubject = str_replace($k, $v, $emailSubject);
$emailBody = str_replace($k, $v, $emailBody);
}
$mailer = JFactory::getMailer();
$mailer->setSubject($emailSubject);
$mailer->setBody($emailBody);
$mailer->addRecipient($table->email);
$mailResult = $mailer->Send();
if ($mailResult instanceof JException)
{
// JError was already called so we just need to return now
return false;
}
elseif ($mailResult === false)
{
$this->setError($mailer->ErrorInfo);
return false;
}
return true;
}
catch (phpmailerException $exception)
{
$this->setError($exception->getMessage());
return false;
}
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success, False on error.
*
* @since 3.9.0
*/
public function save($data)
{
$table = $this->getTable();
$key = $table->getKeyName();
$pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
if (!$pk && !JFactory::getConfig()->get('mailonline', 1))
{
$this->setError(JText::_('COM_PRIVACY_ERROR_CANNOT_CREATE_REQUEST_WHEN_SENDMAIL_DISABLED'));
return false;
}
return parent::save($data);
}
/**
* Method to validate the form data.
*
* @param JForm $form The form to validate against.
* @param array $data The data to validate.
* @param string $group The name of the field group to validate.
*
* @return array|boolean Array of filtered data if valid, false otherwise.
*
* @see JFormRule
* @see JFilterInput
* @since 3.9.0
*/
public function validate($form, $data, $group = null)
{
$validatedData = parent::validate($form, $data, $group);
// If parent validation failed there's no point in doing our extended validation
if ($validatedData === false)
{
return false;
}
// The user cannot create a request for their own account
if (strtolower(JFactory::getUser()->email) === strtolower($validatedData['email']))
{
$this->setError(JText::_('COM_PRIVACY_ERROR_CANNOT_CREATE_REQUEST_FOR_SELF'));
return false;
}
// Check for an active request for this email address
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('COUNT(id)')
->from('#__privacy_requests')
->where('email = ' . $db->quote($validatedData['email']))
->where('request_type = ' . $db->quote($validatedData['request_type']))
->where('status IN (0, 1)');
$activeRequestCount = (int) $db->setQuery($query)->loadResult();
if ($activeRequestCount > 0)
{
$this->setError(JText::_('COM_PRIVACY_ERROR_ACTIVE_REQUEST_FOR_EMAIL'));
return false;
}
return $validatedData;
}
}