Remove internalhttpsession from ApiUpload; remnant from reverted feature.
[lhc/web/wiklou.git] / includes / extauth / MediaWiki.php
1 <?php
2
3 # Copyright (C) 2009 Aryeh Gregor
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * This class supports authentication against an external MediaWiki database,
22 * probably any version back to 1.5 or something. Example configuration:
23 *
24 * $wgExternalAuthType = 'ExternalUser_MediaWiki';
25 * $wgExternalAuthConf = array(
26 * 'DBtype' => 'mysql',
27 * 'DBserver' => 'localhost',
28 * 'DBname' => 'wikidb',
29 * 'DBuser' => 'quasit',
30 * 'DBpassword' => 'a5Cr:yf9u-6[{`g',
31 * 'DBprefix' => '',
32 * );
33 *
34 * All fields must be present. These mean the same things as $wgDBtype,
35 * $wgDBserver, etc. This implementation is quite crude; it could easily
36 * support multiple database servers, for instance, and memcached, and it
37 * probably has bugs. Kind of hard to reuse code when things might rely on who
38 * knows what configuration globals.
39 *
40 * If either wiki uses the UserComparePasswords hook, password authentication
41 * might fail unexpectedly unless they both do the exact same validation.
42 * There may be other corner cases like this where this will fail, but it
43 * should be unlikely.
44 *
45 * @ingroup ExternalUser
46 */
47 class ExternalUser_MediaWiki extends ExternalUser {
48 private $mRow, $mDb;
49
50 protected function initFromName( $name ) {
51 # We might not need the 'usable' bit, but let's be safe. Theoretically
52 # this might return wrong results for old versions, but it's probably
53 # good enough.
54 $name = User::getCanonicalName( $name, 'usable' );
55
56 if ( !is_string( $name ) ) {
57 return false;
58 }
59
60 return $this->initFromCond( array( 'user_name' => $name ) );
61 }
62
63 protected function initFromId( $id ) {
64 return $this->initFromCond( array( 'user_id' => $id ) );
65 }
66
67 private function initFromCond( $cond ) {
68 global $wgExternalAuthConf;
69
70 $class = 'Database' . $wgExternalAuthConf['DBtype'];
71 $this->mDb = new $class(
72 $wgExternalAuthConf['DBserver'],
73 $wgExternalAuthConf['DBuser'],
74 $wgExternalAuthConf['DBpassword'],
75 $wgExternalAuthConf['DBname'],
76 false,
77 0,
78 $wgExternalAuthConf['DBprefix']
79 );
80
81 $row = $this->mDb->selectRow(
82 'user',
83 array(
84 'user_name', 'user_id', 'user_password', 'user_email',
85 'user_email_authenticated'
86 ),
87 $cond,
88 __METHOD__
89 );
90 if ( !$row ) {
91 return false;
92 }
93 $this->mRow = $row;
94
95 return true;
96 }
97
98 # TODO: Implement initFromCookie().
99
100 public function getId() {
101 return $this->mRow->user_id;
102 }
103
104 public function getName() {
105 return $this->mRow->user_name;
106 }
107
108 public function authenticate( $password ) {
109 # This might be wrong if anyone actually uses the UserComparePasswords hook
110 # (on either end), so don't use this if you those are incompatible.
111 return User::comparePasswords( $this->mRow->user_password, $password,
112 $this->mRow->user_id );
113 }
114
115 public function getPref( $pref ) {
116 # FIXME: Return other prefs too. Lots of global-riddled code that does
117 # this normally.
118 if ( $pref === 'emailaddress'
119 && $this->row->user_email_authenticated !== null ) {
120 return $this->mRow->user_email;
121 }
122 return null;
123 }
124
125 public function getGroups() {
126 # FIXME: Untested.
127 $groups = array();
128 $res = $this->mDb->select(
129 'user_groups',
130 'ug_group',
131 array( 'ug_user' => $this->mRow->user_id ),
132 __METHOD__
133 );
134 foreach ( $res as $row ) {
135 $groups[] = $row->ug_group;
136 }
137 return $groups;
138 }
139
140 # TODO: Implement setPref().
141 }