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


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

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

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

		return $data;
	}

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

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

 		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 ) {
		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 ) {
		return strtoupper(substr($username, 0, 1)).substr($username, 1);
	}
}

?>