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