mod_userdb_sql.pm 15.9 KB
Newer Older
Nigel Kukard's avatar
Nigel Kukard committed
1
# SQL user database support
2 3
# Copyright (C) 2007-2016, AllWorldIT
#
Nigel Kukard's avatar
Nigel Kukard committed
4 5 6 7
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
8
#
Nigel Kukard's avatar
Nigel Kukard committed
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
Nigel Kukard's avatar
Nigel Kukard committed
14 15 16 17
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

18
package smradius::modules::userdb::mod_userdb_sql;
Nigel Kukard's avatar
Nigel Kukard committed
19 20 21 22 23

use strict;
use warnings;

# Modules we need
24 25 26
use AWITPT::Cache;
use AWITPT::DB::DBLayer;
use AWITPT::Util;
27
use smradius::attributes;
28 29 30
use smradius::constants;
use smradius::logging;
use smradius::util;
Nigel Kukard's avatar
Nigel Kukard committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

# Exporter stuff
require Exporter;
our (@ISA,@EXPORT,@EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT = qw(
);
@EXPORT_OK = qw(
);



# Plugin info
our $pluginInfo = {
	Name => "SQL User Database",
	Init => \&init,
47

Nigel Kukard's avatar
Nigel Kukard committed
48 49 50
	# User database
	User_find => \&find,
	User_get => \&get,
51 52 53

	# Users data
	Users_data_set => \&data_set,
Robert Anderson's avatar
Robert Anderson committed
54 55 56
	Users_data_get => \&data_get,

	# Cleanup run by smadmin
Nigel Kukard's avatar
Nigel Kukard committed
57
	CleanupOrder => 95,
Robert Anderson's avatar
Robert Anderson committed
58
	Cleanup => \&cleanup
Nigel Kukard's avatar
Nigel Kukard committed
59 60
};

61 62
# Module config
my $config;
Nigel Kukard's avatar
Nigel Kukard committed
63 64 65 66 67 68

## @internal
# Initialize module
sub init
{
	my $server = shift;
69
	my $scfg = $server->{'inifile'};
70 71 72


	# Enable support for database
Robert Anderson's avatar
Robert Anderson committed
73
	if (!$server->{'smradius'}->{'database'}->{'enabled'}) {
74
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Enabling database support.");
Robert Anderson's avatar
Robert Anderson committed
75
		$server->{'smradius'}->{'database'}->{'enabled'} = 1;
76
	}
Nigel Kukard's avatar
Nigel Kukard committed
77

78 79
	# Default configs...
	$config->{'userdb_find_query'} = '
80
		SELECT
Robert Anderson's avatar
Robert Anderson committed
81
			ID, Disabled
82 83 84
		FROM
			@TP@users
		WHERE
85
			Username = %{user.Username}
86
	';
87

88
	$config->{'userdb_get_group_attributes_query'} = '
89
		SELECT
90
			@TP@group_attributes.Name, @TP@group_attributes.Operator, @TP@group_attributes.Value
91 92 93
		FROM
			@TP@group_attributes, @TP@users_to_groups
		WHERE
94
			@TP@users_to_groups.UserID = %{user.ID}
95
			AND @TP@group_attributes.GroupID = @TP@users_to_groups.GroupID
96
			AND @TP@group_attributes.Disabled = 0
97
	';
98

99
	$config->{'userdb_get_user_attributes_query'} = '
100
		SELECT
101
			Name, Operator, Value
102 103 104
		FROM
			@TP@user_attributes
		WHERE
105
			UserID = %{user.ID}
106
			AND Disabled = 0
107
	';
108

109 110 111 112 113
	$config->{'users_data_set_query'} = '
		INSERT INTO
			@TP@users_data (UserID, LastUpdated, Name, Value)
		VALUES
			(
114
				%{user.ID},
115 116 117 118 119
				%{query.LastUpdated},
				%{query.Name},
				%{query.Value}
			)
	';
120

121 122 123 124 125 126 127
	$config->{'users_data_update_query'} = '
		UPDATE
			@TP@users_data
		SET
			LastUpdated = %{query.LastUpdated},
			Value = %{query.Value}
		WHERE
128
			UserID = %{user.ID}
129 130
			AND Name = %{query.Name}
	';
131

132 133 134 135 136 137
	$config->{'users_data_get_query'} = '
		SELECT
			LastUpdated, Name, Value
		FROM
			@TP@users_data
		WHERE
138
			UserID = %{user.ID}
Nigel Kukard's avatar
Nigel Kukard committed
139
			AND Name = %{query.Name}
140
	';
141

142 143 144 145
	$config->{'users_data_delete_query'} = '
		DELETE FROM
			@TP@users_data
		WHERE
146
			UserID = %{user.ID}
147 148 149 150 151
			AND Name = %{query.Name}
	';

	# Default cache time for user data
	$config->{'userdb_data_cache_time'} = 300;
Nigel Kukard's avatar
Nigel Kukard committed
152

153 154 155
	# Setup SQL queries
	if (defined($scfg->{'mod_userdb_sql'})) {
		# Pull in queries
156 157
		if (defined($scfg->{'mod_userdb_sql'}->{'userdb_find_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'userdb_find_query'} ne "") {
158 159 160 161 162
			if (ref($scfg->{'mod_userdb_sql'}->{'userdb_find_query'}) eq "ARRAY") {
				$config->{'userdb_find_query'} = join(' ', @{$scfg->{'mod_userdb_sql'}->{'userdb_find_query'}});
			} else {
				$config->{'userdb_find_query'} = $scfg->{'mod_userdb_sql'}->{'userdb_find_query'};
			}
163 164 165 166
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'userdb_get_group_attributes_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'userdb_get_group_attributes_query'} ne "") {
167
			if (ref($scfg->{'mod_userdb_sql'}->{'userdb_get_group_attributes_query'}) eq "ARRAY") {
168
				$config->{'userdb_get_group_attributes_query'} = join(' ',
169 170
						@{$scfg->{'mod_userdb_sql'}->{'userdb_get_group_attributes_query'}});
			} else {
171
				$config->{'userdb_get_group_attributes_query'} =
172 173
						$scfg->{'mod_userdb_sql'}->{'userdb_get_group_attributes_query'};
			}
174 175 176 177
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'userdb_get_user_attributes_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'userdb_get_user_attributes_query'} ne "") {
178 179 180 181
			if (ref($scfg->{'mod_userdb_sql'}->{'userdb_get_user_attributes_query'}) eq "ARRAY") {
				$config->{'userdb_get_user_attributes_query'} = join(' ',
						@{$scfg->{'mod_userdb_sql'}->{'userdb_get_user_attributes_query'}});
			} else {
182
					$config->{'userdb_get_user_attributes_query'} =
183 184
						$scfg->{'mod_userdb_sql'}->{'userdb_get_user_attributes_query'};
			}
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

		if (defined($scfg->{'mod_userdb_sql'}->{'users_data_set_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'users_data_set_query'} ne "") {
			if (ref($scfg->{'mod_userdb_sql'}->{'users_data_set_query'}) eq "ARRAY") {
				$config->{'users_data_set_query'} = join(' ',
						@{$scfg->{'mod_userdb_sql'}->{'users_data_set_query'}});
			} else {
					$config->{'users_data_set_query'} = $scfg->{'mod_userdb_sql'}->{'users_data_set_query'};
			}
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'users_data_update_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'users_data_update_query'} ne "") {
			if (ref($scfg->{'mod_userdb_sql'}->{'users_data_update_query'}) eq "ARRAY") {
				$config->{'users_data_update_query'} = join(' ',
						@{$scfg->{'mod_userdb_sql'}->{'users_data_update_query'}});
			} else {
					$config->{'users_data_update_query'} = $scfg->{'mod_userdb_sql'}->{'users_data_update_query'};
			}
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'users_data_get_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'users_data_get_query'} ne "") {
			if (ref($scfg->{'mod_userdb_sql'}->{'users_data_get_query'}) eq "ARRAY") {
				$config->{'users_data_get_query'} = join(' ',
						@{$scfg->{'mod_userdb_sql'}->{'users_data_get_query'}});
			} else {
					$config->{'users_data_get_query'} = $scfg->{'mod_userdb_sql'}->{'users_data_get_query'};
			}
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'users_data_delete_query'}) &&
				$scfg->{'mod_userdb_sql'}->{'users_data_delete_query'} ne "") {
			if (ref($scfg->{'mod_userdb_sql'}->{'users_data_delete_query'}) eq "ARRAY") {
				$config->{'users_data_delete_query'} = join(' ',
						@{$scfg->{'mod_userdb_sql'}->{'users_data_delete_query'}});
			} else {
					$config->{'users_data_delete_query'} = $scfg->{'mod_userdb_sql'}->{'users_data_delete_query'};
			}
		}

		if (defined($scfg->{'mod_userdb_sql'}->{'userdb_data_cache_time'})) {
			if ($scfg->{'mod_userdb_sql'}{'userdb_data_cache_time'} =~ /^\s*(yes|true|1)\s*$/i) {
				# Default?
			} elsif ($scfg->{'mod_userdb_sql'}{'userdb_data_cache_time'} =~ /^\s*(no|false|0)\s*$/i) {
231
				$config->{'userdb_data_cache_time'} = undef;
232
			} elsif ($scfg->{'mod_userdb_sql'}{'userdb_data_cache_time'} =~ /^[0-9]+$/) {
233
				$config->{'userdb_data_cache_time'} = $scfg->{'mod_userdb_sql'}{'userdb_data_cache_time'};
234 235 236 237 238 239 240 241 242 243 244 245
			} else {
				$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Value for 'userdb_data_cache_time' is invalid");
			}
		}
	}

	# Log this for info sake
	if (defined($config->{'userdb_data_cache_time'})) {
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Users data caching ENABLED, cache time is %ds.",
				$config->{'userdb_data_cache_time'});
	} else {
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Users caching DISABLED");
246 247
	}
}
Nigel Kukard's avatar
Nigel Kukard committed
248 249 250 251 252

## @find
# Try find a user
#
# @param server Server object
253 254
# @param user SMRadius user hash
# @li Username Username of the user we want
Nigel Kukard's avatar
Nigel Kukard committed
255 256
# @param packet Radius packet
#
Nigel Kukard's avatar
Nigel Kukard committed
257
# @return _UserDB_Data Hash of db query, this is stored in the $user->{'_UserDB_Data'} hash item
Nigel Kukard's avatar
Nigel Kukard committed
258 259 260 261
sub find
{
	my ($server,$user,$packet) = @_;

262 263 264
	# Build template
	my $template;
	foreach my $attr ($packet->attributes) {
265
		$template->{'request'}->{$attr} = $packet->rawattr($attr)
266
	}
267 268 269

	# Add user details, not user ID is available here as thats what we are retrieving
	$template->{'user'}->{'Username'} = $user->{'Username'};
270 271

	# Replace template entries
272
	my @dbDoParams = templateReplace($config->{'userdb_find_query'},$template);
273 274 275

	my $sth = DBSelect(@dbDoParams);
	if (!$sth) {
276
		$server->log(LOG_ERR,"[MOD_USERDB_SQL] Failed to find user data: ".AWITPT::DB::DBLayer::error());
277 278 279 280 281 282 283 284 285 286 287
		return MOD_RES_SKIP;
	}

	# Check if we got a result, if we did not NACK
	my $rows = $sth->rows();
	if ($rows > 1) {
		$server->log(LOG_ERR,"[MOD_USERDB_SQL] More than one result returned for user '".$user->{'Username'}."'");
		return MOD_RES_SKIP;
	} elsif ($rows < 1) {
		$server->log(LOG_DEBUG,"[MOD_USERDB_SQL] User '".$user->{'Username'}."' not found in database");
		return MOD_RES_SKIP;
288 289
	}

290
	# Grab record data
Nigel Kukard's avatar
Nigel Kukard committed
291
	my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(ID Disabled));
Nigel Kukard's avatar
Nigel Kukard committed
292

Robert Anderson's avatar
Robert Anderson committed
293
	# Dont use disabled user
Nigel Kukard's avatar
Nigel Kukard committed
294
	my $res = isBoolean($row->{'Disabled'});
Robert Anderson's avatar
Robert Anderson committed
295 296 297 298 299
	if ($res) {
		$server->log(LOG_DEBUG,"[MOD_USERDB_SQL] User '".$user->{'Username'}."' is disabled");
		return MOD_RES_SKIP;
	}

300
	DBFreeRes($sth);
Nigel Kukard's avatar
Nigel Kukard committed
301

302
	return (MOD_RES_ACK,$row);
Nigel Kukard's avatar
Nigel Kukard committed
303 304 305 306 307 308 309
}


## @get
# Try to get a user
#
# @param server Server object
Nigel Kukard's avatar
Nigel Kukard committed
310
# @param user Server $user hash
Nigel Kukard's avatar
Nigel Kukard committed
311 312
# @param packet Radius packet
#
313 314 315
# @return User attributes hash
# @li Attributes Radius attribute hash
# @li VAttributes Radius vendor attribute hash
Nigel Kukard's avatar
Nigel Kukard committed
316 317 318 319
sub get
{
	my ($server,$user,$packet) = @_;

320 321 322
	# Build template
	my $template;
	foreach my $attr ($packet->attributes) {
323
		$template->{'request'}->{$attr} = $packet->rawattr($attr)
324
	}
325 326 327 328 329

	# Add user details
	$template->{'user'}->{'ID'} = $user->{'ID'};
	$template->{'user'}->{'Username'} = $user->{'Username'};

330
	# Add in userdb data
331
	foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
Nigel Kukard's avatar
Nigel Kukard committed
332
		$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
333
	}
334 335 336

	# Replace template entries
	my @dbDoParams = templateReplace($config->{'userdb_get_group_attributes_query'},$template);
337

338 339 340
	# Query database
	my $sth = DBSelect(@dbDoParams);
	if (!$sth) {
341
		$server->log(LOG_ERR,"Failed to get group attributes: ".AWITPT::DB::DBLayer::error());
342
		return RES_ERROR;
343
	}
344

345 346
	# Loop with group attributes
	while (my $row = $sth->fetchrow_hashref()) {
347
		addAttribute($server,$user,hashifyLCtoMC($row,qw(Name Operator Value)));
348 349
	}

Robert Anderson's avatar
Robert Anderson committed
350
	DBFreeRes($sth);
351 352 353 354 355 356 357 358



	# Replace template entries again
	@dbDoParams = templateReplace($config->{'userdb_get_user_attributes_query'},$template);
	# Query database
	$sth = DBSelect(@dbDoParams);
	if (!$sth) {
359
		$server->log(LOG_ERR,"Failed to get user attributes: ".AWITPT::DB::DBLayer::error());
360
		return RES_ERROR;
361
	}
362

Robert Anderson's avatar
Robert Anderson committed
363
	# Loop with user attributes
364
	while (my $row = $sth->fetchrow_hashref()) {
365
		addAttribute($server,$user,hashifyLCtoMC($row,qw(Name Operator Value)));
366 367
	}

Robert Anderson's avatar
Robert Anderson committed
368
	DBFreeRes($sth);
Nigel Kukard's avatar
Nigel Kukard committed
369

370
	return RES_OK;
Nigel Kukard's avatar
Nigel Kukard committed
371 372 373
}


374 375 376 377
## @data_set
# Set user data
#
# @param server Server object
Nigel Kukard's avatar
Nigel Kukard committed
378
# @param user Server $user hash
379
# @param module Module that is variable pertains to
380 381 382 383 384 385
# @param name Variable name
# @param value Variable value
#
# @return RES_OK on success, RES_ERROR on error
sub data_set
{
386
	my ($server, $user, $module, $name, $value) = @_;
387 388 389 390


	# Build template
	my $template;
391 392 393 394

	# Add user details
	$template->{'user'}->{'ID'} = $user->{'ID'};
	$template->{'user'}->{'Username'} = $user->{'Username'};
395 396 397

	# Add in userdb data
	foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
Nigel Kukard's avatar
Nigel Kukard committed
398
		$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
399 400
	}

401 402 403 404 405
	# Last updated time would be now
	$template->{'query'}->{'LastUpdated'} = $user->{'_Internal'}->{'Timestamp'};
	$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name);
	$template->{'query'}->{'Value'} = $value;

406 407 408 409 410 411
	# Replace template entries
	my @dbDoParams = templateReplace($config->{'users_data_update_query'},$template);

	# Query database
	my $sth = DBDo(@dbDoParams);
	if (!$sth) {
412
		$server->log(LOG_ERR,"Failed to update users data: ".AWITPT::DB::DBLayer::error());
413 414 415 416 417 418 419 420 421 422
		return RES_ERROR;
	}

	# If we updated *something* ...
	if ($sth eq "0E0") {
		@dbDoParams = templateReplace($config->{'users_data_set_query'},$template);

		# Insert
		$sth = DBDo(@dbDoParams);
		if (!$sth) {
423
			$server->log(LOG_ERR,"Failed to set users data: ".AWITPT::DB::DBLayer::error());
424 425 426 427 428 429 430 431
			return RES_ERROR;
		}
	}

	# If we using caching, cache the result of this set
	if (defined($config->{'userdb_data_cache_time'})) {
		# Build hash to store
		my %data;
Nigel Kukard's avatar
Nigel Kukard committed
432
		$data{'CachedUntil'} = $user->{'_Internal'}->{'Timestamp-Unix'} + $config->{'userdb_data_cache_time'};
433
		$data{'LastUpdated'} = $user->{'_Internal'}->{'Timestamp'};
434
		$data{'Module'} = $module;
435 436
		$data{'Name'} = $name;
		$data{'Value'} = $value;
437

438
		# Cache the result
Nigel Kukard's avatar
Nigel Kukard committed
439
		cacheStoreComplexKeyPair('mod_userdb_sql(users_data)',
Nigel Kukard's avatar
Nigel Kukard committed
440
				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name),
441 442
				\%data
		);
443 444 445 446 447 448 449 450 451 452 453
	}

	return RES_OK;
}


## @data_get
# Get user data
#
# @param server Server object
# @param user UserDB hash we got from find()
454
# @param module Module that is variable pertains to
455 456 457 458 459 460 461 462
# @param name Variable name
#
# @return Users data hash
# @li LastUpdated Time of last update
# @li Name Variable Name
# @li Value Variable Value
sub data_get
{
463
	my ($server, $user, $module, $name) = @_;
464 465 466 467


	# Build template
	my $template;
468 469 470 471

	# Add user details
	$template->{'user'}->{'ID'} = $user->{'ID'};
	$template->{'user'}->{'Username'} = $user->{'Username'};
472 473 474

	# Add in userdb data
	foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
475
		$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
476 477
	}

478 479
	$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name);

480 481
	# If we using caching, check how old the result is
	if (defined($config->{'userdb_data_cache_time'})) {
482
		my ($res,$val) = cacheGetComplexKeyPair('mod_userdb_sql(data_get)',
Nigel Kukard's avatar
Nigel Kukard committed
483
				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name)
484 485
		);

486 487 488 489 490 491
		if (defined($val) && $val->{'CachedUntil'} > $user->{'_Internal'}->{'Timestamp-Unix'}) {
			return $val;
		}
	}

	# Replace template entries
492
	my @dbDoParams = templateReplace($config->{'users_data_get_query'},$template);
493 494 495 496

	# Query database
	my $sth = DBSelect(@dbDoParams);
	if (!$sth) {
497
		$server->log(LOG_ERR,"Failed to get users data: ".AWITPT::DB::DBLayer::error());
498 499 500 501
		return RES_ERROR;
	}

	# Fetch user data
Nigel Kukard's avatar
Nigel Kukard committed
502
	my $row = hashifyLCtoMC($sth->fetchrow_hashref(), qw(LastUpdated Name Value));
503

Nigel Kukard's avatar
Nigel Kukard committed
504 505 506 507
	# If there is no result, just return undef
	return if (!defined($row));

	# If there is data, go through the long process of continuing ...
508
	my %data;
Nigel Kukard's avatar
Nigel Kukard committed
509
	$data{'LastUpdated'} = $row->{'LastUpdated'};
510
	$data{'Module'} = $module;
Nigel Kukard's avatar
Nigel Kukard committed
511 512
	$data{'Name'} = $row->{'Name'};
	$data{'Value'} = $row->{'Value'};
513 514 515 516

	# If we using caching and got here, it means that we must cache the result
	if (defined($config->{'userdb_data_cache_time'})) {
		$data{'CachedUntil'} = $user->{'_Internal'}->{'Timestamp-Unix'} + $config->{'userdb_data_cache_time'};
517

518
		# Cache the result
Nigel Kukard's avatar
Nigel Kukard committed
519
		cacheStoreComplexKeyPair('mod_userdb_sql(users_data)',
Nigel Kukard's avatar
Nigel Kukard committed
520
				sprintf('%s/%s/%s',$module,$user->{'_UserDB_Data'}->{'ID'},$name),
521 522
				\%data
		);
523 524 525 526 527 528
	}

	return \%data;
}


Robert Anderson's avatar
Robert Anderson committed
529 530 531
# Clean up of old user variables
sub cleanup
{
532
	my ($server,$runForDate,$resetUserData) = @_;
Robert Anderson's avatar
Robert Anderson committed
533

Nigel Kukard's avatar
Nigel Kukard committed
534
	$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => Removing old user data");
Robert Anderson's avatar
Robert Anderson committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	# Begin operation
	DBBegin();

	# Perform query
	my $sth = DBDo('
		DELETE FROM
			@TP@users_data
		WHERE UserID NOT IN
			(
				SELECT ID FROM users
			)
	');

	# Error and rollback
	if (!$sth) {
Nigel Kukard's avatar
Nigel Kukard committed
550
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => Database has been rolled back, no data deleted");
Robert Anderson's avatar
Robert Anderson committed
551 552 553 554
		DBRollback();
		return;
	}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	if ($resetUserData) {
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => Resetting user data counters");

		# Perform query
		my $sth = DBDo('
			UPDATE
				@TP@users_data
			SET
				Value = 0
			WHERE
				Name = '.DBQuote('CurrentMonthTotalTraffic').'
				OR Name = '.DBQuote('CurrentMonthTotalUptime').'
		');

		# Error and rollback
		if (!$sth) {
			$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => Database has been rolled back, no data reset");
			DBRollback();
			return;
		}
		$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => User data counters have been reset");
	}

Robert Anderson's avatar
Robert Anderson committed
578 579
	# Commit
	DBCommit();
580
	$server->log(LOG_NOTICE,"[MOD_USERDB_SQL] Cleanup => Old user data cleaned up");
Robert Anderson's avatar
Robert Anderson committed
581 582 583
}


Nigel Kukard's avatar
Nigel Kukard committed
584
1;
Nigel Kukard's avatar
Nigel Kukard committed
585
# vim: ts=4