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