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