4 Copyright (C) 2001,2002,2003,2004,2005 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
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 in the current session,
27 * within the user's home page or in a database.
29 * WikiTheme extension: WikiThemes are able to extend the predefined list
32 class WikiPlugin_UserPreferences
38 return _("UserPreferences");
41 function getVersion() {
42 return preg_replace("/[Revision: $]/", '',
46 function getDefaultArguments() {
48 $pagename = $request->getArg('pagename');
49 $user = $request->getUser();
50 if ( isset($user->_prefs) and
51 isset($user->_prefs->_prefs) and
52 isset($user->_prefs->_method) ) {
53 $pref =& $user->_prefs;
55 $pref = $user->getPreferences();
58 //we need a hash of pref => default_value
59 foreach ($pref->_prefs as $name => $obj) {
60 $prefs[$name] = $obj->default_value;
65 function run($dbi, $argstr, &$request, $basepage) {
66 $args = $this->getArgs($argstr, $request);
67 $user =& $request->_user;
68 if (isa($request,'MockRequest'))
70 if ((!$request->isActionPage($request->getArg('pagename'))
71 and (!isset($user->_prefs->_method)
72 or !in_array($user->_prefs->_method, array('ADODB','SQL','PDO'))))
73 or (in_array($request->getArg('action'), array('zip','ziphtml','dumphtml')))
74 or (isa($user,'_ForbiddenUser')))
76 $no_args = $this->getDefaultArguments();
78 // foreach ($no_args as $key => $value) {
79 // $no_args[$value] = false;
81 $no_args['errmsg'] = HTML(HTML::h2(_("Error: The user HomePage must be a valid WikiWord. Sorry, UserPreferences cannot be saved."),HTML::hr()));
82 $no_args['isForm'] = false;
83 return Template('userprefs', $no_args);
85 $userid = $user->UserName();
86 if (// ((defined('ALLOW_BOGO_LOGIN') && ALLOW_BOGO_LOGIN && $user->isSignedIn()) ||
87 $user->isAuthenticated() and !empty($userid))
89 $pref = &$request->_prefs;
90 $args['isForm'] = true;
91 //trigger_error("DEBUG: reading prefs from getPreferences".print_r($pref));
93 if ($request->isPost()) {
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 // Alert message removed, display directly the Preference form with the message.
105 // $alert = new Alert(_("Message"),
106 // _("Your UserPreferences have been successfully reset to default."));
109 $errmsg .= _("Your UserPreferences have been successfully reset to default.");
110 $args['errmsg'] = HTML::div(array('class' => 'feedback'), HTML::p($errmsg));
111 return Template('userprefs', $args);
112 } elseif ($delete and !$request->getArg('verify')) {
113 return HTML::form(array('action' => $request->getPostURL(),
115 HiddenInputs(array('verify' => 1)),
116 HiddenInputs($request->getArgs()),
117 HTML::p(_("Do you really want to reset all your UserPreferences?")),
118 HTML::p(Button('submit:delete', _("Yes"), 'delete'),
120 Button('cancel', _("Cancel")))
122 } elseif ($rp = $request->getArg('pref')) {
123 // replace only changed prefs in $pref with those from request
124 if (!empty($rp['passwd']) and ($rp['passwd2'] != $rp['passwd'])) {
125 $errmsg = _("Wrong password. Try again.");
127 //trigger_error("DEBUG: reading prefs from request".print_r($rp));
128 //trigger_error("DEBUG: writing prefs with setPreferences".print_r($pref));
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);
175 //return $user->PrintLoginForm ($request, $args, false, false);
185 // c-hanging-comment-ender-p: nil
186 // indent-tabs-mode: nil