4 * Copyright (C) 2001,2002,2003,2004,2005 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
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.
30 * WikiTheme extension: WikiThemes are able to extend the predefined list
33 class WikiPlugin_UserPreferences
39 function getDescription()
41 return _("Allow any user to adjust his own preferences.");
44 function getDefaultArguments()
47 $user = $request->getUser();
48 if (isset($user->_prefs) and
49 isset($user->_prefs->_prefs) and
50 isset($user->_prefs->_method)
52 $pref =& $user->_prefs;
54 $pref = $user->getPreferences();
57 //we need a hash of pref => default_value
58 foreach ($pref->_prefs as $name => $obj) {
59 $prefs[$name] = $obj->default_value;
66 * @param string $argstr
67 * @param WikiRequest $request
68 * @param string $basepage
71 function run($dbi, $argstr, &$request, $basepage)
73 $args = $this->getArgs($argstr, $request);
74 $user =& $request->_user;
75 $user->_request = $request;
76 if (defined('FUSIONFORGE') && FUSIONFORGE) {
77 if (!($user->isAuthenticated())) {
78 return HTML::p(array('class' => 'error'),
79 _("Error: You are not logged in, cannot display UserPreferences."));
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 (is_a($user, '_ForbiddenUser'))
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);
94 $userid = $user->UserName();
95 if ($user->isAuthenticated() and !empty($userid)) {
96 $pref = &$request->_prefs;
97 $args['isForm'] = true;
99 if ($request->isPost()) {
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(),
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'),
122 Button('cancel', _("Cancel")))
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.");
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']);
140 if (!$passchanged and method_exists($user, 'changePass')) {
141 $passchanged = $user->changePass($rp['passwd']);
144 $errmsg = _("Password updated.");
146 $errmsg = _("Password was not changed.");
149 $errmsg = _("Password cannot be changed.");
153 $errmsg .= " " . _("No changes.");
155 $request->_setUser($user);
156 $pref = $user->_prefs;
158 $errmsg .= _("One UserPreferences field successfully updated.");
160 $errmsg .= sprintf(_("%d UserPreferences fields successfully updated."), $num);
164 $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
168 $args['available_themes'] = listAvailableThemes();
169 $args['available_languages'] = listAvailableLanguages();
171 return Template('userprefs', $args);
173 // wrong or unauthenticated user
174 return $request->_notAuthorized(WIKIAUTH_BOGO);
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil