'admin/settings/role_activity', 'callback' => 'drupal_get_form', 'callback arguments' => array('role_activity_admin_settings'), 'title' => t('Role Activity settings'), 'description' => t('Settings for logging of activity for users in certain roles.'), 'access' => user_access('administer site configuration'), ); $items[] = array( 'path' => 'admin/logs/role_activity', 'callback' => 'role_activity_log', 'title' => t('Role Activity log'), 'description' => t('Role activity log in chronological order, most recent first.'), 'access' => $access, ); $items[] = array( 'path' => 'admin/logs/role_activity/top_users', 'callback' => 'role_activity_top_users', 'title' => t('Role Activity top users'), 'description' => t('Role activity log, top users.'), 'access' => $access, ); $items[] = array( 'path' => 'admin/logs/role_activity/user', 'callback' => 'role_activity_by_user', 'title' => t('Role Activity by user'), 'description' => t('Role activity log, by user.'), 'access' => $access, 'type' => MENU_CALLBACK, ); } return $items; } function role_activity_perm() { return array('view role activity'); } /** * Get a list of roles to record activity for */ function role_activity_role_list() { $result = array(); foreach(user_roles(TRUE) as $rid => $name) { if (variable_get('role_activity_rid_' . $rid, 0)) { $result[$rid] = $name; } } return $result; } function role_activity_check_role($uid = 0) { foreach(role_activity_role_list() as $rid => $name) { $db_uid = db_result(db_query("SELECT uid FROM {users_roles} WHERE rid = %d AND uid = %d", $rid, $uid)); if ($db_uid) { // Returned a value greater than zero if ($uid == $db_uid) { // And it is equal to the UID we are checking for return TRUE; } } } return FALSE; } function role_activity_admin_settings() { $form['group'] = array( '#type' => 'fieldset', '#collapsible' => TRUE, ); foreach(user_roles(TRUE) as $rid => $name) { $form['group']['role_activity_rid_' . $rid] = array( '#type' => 'checkbox', '#title' => $name, '#return_value' => 1, '#default_value' => variable_get('role_activity_rid_' . $rid, 0), ); } return system_settings_form($form); } /** * Implementation of hook_watchdog. * For Drupal 5.x, this requires backport in http://drupal.org/node/149341 */ function role_activity_watchdog($log) { $user = $log['user']; // Check if the user in the defined roles if (!role_activity_check_role($user->uid)) { return; } $action = role_activity_parse($log['type'], $log['message']); if (!$action) { // It is not something we should log, so we bail out return; } role_activity_write($log, $action); } /** * This is the parsing function that checks log messages for certain events. * Ideally, we should move this to a rule engine via a textbox for regexps * to look for. */ function role_activity_parse($type ='', $message = '') { $action = ''; switch($type) { case 'content': if (preg_match('/(book|page|forum|project|project_issue): updated/i', $message)) { $action = t('Content updated'); } elseif (preg_match('/(book|page|forum|project|project_issue): deleted/i', $message)) { $action = t('Content deleted'); } elseif (preg_match('/Comment: updated/i', $message)) { $action = t('Comment updated'); } elseif (preg_match('/Comment: deleted/i', $message)) { $action = t('Comment deleted'); } break; case 'user': if (preg_match('/Session opened/i', $message)) { $action = t('User login'); } elseif (preg_match('/Session closed/i', $message)) { $action = t('User logoff'); } elseif (preg_match('/Deleted user/i', $message)) { $action = t('User deleted'); } break; case 'aggregator': if (preg_match('/Feed .* added/i', $message)) { $action = t('Feed added'); } elseif (preg_match('/Feed .* deleted/i', $message)) { $action = t('Feed deleted'); } elseif (preg_match('/Updated URL for feed/i', $message)) { $action = t('Feed updated'); } break; } return $action; } function role_activity_write($log, $action) { $user = $log['user']; $time = $log['timestamp']; $type = $log['type']; $referer = $log['referer']; $ip = $log['ip']; $link = $log['link']; $uri = $log['request_uri']; $message = $log['message']; db_query("INSERT INTO {role_activity} (uid, timestamp, type, referer, ip, action, link, uri, message) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $user->uid, $time, $type, $referer, $ip, $action, $link, $uri, $message); } function role_activity_log() { $header = array( array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'), array('data' => t('Action'), 'field' => 'action'), array('data' => t('User'), 'field' => 'uid'), array('data' => t('Path'), 'field' => 'uri'), array('data' => t('IP/Host'), 'field' => 'ip'), array('data' => t('Type'), 'field' => 'type'), array('data' => t('Message'), 'field' => 'message'), array('data' => t('Referer'), 'field' => 'referer'), ); $sql = 'SELECT * FROM {role_activity} '. tablesort_sql($header); $result = pager_query($sql, 50); while ($log = db_fetch_object($result)) { $host = $log->ip; $user = user_load(array('uid' => $log->uid)); $rows[] = array( array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'), array('data' => $log->action), array('data' => theme('username', $user) . ' ' . l(t('Track'), 'admin/logs/role_activity/user/' . $user->uid)), array('data' => $log->uri), l($host, 'http://whois.domaintools.com/'. $log->ip), array('data' => $log->type), array('data' => $log->message), _role_activity_format_path($log->referrer), _role_activity_format_path($log->path, $log->title), ); } return theme('table', $header, $rows) . theme('pager', NULL, 50, 0); } function role_activity_top_users() { $header = array( array('data' => t('User'), 'field' => 'uid'), array('data' => t('# Events'), 'field' => 'count', 'sort' => 'desc'), array('data' => t('Last'), 'field' => 'last'), ); $sql = 'SELECT uid, COUNT(*) AS count, MAX(timestamp) AS last FROM {role_activity} GROUP BY uid'. tablesort_sql($header); $result = pager_query($sql, 50); while ($log = db_fetch_object($result)) { $user = user_load(array('uid' => $log->uid)); $rows[] = array( array('data' => theme('username', $user) . ' ' . l(t('Track'), 'admin/logs/role_activity/user/' . $user->uid)), array('data' => $log->count, 'align' => 'right'), array('data' => format_date($log->last, 'small'), 'nowrap' => 'nowrap'), ); } return theme('table', $header, $rows) . theme('pager', NULL, 50, 0); } function role_activity_by_user($uid = 0) { $header = array( array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'), array('data' => t('Action'), 'field' => 'action'), array('data' => t('Path'), 'field' => 'uri'), array('data' => t('IP/Host'), 'field' => 'ip'), array('data' => t('Type'), 'field' => 'type'), array('data' => t('Message'), 'field' => 'message'), array('data' => t('Referer'), 'field' => 'referer'), ); $sql = 'SELECT * FROM {role_activity} WHERE uid = %d'. tablesort_sql($header); $result = pager_query($sql, 50, 0, NULL, $uid); while ($log = db_fetch_object($result)) { $host = $log->ip; $user = user_load(array('uid' => $log->uid)); $rows[] = array( array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'), array('data' => $log->action), array('data' => $log->uri), l($host, 'http://whois.domaintools.com/'. $log->ip), array('data' => $log->type), array('data' => $log->message), _role_activity_format_path($log->referrer), _role_activity_format_path($log->path, $log->title), ); } $output = t('Details for !user', array('!user' => theme('username', $user))); return $output . theme('table', $header, $rows) . theme('pager', NULL, 50, 0); } function _role_activity_format_path($path, $title = '', $width = 32) { global $base_url; if ($title) { $short_title = truncate_utf8($title, $width, FALSE, TRUE); } else { $title = $path; $short_path = preg_replace('?^' . $base_url. '?', '', $path); $short_title = truncate_utf8($short_path, $width, FALSE, TRUE); } return l($short_title, $path, array('title' => $title)); }