]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/PearDb.php
looser isValidName method
[SourceForge/phpwiki.git] / lib / WikiUser / PearDb.php
1 <?php //-*-php-*-
2 rcs_id('$Id: PearDb.php,v 1.7 2005-02-14 12:28:27 rurban Exp $');
3 /* Copyright (C) 2004 ReiniUrban
4  * This file is part of PhpWiki. Terms and Conditions see LICENSE. (GPL2)
5  */
6
7 class _PearDbPassUser
8 extends _DbPassUser
9 /**
10  * Pear DB methods
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.
13  *
14  * @tables: user
15  * @tables: pref
16  */
17 {
18     var $_authmethod = 'PearDb';
19     function _PearDbPassUser($UserName='',$prefs=false) {
20         //global $DBAuthParams;
21         if (!$this->_prefs and isa($this,"_PearDbPassUser")) {
22             if ($prefs) $this->_prefs = $prefs;
23         }
24         if (!isset($this->_prefs->_method))
25             _PassUser::_PassUser($UserName);
26         elseif (!$this->isValidName($UserName)) {
27             trigger_error(_("Invalid username."), E_USER_WARNING);
28             return false;
29         }
30         $this->_userid = $UserName;
31         // make use of session data. generally we only initialize this every time, 
32         // but do auth checks only once
33         $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
34         return $this;
35     }
36
37     function getPreferences() {
38         // override the generic slow method here for efficiency and not to 
39         // clutter the homepage metadata with prefs.
40         _AnonUser::getPreferences();
41         $this->getAuthDbh();
42         if (isset($this->_prefs->_select)) {
43             $dbh = &$this->_auth_dbi;
44             $db_result = $dbh->query(sprintf($this->_prefs->_select, $dbh->quote($this->_userid)));
45             // patched by frederik@pandora.be
46             $prefs = $db_result->fetchRow();
47             $prefs_blob = @$prefs["prefs"]; 
48             if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
49                 $updated = $this->_prefs->updatePrefs($restored_from_db);
50                 //$this->_prefs = new UserPreferences($restored_from_db);
51                 return $this->_prefs;
52             }
53         }
54         if ($this->_HomePagehandle) {
55             if ($restored_from_page = $this->_prefs->retrieve
56                 ($this->_HomePagehandle->get('pref'))) {
57                 $updated = $this->_prefs->updatePrefs($restored_from_page);
58                 //$this->_prefs = new UserPreferences($restored_from_page);
59                 return $this->_prefs;
60             }
61         }
62         return $this->_prefs;
63     }
64
65     function setPreferences($prefs, $id_only=false) {
66         // if the prefs are changed
67         if ($count = _AnonUser::setPreferences($prefs, 1)) {
68             //global $request;
69             //$user = $request->_user;
70             //unset($user->_auth_dbi);
71             // this must be done in $request->_setUser, not here!
72             //$request->setSessionVar('wiki_user', $user);
73             $this->getAuthDbh();
74             $packed = $this->_prefs->store();
75             if (!$id_only and isset($this->_prefs->_update)) {
76                 $dbh = &$this->_auth_dbi;
77                 $dbh->simpleQuery(sprintf($this->_prefs->_update,
78                                           $dbh->quote($packed),
79                                           $dbh->quote($this->_userid)));
80                 //delete pageprefs:
81                 if ($this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
82                     $this->_HomePagehandle->set('pref', '');
83             } else {
84                 //store prefs in homepage, not in cookie
85                 if ($this->_HomePagehandle and !$id_only)
86                     $this->_HomePagehandle->set('pref', $packed);
87             }
88             return $count; //count($this->_prefs->unpack($packed));
89         }
90         return 0;
91     }
92
93     function userExists() {
94         //global $DBAuthParams;
95         $this->getAuthDbh();
96         $dbh = &$this->_auth_dbi;
97         if (!$dbh) { // needed?
98             return $this->_tryNextUser();
99         }
100         if (!$this->isValidName()) {
101             trigger_error(_("Invalid username."),E_USER_WARNING);
102             return $this->_tryNextUser();
103         }
104         $dbi =& $GLOBALS['request']->_dbi;
105         // Prepare the configured auth statements
106         if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
107             $this->_authselect = $this->prepare($dbi->getAuthParam('auth_check'), 
108                                                 array("userid", "password"));
109         }
110         if (empty($this->_authselect))
111             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
112                               'DBAUTH_AUTH_CHECK', 'SQL'),
113                           E_USER_WARNING);
114         //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
115         if ($this->_auth_crypt_method == 'crypt') {
116             $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
117             if ($rs->numRows())
118                 return true;
119         }
120         else {
121             if (! $dbi->getAuthParam('auth_user_exists'))
122                 trigger_error(fmt("%s is missing",'DBAUTH_AUTH_USER_EXISTS'),
123                               E_USER_WARNING);
124             $this->_authcheck = $this->prepare($dbi->getAuthParam('auth_user_exists'),"userid");
125             $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
126             if ($rs->numRows())
127                 return true;
128         }
129         // maybe the user is allowed to create himself. Generally not wanted in 
130         // external databases, but maybe wanted for the wiki database, for performance 
131         // reasons
132         if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
133             $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
134                                                 array("userid", "password"));
135         }
136         if (!empty($this->_authcreate) and isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd'])) {
137             $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
138             $dbh->simpleQuery(sprintf($this->_authcreate,
139                                       $dbh->quote($passwd),
140                                       $dbh->quote($this->_userid)
141                                       ));
142             return true;
143         }
144         return $this->_tryNextUser();
145     }
146  
147     function checkPass($submitted_password) {
148         //global $DBAuthParams;
149         $this->getAuthDbh();
150         if (!$this->_auth_dbi) {  // needed?
151             return $this->_tryNextPass($submitted_password);
152         }
153         if (!$this->isValidName()) {
154             return $this->_tryNextPass($submitted_password);
155         }
156         if (!$this->_checkPassLength($submitted_password)) {
157             return WIKIAUTH_FORBIDDEN;
158         }
159         if (!isset($this->_authselect))
160             $this->userExists();
161         if (!isset($this->_authselect))
162             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
163                               'DBAUTH_AUTH_CHECK', 'SQL'),
164                           E_USER_WARNING);
165
166         //NOTE: for auth_crypt_method='crypt'  defined('ENCRYPTED_PASSWD',true) must be set
167         $dbh = &$this->_auth_dbi;
168         if ($this->_auth_crypt_method == 'crypt') {
169             $stored_password = $dbh->getOne(sprintf($this->_authselect, 
170                                                     $dbh->quote($this->_userid)));
171             $result = $this->_checkPass($submitted_password, $stored_password);
172         } else {
173             $okay = $dbh->getOne(sprintf($this->_authselect,
174                                          $dbh->quote($submitted_password),
175                                          $dbh->quote($this->_userid)));
176             $result = !empty($okay);
177         }
178
179         if ($result) {
180             $this->_level = WIKIAUTH_USER;
181             return $this->_level;
182         } elseif (USER_AUTH_POLICY === 'strict') {
183             $this->_level = WIKIAUTH_FORBIDDEN;
184             return $this->_level;
185         } else {
186             return $this->_tryNextPass($submitted_password);
187         }
188     }
189
190     function mayChangePass() {
191         return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
192     }
193
194     function storePass($submitted_password) {
195         if (!$this->isValidName()) {
196             return false;
197         }
198         $this->getAuthDbh();
199         $dbh = &$this->_auth_dbi;
200         $dbi =& $GLOBALS['request']->_dbi;
201         if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
202             $this->_authupdate = $this->prepare($dbi->getAuthParam('auth_update'),
203                                                 array("userid", "password"));
204         }
205         if (empty($this->_authupdate)) {
206             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
207                               'DBAUTH_AUTH_UPDATE','SQL'),
208                           E_USER_WARNING);
209             return false;
210         }
211
212         if ($this->_auth_crypt_method == 'crypt') {
213             if (function_exists('crypt'))
214                 $submitted_password = crypt($submitted_password);
215         }
216         $dbh->simpleQuery(sprintf($this->_authupdate,
217                                   $dbh->quote($submitted_password),
218                                   $dbh->quote($this->_userid)
219                                   ));
220         return true;
221     }
222 }
223
224 // $Log: not supported by cvs2svn $
225 // Revision 1.6  2005/01/06 15:44:22  rurban
226 // move password length checker to correct method. thanks to Charles Corrigan
227 //
228 // Revision 1.5  2004/12/26 17:11:17  rurban
229 // just copyright
230 //
231 // Revision 1.4  2004/12/20 16:05:01  rurban
232 // gettext msg unification
233 //
234 // Revision 1.3  2004/12/19 00:58:02  rurban
235 // Enforce PASSWORD_LENGTH_MINIMUM in almost all PassUser checks,
236 // Provide an errormessage if so. Just PersonalPage and BogoLogin not.
237 // Simplify httpauth logout handling and set sessions for all methods.
238 // fix main.php unknown index "x" getLevelDescription() warning.
239 //
240 // Revision 1.2  2004/11/10 15:29:21  rurban
241 // * requires newer Pear_DB (as the internal one): quote() uses now escapeSimple for strings
242 // * ACCESS_LOG_SQL: fix cause request not yet initialized
243 // * WikiDB: moved SQL specific methods upwards
244 // * new Pear_DB quoting: same as ADODB and as newer Pear_DB.
245 //   fixes all around: WikiGroup, WikiUserNew SQL methods, SQL logging
246 //
247 // Revision 1.1  2004/11/01 10:43:58  rurban
248 // seperate PassUser methods into seperate dir (memory usage)
249 // fix WikiUser (old) overlarge data session
250 // remove wikidb arg from various page class methods, use global ->_dbi instead
251 // ...
252 //
253
254 // Local Variables:
255 // mode: php
256 // tab-width: 8
257 // c-basic-offset: 4
258 // c-hanging-comment-ender-p: nil
259 // indent-tabs-mode: nil
260 // End:
261 ?>