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