2 rcs_id('$Id: PearDb.php,v 1.10 2006-03-19 16:26:40 rurban Exp $');
3 /* Copyright (C) 2004 ReiniUrban
4 * This file is part of PhpWiki. Terms and Conditions see LICENSE. (GPL2)
11 * Now optimized not to use prepare, ...query(sprintf($sql,quote())) instead.
12 * We use FETCH_MODE_ROW, so we don't need aliases in the auth_* SQL statements.
17 var $_authmethod = 'PearDb';
18 function _PearDbPassUser($UserName='',$prefs=false) {
19 //global $DBAuthParams;
20 if (!$this->_prefs and isa($this,"_PearDbPassUser")) {
21 if ($prefs) $this->_prefs = $prefs;
23 if (!isset($this->_prefs->_method))
24 _PassUser::_PassUser($UserName);
25 elseif (!$this->isValidName($UserName)) {
26 trigger_error(_("Invalid username."), E_USER_WARNING);
29 $this->_userid = $UserName;
30 // make use of session data. generally we only initialize this every time,
31 // but do auth checks only once
32 $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
36 function getPreferences() {
37 // override the generic slow method here for efficiency and not to
38 // clutter the homepage metadata with prefs.
39 _AnonUser::getPreferences();
41 if (isset($this->_prefs->_select)) {
42 $dbh = &$this->_auth_dbi;
43 $db_result = $dbh->query(sprintf($this->_prefs->_select, $dbh->quote($this->_userid)));
44 // patched by frederik@pandora.be
45 $prefs = $db_result->fetchRow();
46 $prefs_blob = @$prefs["prefs"];
47 if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
48 $updated = $this->_prefs->updatePrefs($restored_from_db);
49 //$this->_prefs = new UserPreferences($restored_from_db);
53 if ($this->_HomePagehandle) {
54 if ($restored_from_page = $this->_prefs->retrieve
55 ($this->_HomePagehandle->get('pref'))) {
56 $updated = $this->_prefs->updatePrefs($restored_from_page);
57 //$this->_prefs = new UserPreferences($restored_from_page);
64 function setPreferences($prefs, $id_only=false) {
65 // if the prefs are changed
66 if ($count = _AnonUser::setPreferences($prefs, 1)) {
68 //$user = $request->_user;
69 //unset($user->_auth_dbi);
70 // this must be done in $request->_setUser, not here!
71 //$request->setSessionVar('wiki_user', $user);
73 $packed = $this->_prefs->store();
74 if (!$id_only and isset($this->_prefs->_update)) {
75 $dbh = &$this->_auth_dbi;
76 // check if the user already exists (not needed with mysql REPLACE)
77 $db_result = $dbh->query(sprintf($this->_prefs->_select,
78 $dbh->quote($this->_userid)));
79 $prefs = $db_result->fetchRow();
80 $prefs_blob = @$prefs["prefs"];
81 // If there are prefs for the user, update them.
82 if($prefs_blob != "" ){
83 $dbh->simpleQuery(sprintf($this->_prefs->_update,
85 $dbh->quote($this->_userid)));
87 // Otherwise, insert a record for them and set it to the defaults.
88 // johst@deakin.edu.au
89 $dbi = $GLOBALS['request']->getDbh();
90 $this->_prefs->_insert = $this->prepare($dbi->getAuthParam('pref_insert'),
91 array("pref_blob", "userid"));
92 $dbh->simpleQuery(sprintf($this->_prefs->_insert,
93 $dbh->quote($packed), $dbh->quote($this->_userid)));
96 if ($this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
97 $this->_HomePagehandle->set('pref', '');
99 //store prefs in homepage, not in cookie
100 if ($this->_HomePagehandle and !$id_only)
101 $this->_HomePagehandle->set('pref', $packed);
103 return $count; //count($this->_prefs->unpack($packed));
108 function userExists() {
109 //global $DBAuthParams;
111 $dbh = &$this->_auth_dbi;
112 if (!$dbh) { // needed?
113 return $this->_tryNextUser();
115 if (!$this->isValidName()) {
116 trigger_error(_("Invalid username."),E_USER_WARNING);
117 return $this->_tryNextUser();
119 $dbi =& $GLOBALS['request']->_dbi;
120 // Prepare the configured auth statements
121 if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
122 $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'),
123 array("password", "userid"));
125 //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
126 if (!$dbi->getAuthParam('auth_user_exists')
127 and $this->_auth_crypt_method == 'crypt'
128 and $this->_authselect)
130 $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
135 if (! $dbi->getAuthParam('auth_user_exists'))
136 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
138 $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'), "userid");
139 $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
143 // User does not exist yet.
144 // Maybe the user is allowed to create himself. Generally not wanted in
145 // external databases, but maybe wanted for the wiki database, for performance
147 if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
148 $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
149 array("password", "userid"));
151 if (!empty($this->_authcreate) and
152 isset($GLOBALS['HTTP_POST_VARS']['auth']) and
153 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd']))
155 $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
156 $dbh->simpleQuery(sprintf($this->_authcreate,
157 $dbh->quote($passwd),
158 $dbh->quote($this->_userid)));
161 return $this->_tryNextUser();
164 function checkPass($submitted_password) {
165 //global $DBAuthParams;
167 if (!$this->_auth_dbi) { // needed?
168 return $this->_tryNextPass($submitted_password);
170 if (!$this->isValidName()) {
171 return $this->_tryNextPass($submitted_password);
173 if (!$this->_checkPassLength($submitted_password)) {
174 return WIKIAUTH_FORBIDDEN;
176 if (!isset($this->_authselect))
178 if (!isset($this->_authselect))
179 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
180 'DBAUTH_AUTH_CHECK', 'SQL'),
183 //NOTE: for auth_crypt_method='crypt' defined('ENCRYPTED_PASSWD',true) must be set
184 $dbh = &$this->_auth_dbi;
185 if ($this->_auth_crypt_method == 'crypt') {
186 $stored_password = $dbh->getOne(sprintf($this->_authselect, $dbh->quote($this->_userid)));
187 $result = $this->_checkPass($submitted_password, $stored_password);
189 // be position independent
190 $okay = $dbh->getOne(sprintf($this->_authselect,
191 $dbh->quote($submitted_password),
192 $dbh->quote($this->_userid)));
193 $result = !empty($okay);
197 $this->_level = WIKIAUTH_USER;
198 return $this->_level;
199 } elseif (USER_AUTH_POLICY === 'strict') {
200 $this->_level = WIKIAUTH_FORBIDDEN;
201 return $this->_level;
203 return $this->_tryNextPass($submitted_password);
207 function mayChangePass() {
208 return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
211 function storePass($submitted_password) {
212 if (!$this->isValidName()) {
216 $dbh = &$this->_auth_dbi;
217 $dbi =& $GLOBALS['request']->_dbi;
218 if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
219 $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
220 array("password", "userid"));
222 if (empty($this->_authupdate)) {
223 trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
224 'DBAUTH_AUTH_UPDATE','SQL'),
229 if ($this->_auth_crypt_method == 'crypt') {
230 if (function_exists('crypt'))
231 $submitted_password = crypt($submitted_password);
233 $dbh->simpleQuery(sprintf($this->_authupdate,
234 $dbh->quote($submitted_password), $dbh->quote($this->_userid)));
239 // $Log: not supported by cvs2svn $
240 // Revision 1.9 2005/10/10 19:43:49 rurban
241 // add DBAUTH_PREF_INSERT: self-creating users. by John Stevens
243 // Revision 1.8 2005/08/06 13:21:09 rurban
244 // switch to natural order password, userid
246 // Revision 1.7 2005/02/14 12:28:27 rurban
247 // fix policy strict. Thanks to Mikhail Vladimirov
249 // Revision 1.6 2005/01/06 15:44:22 rurban
250 // move password length checker to correct method. thanks to Charles Corrigan
252 // Revision 1.5 2004/12/26 17:11:17 rurban
255 // Revision 1.4 2004/12/20 16:05:01 rurban
256 // gettext msg unification
258 // Revision 1.3 2004/12/19 00:58:02 rurban
259 // Enforce PASSWORD_LENGTH_MINIMUM in almost all PassUser checks,
260 // Provide an errormessage if so. Just PersonalPage and BogoLogin not.
261 // Simplify httpauth logout handling and set sessions for all methods.
262 // fix main.php unknown index "x" getLevelDescription() warning.
264 // Revision 1.2 2004/11/10 15:29:21 rurban
265 // * requires newer Pear_DB (as the internal one): quote() uses now escapeSimple for strings
266 // * ACCESS_LOG_SQL: fix cause request not yet initialized
267 // * WikiDB: moved SQL specific methods upwards
268 // * new Pear_DB quoting: same as ADODB and as newer Pear_DB.
269 // fixes all around: WikiGroup, WikiUserNew SQL methods, SQL logging
271 // Revision 1.1 2004/11/01 10:43:58 rurban
272 // seperate PassUser methods into seperate dir (memory usage)
273 // fix WikiUser (old) overlarge data session
274 // remove wikidb arg from various page class methods, use global ->_dbi instead
282 // c-hanging-comment-ender-p: nil
283 // indent-tabs-mode: nil