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