summaryrefslogtreecommitdiff
path: root/extensions/FluxBBAuthPlugin.php
blob: 32d4b14556052b71e3b48bce9e20d96951208d9e (plain)
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
<?php

$wgExtensionCredits['other'][] = array(
	'name' => 'FluxBBAuthPlugin',
	'version' => '1.6',
	'description' => 'Use FluxBB accounts in MediaWiki',
	'author' => 'Pierre Schmitz',
	'url' => 'https://pierre-schmitz.com/'
);

require_once(__DIR__.'/../includes/AuthPlugin.php');

global $FluxBBDatabase;
$FluxBBDatabase = 'fluxbb';

class FluxBBAuthPlugin extends AuthPlugin {

	public static function isValidPassword($password) {
		$length = strlen($password);
		return $length >= 4;
	}

	private function getUserData($username) {
		global $FluxBBDatabase;
		$dbr = wfGetDB( DB_SLAVE );

		return $dbr->selectRow($FluxBBDatabase.'.users', array('username', 'email', 'realname'), array('username' => $username));
	}

	public function userExists( $username ) {
		global $FluxBBDatabase;
		$dbr = wfGetDB( DB_SLAVE );

		try {
			$result = $dbr->select($FluxBBDatabase.'.users', 'id', array('username' => $username));
			$exists = ($result->numRows() > 0 ? true : false);
			$result->free();
		} catch (DBQueryError $e) {
			$exists = false;
		}

		return $exists;
	}

	public function authenticate( $username, $password ) {
		global $FluxBBDatabase;
		$dbr = wfGetDB( DB_SLAVE );

		try {
			$result = $dbr->select($FluxBBDatabase.'.users', 'id', array('username' => $username, 'password' => sha1($password)));
			$authenticated = ($result->numRows() > 0 ? true : false);
			$result->free();
		} catch (DBQueryError $e) {
			$authenticated = false;
		}

		return $authenticated;
	}

	public function modifyUITemplate( &$template, &$type ) {
		$template->set( 'usedomain', false );
	}

	public function updateUser( &$user ) {
		return $this->initUser($user);
	}

	public function autoCreate() {
		return true;
	}

	protected function allowRealNameChange() {
		return false;
	}

	protected function allowEmailChange() {
		return false;
	}

	protected function allowNickChange() {
		return false;
	}

	public function allowPasswordChange() {
		return false;
	}

	public function allowSetLocalPassword() {
		return false;
	}

	public function setPassword( $user, $password ) {
		return false;
	}

	public function updateExternalDB( $user ) {
		return false;
	}

	public function updateExternalDBGroups( $user, $addgroups, $delgroups = array() ) {
		return false;
	}

	public function canCreateAccounts() {
		return false;
	}

	public function addUser( $user, $password, $email = '', $realname = '' ) {
		return false;
	}

	public function strict() {
		return true;
	}

	public function strictUserAuth( $username ) {
		return true;
	}

	public function initUser( &$user, $autocreate = false ) {
		try {
			$data = $this->getUserData($user->getName());
			if (!$data) {
				return false;
			}
			$user->setEmail($data->email);
			$user->confirmEmail();
			$user->setRealName($data->realname);
			$user->saveSettings();
		} catch (Exception $e) {
			return false;
		}
		return true;
	}

	public function getCanonicalName( $username ) {
		try {
			$data = $this->getUserData($username);
			if ($data !== false) {
				return strtoupper(substr($data->username, 0, 1)).substr($data->username, 1);
			}
		} catch (Exception $e) {
		}
		return $username;
	}

}

$wgAuth = new FluxBBAuthPlugin();
$wgHiddenPrefs[] = 'realname';
$wgHooks['isValidPassword'][] = 'FluxBBAuthPlugin::isValidPassword';

?>