-
Notifications
You must be signed in to change notification settings - Fork 341
/
AccessLog.php
255 lines (215 loc) · 5.26 KB
/
AccessLog.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Access Log program
*
* @since 3.0
*
* Original module:
* @copyright @dpredster
* @link https://github.com/dpredster/Access_Log/ (Original extra module, now deprecated)
*
* @package RosarioSIS
* @subpackage modules
*/
require_once 'ProgramFunctions/UserAgent.fnc.php';
DrawHeader( ProgramTitle() );
// Set start date.
$start_date = RequestedDate( 'start', date( 'Y-m-d', time() - 60 * 60 * 24 ) );
// Set end date.
$end_date = RequestedDate( 'end', DBDate() );
if ( $_REQUEST['modfunc'] === 'delete' )
{
// Prompt before deleting log.
if ( DeletePrompt( _( 'Access Log' ) ) )
{
DBQuery( 'DELETE FROM access_log' );
$note[] = _( 'Access Log cleared.' );
// Unset modfunc & redirect URL.
RedirectURL( 'modfunc' );
}
}
if ( count( $_REQUEST ) === 3
&& ! $_REQUEST['modfunc'] )
{
// Only requested modname.
// @since 11.4 Automatically clear Access Log entries older than one year
DBQuery( "DELETE FROM access_log
WHERE CREATED_AT<'" . date( 'Y-m-d', strtotime( '1 year ago' ) ) . "'" );
}
echo ErrorMessage( $note, 'note' );
if ( ! $_REQUEST['modfunc'] )
{
echo '<form action="' . URLEscape( 'Modules.php?modname=' . $_REQUEST['modname'] ) . '" method="GET">';
DrawHeader(
_( 'From' ) . ' ' . DateInput( $start_date, 'start', '', false, false ) . ' - ' .
_( 'To' ) . ' ' . DateInput( $end_date, 'end', '', false, false ) .
Buttons( _( 'Go' ) )
);
echo '</form>';
// Format DB data.
$access_logs_functions = [
'STATUS' => '_makeAccessLogStatus', // Translate status.
'PROFILE' => '_makeAccessLogProfile', // Translate profile.
'USERNAME' => '_makeAccessLogUsername', // Add link to user info.
'CREATED_AT' => 'ProperDateTime', // Display localized & preferred Date & Time.
'USER_AGENT' => '_makeAccessLogUserAgent', // Display Browser & OS.
];
$access_logs_RET = DBGet( "SELECT
DISTINCT USERNAME,PROFILE,CREATED_AT,IP_ADDRESS,STATUS,USER_AGENT
FROM access_log
WHERE CREATED_AT >='" . $start_date . "'
AND CREATED_AT <='" . $end_date . ' 23:59:59' . "'
ORDER BY CREATED_AT DESC
LIMIT 3000", $access_logs_functions );
echo '<form action="' . URLEscape( 'Modules.php?modname=' . $_REQUEST['modname'] . '&modfunc=delete' ) . '" method="POST">';
DrawHeader( '', SubmitButton( _( 'Clear Log' ), '', '' ) );
ListOutput(
$access_logs_RET,
[
'CREATED_AT' => _( 'Date' ),
'USERNAME' => _( 'Username' ),
'PROFILE' => _( 'User Profile' ),
'STATUS' => _( 'Status' ),
'IP_ADDRESS' => _( 'IP Address' ),
'USER_AGENT' => _( 'Browser' ),
],
'Login record',
'Login records',
[],
[],
[
'count' => true,
'save' => true,
// @since 10.9 Add pagination for list > 1000 results
'pagination' => true,
]
);
echo '</form>';
// When clicking on Username, go to Student or User Info. ?>
<script>
$('.al-username').attr('href', function(){
var url = 'Modules.php?modname=Users/User.php&search_modfunc=list&';
if ( $(this).hasClass('student') ) {
url = url.replace( 'Users/User.php', 'Students/Student.php' ) + 'cust[USERNAME]=';
} else {
url += 'username=';
}
return url + this.firstChild.data;
});
</script>
<?php
}
/**
* Make Status
* Successful Login or Failed Login
*
* Local function
* DBGet callback
*
* @since 3.0
* @since 3.5 Banned status.
*
* @param string $value Field value.
* @param string $name 'STATUS'.
*
* @return string Success or Banned or Fail.
*/
function _makeAccessLogStatus( $value, $column )
{
if ( $value === 'B' )
{
return '<span style="color: red;">' . _( 'Banned' ) . '</span>';
}
if ( $value
&& $value !== 'Failed Login' ) // Compatibility with version 1.1.
{
return _( 'Success' );
}
return _( 'Fail' );
}
/**
* Make Profile
* Only for successful logins.
*
* Local function
* DBGet callback
*
* @since 3.0
*
* @param string $value Field value.
* @param string $name 'PROFILE'.
*
* @return string Student, Administrator, Teacher, Parent, or No Access.
*/
function _makeAccessLogProfile( $value, $column )
{
$profile_options = [
'student' => _( 'Student' ),
'admin' => _( 'Administrator' ),
'teacher' => _( 'Teacher' ),
'parent' => _( 'Parent' ),
'none' => _( 'No Access' ),
];
if ( ! isset( $profile_options[ $value ] ) )
{
return '';
}
return $profile_options[ $value ];
}
/**
* Make Username
* Links to user info page.
*
* Local function
* DBGet callback
*
* @since 3.0
*
* @param string $value Field value.
* @param string $name 'USERNAME'.
*
* @return string USername linking to user info page.
*/
function _makeAccessLogUsername( $value, $column )
{
global $THIS_RET;
if ( ! $value )
{
return '';
}
if ( isset( $_REQUEST['_ROSARIO_PDF'] ) )
{
return $value;
}
return '<a class="al-username ' .
( $THIS_RET['PROFILE'] === 'student' ? 'student' : '' ) .
'" href="#">' . $value . '</a>';
}
/**
* Make User Agent
*
* Local function
* DBGet callback
*
* @since 3.0
*
* @link http://php.net/get-browser
*
* @param string $value Field value.
* @param string $name 'USER_AGENT'.
*
* @return string Browser (OS).
*/
function _makeAccessLogUserAgent( $value, $column )
{
if ( empty( $value ) )
{
return $value;
}
$os = GetUserAgentOS( $value );
if ( $os )
{
$os = ' (' . $os . ')';
}
return GetUserAgentBrowser( $value ) . $os;
}