]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/PdoDb.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiUser / PdoDb.php
1 <?php
2
3 /*
4  * Copyright (C) 2004, 2005 ReiniUrban
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 include_once 'lib/WikiUser/Db.php';
24
25 class _PdoDbPassUser
26     extends _DbPassUser
27     /**
28      * PDO DB methods (PHP5)
29      *   prepare, bind, execute.
30      * We use numrical FETCH_MODE_ROW, so we don't need aliases in the auth_* SQL statements.
31      *
32      * @tables: user
33      * @tables: pref
34      */
35 {
36     var $_authmethod = 'PDODb';
37
38     function _PdoDbPassUser($UserName = '', $prefs = false)
39     {
40
41         if (!$this->_prefs and isa($this, "_PdoDbPassUser")) {
42             if ($prefs) $this->_prefs = $prefs;
43         }
44         if (!isset($this->_prefs->_method))
45             _PassUser::_PassUser($UserName);
46         elseif (!$this->isValidName($UserName)) {
47             trigger_error(_("Invalid username."), E_USER_WARNING);
48             return false;
49         }
50         $this->_userid = $UserName;
51         // make use of session data. generally we only initialize this every time,
52         // but do auth checks only once
53         $this->_auth_crypt_method = $GLOBALS['request']->_dbi->getAuthParam('auth_crypt_method');
54         return $this;
55     }
56
57     function getPreferences()
58     {
59         // override the generic slow method here for efficiency and not to
60         // clutter the homepage metadata with prefs.
61         _AnonUser::getPreferences();
62         $this->getAuthDbh();
63         if (isset($this->_prefs->_select)) {
64             $dbh =& $this->_auth_dbi;
65             $db_result = $dbh->query(sprintf($this->_prefs->_select, $dbh->quote($this->_userid)));
66             // patched by frederik@pandora.be
67             $prefs = $db_result->fetch(PDO_FETCH_BOTH);
68             $prefs_blob = @$prefs["prefs"];
69             if ($restored_from_db = $this->_prefs->retrieve($prefs_blob)) {
70                 $updated = $this->_prefs->updatePrefs($restored_from_db);
71                 //$this->_prefs = new UserPreferences($restored_from_db);
72                 return $this->_prefs;
73             }
74         }
75         if ($this->_HomePagehandle) {
76             if ($restored_from_page = $this->_prefs->retrieve
77             ($this->_HomePagehandle->get('pref'))
78             ) {
79                 $updated = $this->_prefs->updatePrefs($restored_from_page);
80                 //$this->_prefs = new UserPreferences($restored_from_page);
81                 return $this->_prefs;
82             }
83         }
84         return $this->_prefs;
85     }
86
87     function setPreferences($prefs, $id_only = false)
88     {
89         // if the prefs are changed
90         if ($count = _AnonUser::setPreferences($prefs, 1)) {
91             $this->getAuthDbh();
92             $packed = $this->_prefs->store();
93             if (!$id_only and isset($this->_prefs->_update)) {
94                 $dbh =& $this->_auth_dbi;
95                 try {
96                     $sth = $dbh->prepare($this->_prefs->_update);
97                     $sth->bindParam("prefs", $packed);
98                     $sth->bindParam("user", $this->_userid);
99                     $sth->execute();
100                 } catch (PDOException $e) {
101                     trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
102                     return false;
103                 }
104                 //delete pageprefs:
105                 if ($this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
106                     $this->_HomePagehandle->set('pref', '');
107             } else {
108                 //store prefs in homepage, not in cookie
109                 if ($this->_HomePagehandle and !$id_only)
110                     $this->_HomePagehandle->set('pref', $packed);
111             }
112             return $count;
113         }
114         return 0;
115     }
116
117     function userExists()
118     {
119         $this->getAuthDbh();
120         $dbh = &$this->_auth_dbi;
121         if (!$dbh) { // needed?
122             return $this->_tryNextUser();
123         }
124         if (!$this->isValidName()) {
125             trigger_error(_("Invalid username."), E_USER_WARNING);
126             return $this->_tryNextUser();
127         }
128         $dbi =& $GLOBALS['request']->_dbi;
129         if ($dbi->getAuthParam('auth_check') and empty($this->_authselect)) {
130             try {
131                 $this->_authselect = $dbh->prepare($dbi->getAuthParam('auth_check'));
132             } catch (PDOException $e) {
133                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
134                 return false;
135             }
136         }
137         //NOTE: for auth_crypt_method='crypt' no special auth_user_exists is needed
138         if (!$dbi->getAuthParam('auth_user_exists')
139             and $this->_auth_crypt_method == 'crypt'
140                 and $this->_authselect
141         ) {
142             try {
143                 $this->_authselect->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
144                 $this->_authselect->execute();
145             } catch (PDOException $e) {
146                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
147                 return false;
148             }
149             if ($this->_authselect->fetchSingle())
150                 return true;
151         } else {
152             if (!$dbi->getAuthParam('auth_user_exists'))
153                 trigger_error(fmt("%s is missing", 'DBAUTH_AUTH_USER_EXISTS'),
154                     E_USER_WARNING);
155             $this->_authcheck = $dbh->prepare($dbi->getAuthParam('auth_check'));
156             $this->_authcheck->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
157             $this->_authcheck->execute();
158             if ($this->_authcheck->fetchSingle())
159                 return true;
160         }
161         // User does not exist yet.
162         // Maybe the user is allowed to create himself. Generally not wanted in
163         // external databases, but maybe wanted for the wiki database, for performance
164         // reasons
165         if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
166             try {
167                 $this->_authcreate = $dbh->prepare($dbi->getAuthParam('auth_create'));
168             } catch (PDOException $e) {
169                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
170                 return false;
171             }
172         }
173         if (!empty($this->_authcreate) and
174             isset($GLOBALS['HTTP_POST_VARS']['auth']) and
175                 isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd'])
176         ) {
177             $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
178             try {
179                 $this->_authcreate->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
180                 $this->_authcreate->bindParam("password", $passwd, PDO_PARAM_STR, 48);
181                 $rs = $this->_authselect->execute();
182             } catch (PDOException $e) {
183                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
184                 return false;
185             }
186             if ($rs)
187                 return true;
188         }
189         return $this->_tryNextUser();
190     }
191
192     function checkPass($submitted_password)
193     {
194         //global $DBAuthParams;
195         $this->getAuthDbh();
196         if (!$this->_auth_dbi) { // needed?
197             return $this->_tryNextPass($submitted_password);
198         }
199         if (!$this->isValidName()) {
200             return $this->_tryNextPass($submitted_password);
201         }
202         if (!$this->_checkPassLength($submitted_password)) {
203             return WIKIAUTH_FORBIDDEN;
204         }
205         if (!isset($this->_authselect))
206             $this->userExists();
207         if (!isset($this->_authselect))
208             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
209                     'DBAUTH_AUTH_CHECK', 'SQL'),
210                 E_USER_WARNING);
211
212         //NOTE: for auth_crypt_method='crypt'  defined('ENCRYPTED_PASSWD',true) must be set
213         $dbh = &$this->_auth_dbi;
214         if ($this->_auth_crypt_method == 'crypt') {
215             try {
216                 $this->_authselect->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
217                 $this->_authselect->execute();
218                 $rs = $this->_authselect->fetch(PDO_FETCH_BOTH);
219             } catch (PDOException $e) {
220                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
221                 return false;
222             }
223             $stored_password = @$rs[0];
224             $result = $this->_checkPass($submitted_password, $stored_password);
225         } else {
226             try {
227                 $this->_authselect->bindParam("password", $submitted_password, PDO_PARAM_STR, 48);
228                 $this->_authselect->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
229                 $this->_authselect->execute();
230                 $rs = $this->_authselect->fetch(PDO_FETCH_BOTH);
231             } catch (PDOException $e) {
232                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
233                 return false;
234             }
235             $okay = @$rs[0];
236             $result = !empty($okay);
237         }
238
239         if ($result) {
240             $this->_level = WIKIAUTH_USER;
241             return $this->_level;
242         } elseif (USER_AUTH_POLICY === 'strict') {
243             $this->_level = WIKIAUTH_FORBIDDEN;
244             return $this->_level;
245         } else {
246             return $this->_tryNextPass($submitted_password);
247         }
248     }
249
250     function mayChangePass()
251     {
252         return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
253     }
254
255     function storePass($submitted_password)
256     {
257         if (!$this->isValidName()) {
258             return false;
259         }
260         $this->getAuthDbh();
261         $dbh = &$this->_auth_dbi;
262         $dbi =& $GLOBALS['request']->_dbi;
263         if ($dbi->getAuthParam('auth_update') and empty($this->_authupdate)) {
264             try {
265                 $this->_authupdate = $dbh->prepare($dbi->getAuthParam('auth_update'));
266             } catch (PDOException $e) {
267                 trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
268                 return false;
269             }
270         }
271         if (empty($this->_authupdate)) {
272             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
273                     'DBAUTH_AUTH_UPDATE', 'SQL'),
274                 E_USER_WARNING);
275             return false;
276         }
277
278         if ($this->_auth_crypt_method == 'crypt') {
279             if (function_exists('crypt'))
280                 $submitted_password = crypt($submitted_password);
281         }
282         try {
283             $this->_authupdate->bindParam("password", $submitted_password, PDO_PARAM_STR, 48);
284             $this->_authupdate->bindParam("userid", $this->_userid, PDO_PARAM_STR, 48);
285             $this->_authupdate->execute();
286         } catch (PDOException $e) {
287             trigger_error("SQL Error: " . $e->getMessage(), E_USER_WARNING);
288             return false;
289         }
290         return true;
291     }
292 }
293
294 // Local Variables:
295 // mode: php
296 // tab-width: 8
297 // c-basic-offset: 4
298 // c-hanging-comment-ender-p: nil
299 // indent-tabs-mode: nil
300 // End: