Completed
Pull Request — development (#2907)
by Martyn
13:27
created

Logging.subs.php ➔ updateLogActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.4066

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 5
dl 0
loc 21
ccs 8
cts 15
cp 0.5333
crap 2.4066
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains some useful functions for logging.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 1.1 Release Candidate 1
15
 *
16
 */
17
18
/**
19
 * @todo
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
20
 *
21
 * @param string $session_id
22
 */
23
function deleteLogOnlineInterval($session_id)
24
{
25
	global $modSettings;
26
27
	$db = database();
28
29
	$db->query('delete_log_online_interval', '
30
		DELETE FROM {db_prefix}log_online
31
		WHERE log_time < {int:log_time}
32
			AND session != {string:session}',
33
		array(
34
			'log_time' => time() - $modSettings['lastActive'] * 60,
35
			'session' => $session_id,
36
		)
37
	);
38
}
39
40
/**
41
 * Update a users entry in the online log
42
 *
43
 * @param string $session_id
44
 * @param string $serialized
45
 */
46
function updateLogOnline($session_id, $serialized)
47
{
48
	global $user_info;
49
50
	$db = database();
51
52
	$db->query('', '
53
		UPDATE {db_prefix}log_online
54
		SET
55
			log_time = {int:log_time},
56
			ip = {string:ip},
57
			url = {string:url}
58
		WHERE session = {string:session}',
59
		array(
60
			'log_time' => time(),
61
			'ip' => $user_info['ip'],
62
			'url' => $serialized,
63
			'session' => $session_id,
64
		)
65
	);
66
67
	// Guess it got deleted.
68
	if ($db->affected_rows() == 0)
69
		$_SESSION['log_time'] = 0;
70
}
71
72
/**
73
 * Update a users entry in the online log
74
 *
75
 * @param string $session_id
76
 * @param string $serialized
77
 * @param boolean $do_delete
78
 */
79
function insertdeleteLogOnline($session_id, $serialized, $do_delete = false)
80
{
81
	global $user_info, $modSettings;
82
83
	$db = database();
84
85
	if ($do_delete || !empty($user_info['id']))
86
		$db->query('', '
87
			DELETE FROM {db_prefix}log_online
88
			WHERE ' . ($do_delete ? 'log_time < {int:log_time}' : '') . ($do_delete && !empty($user_info['id']) ? ' OR ' : '') . (empty($user_info['id']) ? '' : 'id_member = {int:current_member}'),
89
			array(
90
				'current_member' => $user_info['id'],
91
				'log_time' => time() - $modSettings['lastActive'] * 60,
92
			)
93
		);
94
95
	$db->insert($do_delete ? 'ignore' : 'replace',
96
		'{db_prefix}log_online',
97
		array(
98
			'session' => 'string', 'id_member' => 'int', 'id_spider' => 'int', 'log_time' => 'int', 'ip' => 'string', 'url' => 'string'
99
		),
100
		array(
101
			$session_id, $user_info['id'], empty($_SESSION['id_robot']) ? 0 : $_SESSION['id_robot'], time(), $user_info['ip'], $serialized
102
		),
103
		array(
104
			'session'
105
		)
106
	);
107
}
108
109
/**
110
 * Update the system tracking statistics
111
 *
112
 * - Used by trackStats
113
 *
114
 * @param mixed[] $update_parameters
115
 * @param string $setStringUpdate
116
 * @param mixed[] $insert_keys
117
 * @param mixed[] $cache_stats
118
 * @param string $date
119
 */
120
function updateLogActivity($update_parameters, $setStringUpdate, $insert_keys, $cache_stats, $date)
121
{
122 1
	$db = database();
123
124 1
	$db->query('', '
125
		UPDATE {db_prefix}log_activity
126 1
		SET ' . $setStringUpdate . '
127 1
		WHERE date = {date:current_date}',
128
		$update_parameters
129 1
	);
130
131 1
	if ($db->affected_rows() == 0)
132 1
	{
133
		$db->insert('ignore',
134
			'{db_prefix}log_activity',
135
			array_merge($insert_keys, array('date' => 'date')),
136
			array_merge($cache_stats, array($date)),
137
			array('date')
138
		);
139
	}
140 1
}
141
142
/**
143
 * Actualize login history, for the passed member and IPs.
144
 *
145
 * - It will log it as entry for the current time.
146
 *
147
 * @param int $id_member
148
 * @param string $ip
149
 * @param string $ip2
150
 */
151
function logLoginHistory($id_member, $ip, $ip2)
152
{
153 6
	$db = database();
154
155 6
	$db->insert('insert',
156 6
		'{db_prefix}member_logins',
157
		array(
158 6
			'id_member' => 'int', 'time' => 'int', 'ip' => 'string', 'ip2' => 'string',
159 4
		),
160
		array(
161 6
			$id_member, time(), $ip, $ip2
162 4
		),
163
		array(
164 6
			'id_member', 'time'
165 4
		)
166 4
	);
167 6
}
168
169
/**
170
 * Checks if a messages or topic has been reported
171
 *
172
 * @param string $msg_id
173
 * @param string $topic_id
174
 * @param string $type
175
 */
176
function loadLogReported($msg_id, $topic_id, $type = 'msg')
177
{
178
	$db = database();
179
180
	$request = $db->query('', '
181
		SELECT id_report
182
		FROM {db_prefix}log_reported
183
		WHERE {raw:column_name} = {int:reported}
184
			AND type = {string:type}
185
		LIMIT 1',
186
		array(
187
			'column_name' => !empty($msg_id) ? 'id_msg' : 'id_topic',
188
			'reported' => !empty($msg_id) ? $msg_id : $topic_id,
189
			'type' => $type,
190
		)
191
	);
192
	$num = $db->num_rows($request);
193
	$db->free_result($request);
194
195
	return ($num > 0);
196
}
197
198
/**
199
 * Log a change to the forum, such as moderation events or administrative changes.
200
 *
201
 * @param mixed[] $inserts
202
 */
203
function insertLogActions($inserts)
204
{
205
	$db = database();
206
207
	$db->insert('',
208
		'{db_prefix}log_actions',
209
		array(
210
			'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
211
			'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
212
		),
213
		$inserts,
214
		array('id_action')
215
	);
216
217
	return $db->insert_id('{db_prefix}log_actions', 'id_action');
218
}
219
220
function deleteMemberLogOnline()
221
{
222
	global $user_info;
223
224
	$db = database();
225
226
	$db->query('', '
227
		DELETE FROM {db_prefix}log_online
228
		WHERE id_member = {int:current_member}',
229
		array(
230
			'current_member' => $user_info['id'],
231
		)
232
	);
233
}
234
235
/**
236
 * Delete expired/outdated session from log_online
237
 *
238
 * @package Authorization
239
 * @param string $session
240
 */
241
function deleteOnline($session)
242
{
243
	$db = database();
244
245
	$db->query('', '
246
		DELETE FROM {db_prefix}log_online
247
		WHERE session = {string:session}',
248
		array(
249
			'session' => $session,
250
		)
251
	);
252
}
253
254
/**
255
 * Set the passed users online or not, in the online log table
256
 *
257
 * @package Authorization
258
 * @param int[]|int $ids ids of the member(s) to log
259
 * @param bool $on = false if true, add the user(s) to online log, if false, remove 'em
260
 */
261
function logOnline($ids, $on = false)
262
{
263
	$db = database();
264
265
	if (!is_array($ids))
266
		$ids = array($ids);
267
268
	if (empty($on))
269
	{
270
		// set the user(s) out of log_online
271
		$db->query('', '
272
			DELETE FROM {db_prefix}log_online
273
			WHERE id_member IN ({array_int:members})',
274
			array(
275
				'members' => $ids,
276
			)
277
		);
278
	}
279
}