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