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

$wgHooks['isValidPassword'][] = 'FluxBBAuthPlugin::isValidPassword';

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

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


class FluxBBAuthPlugin extends AuthPlugin {

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

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

	$result = $dbr->safeQuery('SELECT id, username, email, realname FROM fluxbb.users WHERE username = ?', $username);
	$data = $result->fetchRow();
	$result->free();

	return $data;
}

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

	try {
		$result = $dbr->safeQuery('SELECT id FROM fluxbb.users WHERE username = ?', $username);
		$exists = ($result->numRows() > 0 ? true : false);
		$result->free();
	} catch (Exception $e) {
		$exists = false;
	}

	return $exists;
}

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

	try {
		$result = $dbr->safeQuery('SELECT id FROM fluxbb.users WHERE username = ? AND password = ?', $username, sha1($password));
		$authenticated = ($result->numRows() > 0 ? true : false);
		$result->free();
	} catch (Exception $e) {
		$authenticated = false;
	}

	return $authenticated;
}

public function modifyUITemplate( &$template, &$type ) {
	$template->set( 'usedomain', false );
	$template->set('link', 'Um Dich hier anzumelden, nutze Deine Konto-Daten aus dem <a href="https://bbs.archlinux.de/">archlinux.de-Forum</a>.');
}

public function setDomain( $domain ) {
	$this->domain = $domain;
}

public function validDomain( $domain ) {
	return true;
}

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

public function autoCreate() {
	return true;
}

public function allowPropChange( $prop = '' ) {
	if( $prop == 'realname' ) {
		return false;
	} elseif( $prop == 'emailaddress' ) {
		return false;
	} elseif( $prop == 'nickname' ) {
		return false;
	} else {
		return true;
	}
}
public function allowPasswordChange() {
	return false;
}

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

public function updateExternalDB( $user ) {
	// this way userdata is allways overwritten by external db
	return $this->initUser($user);
}

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());
		$user->setEmail($data['email']);
		$user->confirmEmail();
		$user->setRealName($data['realname']);
	} catch (Exception $e) {
		return false;
	}
	return true;
}

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

}

?>