4 * Copyright (C) 2004 ReiniUrban
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 include_once 'lib/WikiUser/Db.php';
29 * Simple sprintf, no prepare.
31 * Warning: Since we use FETCH_MODE_ASSOC (string hash) and not the also faster
32 * FETCH_MODE_ROW (numeric), we have to use the correct aliases in auth_* sql statements!
34 * TODO: Change FETCH_MODE in adodb WikiDB sublasses.
39 var $_authmethod = 'AdoDb';
41 function _AdoDbPassUser($UserName = '', $prefs = false)
43 if (!$this->_prefs and isa($this, "_AdoDbPassUser")) {
44 if ($prefs) $this->_prefs = $prefs;
45 if (!isset($this->_prefs->_method))
46 _PassUser::_PassUser($UserName);
48 if (!$this->isValidName($UserName)) {
49 trigger_error(_("Invalid username."), E_USER_WARNING);
52 $this->_userid = $UserName;
54 $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
55 // Don't prepare the configured auth statements anymore
59 function getPreferences()
61 // override the generic slow method here for efficiency
62 _AnonUser::getPreferences();
64 if (isset($this->_prefs->_select)) {
65 $dbh = & $this->_auth_dbi;
66 $rs = $dbh->Execute(sprintf($this->_prefs->_select, $dbh->qstr($this->_userid)));
70 $prefs_blob = @$rs->fields['prefs'];
72 if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
73 $updated = $this->_prefs->updatePrefs($restored_from_db);
74 //$this->_prefs = new UserPreferences($restored_from_db);
79 if (!empty($this->_HomePagehandle)) {
80 if ($restored_from_page = $this->_prefs->retrieve
81 ($this->_HomePagehandle->get('pref'))
83 $updated = $this->_prefs->updatePrefs($restored_from_page);
84 //$this->_prefs = new UserPreferences($restored_from_page);
91 function setPreferences($prefs, $id_only = false)
93 // if the prefs are changed
94 if (_AnonUser::setPreferences($prefs, 1)) {
96 $packed = $this->_prefs->store();
97 //$user = $request->_user;
98 //unset($user->_auth_dbi);
99 if (!$id_only and isset($this->_prefs->_update)) {
101 $dbh = &$this->_auth_dbi;
102 // check if the user already exists (not needed with mysql REPLACE)
103 $rs = $dbh->Execute(sprintf($this->_prefs->_select, $dbh->qstr($this->_userid)));
108 $prefs_blob = @$rs->fields['prefs'];
112 $db_result = $dbh->Execute(sprintf($this->_prefs->_update,
114 $dbh->qstr($this->_userid)));
116 // Otherwise, insert a record for them and set it to the defaults.
117 $dbi = $request->getDbh();
118 $this->_prefs->_insert = $this->prepare($dbi->getAuthParam('pref_insert'),
119 array("pref_blob", "userid"));
120 $db_result = $dbh->Execute(sprintf($this->_prefs->_insert,
122 $dbh->qstr($this->_userid)));
126 if ($this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
127 $this->_HomePagehandle->set('pref', '');
129 //store prefs in homepage, not in cookie
130 if ($this->_HomePagehandle and !$id_only)
131 $this->_HomePagehandle->set('pref', $packed);
133 return count($this->_prefs->unpack($packed));
138 function userExists()
141 $dbh = &$this->_auth_dbi;
142 if (!$dbh) { // needed?
143 return $this->_tryNextUser();
145 if (!$this->isValidName()) {
146 return $this->_tryNextUser();
148 $dbi =& $GLOBALS['request']->_dbi;
149 // Prepare the configured auth statements
150 if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
151 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
152 array("password", "userid"));
154 //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
155 if (!$dbi->getAuthParam('auth_user_exists')
156 and $this->_auth_crypt_method == 'crypt'
157 and $this->_authselect
159 $rs = $dbh->Execute(sprintf($this->_authselect, $dbh->qstr($this->_userid)));
167 if (!$dbi->getAuthParam('auth_user_exists'))
168 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
170 $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'),
172 $rs = $dbh->Execute(sprintf($this->_authcheck, $dbh->qstr($this->_userid)));
180 // User does not exist yet.
181 // Maybe the user is allowed to create himself. Generally not wanted in
182 // external databases, but maybe wanted for the wiki database, for performance
184 if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
185 $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
186 array("password", "userid"));
188 if (!empty($this->_authcreate) and
189 isset($GLOBALS['HTTP_POST_VARS']['auth']) and
190 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd'])
192 $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
193 $dbh->Execute(sprintf($this->_authcreate,
195 $dbh->qstr($this->_userid)));
199 return $this->_tryNextUser();
202 function checkPass($submitted_password)
204 //global $DBAuthParams;
206 if (!$this->_auth_dbi) { // needed?
207 return $this->_tryNextPass($submitted_password);
209 if (!$this->isValidName()) {
210 trigger_error(_("Invalid username."), E_USER_WARNING);
211 return $this->_tryNextPass($submitted_password);
213 if (!$this->_checkPassLength($submitted_password)) {
214 return WIKIAUTH_FORBIDDEN;
216 $dbh =& $this->_auth_dbi;
217 $dbi =& $GLOBALS['request']->_dbi;
218 if (empty($this->_authselect) and $dbi->getAuthParam('auth_check')) {
219 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
220 array("password", "userid"));
222 if (!isset($this->_authselect))
224 if (!isset($this->_authselect))
225 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
226 'DBAUTH_AUTH_CHECK', 'ADODB'),
228 //NOTE: for auth_crypt_method='crypt' defined('ENCRYPTED_PASSWD',true) must be set
229 if ($this->_auth_crypt_method == 'crypt') {
230 $rs = $dbh->Execute(sprintf($this->_authselect,
231 $dbh->qstr($this->_userid)));
233 $stored_password = $rs->fields['password'];
235 $result = $this->_checkPass($submitted_password, $stored_password);
241 $rs = $dbh->Execute(sprintf($this->_authselect,
242 $dbh->qstr($submitted_password),
243 $dbh->qstr($this->_userid)));
244 if (isset($rs->fields['ok']))
245 $okay = $rs->fields['ok'];
246 elseif (isset($rs->fields[0]))
247 $okay = $rs->fields[0]; else {
248 if (is_array($rs->fields))
249 $okay = reset($rs->fields);
254 $result = !empty($okay);
258 $this->_level = WIKIAUTH_USER;
259 return $this->_level;
260 } elseif (USER_AUTH_POLICY === 'strict') {
261 $this->_level = WIKIAUTH_FORBIDDEN;
262 return $this->_level;
264 return $this->_tryNextPass($submitted_password);
268 function mayChangePass()
270 return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
273 function storePass($submitted_password)
276 $dbh = &$this->_auth_dbi;
277 $dbi =& $GLOBALS['request']->_dbi;
278 if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
279 $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
280 array("password", "userid"));
282 if (!isset($this->_authupdate)) {
283 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
284 'DBAUTH_AUTH_UPDATE', 'ADODB'),
289 if ($this->_auth_crypt_method == 'crypt') {
290 if (function_exists('crypt'))
291 $submitted_password = crypt($submitted_password);
293 $rs = $dbh->Execute(sprintf($this->_authupdate,
294 $dbh->qstr($submitted_password),
295 $dbh->qstr($this->_userid)
306 // c-hanging-comment-ender-p: nil
307 // indent-tabs-mode: nil