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 $this->_prefs->updatePrefs($restored_from_db);
72 if (isset($this->_HomePagehandle) && $this->_HomePagehandle) {
73 if ($restored_from_page = $this->_prefs->retrieve
74 ($this->_HomePagehandle->get('pref'))
76 $this->_prefs->updatePrefs($restored_from_page);
83 function setPreferences($prefs, $id_only = false)
85 // if the prefs are changed
86 if ($count = _AnonUser::setPreferences($prefs, 1)) {
88 //$user = $request->_user;
89 //unset($user->_auth_dbi);
90 // this must be done in $request->_setUser, not here!
91 //$request->setSessionVar('wiki_user', $user);
93 $packed = $this->_prefs->store();
94 if (!$id_only and isset($this->_prefs->_update)) {
95 $dbh = &$this->_auth_dbi;
96 // check if the user already exists (not needed with mysql REPLACE)
97 $db_result = $dbh->query(sprintf($this->_prefs->_select,
98 $dbh->quote($this->_userid)));
99 $prefs = $db_result->fetchRow();
100 $prefs_blob = @$prefs["prefs"];
101 // If there are prefs for the user, update them.
102 if ($prefs_blob != "") {
103 $dbh->simpleQuery(sprintf($this->_prefs->_update,
104 $dbh->quote($packed),
105 $dbh->quote($this->_userid)));
107 // Otherwise, insert a record for them and set it to the defaults.
108 // johst@deakin.edu.au
109 $dbi = $GLOBALS['request']->getDbh();
110 $this->_prefs->_insert = $this->prepare($dbi->getAuthParam('pref_insert'),
111 array("pref_blob", "userid"));
112 $dbh->simpleQuery(sprintf($this->_prefs->_insert,
113 $dbh->quote($packed), $dbh->quote($this->_userid)));
116 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
117 $this->_HomePagehandle->set('pref', '');
119 //store prefs in homepage, not in cookie
120 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and !$id_only)
121 $this->_HomePagehandle->set('pref', $packed);
123 return $count; //count($this->_prefs->unpack($packed));
128 function userExists()
130 //global $DBAuthParams;
132 $dbh = &$this->_auth_dbi;
133 if (!$dbh) { // needed?
134 return $this->_tryNextUser();
136 if (!$this->isValidName()) {
137 trigger_error(_("Invalid username."), E_USER_WARNING);
138 return $this->_tryNextUser();
140 $dbi =& $GLOBALS['request']->_dbi;
141 // Prepare the configured auth statements
142 if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
143 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
144 array("password", "userid"));
146 //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
147 if (!$dbi->getAuthParam('auth_user_exists')
148 and $this->_auth_crypt_method == 'crypt'
149 and $this->_authselect
151 $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
155 if (!$dbi->getAuthParam('auth_user_exists'))
156 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
158 $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'), "userid");
159 $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
163 // User does not exist yet.
164 // Maybe the user is allowed to create himself. Generally not wanted in
165 // external databases, but maybe wanted for the wiki database, for performance
167 if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
168 $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
169 array("password", "userid"));
171 if (!empty($this->_authcreate) and
172 isset($GLOBALS['HTTP_POST_VARS']['auth']) and
173 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd'])
175 $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
176 $dbh->simpleQuery(sprintf($this->_authcreate,
177 $dbh->quote($passwd),
178 $dbh->quote($this->_userid)));
181 return $this->_tryNextUser();
184 function checkPass($submitted_password)
186 //global $DBAuthParams;
188 if (!$this->_auth_dbi) { // needed?
189 return $this->_tryNextPass($submitted_password);
191 if (!$this->isValidName()) {
192 return $this->_tryNextPass($submitted_password);
194 if (!$this->_checkPassLength($submitted_password)) {
195 return WIKIAUTH_FORBIDDEN;
197 if (!isset($this->_authselect))
199 if (!isset($this->_authselect))
200 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != ā%sā",
201 'DBAUTH_AUTH_CHECK', 'SQL'),
204 //NOTE: for auth_crypt_method='crypt' defined('ENCRYPTED_PASSWD',true) must be set
205 $dbh = &$this->_auth_dbi;
206 if ($this->_auth_crypt_method == 'crypt') {
207 $stored_password = $dbh->getOne(sprintf($this->_authselect, $dbh->quote($this->_userid)));
208 $result = $this->_checkPass($submitted_password, $stored_password);
210 // be position independent
211 $okay = $dbh->getOne(sprintf($this->_authselect,
212 $dbh->quote($submitted_password),
213 $dbh->quote($this->_userid)));
214 $result = !empty($okay);
218 $this->_level = WIKIAUTH_USER;
219 return $this->_level;
220 } elseif (USER_AUTH_POLICY === 'strict') {
221 $this->_level = WIKIAUTH_FORBIDDEN;
222 return $this->_level;
224 return $this->_tryNextPass($submitted_password);
228 function mayChangePass()
230 return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
233 function storePass($submitted_password)
235 if (!$this->isValidName()) {
239 $dbh = &$this->_auth_dbi;
240 $dbi =& $GLOBALS['request']->_dbi;
241 if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
242 $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
243 array("password", "userid"));
245 if (empty($this->_authupdate)) {
246 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != ā%sā",
247 'DBAUTH_AUTH_UPDATE', 'SQL'),
252 if ($this->_auth_crypt_method == 'crypt') {
253 if (function_exists('crypt'))
254 $submitted_password = crypt($submitted_password);
256 $dbh->simpleQuery(sprintf($this->_authupdate,
257 $dbh->quote($submitted_password), $dbh->quote($this->_userid)));
266 // c-hanging-comment-ender-p: nil
267 // indent-tabs-mode: nil