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