]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UserPreferences.php
Use HTML list
[SourceForge/phpwiki.git] / lib / plugin / UserPreferences.php
1 <?php
2
3 /**
4  * Copyright (C) 2001,2002,2003,2004,2005 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Plugin to allow any user to adjust his own preferences.
26  * This must be used in the page "UserPreferences".
27  * Prefs are stored in metadata in the current session,
28  *  within the user's home page or in a database.
29  *
30  * WikiTheme extension: WikiThemes are 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     {
40         return _("UserPreferences");
41     }
42
43     function getDescription()
44     {
45         return _("Allow any user to adjust his own preferences.");
46     }
47
48     function getDefaultArguments()
49     {
50         global $request;
51         $pagename = $request->getArg('pagename');
52         $user = $request->getUser();
53         if (isset($user->_prefs) and
54             isset($user->_prefs->_prefs) and
55                 isset($user->_prefs->_method)
56         ) {
57             $pref =& $user->_prefs;
58         } else {
59             $pref = $user->getPreferences();
60         }
61         $prefs = array();
62         //we need a hash of pref => default_value
63         foreach ($pref->_prefs as $name => $obj) {
64             $prefs[$name] = $obj->default_value;
65         }
66         return $prefs;
67     }
68
69     function run($dbi, $argstr, &$request, $basepage)
70     {
71         $args = $this->getArgs($argstr, $request);
72         $user =& $request->_user;
73         $user->_request = $request;
74         if (isa($request, 'MockRequest'))
75             return '';
76         if (defined('FUSIONFORGE') and FUSIONFORGE) {
77             if (!($user->isAuthenticated())) {
78                 return HTML::p(array('class' => 'error'),
79                     _("Error: You are not logged in, cannot display UserPreferences."));
80             }
81         }
82         if ((!isActionPage($request->getArg('pagename'))
83             and (!isset($user->_prefs->_method)
84                 or !in_array($user->_prefs->_method, array('ADODB', 'SQL', 'PDO'))))
85             or (in_array($request->getArg('action'), array('zip', 'ziphtml', 'dumphtml')))
86             or (isa($user, '_ForbiddenUser'))
87         ) {
88             $no_args = $this->getDefaultArguments();
89             $no_args['errmsg'] = HTML::p(array('class' => 'error'),
90                 _("Error: The user HomePage must be a valid WikiWord. Sorry, UserPreferences cannot be saved."));
91             $no_args['isForm'] = false;
92             return Template('userprefs', $no_args);
93         }
94         $userid = $user->UserName();
95         if ($user->isAuthenticated() and !empty($userid)) {
96             $pref = &$request->_prefs;
97             $args['isForm'] = true;
98
99             if ($request->isPost()) {
100                 $errmsg = '';
101                 $delete = $request->getArg('delete');
102                 if ($delete and $request->getArg('verify')) {
103                     // deleting prefs, verified
104                     $default_prefs = $pref->defaultPreferences();
105                     $default_prefs['userid'] = $user->UserName();
106                     $user->setPreferences($default_prefs);
107                     $request->_setUser($user);
108                     $request->setArg("verify", false);
109                     $request->setArg("delete", false);
110                     $errmsg .= _("Your UserPreferences have been successfully reset to default.");
111                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
112                     return Template('userprefs', $args);
113                 } elseif ($delete and !$request->getArg('verify')) {
114                     return HTML::fieldset(
115                         HTML::form(array('action' => $request->getPostURL(),
116                                 'method' => 'post'),
117                             HiddenInputs(array('verify' => 1)),
118                             HiddenInputs($request->getArgs()),
119                             HTML::p(_("Do you really want to reset all your UserPreferences?")),
120                             HTML::p(Button('submit:delete', _("Yes"), 'delete'),
121                                 HTML::Raw('&nbsp;'),
122                                 Button('cancel', _("Cancel")))
123                         ));
124                 } elseif ($rp = $request->getArg('pref')) {
125                     // replace only changed prefs in $pref with those from request
126                     if (!empty($rp['passwd']) and ($rp['passwd2'] != $rp['passwd'])) {
127                         $errmsg = _("Wrong password. Try again.");
128                     } else {
129                         if (empty($rp['passwd'])) unset($rp['passwd']);
130                         // fix to set system pulldown's. empty values don't get posted
131                         if (empty($rp['theme'])) $rp['theme'] = '';
132                         if (empty($rp['lang'])) $rp['lang'] = '';
133                         $num = $user->setPreferences($rp);
134                         if (!empty($rp['passwd'])) {
135                             $passchanged = false;
136                             if ($user->mayChangePass()) {
137                                 if (method_exists($user, 'storePass')) {
138                                     $passchanged = $user->storePass($rp['passwd']);
139                                 }
140                                 if (!$passchanged and method_exists($user, 'changePass')) {
141                                     $passchanged = $user->changePass($rp['passwd']);
142                                 }
143                                 if ($passchanged) {
144                                     $errmsg = _("Password updated.");
145                                 } else {
146                                     $errmsg = _("Password was not changed.");
147                                 }
148                             } else {
149                                 $errmsg = _("Password cannot be changed.");
150                             }
151                         }
152                         if (!$num) {
153                             $errmsg .= " " . _("No changes.");
154                         } else {
155                             $request->_setUser($user);
156                             $pref = $user->_prefs;
157                             if ($num == 1) {
158                                 $errmsg .= _("One UserPreferences field successfully updated.");
159                             } else {
160                                 $errmsg .= sprintf(_("%d UserPreferences fields successfully updated."), $num);
161                             }
162                         }
163                     }
164                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
165
166                 }
167             }
168             $args['available_themes'] = listAvailableThemes();
169             $args['available_languages'] = listAvailableLanguages();
170
171             return Template('userprefs', $args);
172         } else {
173             // wrong or unauthenticated user
174             return $request->_notAuthorized(WIKIAUTH_BOGO);
175         }
176     }
177 }
178
179 // Local Variables:
180 // mode: php
181 // tab-width: 8
182 // c-basic-offset: 4
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil
185 // End: