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