-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_guard.install
193 lines (186 loc) · 5.09 KB
/
session_guard.install
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @file
* Install and update functions for the session_guard module.
*/
/**
* Implements hook_schema().
*/
function session_guard_schema() {
$schema['session_log'] = array(
'description' => 'Stores grouped data from accesslog table.',
'fields' => array(
'lid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique session_log ID.',
),
'hostname' => array(
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'description' => 'Hostname of user that visited the page.',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
'description' => 'User {users}.uid that visited the page.',
),
'timestamp' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp of when the page was visited.',
),
),
'indexes' => array(
'timestamp' => array('timestamp'),
'uid' => array('uid'),
),
'primary key' => array('lid'),
'foreign keys' => array(
'visitor' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
$schema['fishy_session'] = array(
'description' => 'Stores the fishy list.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique fishy_session ID.',
),
'hostname' => array(
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'description' => 'Hostname of user that visited the page.',
),
'hostname_old' => array(
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'description' => 'Previous hostname of user that visited the page.',
),
'timestamp' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp of user login',
),
'timestamp_old' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp of when the record has been created in accesslog table',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
'description' => 'User {users}.uid that visited the page.',
),
'created' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp of the record when has been created',
),
),
'indexes' => array(
'created' => array('created'),
'uid' => array('uid'),
),
'primary key' => array('id'),
'foreign keys' => array(
'visitor' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
$schema['active_session'] = array(
'description' => 'Stores the current active session.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique active_session ID.',
),
'opened_sessions' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The number of opened sessions',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
'description' => 'User {users}.uid that visited the page.',
),
'created' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp of the record when has been created',
),
),
'indexes' => array(
'created' => array('created'),
'uid' => array('uid'),
),
'primary key' => array('id'),
'foreign keys' => array(
'visitor' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function session_guard_install() {
session_guard_explore_old_accesslog();
}
/**
* Explores the logs before the cron_last date in accesslog table
* to find any fishy.
*/
function session_guard_explore_old_accesslog() {
$logs = session_guard_get_old_accesslog();
session_guard_write_log($logs);
$fishy_list = session_guard_search_fishy($logs);
if (!empty($fishy_list)) {
session_guard_write_fishy($fishy_list);
}
}
/**
* Gets the accesslog table records until the last cron execution.
*/
function session_guard_get_old_accesslog() {
$query = db_select('accesslog', 'a');
$query->fields('a');
$query->condition('uid', 0, '<>');
$query->condition('timestamp', variable_get('cron_last'), '<=');
$query->groupBy('hostname');
$query->groupBy('uid');
$query->orderBy('timestamp', 'DESC');
return $query->execute()->fetchAll();
}