Skip to content

Commit

Permalink
Merge pull request #38 from joomdonation/actionlog_plugin_group
Browse files Browse the repository at this point in the history
3dp extensions support
Introduce actionlogs plugin group
thanks @joomdonation
  • Loading branch information
alikon authored May 19, 2018
2 parents b65be81 + edde03c commit 99343f7
Show file tree
Hide file tree
Showing 22 changed files with 1,157 additions and 1,000 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INSERT INTO `#__extensions` (`extension_id`, `package_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES
(36, 0, 'com_userlogs', 'component', 'com_userlogs', '', 1, 1, 1, 1, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0","ip_logging":"1","loggable_extensions":["com_banners","com_cache","com_categories","com_config","com_contact","com_content","com_installer","com_media","com_menus","com_messages","com_modules","com_newsfeeds","com_plugins","com_redirect","com_tags","com_templates","com_users"]}', '', '', 0, '0000-00-00 00:00:00', 0, 0);
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0"}', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(483, 0, 'plg_actionlogs_joomla', 'plugin', 'joomla', 'actionlogs', 0, 1, 1, 0, '', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0);


--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INSERT INTO "#__extensions" ("extension_id", "package_id", "name", "type", "element", "folder", "client_id", "enabled", "access", "protected", "manifest_cache", "params", "custom_data", "system_data", "checked_out", "checked_out_time", "ordering", "state") VALUES
(36, 0, 'com_userlogs', 'component', 'com_userlogs', '', 1, 1, 1, 1, '', '', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0","ip_logging":"1","loggable_extensions":["com_banners","com_cache","com_categories","com_config","com_contact","com_content","com_installer","com_media","com_menus","com_messages","com_modules","com_newsfeeds","com_plugins","com_redirect","com_tags","com_templates","com_users"]}', '', '', 0, '1970-01-01 00:00:00', 0, 0);
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0"}', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(483, 0, 'plg_actionlogs_joomla', 'plugin', 'joomla', 'actionlogs', 0, 1, 1, 0, '', '{}', '', '', 0, '1970-01-01 00:00:00', 0, 0);

--
-- Table: #__user_logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ SET IDENTITY_INSERT #__extensions ON;

INSERT INTO [#__extensions] ([extension_id], [package_id], [name], [type], [element], [folder], [client_id], [enabled], [access], [protected], [manifest_cache], [params], [custom_data], [system_data], [checked_out], [checked_out_time], [ordering], [state]) VALUES
(36, 0, 'com_userlogs', 'component', 'com_userlogs', '', 1, 1, 1, 1, '', '', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0","ip_logging":"1","loggable_extensions":["com_banners","com_cache","com_categories","com_config","com_contact","com_content","com_installer","com_media","com_menus","com_messages","com_modules","com_newsfeeds","com_plugins","com_redirect","com_tags","com_templates","com_users"]}', '', '', 0, '1900-01-01 00:00:00', 0, 0);
(482, 0, 'plg_system_userlogs', 'plugin', 'userlogs', 'system', 0, 0, 1, 0, '', '{"logDeletePeriod":"0"}', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(483, 0, 'plg_actionlogs_joomla', 'plugin', 'joomla', 'actionlogs', 0, 1, 1, 0, '', '{}', '', '', 0, '1900-01-01 00:00:00', 0, 0);

SET IDENTITY_INSERT #__extensions OFF;

Expand Down
31 changes: 31 additions & 0 deletions administrator/components/com_userlogs/config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<fieldset name="userlogs" label="COM_USERLOGS_OPTIONS" addfieldpath="/administrator/components/com_userlogs/models/fields">
<field
name="ip_logging"
type="radio"
label="COM_USERLOGS_IP_LOGGING"
description="COM_USERLOGS_IP_LOGGING_DESC"
class="btn-group btn-group-yesno"
default="0"
>
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>
<field
name="csv_delimiter"
type="list"
label="COM_USERLOGS_CSV_DELIMITER"
description="COM_USERLOGS_CSV_DELIMITER_DESC"
default=","
>
<option value=",">COM_USERLOGS_COMMA</option>
<option value=";">COM_USERLOGS_SEMICOLON</option>
</field>
<field
name="loggable_extensions"
type="logtype"
label="COM_USERLOGS_LOG_EXTENSIONS"
description="COM_USERLOGS_LOG_EXTENSIONS_DESC"
multiple="true"
default="com_banners,com_cache,com_categories,com_config,com_contact,com_content,com_installer,com_media,com_menus,com_messages,com_modules,com_newsfeeds,com_plugins,com_redirect,com_tags,com_templates,com_users"
/>
</fieldset>
<fieldset
name="permissions"
label="JCONFIG_PERMISSIONS_LABEL"
Expand Down
67 changes: 56 additions & 11 deletions administrator/components/com_userlogs/helpers/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Filesystem\Path;

/**
* Userlogs component helper.
*
Expand All @@ -27,8 +30,9 @@ class UserlogsHelper
*/
public static function dataToCsv($data)
{
$date = JFactory::getDate();
$filename = "logs_" . $date;
$date = JFactory::getDate();
$filename = "logs_" . $date;
$csvDelimiter = ComponentHelper::getComponent('com_userlogs')->getParams()->get('csv_delimiter', ',');

$app = JFactory::getApplication();
$app->setHeader('Content-Type', 'application/csv', true)
Expand All @@ -42,17 +46,19 @@ public static function dataToCsv($data)
$fp = fopen('php://temp', 'r+');
ob_end_clean();

fputcsv($fp, $headers);
fputcsv($fp, $headers, $csvDelimiter);

foreach ($data as $log)
foreach ($data as $row)
{
$log = (array) $log;
$log['ip_address'] = JText::_($log['ip_address']);
$log['extension'] = self::translateExtensionName(strtoupper(strtok($log['extension'], '.')));

$app->triggerEvent('onLogMessagePrepare', array(&$log['message'], $log['extension']));

fputcsv($fp, $log, ',');
$log = array();
$log['id'] = $row->id;
$log['message'] = strip_tags(self::getHumanReadableLogMessage($row));
$log['date'] = $row->log_date;
$log['extension'] = self::translateExtensionName(strtoupper(strtok($row->extension, '.')));
$log['name'] = $row->name;
$log['ip_address'] = JText::_($row->ip_address);

fputcsv($fp, $log, $csvDelimiter);
}

rewind($fp);
Expand Down Expand Up @@ -169,4 +175,43 @@ public static function getHumanReadableLogMessage($log)

return $message;
}

/**
* Get link to an item of given content type
*
* @param string $component
* @param string $contentType
* @param int $id
* @param string $urlVar
*
* @return string Link to the content item
* @since __DEPLOY_VERSION__
*/
public static function getContentTypeLink($component, $contentType, $id, $urlVar = 'id')
{
// Try to find the component helper.
$eName = str_replace('com_', '', $component);
$file = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/helpers/' . $eName . '.php');

if (file_exists($file))
{
$prefix = ucfirst(str_replace('com_', '', $component));
$cName = $prefix . 'Helper';

JLoader::register($cName, $file);

if (class_exists($cName) && is_callable(array($cName, 'getContentTypeLink')))
{
return $cName::getContentTypeLink($contentType, $id);
}
}

if (empty($urlVar))
{
$urlVar = 'id';
}

// Return default link to avoid having to implement getContentTypeLink in most of our components
return 'index.php?option=' . $component . '&task=' . $contentType . '.edit&' . $urlVar . '=' . $id;
}
}
46 changes: 46 additions & 0 deletions administrator/components/com_userlogs/layouts/logstable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_userlogs
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

JFactory::getLanguage()->load("com_userlogs", JPATH_ADMINISTRATOR, null, false, true);

$messages = $displayData['messages'];
?>
<h1>
<?php echo JText::_('COM_USERLOGS_EMAIL_SUBJECT'); ?>
</h1>
<h2>
<?php echo JText::_('COM_USERLOGS_EMAIL_DESC'); ?>
</h2>
<table>
<thead>
<th><?php echo JText::_('COM_USERLOGS_MESSAGE'); ?></th>
<th><?php echo JText::_('COM_USERLOGS_DATE'); ?></th>
<th><?php echo JText::_('COM_USERLOGS_EXTENSION'); ?></th>
<th><?php echo JText::_('COM_USERLOGS_NAME'); ?></th>
<th><?php echo JText::_('COM_USERLOGS_IP_ADDRESS'); ?></th>
</thead>
<tbody>
<?php
foreach ($messages as $message)
{
?>
<tr>
<td><?php echo $message->message; ?></td>
<td><?php echo $message->log_date; ?></td>
<td><?php echo $message->extension; ?></td>
<td><?php echo $displayData['username']; ?></td>
<td><?php echo $message->ip_address; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
File renamed without changes.
155 changes: 155 additions & 0 deletions administrator/components/com_userlogs/models/userlog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_userlogs
*
* @copyright Copyright (C) 2005 - 2018 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;

/**
* Methods supporting a list of article records.
*
* @since __DEPLOY_VERSION__
*/
class UserlogsModelUserlog extends JModelLegacy
{
/**
* Function to add logs to the database
* This method adds a record to #__user_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
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addLogsToDb($messages, $messageLanguageKey, $context)
{
$user = JFactory::getUser();
$db = $this->getDbo();
$date = JFactory::getDate();
$params = ComponentHelper::getComponent('com_userlogs')->getParams();

if ($params->get('ip_logging', 0))
{
$ip = JFactory::getApplication()->input->server->get('REMOTE_ADDR');
}
else
{
$ip = JText::_('COM_USERLOGS_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;

try
{
$db->insertObject('#__user_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 __DEPLOY_VERSION__
*/
protected function sendNotificationEmails($messages, $username, $context)
{
$db = $this->getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName(array('email', 'params')))
->from($db->quoteName('#__users'))
->where($db->quoteName('params') . ' LIKE ' . $db->quote('%"logs_notification_option":"1"%'));

$db->setQuery($query);

try
{
$users = $db->loadObjectList();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());

return;
}

$recipients = array();

foreach ($users as $user)
{
$userParams = json_decode($user->params, true);
$extensions = $userParams['logs_notification_extensions'];

if (in_array(strtok($context, '.'), $extensions))
{
$recipients[] = $user->email;
}
}

if (empty($recipients))
{
return;
}

$layout = new JLayoutFile('components.com_userlogs.layouts.logstable', JPATH_ADMINISTRATOR);
$extension = UserlogsHelper::translateExtensionName(strtoupper(strtok($context, '.')));

foreach ($messages as $message)
{
$message->extension = $extension;
$message->message = UserlogsHelper::getHumanReadableLogMessage($message);
}

$displayData = array(
'messages' => $messages,
'username' => $username,
);

$body = $layout->render($displayData);
$mailer = JFactory::getMailer();
$mailer->addRecipient($recipients);
$mailer->setSubject(JText::_('COM_USERLOGS_EMAIL_SUBJECT'));
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);

if (!$mailer->Send())
{
JError::raiseWarning(500, JText::_('JERROR_SENDING_EMAIL'));
}
}
}
7 changes: 4 additions & 3 deletions administrator/components/com_userlogs/models/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,13 @@ public function getLogsData($pks = null)
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('a.*')
->from($db->quoteName('#__user_logs', 'a'));
->select('a.*, u.name')
->from('#__user_logs AS a')
->innerJoin('#__users AS u ON a.user_id = u.id');

if (is_array($pks) && count($pks) > 0)
{
$query->where($db->qn('a.id') . ' IN (' . implode(',', $pks) . ')');
$query->where($db->quoteName('a.id') . ' IN (' . implode(',', $pks) . ')');
}

$db->setQuery($query);
Expand Down
Loading

0 comments on commit 99343f7

Please sign in to comment.