| 1 |
<?php
|
| 2 |
// $Id: role_activity.module,v 1.10 2009/01/29 18:33:22 goba Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Tracking the activity of certain roles.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function role_activity_menu() {
|
| 13 |
$items = array();
|
| 14 |
$access = array('view role activity');
|
| 15 |
|
| 16 |
$items['admin/settings/role_activity'] = array(
|
| 17 |
'page callback' => 'drupal_get_form',
|
| 18 |
'page arguments' => array('role_activity_admin_settings'),
|
| 19 |
'title' => 'User activity logging settings',
|
| 20 |
'description' => 'Settings for logging of activity for users in certain roles.',
|
| 21 |
'access arguments' => array('administer site configuration'),
|
| 22 |
);
|
| 23 |
$items['admin/reports/role_activity'] = array(
|
| 24 |
'title' => 'User activity log',
|
| 25 |
'page callback' => 'role_activity_log',
|
| 26 |
'description' => 'User activity log in chronological order, most recent first.',
|
| 27 |
'access arguments' => $access,
|
| 28 |
);
|
| 29 |
$items['admin/reports/role_activity/list'] = array(
|
| 30 |
'title' => 'Recent activity',
|
| 31 |
'access arguments' => $access,
|
| 32 |
'type' => MENU_DEFAULT_LOCAL_TASK
|
| 33 |
);
|
| 34 |
$items['admin/reports/role_activity/top_users'] = array(
|
| 35 |
'title' => 'Top users',
|
| 36 |
'page callback' => 'role_activity_top_users',
|
| 37 |
'access arguments' => $access,
|
| 38 |
'type' => MENU_LOCAL_TASK
|
| 39 |
);
|
| 40 |
$items['admin/reports/role_activity/user'] = array(
|
| 41 |
'title' => 'Activity by user',
|
| 42 |
'page callback' => 'role_activity_by_user',
|
| 43 |
'access arguments' => $access,
|
| 44 |
'type' => MENU_CALLBACK,
|
| 45 |
);
|
| 46 |
|
| 47 |
return $items;
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_perm().
|
| 52 |
*/
|
| 53 |
function role_activity_perm() {
|
| 54 |
return array('view role activity');
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Get a list of roles to record activity for.
|
| 59 |
*/
|
| 60 |
function role_activity_role_list() {
|
| 61 |
$result = array();
|
| 62 |
foreach (user_roles(TRUE) as $rid => $name) {
|
| 63 |
if (variable_get('role_activity_rid_'. $rid, 0)) {
|
| 64 |
$result[$rid] = $name;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
return $result;
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Check whether the given user has any of the roles we shold monitor.
|
| 72 |
*/
|
| 73 |
function role_activity_check_role($uid = 0) {
|
| 74 |
$roles = array_keys(role_activity_role_list());
|
| 75 |
$params = array_merge($roles, array($uid));
|
| 76 |
return (bool) db_result(db_query("SELECT uid FROM {users_roles} WHERE rid IN (". db_placeholders($roles) .") AND uid = %d", $params));
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Administrator settings page.
|
| 81 |
*/
|
| 82 |
function role_activity_admin_settings() {
|
| 83 |
$form['group'] = array(
|
| 84 |
'#type' => 'fieldset',
|
| 85 |
'#collapsible' => TRUE,
|
| 86 |
'#description' => t('Activity will be monitored for these roles.'),
|
| 87 |
'#title' => t('Track activity for these roles')
|
| 88 |
);
|
| 89 |
foreach (user_roles(TRUE) as $rid => $name) {
|
| 90 |
$form['group']['role_activity_rid_'. $rid] = array(
|
| 91 |
'#type' => 'checkbox',
|
| 92 |
'#title' => $name,
|
| 93 |
'#return_value' => 1,
|
| 94 |
'#default_value' => variable_get('role_activity_rid_'. $rid, 0),
|
| 95 |
);
|
| 96 |
}
|
| 97 |
return system_settings_form($form);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_watchdog().
|
| 102 |
*/
|
| 103 |
function role_activity_watchdog($log_entry) {
|
| 104 |
if (!role_activity_check_role($log_entry['user']->uid)) {
|
| 105 |
// If user is not among the roles we should check.
|
| 106 |
return;
|
| 107 |
}
|
| 108 |
if (!($action = role_activity_parse($log_entry['type'], $log_entry['message']))) {
|
| 109 |
// It the parser did not find that we should the action, skip.
|
| 110 |
return;
|
| 111 |
}
|
| 112 |
|
| 113 |
role_activity_write($log_entry, $action);
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Parse the watchdog messages for certain patterns.
|
| 118 |
*
|
| 119 |
* @todo
|
| 120 |
* Ideally, we should move this to a rule engine via a textbox
|
| 121 |
* for regexps to look for.
|
| 122 |
*/
|
| 123 |
function role_activity_parse($type = '', $message = '') {
|
| 124 |
$action = '';
|
| 125 |
|
| 126 |
switch ($type) {
|
| 127 |
case 'content':
|
| 128 |
if (preg_match('/@type: updated/i', $message)) {
|
| 129 |
$action = t('Content updated');
|
| 130 |
}
|
| 131 |
elseif (preg_match('/@type: deleted/i', $message)) {
|
| 132 |
$action = t('Content deleted');
|
| 133 |
}
|
| 134 |
elseif (preg_match('/Comment: updated/i', $message)) {
|
| 135 |
$action = t('Comment updated');
|
| 136 |
}
|
| 137 |
// @todo: no such watchdog message in Drupal 6.
|
| 138 |
/*elseif (preg_match('/Comment: deleted/i', $message)) {
|
| 139 |
$action = t('Comment deleted');
|
| 140 |
}*/
|
| 141 |
break;
|
| 142 |
|
| 143 |
case 'user':
|
| 144 |
if (preg_match('/Session opened/i', $message)) {
|
| 145 |
$action = t('User login');
|
| 146 |
}
|
| 147 |
elseif (preg_match('/Session closed/i', $message)) {
|
| 148 |
$action = t('User logoff');
|
| 149 |
}
|
| 150 |
elseif (preg_match('/Deleted user/i', $message)) {
|
| 151 |
$action = t('User deleted');
|
| 152 |
}
|
| 153 |
break;
|
| 154 |
|
| 155 |
case 'aggregator':
|
| 156 |
if (preg_match('/Feed %feed added/i', $message)) {
|
| 157 |
$action = t('Feed added');
|
| 158 |
}
|
| 159 |
elseif (preg_match('/Feed %feed deleted/i', $message)) {
|
| 160 |
$action = t('Feed deleted');
|
| 161 |
}
|
| 162 |
elseif (preg_match('/Updated URL for feed/i', $message)) {
|
| 163 |
$action = t('Feed updated');
|
| 164 |
}
|
| 165 |
break;
|
| 166 |
}
|
| 167 |
return $action;
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Store role activity information.
|
| 172 |
*/
|
| 173 |
function role_activity_write($log, $action) {
|
| 174 |
db_query("INSERT INTO {role_activity}
|
| 175 |
(uid, timestamp, type, referer, ip, action, link, uri, message) VALUES
|
| 176 |
(%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
|
| 177 |
$log['user']->uid,
|
| 178 |
$log['timestamp'],
|
| 179 |
$log['type'],
|
| 180 |
$log['referer'],
|
| 181 |
$log['ip'],
|
| 182 |
$action,
|
| 183 |
$log['link'],
|
| 184 |
$log['request_uri'],
|
| 185 |
t($log['message'], $log['variables'])
|
| 186 |
);
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Review role activity log.
|
| 191 |
*/
|
| 192 |
function role_activity_log() {
|
| 193 |
$header = array(
|
| 194 |
array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
|
| 195 |
array('data' => t('Action'), 'field' => 'action'),
|
| 196 |
array('data' => t('User'), 'field' => 'uid'),
|
| 197 |
array('data' => t('Activity')),
|
| 198 |
array('data' => t('Path'), 'field' => 'uri'),
|
| 199 |
array('data' => t('IP/Host'), 'field' => 'ip'),
|
| 200 |
array('data' => t('Type'), 'field' => 'type'),
|
| 201 |
array('data' => t('Message'), 'field' => 'message'),
|
| 202 |
array('data' => t('Referrer'), 'field' => 'referer'),
|
| 203 |
array('data' => t('Link')),
|
| 204 |
);
|
| 205 |
|
| 206 |
$sql = 'SELECT * FROM {role_activity} '. tablesort_sql($header);
|
| 207 |
|
| 208 |
$result = pager_query($sql, 50);
|
| 209 |
while ($log = db_fetch_object($result)) {
|
| 210 |
$host = $log->ip;
|
| 211 |
$account = user_load(array('uid' => $log->uid));
|
| 212 |
|
| 213 |
$rows[] = array(
|
| 214 |
array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'),
|
| 215 |
array('data' => $log->action),
|
| 216 |
array('data' => theme('username', $account)),
|
| 217 |
array('data' => l(t('Activity'), 'admin/reports/role_activity/user/'. $account->uid)),
|
| 218 |
array('data' => $log->uri),
|
| 219 |
l($host, 'http://whois.domaintools.com/'. $log->ip),
|
| 220 |
array('data' => $log->type),
|
| 221 |
array('data' => $log->message),
|
| 222 |
_role_activity_format_path($log->referer),
|
| 223 |
$log->link,
|
| 224 |
);
|
| 225 |
}
|
| 226 |
|
| 227 |
return theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Top users by activity on the site.
|
| 232 |
*/
|
| 233 |
function role_activity_top_users() {
|
| 234 |
$header = array(
|
| 235 |
array('data' => t('User'), 'field' => 'uid'),
|
| 236 |
array('data' => t('Activity')),
|
| 237 |
array('data' => t('Number of events'), 'field' => 'count', 'sort' => 'desc'),
|
| 238 |
array('data' => t('Last'), 'field' => 'last'),
|
| 239 |
);
|
| 240 |
|
| 241 |
$sql = 'SELECT uid, COUNT(*) AS count, MAX(timestamp) AS last FROM {role_activity} GROUP BY uid '. tablesort_sql($header);
|
| 242 |
|
| 243 |
$result = pager_query($sql, 50);
|
| 244 |
while ($log = db_fetch_object($result)) {
|
| 245 |
$account = user_load(array('uid' => $log->uid));
|
| 246 |
|
| 247 |
$rows[] = array(
|
| 248 |
array('data' => theme('username', $account)),
|
| 249 |
array('data' => l(t('Activity'), 'admin/reports/role_activity/user/'. $account->uid)),
|
| 250 |
array('data' => $log->count, 'align' => 'right'),
|
| 251 |
array('data' => format_date($log->last, 'small'), 'nowrap' => 'nowrap'),
|
| 252 |
);
|
| 253 |
}
|
| 254 |
|
| 255 |
return theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Per-user activity data display on the site.
|
| 260 |
*/
|
| 261 |
function role_activity_by_user($uid = 0) {
|
| 262 |
$header = array(
|
| 263 |
array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
|
| 264 |
array('data' => t('Action'), 'field' => 'action'),
|
| 265 |
array('data' => t('Path'), 'field' => 'uri'),
|
| 266 |
array('data' => t('IP/Host'), 'field' => 'ip'),
|
| 267 |
array('data' => t('Type'), 'field' => 'type'),
|
| 268 |
array('data' => t('Message'), 'field' => 'message'),
|
| 269 |
array('data' => t('Referrer'), 'field' => 'referer'),
|
| 270 |
array('data' => t('Link')),
|
| 271 |
);
|
| 272 |
|
| 273 |
$account = user_load(array('uid' => $uid));
|
| 274 |
$sql = 'SELECT * FROM {role_activity} WHERE uid = %d '. tablesort_sql($header);
|
| 275 |
|
| 276 |
$result = pager_query($sql, 50, 0, NULL, $uid);
|
| 277 |
while ($log = db_fetch_object($result)) {
|
| 278 |
$host = $log->ip;
|
| 279 |
|
| 280 |
$rows[] = array(
|
| 281 |
array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'),
|
| 282 |
array('data' => $log->action),
|
| 283 |
array('data' => $log->uri),
|
| 284 |
l($host, 'http://whois.domaintools.com/'. $log->ip),
|
| 285 |
array('data' => $log->type),
|
| 286 |
array('data' => $log->message),
|
| 287 |
_role_activity_format_path($log->referer),
|
| 288 |
$log->link,
|
| 289 |
);
|
| 290 |
}
|
| 291 |
|
| 292 |
$output = t('Details for !user', array('!user' => theme('username', $account)));
|
| 293 |
return $output . theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
|
| 294 |
}
|
| 295 |
|
| 296 |
/**
|
| 297 |
* Provide a short title for linking to the related path.
|
| 298 |
*/
|
| 299 |
function _role_activity_format_path($path, $width = 32) {
|
| 300 |
global $base_url;
|
| 301 |
$short_path = preg_replace('?^'. $base_url .'?', '', $path);
|
| 302 |
$short_title = truncate_utf8($short_path, $width, FALSE, TRUE);
|
| 303 |
return l($short_title, $path, array('title' => $path));
|
| 304 |
}
|