Skip to content
This repository has been archived by the owner on Feb 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #21 from joomdonation/feature/logging
Browse files Browse the repository at this point in the history
Small fixes + clean up
  • Loading branch information
alikon authored May 6, 2018
2 parents f466d95 + 23f8b35 commit fc2cf60
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
11 changes: 4 additions & 7 deletions administrator/components/com_userlogs/controllers/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,19 @@ public function delete()
// Check for request forgeries.
JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

// Get the input
$app = JFactory::getApplication();

// Sanitize the input
$pks = ArrayHelper::toInteger($app->input->post->get('cid', array(), 'array'));
$pks = ArrayHelper::toInteger($this->input->post->get('cid', array(), 'array'));

// Get the logs data
$data = $this->getModel('userlogs')->delete($pks);

if ($data)
{
$app->enqueueMessage(JText::_('COM_USERLOGS_DELETE_SUCCESS'), 'message');
$this->setMessage(JText::_('COM_USERLOGS_DELETE_SUCCESS'));
}
else
{
$app->enqueueMessage(JText::_('COM_USERLOGS_DELETE_FAIL'), 'error');
$this->setMessage(JText::_('COM_USERLOGS_DELETE_FAIL'), 'error');
}

// Redirect to the list screen.
Expand All @@ -104,7 +101,7 @@ public function exportSelectedLogs()
JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

// Get selected logs
$pks = ArrayHelper::toInteger(JFactory::getApplication()->input->post->get('cid', array(), 'array'));
$pks = ArrayHelper::toInteger($this->input->post->get('cid', array(), 'array'));

// Get the logs data
$data = $this->getModel('userlogs')->getLogsData($pks);
Expand Down
13 changes: 7 additions & 6 deletions administrator/components/com_userlogs/helpers/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static function dataToCsv($data)
$date = JFactory::getDate();
$filename = "logs_" . $date;
$data = json_decode(json_encode($data), true);
$dispatcher = JEventDispatcher::getInstance();

$app = JFactory::getApplication();
$app->setHeader('Content-Type', 'application/csv', true)
Expand All @@ -39,7 +38,7 @@ public static function dataToCsv($data)

$app->sendHeaders();

$headers = ['Id', 'Message', 'Date', 'Extension', 'User', 'Ip'];
$headers = array('Id', 'Message', 'Date', 'Extension', 'User', 'Ip');

$fp = fopen('php://temp', 'r+');
ob_end_clean();
Expand All @@ -48,7 +47,7 @@ public static function dataToCsv($data)

foreach ($data as $log)
{
$dispatcher->trigger('onLogMessagePrepare', array (&$log['message'], $log['extension']));
$app->triggerEvent('onLogMessagePrepare', array (&$log['message'], $log['extension']));
$log['ip_address'] = JText::_($log['ip_address']);
$log['extension'] = self::translateExtensionName(strtoupper(strtok($log['extension'], '.')));

Expand Down Expand Up @@ -98,7 +97,7 @@ public static function getLogMessageParams($context)
$query = $db->getQuery(true)
->select('a.*')
->from($db->quoteName('#__user_logs_tables_data', 'a'))
->where($db->quoteName('a.type_alias') . ' = "' . $context . '"');
->where($db->quoteName('a.type_alias') . ' = ' .$db->quote($context));

$db->setQuery($query);

Expand Down Expand Up @@ -131,8 +130,10 @@ public static function getDataByPks($pks, $field, $tableType, $tablePrefix = 'JT

foreach ($pks as $pk)
{
$table->load($pk);
$items[] = $table->get($field);
if ($table->load($pk))
{
$items[] = $table->get($field);
}
}

return $items;
Expand Down
10 changes: 4 additions & 6 deletions administrator/components/com_userlogs/models/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function __construct($config = array())
'a.user_id', 'user',
'a.message', 'message',
'a.log_date', 'log_date',
'a.ip_address', 'ip_address'
'a.ip_address', 'ip_address',
'dateRange',
);
}

Expand Down Expand Up @@ -181,7 +182,7 @@ protected function checkIn()
*
* @param string $range The textual range to construct the filter for.
*
* @return string The date range to filter on.
* @return array The date range to filter on.
*
* @since __DEPLOY_VERSION__
*/
Expand Down Expand Up @@ -248,9 +249,7 @@ public function getLogsData($pks = null)

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

$query->where($db->qn('a.id') . ' IN (' . implode(',', $pks) . ')');
}

$db->setQuery($query);
Expand All @@ -273,7 +272,6 @@ public function delete(&$pks)
JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

// Get the table
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_userlogs/tables');
$table = $this->getTable('Userlogs', 'JTable');

if (!JFactory::getUser()->authorise('core.delete', $this->option))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@
<td class="center">
<?php echo JHtml::_('grid.id', $i, $item->id); ?>
</td>
<td>
<?php JEventDispatcher::getInstance()->trigger('onLogMessagePrepare', array (&$item->message, $item->extension)); ?>
<td>
<?php echo $this->escape($item->message); ?>
</td>
<td>
Expand Down
10 changes: 10 additions & 0 deletions administrator/components/com_userlogs/views/userlogs/view.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public function display($tpl = null)
$this->activeFilters = $this->get('ActiveFilters');
$this->pagination = $this->get('Pagination');

if (!empty($this->items))
{
$app = JFactory::getApplication();

foreach ($this->items as $item)
{
$app->triggerEvent('onLogMessagePrepare', array (&$item->message, $item->extension));
}
}

if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode("\n", $errors));
Expand Down
40 changes: 18 additions & 22 deletions plugins/system/userlogs/userlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ public function __construct(&$subject, $config)
* Function to add logs to the database
* This method adds a record to #__user_logs contains (message, date, context, user)
*
* @param string $message The contents of the message to be logged
* @param array $message The contents of the message to be logged
* @param string $context The context of the content passed to the plugin
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
*/
protected function addLogsToDb($message, $context)
{
$user = JFactory::getUser();
$date = JFactory::getDate();
$dispatcher = JEventDispatcher::getInstance();
$query = $this->db->getQuery(true);

if ($this->params->get('ip_logging', 0))
Expand Down Expand Up @@ -126,7 +125,7 @@ protected function addLogsToDb($message, $context)
return false;
}

$dispatcher->trigger('onUserLogsAfterMessageLog', array ($json_message, $date, $context, $user->name, $ip));
$this->app->triggerEvent('onUserLogsAfterMessageLog', array ($json_message, $date, $context, $user->name, $ip));
}

/**
Expand Down Expand Up @@ -286,11 +285,11 @@ public function onExtensionAfterInstall($installer, $eid)
return;
}

$extensionName = (array) $installer->get('manifest')->name;
$message = array(
$manifest = $installer->get('manifest');
$message = array(
'event' => 'onExtensionAfterInstall',
'extension_name' => $extensionName[0],
'extension_type' => $installer->get('manifest')->attributes()['type'],
'extension_name' => (string) $manifest->name,
'extension_type' => (string) $manifest->attributes()->type,
);

$this->addLogsToDb($message, $context);
Expand Down Expand Up @@ -318,11 +317,11 @@ public function onExtensionAfterUninstall($installer, $eid, $result)
return;
}

$extensionName = (array) $installer->get('manifest')->name;
$manifest = $installer->get('manifest');
$message = array(
'event' => 'onExtensionAfterUninstall',
'extension_name' => $extensionName[0],
'extension_type' => $installer->get('manifest')->attributes()['type'],
'extension_name' => (string) $manifest->name,
'extension_type' => (string) $manifest->attributes()->type,
);

$this->addLogsToDb($message, $context);
Expand All @@ -349,11 +348,11 @@ public function onExtensionAfterUpdate($installer, $eid)
return;
}

$extensionName = (array) $installer->get('manifest')->name;
$manifest = $installer->get('manifest');
$message = array(
'event' => 'onExtensionAfterUpdate',
'extension_name' => $extensionName[0],
'extension_type' => $installer->get('manifest')->attributes()['type'],
'extension_name' => (string) $manifest->name,
'extension_type' => (string) $manifest->attributes()->type,
);

$this->addLogsToDb($message, $context);
Expand Down Expand Up @@ -418,8 +417,6 @@ public function onExtensionAfterDelete($context, $table)
return;
}

$isNew_string = $isNew ? 'true' : 'false';

$message = array(
'event' => 'onExtensionAfterDelete',
'title' => $table->title,
Expand Down Expand Up @@ -453,7 +450,6 @@ public function onUserAfterSave($user, $isnew, $success, $msg)
}

$isNew_string = $isnew ? 'true' : 'false';
$success_string = $success ? 'true' : 'false';

$message = array(
'edited_user' => $user['name'],
Expand Down Expand Up @@ -574,7 +570,7 @@ public function onUserAfterDeleteGroup($group, $success, $msg)
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
*/
public function onLogMessagePrepare(&$message, $extension)
{
// Load the language
Expand Down Expand Up @@ -779,7 +775,6 @@ public function onContentPrepareForm($form, $data)
*/
public function onUserLogsAfterMessageLog($message, $date, $context, $userName, $ip)
{
$dispatcher = JEventDispatcher::getInstance();
$query = $this->db->getQuery(true);

$query->select('a.email, a.params')
Expand All @@ -803,7 +798,8 @@ public function onUserLogsAfterMessageLog($message, $date, $context, $userName,

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

if (in_array(strtok($context, '.'), $extensions))
{
Expand All @@ -816,7 +812,7 @@ public function onUserLogsAfterMessageLog($message, $date, $context, $userName,
return;
}

$dispatcher->trigger('onLogMessagePrepare', array (&$message, $context));
$this->app->triggerEvent('onLogMessagePrepare', array (&$message, $context));
$layout = new JLayoutFile('plugins.system.userlogs.layouts.logstable', JPATH_ROOT);

$displayData = array(
Expand All @@ -842,7 +838,7 @@ public function onUserLogsAfterMessageLog($message, $date, $context, $userName,
$mailer->Encoding = 'base64';
$mailer->setBody($body);

if (!$mail->Send())
if (!$mailer->Send())
{
$this->app->enqueueMessage(JText::_('JERROR_SENDING_EMAIL'), 'warning');
}
Expand Down

0 comments on commit fc2cf60

Please sign in to comment.