Issues (4122)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

maintenance/removeUnusedAccounts.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 26.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Remove unused user accounts from the database
4
 * An unused account is one which has made no edits
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 * @file
22
 * @ingroup Maintenance
23
 * @author Rob Church <[email protected]>
24
 */
25
26
require_once __DIR__ . '/Maintenance.php';
27
28
/**
29
 * Maintenance script that removes unused user accounts from the database.
30
 *
31
 * @ingroup Maintenance
32
 */
33
class RemoveUnusedAccounts extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addOption( 'delete', 'Actually delete the account' );
37
		$this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
38
		$this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
39
	}
40
41
	public function execute() {
42
43
		$this->output( "Remove unused accounts\n\n" );
44
45
		# Do an initial scan for inactive accounts and report the result
46
		$this->output( "Checking for unused user accounts...\n" );
47
		$del = [];
48
		$dbr = $this->getDB( DB_REPLICA );
49
		$res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ );
50
		if ( $this->hasOption( 'ignore-groups' ) ) {
51
			$excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
52
		} else {
53
			$excludedGroups = [];
54
		}
55
		$touched = $this->getOption( 'ignore-touched', "1" );
56
		if ( !ctype_digit( $touched ) ) {
57
			$this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
58
		}
59
		$touchedSeconds = 86400 * $touched;
60
		foreach ( $res as $row ) {
61
			# Check the account, but ignore it if it's within a $excludedGroups
62
			# group or if it's touched within the $touchedSeconds seconds.
63
			$instance = User::newFromId( $row->user_id );
64
			if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
65
				&& $this->isInactiveAccount( $row->user_id, true )
66
				&& wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
67
			) {
68
				# Inactive; print out the name and flag it
69
				$del[] = $row->user_id;
70
				$this->output( $row->user_name . "\n" );
71
			}
72
		}
73
		$count = count( $del );
74
		$this->output( "...found {$count}.\n" );
75
76
		# If required, go back and delete each marked account
77
		if ( $count > 0 && $this->hasOption( 'delete' ) ) {
78
			$this->output( "\nDeleting unused accounts..." );
79
			$dbw = $this->getDB( DB_MASTER );
80
			$dbw->delete( 'user', [ 'user_id' => $del ], __METHOD__ );
81
			$dbw->delete( 'user_groups', [ 'ug_user' => $del ], __METHOD__ );
82
			$dbw->delete( 'user_former_groups', [ 'ufg_user' => $del ], __METHOD__ );
83
			$dbw->delete( 'user_properties', [ 'up_user' => $del ], __METHOD__ );
84
			$dbw->delete( 'logging', [ 'log_user' => $del ], __METHOD__ );
85
			$dbw->delete( 'recentchanges', [ 'rc_user' => $del ], __METHOD__ );
86
			$this->output( "done.\n" );
87
			# Update the site_stats.ss_users field
88
			$users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
89
			$dbw->update(
90
				'site_stats',
91
				[ 'ss_users' => $users ],
92
				[ 'ss_row_id' => 1 ],
93
				__METHOD__
94
			);
95
		} elseif ( $count > 0 ) {
96
			$this->output( "\nRun the script again with --delete to remove them from the database.\n" );
97
		}
98
		$this->output( "\n" );
99
	}
100
101
	/**
102
	 * Could the specified user account be deemed inactive?
103
	 * (No edits, no deleted edits, no log entries, no current/old uploads)
104
	 *
105
	 * @param int $id User's ID
106
	 * @param bool $master Perform checking on the master
107
	 * @return bool
108
	 */
109
	private function isInactiveAccount( $id, $master = false ) {
110
		$dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
111
		$checks = [
112
			'revision' => 'rev',
113
			'archive' => 'ar',
114
			'image' => 'img',
115
			'oldimage' => 'oi',
116
			'filearchive' => 'fa'
117
		];
118
		$count = 0;
119
120
		$this->beginTransaction( $dbo, __METHOD__ );
121
		foreach ( $checks as $table => $fprefix ) {
122
			$conds = [ $fprefix . '_user' => $id ];
123
			$count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
124
		}
125
126
		$conds = [ 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ];
127
		$count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
128
129
		$this->commitTransaction( $dbo, __METHOD__ );
130
131
		return $count == 0;
132
	}
133
}
134
135
$maintClass = "RemoveUnusedAccounts";
136
require_once RUN_MAINTENANCE_IF_MAIN;
137