]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UserPreferences.php
getName should not translate
[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     public $bool_args;
37
38     function getDescription()
39     {
40         return _("Allow any user to adjust his own preferences.");
41     }
42
43     function getDefaultArguments()
44     {
45         global $request;
46         $user = $request->getUser();
47         if (isset($user->_prefs) and
48             isset($user->_prefs->_prefs) and
49                 isset($user->_prefs->_method)
50         ) {
51             $pref =& $user->_prefs;
52         } else {
53             $pref = $user->getPreferences();
54         }
55         $prefs = array();
56         //we need a hash of pref => default_value
57         foreach ($pref->_prefs as $name => $obj) {
58             $prefs[$name] = $obj->default_value;
59         }
60         return $prefs;
61     }
62
63     function run($dbi, $argstr, &$request, $basepage)
64     {
65         $args = $this->getArgs($argstr, $request);
66         $user =& $request->_user;
67         $user->_request = $request;
68         if (isa($request, 'MockRequest'))
69             return '';
70         if (defined('FUSIONFORGE') and FUSIONFORGE) {
71             if (!($user->isAuthenticated())) {
72                 return HTML::p(array('class' => 'error'),
73                     _("Error: You are not logged in, cannot display UserPreferences."));
74             }
75         }
76         if ((!isActionPage($request->getArg('pagename'))
77             and (!isset($user->_prefs->_method)
78                 or !in_array($user->_prefs->_method, array('ADODB', 'SQL', 'PDO'))))
79             or (in_array($request->getArg('action'), array('zip', 'ziphtml', 'dumphtml')))
80             or (isa($user, '_ForbiddenUser'))
81         ) {
82             $no_args = $this->getDefaultArguments();
83             $no_args['errmsg'] = HTML::p(array('class' => 'error'),
84                 _("Error: The user HomePage must be a valid WikiWord. Sorry, UserPreferences cannot be saved."));
85             $no_args['isForm'] = false;
86             return Template('userprefs', $no_args);
87         }
88         $userid = $user->UserName();
89         if ($user->isAuthenticated() and !empty($userid)) {
90             $pref = &$request->_prefs;
91             $args['isForm'] = true;
92
93             if ($request->isPost()) {
94                 $errmsg = '';
95                 $delete = $request->getArg('delete');
96                 if ($delete and $request->getArg('verify')) {
97                     // deleting prefs, verified
98                     $default_prefs = $pref->defaultPreferences();
99                     $default_prefs['userid'] = $user->UserName();
100                     $user->setPreferences($default_prefs);
101                     $request->_setUser($user);
102                     $request->setArg("verify", false);
103                     $request->setArg("delete", false);
104                     $errmsg .= _("Your UserPreferences have been successfully reset to default.");
105                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
106                     return Template('userprefs', $args);
107                 } elseif ($delete and !$request->getArg('verify')) {
108                     return HTML::fieldset(
109                         HTML::form(array('action' => $request->getPostURL(),
110                                 'method' => 'post'),
111                             HiddenInputs(array('verify' => 1)),
112                             HiddenInputs($request->getArgs()),
113                             HTML::p(_("Do you really want to reset all your UserPreferences?")),
114                             HTML::p(Button('submit:delete', _("Yes"), 'delete'),
115                                 HTML::raw('&nbsp;'),
116                                 Button('cancel', _("Cancel")))
117                         ));
118                 } elseif ($rp = $request->getArg('pref')) {
119                     // replace only changed prefs in $pref with those from request
120                     if (!empty($rp['passwd']) and ($rp['passwd2'] != $rp['passwd'])) {
121                         $errmsg = _("Wrong password. Try again.");
122                     } else {
123                         if (empty($rp['passwd'])) unset($rp['passwd']);
124                         // fix to set system pulldown's. empty values don't get posted
125                         if (empty($rp['theme'])) $rp['theme'] = '';
126                         if (empty($rp['lang'])) $rp['lang'] = '';
127                         $num = $user->setPreferences($rp);
128                         if (!empty($rp['passwd'])) {
129                             $passchanged = false;
130                             if ($user->mayChangePass()) {
131                                 if (method_exists($user, 'storePass')) {
132                                     $passchanged = $user->storePass($rp['passwd']);
133                                 }
134                                 if (!$passchanged and method_exists($user, 'changePass')) {
135                                     $passchanged = $user->changePass($rp['passwd']);
136                                 }
137                                 if ($passchanged) {
138                                     $errmsg = _("Password updated.");
139                                 } else {
140                                     $errmsg = _("Password was not changed.");
141                                 }
142                             } else {
143                                 $errmsg = _("Password cannot be changed.");
144                             }
145                         }
146                         if (!$num) {
147                             $errmsg .= " " . _("No changes.");
148                         } else {
149                             $request->_setUser($user);
150                             $pref = $user->_prefs;
151                             if ($num == 1) {
152                                 $errmsg .= _("One UserPreferences field successfully updated.");
153                             } else {
154                                 $errmsg .= sprintf(_("%d UserPreferences fields successfully updated."), $num);
155                             }
156                         }
157                     }
158                     $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
159
160                 }
161             }
162             $args['available_themes'] = listAvailableThemes();
163             $args['available_languages'] = listAvailableLanguages();
164
165             return Template('userprefs', $args);
166         } else {
167             // wrong or unauthenticated user
168             return $request->_notAuthorized(WIKIAUTH_BOGO);
169         }
170     }
171 }
172
173 // Local Variables:
174 // mode: php
175 // tab-width: 8
176 // c-basic-offset: 4
177 // c-hanging-comment-ender-p: nil
178 // indent-tabs-mode: nil
179 // End: