PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gforge/plugins/wiki/www/lib/plugin/UserPreferences.php

https://github.com/neymanna/fusionforge
PHP | 325 lines | 122 code | 9 blank | 194 comment | 23 complexity | cf3e2f5e7b2b629efe1b43a8059c3079 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php // -*-php-*-
  2. rcs_id('$Id: UserPreferences.php,v 1.37 2006/03/07 21:06:56 rurban Exp $');
  3. /**
  4. Copyright (C) 2001,2002,2003,2004,2005 $ThePhpWikiProgrammingTeam
  5. This file is part of PhpWiki.
  6. PhpWiki is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. PhpWiki is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with PhpWiki; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * Plugin to allow any user to adjust his own preferences.
  20. * This must be used in the page "UserPreferences".
  21. * Prefs are stored in metadata in the current session,
  22. * within the user's home page or in a database.
  23. *
  24. * Theme extension: Themes are able to extend the predefined list
  25. * of preferences.
  26. */
  27. class WikiPlugin_UserPreferences
  28. extends WikiPlugin
  29. {
  30. var $bool_args;
  31. function getName () {
  32. return _("UserPreferences");
  33. }
  34. function getVersion() {
  35. return preg_replace("/[Revision: $]/", '',
  36. "\$Revision: 1.37 $");
  37. }
  38. function getDefaultArguments() {
  39. global $request;
  40. $pagename = $request->getArg('pagename');
  41. $user = $request->getUser();
  42. if ( isset($user->_prefs) and
  43. isset($user->_prefs->_prefs) and
  44. isset($user->_prefs->_method) ) {
  45. $pref =& $user->_prefs;
  46. } else {
  47. $pref = $user->getPreferences();
  48. }
  49. $prefs = array();
  50. //we need a hash of pref => default_value
  51. foreach ($pref->_prefs as $name => $obj) {
  52. $prefs[$name] = $obj->default_value;
  53. }
  54. return $prefs;
  55. }
  56. function run($dbi, $argstr, &$request, $basepage) {
  57. $args = $this->getArgs($argstr, $request);
  58. $user =& $request->_user;
  59. if (isa($request,'MockRequest'))
  60. return '';
  61. if ((!$request->isActionPage($request->getArg('pagename'))
  62. and (!isset($user->_prefs->_method)
  63. or !in_array($user->_prefs->_method, array('ADODB','SQL','PDO'))))
  64. or (in_array($request->getArg('action'), array('zip','ziphtml','dumphtml')))
  65. or (isa($user,'_ForbiddenUser')))
  66. {
  67. $no_args = $this->getDefaultArguments();
  68. // ?
  69. // foreach ($no_args as $key => $value) {
  70. // $no_args[$value] = false;
  71. // }
  72. $no_args['errmsg'] = HTML(HTML::h2(_("Error: The user HomePage must be a valid WikiWord. Sorry, UserPreferences cannot be saved."),HTML::hr()));
  73. $no_args['isForm'] = false;
  74. return Template('userprefs', $no_args);
  75. }
  76. $userid = $user->UserName();
  77. if (// ((defined('ALLOW_BOGO_LOGIN') && ALLOW_BOGO_LOGIN && $user->isSignedIn()) ||
  78. $user->isAuthenticated() and !empty($userid))
  79. {
  80. $pref = &$request->_prefs;
  81. $args['isForm'] = true;
  82. //trigger_error("DEBUG: reading prefs from getPreferences".print_r($pref));
  83. if ($request->isPost()) {
  84. $errmsg = '';
  85. $delete = $request->getArg('delete');
  86. if ($delete and $request->getArg('verify')) {
  87. // deleting prefs, verified
  88. $default_prefs = $pref->defaultPreferences();
  89. $default_prefs['userid'] = $user->UserName();
  90. $user->setPreferences($default_prefs);
  91. $request->_setUser($user);
  92. $request->setArg("verify",false);
  93. $request->setArg("delete",false);
  94. $alert = new Alert(_("Message"),
  95. _("Your UserPreferences have been successfully deleted."));
  96. $alert->show();
  97. return;
  98. } elseif ($delete and !$request->getArg('verify')) {
  99. return HTML::form(array('action' => $request->getPostURL(),
  100. 'method' => 'post'),
  101. HiddenInputs(array('verify' => 1)),
  102. HiddenInputs($request->getArgs()),
  103. HTML::p(_("Do you really want to delete all your UserPreferences?")),
  104. HTML::p(Button('submit:delete', _("Yes"), 'delete'),
  105. HTML::Raw('&nbsp;'),
  106. Button('cancel', _("Cancel")))
  107. );
  108. } elseif ($rp = $request->getArg('pref')) {
  109. // replace only changed prefs in $pref with those from request
  110. if (!empty($rp['passwd']) and ($rp['passwd2'] != $rp['passwd'])) {
  111. $errmsg = _("Wrong password. Try again.");
  112. } else {
  113. //trigger_error("DEBUG: reading prefs from request".print_r($rp));
  114. //trigger_error("DEBUG: writing prefs with setPreferences".print_r($pref));
  115. if (empty($rp['passwd'])) unset($rp['passwd']);
  116. // fix to set system pulldown's. empty values don't get posted
  117. if (empty($rp['theme'])) $rp['theme'] = '';
  118. if (empty($rp['lang'])) $rp['lang'] = '';
  119. $num = $user->setPreferences($rp);
  120. if (!empty($rp['passwd'])) {
  121. $passchanged = false;
  122. if ($user->mayChangePass()) {
  123. if (method_exists($user, 'storePass')) {
  124. $passchanged = $user->storePass($rp['passwd']);
  125. }
  126. if (!$passchanged and method_exists($user, 'changePass')) {
  127. $passchanged = $user->changePass($rp['passwd']);
  128. }
  129. if ($passchanged) {
  130. $errmsg = _("Password updated.");
  131. } else {
  132. $errmsg = _("Password was not changed.");
  133. }
  134. } else {
  135. $errmsg = _("Password cannot be changed.");
  136. }
  137. }
  138. if (!$num) {
  139. $errmsg .= " " ._("No changes.");
  140. } else {
  141. $request->_setUser($user);
  142. $pref = $user->_prefs;
  143. $errmsg .= sprintf(_("%d UserPreferences fields successfully updated."), $num);
  144. }
  145. }
  146. $args['errmsg'] = HTML(HTML::h2($errmsg), HTML::hr());
  147. }
  148. }
  149. $args['available_themes'] = listAvailableThemes();
  150. $args['available_languages'] = listAvailableLanguages();
  151. return Template('userprefs', $args);
  152. } else {
  153. // wrong or unauthenticated user
  154. return $request->_notAuthorized(WIKIAUTH_BOGO);
  155. //return $user->PrintLoginForm ($request, $args, false, false);
  156. }
  157. }
  158. };
  159. // $Log: UserPreferences.php,v $
  160. // Revision 1.37 2006/03/07 21:06:56 rurban
  161. // add PdoDbPassUser
  162. //
  163. // Revision 1.36 2005/06/30 05:17:14 rurban
  164. // update (c) date
  165. //
  166. // Revision 1.35 2004/10/13 14:13:55 rurban
  167. // fix cannot edit prefs
  168. //
  169. // Revision 1.34 2004/10/05 00:10:49 rurban
  170. // adjust for unittests. They finally pass all tests
  171. //
  172. // Revision 1.33 2004/10/04 23:39:34 rurban
  173. // just aesthetics
  174. //
  175. // Revision 1.32 2004/06/28 12:51:41 rurban
  176. // improved dumphtml and virgin setup
  177. //
  178. // Revision 1.31 2004/06/27 10:26:03 rurban
  179. // oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
  180. //
  181. // Revision 1.30 2004/06/15 09:15:52 rurban
  182. // IMPORTANT: fixed passwd handling for passwords stored in prefs:
  183. // fix encrypted usage, actually store and retrieve them from db
  184. // fix bogologin with passwd set.
  185. // fix php crashes with call-time pass-by-reference (references wrongly used
  186. // in declaration AND call). This affected mainly Apache2 and IIS.
  187. // (Thanks to John Cole to detect this!)
  188. //
  189. // Revision 1.29 2004/05/06 13:26:01 rurban
  190. // omit "Okay", this is default
  191. //
  192. // Revision 1.28 2004/05/05 13:38:09 rurban
  193. // Support to remove all UserPreferences
  194. //
  195. // Revision 1.27 2004/05/03 13:16:47 rurban
  196. // fixed UserPreferences update, esp for boolean and int
  197. //
  198. // Revision 1.26 2004/05/03 11:40:42 rurban
  199. // put listAvailableLanguages() and listAvailableThemes() from SystemInfo and
  200. // UserPreferences into Themes.php
  201. //
  202. // Revision 1.25 2004/04/07 23:13:19 rurban
  203. // fixed pear/File_Passwd for Windows
  204. // fixed FilePassUser sessions (filehandle revive) and password update
  205. //
  206. // Revision 1.24 2004/04/06 20:00:11 rurban
  207. // Cleanup of special PageList column types
  208. // Added support of plugin and theme specific Pagelist Types
  209. // Added support for theme specific UserPreferences
  210. // Added session support for ip-based throttling
  211. // sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15);
  212. // Enhanced postgres schema
  213. // Added DB_Session_dba support
  214. //
  215. // Revision 1.23 2004/04/02 15:06:56 rurban
  216. // fixed a nasty ADODB_mysql session update bug
  217. // improved UserPreferences layout (tabled hints)
  218. // fixed UserPreferences auth handling
  219. // improved auth stability
  220. // improved old cookie handling: fixed deletion of old cookies with paths
  221. //
  222. // Revision 1.22 2004/03/24 19:39:03 rurban
  223. // php5 workaround code (plus some interim debugging code in XmlElement)
  224. // php5 doesn't work yet with the current XmlElement class constructors,
  225. // WikiUserNew does work better than php4.
  226. // rewrote WikiUserNew user upgrading to ease php5 update
  227. // fixed pref handling in WikiUserNew
  228. // added Email Notification
  229. // added simple Email verification
  230. // removed emailVerify userpref subclass: just a email property
  231. // changed pref binary storage layout: numarray => hash of non default values
  232. // print optimize message only if really done.
  233. // forced new cookie policy: delete pref cookies, use only WIKI_ID as plain string.
  234. // prefs should be stored in db or homepage, besides the current session.
  235. //
  236. // Revision 1.21 2004/03/14 16:26:21 rurban
  237. // copyright line
  238. //
  239. // Revision 1.20 2004/03/12 23:20:58 rurban
  240. // pref fixes (base64)
  241. //
  242. // Revision 1.19 2004/02/27 13:21:17 rurban
  243. // several performance improvements, esp. with peardb
  244. // simplified loops
  245. // storepass seperated from prefs if defined so
  246. // stacked and strict still not working
  247. //
  248. // Revision 1.18 2004/02/24 15:20:06 rurban
  249. // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
  250. //
  251. // Revision 1.17 2004/02/17 12:11:36 rurban
  252. // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
  253. //
  254. // Revision 1.16 2004/02/15 21:34:37 rurban
  255. // PageList enhanced and improved.
  256. // fixed new WikiAdmin... plugins
  257. // editpage, Theme with exp. htmlarea framework
  258. // (htmlarea yet committed, this is really questionable)
  259. // WikiUser... code with better session handling for prefs
  260. // enhanced UserPreferences (again)
  261. // RecentChanges for show_deleted: how should pages be deleted then?
  262. //
  263. // Revision 1.15 2004/01/27 22:37:50 rurban
  264. // fixed default args: no objects
  265. //
  266. // Revision 1.14 2004/01/26 09:18:00 rurban
  267. // * changed stored pref representation as before.
  268. // the array of objects is 1) bigger and 2)
  269. // less portable. If we would import packed pref
  270. // objects and the object definition was changed, PHP would fail.
  271. // This doesn't happen with an simple array of non-default values.
  272. // * use $prefs->retrieve and $prefs->store methods, where retrieve
  273. // understands the interim format of array of objects also.
  274. // * simplified $prefs->get() and fixed $prefs->set()
  275. // * added $user->_userid and class '_WikiUser' portability functions
  276. // * fixed $user object ->_level upgrading, mostly using sessions.
  277. // this fixes yesterdays problems with loosing authorization level.
  278. // * fixed WikiUserNew::checkPass to return the _level
  279. // * fixed WikiUserNew::isSignedIn
  280. // * added explodePageList to class PageList, support sortby arg
  281. // * fixed UserPreferences for WikiUserNew
  282. // * fixed WikiPlugin for empty defaults array
  283. // * UnfoldSubpages: added pagename arg, renamed pages arg,
  284. // removed sort arg, support sortby arg
  285. //
  286. // Revision 1.13 2003/12/04 20:27:00 carstenklapp
  287. // Use the API.
  288. //
  289. // Revision 1.12 2003/12/01 22:21:33 carstenklapp
  290. // Bugfix: UserPreferences are no longer clobbered when signing in after
  291. // the previous session has ended (i.e. user closed browser then signed
  292. // in again). This is still a bit of a mess, and the preferences do not
  293. // take effect until the next page browse/link has been clicked.
  294. //
  295. // Revision 1.11 2003/09/19 22:01:19 carstenklapp
  296. // BOGO users allowed preferences too when ALLOW_BOGO_LOGIN == true.
  297. //
  298. // Revision 1.10 2003/09/13 21:57:26 carstenklapp
  299. // Reformatting only.
  300. //
  301. // Revision 1.9 2003/09/13 21:53:41 carstenklapp
  302. // Added lang and theme arguments, getVersion(), copyright and cvs log.
  303. //
  304. // For emacs users
  305. // Local Variables:
  306. // mode: php
  307. // tab-width: 8
  308. // c-basic-offset: 4
  309. // c-hanging-comment-ender-p: nil
  310. // indent-tabs-mode: nil
  311. // End:
  312. ?>