actionlog.php
4.65 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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_actionlogs
*
* @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\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\FileLayout;
use Joomla\Utilities\IpHelper;
JLoader::register('ActionlogsHelper', JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.php');
/**
* Methods supporting a list of Actionlog records.
*
* @since 3.9.0
*/
class ActionlogsModelActionlog extends JModelLegacy
{
/**
* Function to add logs to the database
* This method adds a record to #__action_logs contains (message_language_key, message, date, context, user)
*
* @param array $messages The contents of the messages to be logged
* @param string $messageLanguageKey The language key of the message
* @param string $context The context of the content passed to the plugin
* @param integer $userId ID of user perform the action, usually ID of current logged in user
*
* @return void
*
* @since 3.9.0
*/
public function addLog($messages, $messageLanguageKey, $context, $userId = null)
{
$user = Factory::getUser($userId);
$db = $this->getDbo();
$date = Factory::getDate();
$params = ComponentHelper::getComponent('com_actionlogs')->getParams();
if ($params->get('ip_logging', 0))
{
$ip = IpHelper::getIp();
if (!filter_var($ip, FILTER_VALIDATE_IP))
{
$ip = 'COM_ACTIONLOGS_IP_INVALID';
}
}
else
{
$ip = 'COM_ACTIONLOGS_DISABLED';
}
$loggedMessages = array();
foreach ($messages as $message)
{
$logMessage = new stdClass;
$logMessage->message_language_key = $messageLanguageKey;
$logMessage->message = json_encode($message);
$logMessage->log_date = (string) $date;
$logMessage->extension = $context;
$logMessage->user_id = $user->id;
$logMessage->ip_address = $ip;
$logMessage->item_id = isset($message['id']) ? (int) $message['id'] : 0;
try
{
$db->insertObject('#__action_logs', $logMessage);
$loggedMessages[] = $logMessage;
}
catch (RuntimeException $e)
{
// Ignore it
}
}
// Send notification email to users who choose to be notified about the action logs
$this->sendNotificationEmails($loggedMessages, $user->name, $context);
}
/**
* Send notification emails about the action log
*
* @param array $messages The logged messages
* @param string $username The username
* @param string $context The Context
*
* @return void
*
* @since 3.9.0
*/
protected function sendNotificationEmails($messages, $username, $context)
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$params = ComponentHelper::getParams('com_actionlogs');
$showIpColumn = (bool) $params->get('ip_logging', 0);
$query
->select($db->quoteName(array('u.email', 'l.extensions')))
->from($db->quoteName('#__users', 'u'))
->join(
'INNER',
$db->quoteName('#__action_logs_users', 'l') . ' ON ( ' . $db->quoteName('l.notify') . ' = 1 AND '
. $db->quoteName('l.user_id') . ' = ' . $db->quoteName('u.id') . ')'
);
$db->setQuery($query);
try
{
$users = $db->loadObjectList();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());
return;
}
$recipients = array();
foreach ($users as $user)
{
$extensions = json_decode($user->extensions, true);
if ($extensions && in_array(strtok($context, '.'), $extensions))
{
$recipients[] = $user->email;
}
}
if (empty($recipients))
{
return;
}
$layout = new FileLayout('components.com_actionlogs.layouts.logstable', JPATH_ADMINISTRATOR);
$extension = strtok($context, '.');
ActionlogsHelper::loadTranslationFiles($extension);
foreach ($messages as $message)
{
$message->extension = Text::_($extension);
$message->message = ActionlogsHelper::getHumanReadableLogMessage($message);
}
$displayData = array(
'messages' => $messages,
'username' => $username,
'showIpColumn' => $showIpColumn,
);
$body = $layout->render($displayData);
$mailer = Factory::getMailer();
$mailer->addRecipient($recipients);
$mailer->setSubject(Text::_('COM_ACTIONLOGS_EMAIL_SUBJECT'));
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
if (!$mailer->Send())
{
JError::raiseWarning(500, Text::_('JERROR_SENDING_EMAIL'));
}
}
}