]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UserPreferences.php
add PdoDbPassUser
[SourceForge/phpwiki.git] / lib / plugin / UserPreferences.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UserPreferences.php,v 1.37 2006-03-07 21:06:56 rurban Exp $');
3 /**
4  Copyright (C) 2001,2002,2003,2004,2005 $ThePhpWikiProgrammingTeam
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Plugin to allow any user to adjust his own preferences.
25  * This must be used in the page "UserPreferences".
26  * Prefs are stored in metadata in the current session, 
27  *  within the user's home page or in a database.
28  *
29  * Theme extension: Themes are able to extend the predefined list 
30  * of preferences.
31  */
32 class WikiPlugin_UserPreferences
33 extends WikiPlugin
34 {
35     var $bool_args;
36
37     function getName () {
38         return _("UserPreferences");
39     }
40
41     function getVersion() {
42         return preg_replace("/[Revision: $]/", '',
43                             "\$Revision: 1.37 $");
44     }
45
46     function getDefaultArguments() {
47         global $request;
48         $pagename = $request->getArg('pagename');
49         $user = $request->getUser();
50         if ( isset($user->_prefs) and 
51              isset($user->_prefs->_prefs) and 
52              isset($user->_prefs->_method) ) {
53             $pref =& $user->_prefs;
54         } else {
55             $pref = $user->getPreferences();
56         }
57         $prefs = array();
58         //we need a hash of pref => default_value
59         foreach ($pref->_prefs as $name => $obj) {
60             $prefs[$name] = $obj->default_value;
61         }
62         return $prefs;
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66         $args = $this->getArgs($argstr, $request);
67         $user =& $request->_user;
68         if (isa($request,'MockRequest'))
69             return '';
70         if ((!$request->isActionPage($request->getArg('pagename')) 
71              and (!isset($user->_prefs->_method) 
72                   or !in_array($user->_prefs->_method, array('ADODB','SQL','PDO'))))
73             or (in_array($request->getArg('action'), array('zip','ziphtml','dumphtml')))
74             or (isa($user,'_ForbiddenUser')))
75         {
76             $no_args = $this->getDefaultArguments();
77 // ?
78 //            foreach ($no_args as $key => $value) {
79 //                $no_args[$value] = false;
80 //            }
81             $no_args['errmsg'] = HTML(HTML::h2(_("Error: The user HomePage must be a valid WikiWord. Sorry, UserPreferences cannot be saved."),HTML::hr()));
82             $no_args['isForm'] = false;
83             return Template('userprefs', $no_args);
84         }
85         $userid = $user->UserName();
86         if (// ((defined('ALLOW_BOGO_LOGIN') && ALLOW_BOGO_LOGIN && $user->isSignedIn()) ||
87              $user->isAuthenticated() and !empty($userid))
88         {
89             $pref = &$request->_prefs;
90             $args['isForm'] = true;
91             //trigger_error("DEBUG: reading prefs from getPreferences".print_r($pref));
92  
93             if ($request->isPost()) {
94                 $errmsg = '';
95                 $delete = $request->getArg('delete');
96                 if ($delete and $request->getArg('verify')) {
97                     // deleting prefs, verified
98                     $default_prefs = $pref->defaultPreferences();
99                     $default_prefs['userid'] = $user->UserName();
100                     $user->setPreferences($default_prefs);
101                     $request->_setUser($user);
102                     $request->setArg("verify",false);
103                     $request->setArg("delete",false);
104                     $alert = new Alert(_("Message"),
105                                        _("Your UserPreferences have been successfully deleted."));
106                     $alert->show();
107                     return;
108                 } elseif ($delete and !$request->getArg('verify')) {
109                     return HTML::form(array('action' => $request->getPostURL(),
110                                             'method' => 'post'),
111                                        HiddenInputs(array('verify' => 1)),
112                                        HiddenInputs($request->getArgs()),
113                                        HTML::p(_("Do you really want to delete all your UserPreferences?")),
114                                        HTML::p(Button('submit:delete', _("Yes"), 'delete'),
115                                                HTML::Raw('&nbsp;'),
116                                                Button('cancel', _("Cancel")))
117                                        );
118                 } elseif ($rp = $request->getArg('pref')) {
119                     // replace only changed prefs in $pref with those from request
120                     if (!empty($rp['passwd']) and ($rp['passwd2'] != $rp['passwd'])) {
121                         $errmsg = _("Wrong password. Try again.");
122                     } else {
123                         //trigger_error("DEBUG: reading prefs from request".print_r($rp));
124                         //trigger_error("DEBUG: writing prefs with setPreferences".print_r($pref));
125                         if (empty($rp['passwd'])) unset($rp['passwd']);
126                         // fix to set system pulldown's. empty values don't get posted
127                         if (empty($rp['theme'])) $rp['theme'] = '';
128                         if (empty($rp['lang']))  $rp['lang']  = '';
129                         $num = $user->setPreferences($rp);
130                         if (!empty($rp['passwd'])) {
131                             $passchanged = false;
132                             if ($user->mayChangePass()) {
133                                 if (method_exists($user, 'storePass')) {
134                                     $passchanged = $user->storePass($rp['passwd']);
135                                 }
136                                 if (!$passchanged and method_exists($user, 'changePass')) {
137                                     $passchanged = $user->changePass($rp['passwd']);
138                                 }
139                                 if ($passchanged) {
140                                     $errmsg = _("Password updated.");
141                                 } else {
142                                     $errmsg = _("Password was not changed.");
143                                 }
144                             } else {
145                                 $errmsg = _("Password cannot be changed.");
146                             }
147                         }
148                         if (!$num) {
149                             $errmsg .= " " ._("No changes.");
150                         } else {
151                             $request->_setUser($user);
152                             $pref = $user->_prefs;      
153                             $errmsg .= sprintf(_("%d UserPreferences fields successfully updated."), $num);
154                         }  
155                     }
156                     $args['errmsg'] = HTML(HTML::h2($errmsg), HTML::hr());
157                 }
158             }
159             $args['available_themes'] = listAvailableThemes();
160             $args['available_languages'] = listAvailableLanguages();
161
162             return Template('userprefs', $args);
163         } else {
164             // wrong or unauthenticated user
165             return $request->_notAuthorized(WIKIAUTH_BOGO);
166             //return $user->PrintLoginForm ($request, $args, false, false);
167         }
168     }
169 };
170
171 // $Log: not supported by cvs2svn $
172 // Revision 1.36  2005/06/30 05:17:14  rurban
173 // update (c) date
174 //
175 // Revision 1.35  2004/10/13 14:13:55  rurban
176 // fix cannot edit prefs
177 //
178 // Revision 1.34  2004/10/05 00:10:49  rurban
179 // adjust for unittests. They finally pass all tests
180 //
181 // Revision 1.33  2004/10/04 23:39:34  rurban
182 // just aesthetics
183 //
184 // Revision 1.32  2004/06/28 12:51:41  rurban
185 // improved dumphtml and virgin setup
186 //
187 // Revision 1.31  2004/06/27 10:26:03  rurban
188 // oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
189 //
190 // Revision 1.30  2004/06/15 09:15:52  rurban
191 // IMPORTANT: fixed passwd handling for passwords stored in prefs:
192 //   fix encrypted usage, actually store and retrieve them from db
193 //   fix bogologin with passwd set.
194 // fix php crashes with call-time pass-by-reference (references wrongly used
195 //   in declaration AND call). This affected mainly Apache2 and IIS.
196 //   (Thanks to John Cole to detect this!)
197 //
198 // Revision 1.29  2004/05/06 13:26:01  rurban
199 // omit "Okay", this is default
200 //
201 // Revision 1.28  2004/05/05 13:38:09  rurban
202 // Support to remove all UserPreferences
203 //
204 // Revision 1.27  2004/05/03 13:16:47  rurban
205 // fixed UserPreferences update, esp for boolean and int
206 //
207 // Revision 1.26  2004/05/03 11:40:42  rurban
208 // put listAvailableLanguages() and listAvailableThemes() from SystemInfo and
209 // UserPreferences into Themes.php
210 //
211 // Revision 1.25  2004/04/07 23:13:19  rurban
212 // fixed pear/File_Passwd for Windows
213 // fixed FilePassUser sessions (filehandle revive) and password update
214 //
215 // Revision 1.24  2004/04/06 20:00:11  rurban
216 // Cleanup of special PageList column types
217 // Added support of plugin and theme specific Pagelist Types
218 // Added support for theme specific UserPreferences
219 // Added session support for ip-based throttling
220 //   sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
221 // Enhanced postgres schema
222 // Added DB_Session_dba support
223 //
224 // Revision 1.23  2004/04/02 15:06:56  rurban
225 // fixed a nasty ADODB_mysql session update bug
226 // improved UserPreferences layout (tabled hints)
227 // fixed UserPreferences auth handling
228 // improved auth stability
229 // improved old cookie handling: fixed deletion of old cookies with paths
230 //
231 // Revision 1.22  2004/03/24 19:39:03  rurban
232 // php5 workaround code (plus some interim debugging code in XmlElement)
233 //   php5 doesn't work yet with the current XmlElement class constructors,
234 //   WikiUserNew does work better than php4.
235 // rewrote WikiUserNew user upgrading to ease php5 update
236 // fixed pref handling in WikiUserNew
237 // added Email Notification
238 // added simple Email verification
239 // removed emailVerify userpref subclass: just a email property
240 // changed pref binary storage layout: numarray => hash of non default values
241 // print optimize message only if really done.
242 // forced new cookie policy: delete pref cookies, use only WIKI_ID as plain string.
243 //   prefs should be stored in db or homepage, besides the current session.
244 //
245 // Revision 1.21  2004/03/14 16:26:21  rurban
246 // copyright line
247 //
248 // Revision 1.20  2004/03/12 23:20:58  rurban
249 // pref fixes (base64)
250 //
251 // Revision 1.19  2004/02/27 13:21:17  rurban
252 // several performance improvements, esp. with peardb
253 // simplified loops
254 // storepass seperated from prefs if defined so
255 // stacked and strict still not working
256 //
257 // Revision 1.18  2004/02/24 15:20:06  rurban
258 // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
259 //
260 // Revision 1.17  2004/02/17 12:11:36  rurban
261 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
262 //
263 // Revision 1.16  2004/02/15 21:34:37  rurban
264 // PageList enhanced and improved.
265 // fixed new WikiAdmin... plugins
266 // editpage, Theme with exp. htmlarea framework
267 //   (htmlarea yet committed, this is really questionable)
268 // WikiUser... code with better session handling for prefs
269 // enhanced UserPreferences (again)
270 // RecentChanges for show_deleted: how should pages be deleted then?
271 //
272 // Revision 1.15  2004/01/27 22:37:50  rurban
273 // fixed default args: no objects
274 //
275 // Revision 1.14  2004/01/26 09:18:00  rurban
276 // * changed stored pref representation as before.
277 //   the array of objects is 1) bigger and 2)
278 //   less portable. If we would import packed pref
279 //   objects and the object definition was changed, PHP would fail.
280 //   This doesn't happen with an simple array of non-default values.
281 // * use $prefs->retrieve and $prefs->store methods, where retrieve
282 //   understands the interim format of array of objects also.
283 // * simplified $prefs->get() and fixed $prefs->set()
284 // * added $user->_userid and class '_WikiUser' portability functions
285 // * fixed $user object ->_level upgrading, mostly using sessions.
286 //   this fixes yesterdays problems with loosing authorization level.
287 // * fixed WikiUserNew::checkPass to return the _level
288 // * fixed WikiUserNew::isSignedIn
289 // * added explodePageList to class PageList, support sortby arg
290 // * fixed UserPreferences for WikiUserNew
291 // * fixed WikiPlugin for empty defaults array
292 // * UnfoldSubpages: added pagename arg, renamed pages arg,
293 //   removed sort arg, support sortby arg
294 //
295 // Revision 1.13  2003/12/04 20:27:00  carstenklapp
296 // Use the API.
297 //
298 // Revision 1.12  2003/12/01 22:21:33  carstenklapp
299 // Bugfix: UserPreferences are no longer clobbered when signing in after
300 // the previous session has ended (i.e. user closed browser then signed
301 // in again). This is still a bit of a mess, and the preferences do not
302 // take effect until the next page browse/link has been clicked.
303 //
304 // Revision 1.11  2003/09/19 22:01:19  carstenklapp
305 // BOGO users allowed preferences too when ALLOW_BOGO_LOGIN == true.
306 //
307 // Revision 1.10  2003/09/13 21:57:26  carstenklapp
308 // Reformatting only.
309 //
310 // Revision 1.9  2003/09/13 21:53:41  carstenklapp
311 // Added lang and theme arguments, getVersion(), copyright and cvs log.
312 //
313
314 // For emacs users
315 // Local Variables:
316 // mode: php
317 // tab-width: 8
318 // c-basic-offset: 4
319 // c-hanging-comment-ender-p: nil
320 // indent-tabs-mode: nil
321 // End:
322 ?>