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.
22 include_once 'lib/WikiUser/Db.php';
28 * Now optimized not to use prepare, ...query(sprintf($sql,quote())) instead.
29 * We use FETCH_MODE_ROW, so we don't need aliases in the auth_* SQL statements.
34 var $_authmethod = 'PearDb';
36 function _PearDbPassUser($UserName = '', $prefs = false)
38 //global $DBAuthParams;
39 if (!$this->_prefs and isa($this, "_PearDbPassUser")) {
40 if ($prefs) $this->_prefs = $prefs;
42 if (!isset($this->_prefs->_method))
43 _PassUser::_PassUser($UserName);
44 elseif (!$this->isValidName($UserName)) {
45 trigger_error(_("Invalid username."), E_USER_WARNING);
48 $this->_userid = $UserName;
49 // make use of session data. generally we only initialize this every time,
50 // but do auth checks only once
51 $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
55 function getPreferences()
57 // override the generic slow method here for efficiency and not to
58 // clutter the homepage metadata with prefs.
59 _AnonUser::getPreferences();
61 if (isset($this->_prefs->_select)) {
62 $dbh = &$this->_auth_dbi;
63 $db_result = $dbh->query(sprintf($this->_prefs->_select, $dbh->quote($this->_userid)));
64 // patched by frederik@pandora.be
65 $prefs = $db_result->fetchRow();
66 $prefs_blob = @$prefs["prefs"];
67 if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
68 $updated = $this->_prefs->updatePrefs($restored_from_db);
69 //$this->_prefs = new UserPreferences($restored_from_db);
73 if (isset($this->_HomePagehandle) && $this->_HomePagehandle) {
74 if ($restored_from_page = $this->_prefs->retrieve
75 ($this->_HomePagehandle->get('pref'))
77 $updated = $this->_prefs->updatePrefs($restored_from_page);
78 //$this->_prefs = new UserPreferences($restored_from_page);
85 function setPreferences($prefs, $id_only = false)
87 // if the prefs are changed
88 if ($count = _AnonUser::setPreferences($prefs, 1)) {
90 //$user = $request->_user;
91 //unset($user->_auth_dbi);
92 // this must be done in $request->_setUser, not here!
93 //$request->setSessionVar('wiki_user', $user);
95 $packed = $this->_prefs->store();
96 if (!$id_only and isset($this->_prefs->_update)) {
97 $dbh = &$this->_auth_dbi;
98 // check if the user already exists (not needed with mysql REPLACE)
99 $db_result = $dbh->query(sprintf($this->_prefs->_select,
100 $dbh->quote($this->_userid)));
101 $prefs = $db_result->fetchRow();
102 $prefs_blob = @$prefs["prefs"];
103 // If there are prefs for the user, update them.
104 if ($prefs_blob != "") {
105 $dbh->simpleQuery(sprintf($this->_prefs->_update,
106 $dbh->quote($packed),
107 $dbh->quote($this->_userid)));
109 // Otherwise, insert a record for them and set it to the defaults.
110 // johst@deakin.edu.au
111 $dbi = $GLOBALS['request']->getDbh();
112 $this->_prefs->_insert = $this->prepare($dbi->getAuthParam('pref_insert'),
113 array("pref_blob", "userid"));
114 $dbh->simpleQuery(sprintf($this->_prefs->_insert,
115 $dbh->quote($packed), $dbh->quote($this->_userid)));
118 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
119 $this->_HomePagehandle->set('pref', '');
121 //store prefs in homepage, not in cookie
122 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and !$id_only)
123 $this->_HomePagehandle->set('pref', $packed);
125 return $count; //count($this->_prefs->unpack($packed));
130 function userExists()
132 //global $DBAuthParams;
134 $dbh = &$this->_auth_dbi;
135 if (!$dbh) { // needed?
136 return $this->_tryNextUser();
138 if (!$this->isValidName()) {
139 trigger_error(_("Invalid username."), E_USER_WARNING);
140 return $this->_tryNextUser();
142 $dbi =& $GLOBALS['request']->_dbi;
143 // Prepare the configured auth statements
144 if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
145 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
146 array("password", "userid"));
148 //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
149 if (!$dbi->getAuthParam('auth_user_exists')
150 and $this->_auth_crypt_method == 'crypt'
151 and $this->_authselect
153 $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
157 if (!$dbi->getAuthParam('auth_user_exists'))
158 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
160 $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'), "userid");
161 $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
165 // User does not exist yet.
166 // Maybe the user is allowed to create himself. Generally not wanted in
167 // external databases, but maybe wanted for the wiki database, for performance
169 if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
170 $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
171 array("password", "userid"));
173 if (!empty($this->_authcreate) and
174 isset($GLOBALS['HTTP_POST_VARS']['auth']) and
175 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd'])
177 $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
178 $dbh->simpleQuery(sprintf($this->_authcreate,
179 $dbh->quote($passwd),
180 $dbh->quote($this->_userid)));
183 return $this->_tryNextUser();
186 function checkPass($submitted_password)
188 //global $DBAuthParams;
190 if (!$this->_auth_dbi) { // needed?
191 return $this->_tryNextPass($submitted_password);
193 if (!$this->isValidName()) {
194 return $this->_tryNextPass($submitted_password);
196 if (!$this->_checkPassLength($submitted_password)) {
197 return WIKIAUTH_FORBIDDEN;
199 if (!isset($this->_authselect))
201 if (!isset($this->_authselect))
202 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
203 'DBAUTH_AUTH_CHECK', 'SQL'),
206 //NOTE: for auth_crypt_method='crypt' defined('ENCRYPTED_PASSWD',true) must be set
207 $dbh = &$this->_auth_dbi;
208 if ($this->_auth_crypt_method == 'crypt') {
209 $stored_password = $dbh->getOne(sprintf($this->_authselect, $dbh->quote($this->_userid)));
210 $result = $this->_checkPass($submitted_password, $stored_password);
212 // be position independent
213 $okay = $dbh->getOne(sprintf($this->_authselect,
214 $dbh->quote($submitted_password),
215 $dbh->quote($this->_userid)));
216 $result = !empty($okay);
220 $this->_level = WIKIAUTH_USER;
221 return $this->_level;
222 } elseif (USER_AUTH_POLICY === 'strict') {
223 $this->_level = WIKIAUTH_FORBIDDEN;
224 return $this->_level;
226 return $this->_tryNextPass($submitted_password);
230 function mayChangePass()
232 return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
235 function storePass($submitted_password)
237 if (!$this->isValidName()) {
241 $dbh = &$this->_auth_dbi;
242 $dbi =& $GLOBALS['request']->_dbi;
243 if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
244 $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
245 array("password", "userid"));
247 if (empty($this->_authupdate)) {
248 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
249 'DBAUTH_AUTH_UPDATE', 'SQL'),
254 if ($this->_auth_crypt_method == 'crypt') {
255 if (function_exists('crypt'))
256 $submitted_password = crypt($submitted_password);
258 $dbh->simpleQuery(sprintf($this->_authupdate,
259 $dbh->quote($submitted_password), $dbh->quote($this->_userid)));
268 // c-hanging-comment-ender-p: nil
269 // indent-tabs-mode: nil