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';
35 function _PearDbPassUser($UserName='',$prefs=false) {
36 //global $DBAuthParams;
37 if (!$this->_prefs and isa($this,"_PearDbPassUser")) {
38 if ($prefs) $this->_prefs = $prefs;
40 if (!isset($this->_prefs->_method))
41 _PassUser::_PassUser($UserName);
42 elseif (!$this->isValidName($UserName)) {
43 trigger_error(_("Invalid username."), E_USER_WARNING);
46 $this->_userid = $UserName;
47 // make use of session data. generally we only initialize this every time,
48 // but do auth checks only once
49 $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
53 function getPreferences() {
54 // override the generic slow method here for efficiency and not to
55 // clutter the homepage metadata with prefs.
56 _AnonUser::getPreferences();
58 if (isset($this->_prefs->_select)) {
59 $dbh = &$this->_auth_dbi;
60 $db_result = $dbh->query(sprintf($this->_prefs->_select, $dbh->quote($this->_userid)));
61 // patched by frederik@pandora.be
62 $prefs = $db_result->fetchRow();
63 $prefs_blob = @$prefs["prefs"];
64 if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
65 $updated = $this->_prefs->updatePrefs($restored_from_db);
66 //$this->_prefs = new UserPreferences($restored_from_db);
70 if (isset($this->_HomePagehandle) && $this->_HomePagehandle) {
71 if ($restored_from_page = $this->_prefs->retrieve
72 ($this->_HomePagehandle->get('pref'))) {
73 $updated = $this->_prefs->updatePrefs($restored_from_page);
74 //$this->_prefs = new UserPreferences($restored_from_page);
81 function setPreferences($prefs, $id_only=false) {
82 // if the prefs are changed
83 if ($count = _AnonUser::setPreferences($prefs, 1)) {
85 //$user = $request->_user;
86 //unset($user->_auth_dbi);
87 // this must be done in $request->_setUser, not here!
88 //$request->setSessionVar('wiki_user', $user);
90 $packed = $this->_prefs->store();
91 if (!$id_only and isset($this->_prefs->_update)) {
92 $dbh = &$this->_auth_dbi;
93 // check if the user already exists (not needed with mysql REPLACE)
94 $db_result = $dbh->query(sprintf($this->_prefs->_select,
95 $dbh->quote($this->_userid)));
96 $prefs = $db_result->fetchRow();
97 $prefs_blob = @$prefs["prefs"];
98 // If there are prefs for the user, update them.
99 if($prefs_blob != "" ){
100 $dbh->simpleQuery(sprintf($this->_prefs->_update,
101 $dbh->quote($packed),
102 $dbh->quote($this->_userid)));
104 // Otherwise, insert a record for them and set it to the defaults.
105 // johst@deakin.edu.au
106 $dbi = $GLOBALS['request']->getDbh();
107 $this->_prefs->_insert = $this->prepare($dbi->getAuthParam('pref_insert'),
108 array("pref_blob", "userid"));
109 $dbh->simpleQuery(sprintf($this->_prefs->_insert,
110 $dbh->quote($packed), $dbh->quote($this->_userid)));
113 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
114 $this->_HomePagehandle->set('pref', '');
116 //store prefs in homepage, not in cookie
117 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and !$id_only)
118 $this->_HomePagehandle->set('pref', $packed);
120 return $count; //count($this->_prefs->unpack($packed));
125 function userExists() {
126 //global $DBAuthParams;
128 $dbh = &$this->_auth_dbi;
129 if (!$dbh) { // needed?
130 return $this->_tryNextUser();
132 if (!$this->isValidName()) {
133 trigger_error(_("Invalid username."),E_USER_WARNING);
134 return $this->_tryNextUser();
136 $dbi =& $GLOBALS['request']->_dbi;
137 // Prepare the configured auth statements
138 if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
139 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
140 array("password", "userid"));
142 //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
143 if (!$dbi->getAuthParam('auth_user_exists')
144 and $this->_auth_crypt_method == 'crypt'
145 and $this->_authselect)
147 $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
152 if (! $dbi->getAuthParam('auth_user_exists'))
153 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
155 $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'), "userid");
156 $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
160 // User does not exist yet.
161 // Maybe the user is allowed to create himself. Generally not wanted in
162 // external databases, but maybe wanted for the wiki database, for performance
164 if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
165 $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
166 array("password", "userid"));
168 if (!empty($this->_authcreate) and
169 isset($GLOBALS['HTTP_POST_VARS']['auth']) and
170 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd']))
172 $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
173 $dbh->simpleQuery(sprintf($this->_authcreate,
174 $dbh->quote($passwd),
175 $dbh->quote($this->_userid)));
178 return $this->_tryNextUser();
181 function checkPass($submitted_password) {
182 //global $DBAuthParams;
184 if (!$this->_auth_dbi) { // needed?
185 return $this->_tryNextPass($submitted_password);
187 if (!$this->isValidName()) {
188 return $this->_tryNextPass($submitted_password);
190 if (!$this->_checkPassLength($submitted_password)) {
191 return WIKIAUTH_FORBIDDEN;
193 if (!isset($this->_authselect))
195 if (!isset($this->_authselect))
196 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
197 'DBAUTH_AUTH_CHECK', 'SQL'),
200 //NOTE: for auth_crypt_method='crypt' defined('ENCRYPTED_PASSWD',true) must be set
201 $dbh = &$this->_auth_dbi;
202 if ($this->_auth_crypt_method == 'crypt') {
203 $stored_password = $dbh->getOne(sprintf($this->_authselect, $dbh->quote($this->_userid)));
204 $result = $this->_checkPass($submitted_password, $stored_password);
206 // be position independent
207 $okay = $dbh->getOne(sprintf($this->_authselect,
208 $dbh->quote($submitted_password),
209 $dbh->quote($this->_userid)));
210 $result = !empty($okay);
214 $this->_level = WIKIAUTH_USER;
215 return $this->_level;
216 } elseif (USER_AUTH_POLICY === 'strict') {
217 $this->_level = WIKIAUTH_FORBIDDEN;
218 return $this->_level;
220 return $this->_tryNextPass($submitted_password);
224 function mayChangePass() {
225 return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
228 function storePass($submitted_password) {
229 if (!$this->isValidName()) {
233 $dbh = &$this->_auth_dbi;
234 $dbi =& $GLOBALS['request']->_dbi;
235 if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
236 $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
237 array("password", "userid"));
239 if (empty($this->_authupdate)) {
240 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
241 'DBAUTH_AUTH_UPDATE','SQL'),
246 if ($this->_auth_crypt_method == 'crypt') {
247 if (function_exists('crypt'))
248 $submitted_password = crypt($submitted_password);
250 $dbh->simpleQuery(sprintf($this->_authupdate,
251 $dbh->quote($submitted_password), $dbh->quote($this->_userid)));
260 // c-hanging-comment-ender-p: nil
261 // indent-tabs-mode: nil