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

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

$wgExtensionCredits['other'][] 	= array(
	'name' => 'LLAuthPlugin',
	'description' => 'Authentifizierung am LL-Forum',
	'author' => 'Pierre Schmitz',
	'url' => 'http://www.archlinux.de',
);

require_once('includes/AuthPlugin.php');

class LLAuthPlugin extends AuthPlugin {

	private $dbLink	= null;
	private $data 	= null;

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

	function __destruct()
		{
		if (!is_null($this->dbLink))
			{
			mysqli_close($this->dbLink);
			}
		}

	private function connect()
		{
		global $wgDBuser, $wgDBpassword;

		if (is_null($this->dbLink))
			{
			$this->dbLink = mysqli_connect('localhost', $wgDBuser, $wgDBpassword, 'current');
			}
		}

	private function getUserData($username) {
		if (is_null($this->data))
			{
			$this->connect();
			$result = mysqli_query($this->dbLink, 'SELECT id, name, email, realname FROM users WHERE name = \''.mysqli_escape_string($this->dbLink, $username).'\'');
			$data = mysqli_fetch_assoc($result);
			mysqli_free_result($result);

			$this->data = $data;
			}

		return $this->data;
	}

	public function userExists( $username ) {
		$this->connect();
		$result = mysqli_query($this->dbLink, 'SELECT id FROM users WHERE name = \''.mysqli_escape_string($this->dbLink, $username).'\'');
		$exists = mysqli_num_rows($result) > 0;
		mysqli_free_result($result);

 		return $exists;
	}

	public function authenticate( $username, $password ) {
		$this->connect();
		$result = mysqli_query($this->dbLink, 'SELECT id FROM users WHERE name = \''.mysqli_escape_string($this->dbLink, $username).'\' AND password = \''.mysqli_escape_string($this->dbLink, sha1($password)).'\' ');
		$authenticated = mysqli_num_rows($result) > 0;
		mysqli_free_result($result);

 		return $authenticated;
	}

	public function modifyUITemplate( &$template ) {
		$template->set( 'usedomain', false );
		$template->set('link', 'Um Dich hier anzumelden, nutze Deine Konto-Daten aus dem <a href="http://forum.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 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 ) {
		$data = $this->getUserData($user->getName());
		$user->setEmail($data['email']);
		$user->confirmEmail();
		$user->setRealName($data['realname']);
		return true;
	}

	public function getCanonicalName( $username ) {
		// fix bug #122
		$data = $this->getUserData($username);
		// needed for update.php
		if (is_null($data))
			{
			return $username;
			}
		else
			{
			// make sure that first char is uppercase
			return strtoupper(substr($data['name'], 0, 1)).substr($data['name'], 1);
			}
	}
}

?>