]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/PearDb.php
New FSF address
[SourceForge/phpwiki.git] / lib / WikiUser / PearDb.php
1 <?php //-*-php-*-
2 // $Id$
3 /*
4  * Copyright (C) 2004 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 include_once("lib/WikiUser/Db.php");
23
24 class _PearDbPassUser
25 extends _DbPassUser
26 /**
27  * Pear DB methods
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.
30  *
31  * @tables: pref
32  */
33 {
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;
39         }
40         if (!isset($this->_prefs->_method))
41             _PassUser::_PassUser($UserName);
42         elseif (!$this->isValidName($UserName)) {
43             trigger_error(_("Invalid username."), E_USER_WARNING);
44             return false;
45         }
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');
50         return $this;
51     }
52
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();
57         $this->getAuthDbh();
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);
67                 return $this->_prefs;
68             }
69         }
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);
75                 return $this->_prefs;
76             }
77         }
78         return $this->_prefs;
79     }
80
81     function setPreferences($prefs, $id_only=false) {
82         // if the prefs are changed
83         if ($count = _AnonUser::setPreferences($prefs, 1)) {
84             //global $request;
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);
89             $this->getAuthDbh();
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)));
103                 } else {
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)));
111                 }
112                 //delete pageprefs:
113                 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and $this->_HomePagehandle->get('pref'))
114                     $this->_HomePagehandle->set('pref', '');
115             } else {
116                 //store prefs in homepage, not in cookie
117                 if (isset($this->_HomePagehandle) && $this->_HomePagehandle and !$id_only)
118                     $this->_HomePagehandle->set('pref', $packed);
119             }
120             return $count; //count($this->_prefs->unpack($packed));
121         }
122         return 0;
123     }
124
125     function userExists() {
126         //global $DBAuthParams;
127         $this->getAuthDbh();
128         $dbh = &$this->_auth_dbi;
129         if (!$dbh) { // needed?
130             return $this->_tryNextUser();
131         }
132         if (!$this->isValidName()) {
133             trigger_error(_("Invalid username."),E_USER_WARNING);
134             return $this->_tryNextUser();
135         }
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"));
141         }
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)
146         {
147             $rs = $dbh->query(sprintf($this->_authselect, $dbh->quote($this->_userid)));
148             if ($rs->numRows())
149                 return true;
150         }
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 = $this->prepare($dbi->getAuthParam('auth_user_exists'), "userid");
156             $rs = $dbh->query(sprintf($this->_authcheck, $dbh->quote($this->_userid)));
157             if ($rs->numRows())
158                 return true;
159         }
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
163         // reasons
164         if (empty($this->_authcreate) and $dbi->getAuthParam('auth_create')) {
165             $this->_authcreate = $this->prepare($dbi->getAuthParam('auth_create'),
166                                                 array("password", "userid"));
167         }
168         if (!empty($this->_authcreate) and
169             isset($GLOBALS['HTTP_POST_VARS']['auth']) and
170             isset($GLOBALS['HTTP_POST_VARS']['auth']['passwd']))
171         {
172             $passwd = $GLOBALS['HTTP_POST_VARS']['auth']['passwd'];
173             $dbh->simpleQuery(sprintf($this->_authcreate,
174                                       $dbh->quote($passwd),
175                                       $dbh->quote($this->_userid)));
176             return true;
177         }
178         return $this->_tryNextUser();
179     }
180
181     function checkPass($submitted_password) {
182         //global $DBAuthParams;
183         $this->getAuthDbh();
184         if (!$this->_auth_dbi) {  // needed?
185             return $this->_tryNextPass($submitted_password);
186         }
187         if (!$this->isValidName()) {
188             return $this->_tryNextPass($submitted_password);
189         }
190         if (!$this->_checkPassLength($submitted_password)) {
191             return WIKIAUTH_FORBIDDEN;
192         }
193         if (!isset($this->_authselect))
194             $this->userExists();
195         if (!isset($this->_authselect))
196             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
197                               'DBAUTH_AUTH_CHECK', 'SQL'),
198                           E_USER_WARNING);
199
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);
205         } else {
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);
211         }
212
213         if ($result) {
214             $this->_level = WIKIAUTH_USER;
215             return $this->_level;
216         } elseif (USER_AUTH_POLICY === 'strict') {
217             $this->_level = WIKIAUTH_FORBIDDEN;
218             return $this->_level;
219         } else {
220             return $this->_tryNextPass($submitted_password);
221         }
222     }
223
224     function mayChangePass() {
225         return $GLOBALS['request']->_dbi->getAuthParam('auth_update');
226     }
227
228     function storePass($submitted_password) {
229         if (!$this->isValidName()) {
230             return false;
231         }
232         $this->getAuthDbh();
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"));
238         }
239         if (empty($this->_authupdate)) {
240             trigger_error(fmt("Either %s is missing or DATABASE_TYPE != '%s'",
241                               'DBAUTH_AUTH_UPDATE','SQL'),
242                           E_USER_WARNING);
243             return false;
244         }
245
246         if ($this->_auth_crypt_method == 'crypt') {
247             if (function_exists('crypt'))
248                 $submitted_password = crypt($submitted_password);
249         }
250         $dbh->simpleQuery(sprintf($this->_authupdate,
251                                   $dbh->quote($submitted_password), $dbh->quote($this->_userid)));
252         return true;
253     }
254 }
255
256 // Local Variables:
257 // mode: php
258 // tab-width: 8
259 // c-basic-offset: 4
260 // c-hanging-comment-ender-p: nil
261 // indent-tabs-mode: nil
262 // End:
263 ?>