PageRenderTime 79ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/usernamepass/index.php

https://github.com/whale2/users
PHP | 825 lines | 760 code | 49 blank | 16 comment | 63 complexity | 29dc37869331bac310654f5770cf8897 MD5 | raw file
  1. <?php
  2. class UsernamePasswordAuthenticationModule extends AuthenticationModule
  3. {
  4. public function getID()
  5. {
  6. return "userpass";
  7. }
  8. public function getLegendColor()
  9. {
  10. return "a3a3a3";
  11. }
  12. public function getTitle()
  13. {
  14. return "Username / Password";
  15. }
  16. public function getUserCredentials($user)
  17. {
  18. $db = UserConfig::getDB();
  19. $userid = $user->getID();
  20. if ($stmt = $db->prepare('SELECT username FROM '.UserConfig::$mysql_prefix.'users WHERE id = ?'))
  21. {
  22. if (!$stmt->bind_param('i', $userid))
  23. {
  24. throw new Exception("Can't bind parameter".$stmt->error);
  25. }
  26. if (!$stmt->execute())
  27. {
  28. throw new Exception("Can't execute statement: ".$stmt->error);
  29. }
  30. if (!$stmt->bind_result($username))
  31. {
  32. throw new Exception("Can't bind result: ".$stmt->error);
  33. }
  34. $stmt->fetch();
  35. $stmt->close();
  36. // if user used password recovery and remembered his old password
  37. // then clean temporary password and password reset flag
  38. // (don't reset the flag if was was set for some other reasons)
  39. if (!is_null($username))
  40. {
  41. return new UsernamePassUserCredentials($username);
  42. }
  43. }
  44. else
  45. {
  46. throw new Exception("Can't prepare statement: ".$db->error);
  47. }
  48. return null;
  49. }
  50. public function getTotalConnectedUsers()
  51. {
  52. $db = UserConfig::getDB();
  53. $conns = 0;
  54. if ($stmt = $db->prepare('SELECT count(*) AS conns FROM '.UserConfig::$mysql_prefix.'users WHERE username IS NOT NULL'))
  55. {
  56. if (!$stmt->execute())
  57. {
  58. throw new Exception("Can't execute statement: ".$stmt->error);
  59. }
  60. if (!$stmt->bind_result($conns))
  61. {
  62. throw new Exception("Can't bind result: ".$stmt->error);
  63. }
  64. $stmt->fetch();
  65. $stmt->close();
  66. }
  67. else
  68. {
  69. throw new Exception("Can't prepare statement: ".$db->error);
  70. }
  71. return $conns;
  72. }
  73. /*
  74. * retrieves aggregated registrations numbers
  75. */
  76. public function getDailyRegistrations()
  77. {
  78. $db = UserConfig::getDB();
  79. $dailyregs = array();
  80. if ($stmt = $db->prepare('SELECT CAST(regtime AS DATE) AS regdate, count(*) AS regs FROM '.UserConfig::$mysql_prefix.'users WHERE username IS NOT NULL GROUP BY regdate'))
  81. {
  82. if (!$stmt->execute())
  83. {
  84. throw new Exception("Can't execute statement: ".$stmt->error);
  85. }
  86. if (!$stmt->bind_result($regdate, $regs))
  87. {
  88. throw new Exception("Can't bind result: ".$stmt->error);
  89. }
  90. while($stmt->fetch() === TRUE)
  91. {
  92. $dailyregs[] = array('regdate' => $regdate, 'regs' => $regs);
  93. }
  94. $stmt->close();
  95. }
  96. else
  97. {
  98. throw new Exception("Can't prepare statement: ".$db->error);
  99. }
  100. return $dailyregs;
  101. }
  102. public function renderLoginForm($action)
  103. {
  104. ?>
  105. <style>
  106. #userbase-usernamepass-login-form {
  107. font: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  108. padding: 0.4em 1em;
  109. margin: 0;
  110. width: 480px;
  111. border: 4px solid #ccc;
  112. border-radius: 7px;
  113. -moz-border-radius: 7px;
  114. -webkit-border-radius: 7px;
  115. }
  116. #userbase-usernamepass-login-form li {
  117. font-size: 1.2em;
  118. line-height: 1.5;
  119. clear: both;
  120. margin: 0 0 .75em;
  121. padding: 0;
  122. }
  123. #userbase-usernamepass-login-form fieldset {
  124. border: 0;
  125. padding: 0;
  126. margin: 0;
  127. }
  128. #userbase-usernamepass-login-form legend {
  129. border: 0;
  130. padding: 0;
  131. margin: 0;
  132. font-size: 1.8em;
  133. line-height: 1.8;
  134. padding-bottom: .6em;
  135. }
  136. #userbase-usernamepass-login-form ul {
  137. list-style: none;
  138. margin: 0;
  139. padding: 0;
  140. }
  141. #userbase-usernamepass-login-form label {
  142. display: block;
  143. float: left;
  144. line-height: 1.6;
  145. margin-right: 10px;
  146. text-align: right;
  147. width: 110px;
  148. padding: 3px 0;
  149. }
  150. #userbase-usernamepass-login-form label:after {
  151. content: ':';
  152. }
  153. #userbase-usernamepass-login-button {
  154. margin-left: 125px;
  155. padding: 0.3em 25px;
  156. cursor: pointer;
  157. }
  158. #userbase-usernamepass-login-forgotpass {
  159. margin-left: 130px;
  160. cursor: pointer;
  161. font-size: 0.6em;
  162. display: block;
  163. }
  164. #userbase-usernamepass-login-form input {
  165. background: #f6f6f6;
  166. border: 2px solid #888;
  167. border-radius: 2px;
  168. -moz-border-radius: 2px;
  169. -webkit-border-radius: 2px;
  170. padding: 4px;
  171. }
  172. #userbase-usernamepass-login-form input:focus {
  173. background: #fff;
  174. }
  175. #userbase-usernamepass-login-form .remember label {
  176. display: block;
  177. float: none;
  178. margin-left: 127px;
  179. text-align: left;
  180. width: 270px;
  181. }
  182. #userbase-usernamepass-login-form .remember input {
  183. border: 0;
  184. background: #fff;
  185. }
  186. #userbase-usernamepass-login-form .remember {
  187. margin-bottom: 0;
  188. }
  189. #userbase-usernamepass-login-form .remember label:after {
  190. content: ''
  191. }
  192. </style>
  193. <form id="userbase-usernamepass-login-form" action="<?php echo $action?>" method="POST">
  194. <fieldset>
  195. <legend>Enter your username and password to log in</legend>
  196. <ul>
  197. <li><label for="userbase-usernamepass-login-username">Username</label><input id="userbase-usernamepass-login-username" name="username" type="text" size="25" maxlength="25"/></li>
  198. <li><label for="userbase-usernamepass-login-password">Password</label><input id="userbase-usernamepass-login-password" name="pass" type="password" size="25" autocomplete="off"/><a id="userbase-usernamepass-login-forgotpass" href="<?php echo UserConfig::$USERSROOTURL?>/modules/usernamepass/forgotpassword.php">Forgot password?</a></li>
  199. <?php if (UserConfig::$allowRememberMe) {?><li class="remember"><label for="remember"><input type="checkbox" name="remember" value="yes" id="remember"<?php if (UserConfig::$rememberMeDefault) {?> checked<?php }?>/>remember me</label></li> <?php }?>
  200. <li><button id="userbase-usernamepass-login-button" type="submit" name="login">Log in</button><?php if (UserConfig::$enableRegistration) {?> <a href="<?php echo UserConfig::$USERSROOTURL?>/register.php">or register</a><?php } ?></li>
  201. </ul>
  202. </fieldset>
  203. </form>
  204. <?php
  205. }
  206. public function renderRegistrationForm($full = false, $action = null, $errors = null, $data = null)
  207. {
  208. ?>
  209. <style>
  210. #userbase-usernamepass-register-form {
  211. font: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  212. padding: 0.4em 1em;
  213. margin: 0;
  214. width: 470px;
  215. border: 4px solid #ccc;
  216. border-radius: 7px;
  217. -moz-border-radius: 7px;
  218. -webkit-border-radius: 7px;
  219. }
  220. #userbase-usernamepass-register-form li {
  221. font-size: 1.2em;
  222. line-height: 1.5;
  223. clear: both;
  224. margin: 0 0 .75em;
  225. padding: 0;
  226. }
  227. #userbase-usernamepass-register-form fieldset {
  228. border: 0;
  229. padding: 0;
  230. margin: 0;
  231. }
  232. #userbase-usernamepass-register-form legend {
  233. border: 0;
  234. padding: 0;
  235. margin: 0;
  236. font-size: 1.8em;
  237. line-height: 1.8;
  238. padding-bottom: .6em;
  239. }
  240. #userbase-usernamepass-register-form ul {
  241. list-style: none;
  242. margin: 0;
  243. padding: 0;
  244. }
  245. #userbase-usernamepass-register-form label {
  246. display: block;
  247. float: left;
  248. line-height: 1.6;
  249. margin-right: 10px;
  250. text-align: right;
  251. width: 140px;
  252. padding: 3px 0;
  253. }
  254. #userbase-usernamepass-register-form label:after {
  255. content: ':';
  256. }
  257. #userbase-usernamepass-register-button {
  258. margin-left: 155px;
  259. padding: 0.3em 25px;
  260. cursor: pointer;
  261. }
  262. #userbase-usernamepass-register-forgotpass {
  263. margin-left: 130px;
  264. cursor: pointer;
  265. font-size: 0.6em;
  266. display: block;
  267. }
  268. #userbase-usernamepass-register-form input {
  269. background: #f6f6f6;
  270. border: 2px solid #888;
  271. border-radius: 2px;
  272. -moz-border-radius: 2px;
  273. -webkit-border-radius: 2px;
  274. padding: 4px;
  275. }
  276. #userbase-usernamepass-register-form input:focus {
  277. background: #fff;
  278. }
  279. #userbase-usernamepass-register-form abbr {
  280. cursor: help;
  281. font-style: normal;
  282. border: 0;
  283. color: red;
  284. font-size: 1.2em;
  285. font-weight: bold;
  286. }
  287. </style>
  288. <form id="userbase-usernamepass-register-form" action="<?php echo $action?>" method="POST">
  289. <fieldset>
  290. <legend>Enter your information to create an account</legend>
  291. <ul>
  292. <li><label for="userbase-usernamepass-register-username">Username</label><input id="userbase-usernamepass-register-username" name="username" type="text" size="25" maxlength="25" value="<?php echo array_key_exists('username', $data) ? UserTools::escape($data['username']) : ''?>"/><?php echo array_key_exists('username', $errors) ? ' <abbr title="'.UserTools::escape(implode("\n", $errors['username'])).'">*</abbr>' : ''?></li>
  293. <li><label for="userbase-usernamepass-register-pass">Password</label><input id="userbase-usernamepass-register-pass" name="pass" type="password" size="25" autocomplete="off"/><?php echo array_key_exists('pass', $errors) ? ' <abbr title="'.UserTools::escape(implode("\n", $errors['pass'])).'">*</abbr>' : ''?></li>
  294. <li><label for="userbase-usernamepass-register-passrepeat">Repeat password</label><input id="userbase-usernamepass-register-passrepeat" name="repeatpass" type="password" size="25" autocomplete="off"/><?php echo array_key_exists('repeatpass', $errors) ? ' <abbr title="'.UserTools::escape(implode("\n", $errors['repeatpass'])).'">*</abbr>' : ''?></li>
  295. <li><label for="userbase-usernamepass-register-name">Name</label><input id="userbase-usernamepass-register-name" name="name" type="test" size="25" value="<?php echo array_key_exists('name', $data) ? UserTools::escape($data['name']) : ''?>"/><?php echo array_key_exists('name', $errors) ? ' <abbr title="'.UserTools::escape(implode("\n", $errors['name'])).'">*</abbr>' : ''?></li>
  296. <li><label for="userbase-usernamepass-register-email">E-mail</label><input id="userbase-usernamepass-register-email" name="email" type="text" size="25" value="<?php echo array_key_exists('email', $data) ? UserTools::escape($data['email']) : ''?>"/><?php echo array_key_exists('email', $errors) ? ' <abbr title="'.UserTools::escape(implode("\n", $errors['email'])).'">*</abbr>' : ''?></li>
  297. <li><button id="userbase-usernamepass-register-button" type="submit" name="register">Register</button> <a href="<?php echo UserConfig::$USERSROOTURL?>/login.php">or login here</a></li>
  298. </ul>
  299. </table>
  300. </form>
  301. <?php
  302. }
  303. /*
  304. * Renders user editing form
  305. *
  306. * Parameters:
  307. * $action - form action to post back to
  308. * $errors - error messages to display
  309. * $user - user object for current user that is being edited
  310. * $data - data submitted to the form
  311. */
  312. public function renderEditUserForm($action, $errors, $user, $data)
  313. {
  314. ?>
  315. <style>
  316. #userbase-usernamepass-edit-form {
  317. font: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  318. padding: 0.4em 1em;
  319. margin: 0;
  320. width: 520px;
  321. border: 4px solid #ccc;
  322. border-radius: 7px;
  323. -moz-border-radius: 7px;
  324. -webkit-border-radius: 7px;
  325. }
  326. #userbase-usernamepass-edit-form li {
  327. font-size: 1.2em;
  328. line-height: 1.5;
  329. clear: both;
  330. margin: 0 0 .75em;
  331. padding: 0;
  332. }
  333. #userbase-usernamepass-edit-form fieldset {
  334. border: 0;
  335. padding: 0;
  336. margin: 0;
  337. }
  338. #userbase-usernamepass-edit-form legend {
  339. border: 0;
  340. padding: 0;
  341. margin: 0;
  342. font-size: 1.8em;
  343. line-height: 1.8;
  344. padding-bottom: .6em;
  345. }
  346. #userbase-usernamepass-edit-form ul {
  347. list-style: none;
  348. margin: 0;
  349. padding: 0;
  350. }
  351. #userbase-usernamepass-edit-form label {
  352. display: block;
  353. float: left;
  354. line-height: 1.6;
  355. margin-right: 10px;
  356. text-align: right;
  357. width: 165px;
  358. padding: 3px 0;
  359. }
  360. #userbase-usernamepass-edit-form label:after {
  361. content: ':';
  362. }
  363. #userbase-usernamepass-edit-button {
  364. margin-left: 180px;
  365. padding: 0.3em 25px;
  366. cursor: pointer;
  367. }
  368. #userbase-usernamepass-edit-forgotpass {
  369. margin-left: 130px;
  370. cursor: pointer;
  371. font-size: 0.6em;
  372. display: block;
  373. }
  374. #userbase-usernamepass-edit-form input {
  375. background: #f6f6f6;
  376. border: 2px solid #888;
  377. border-radius: 2px;
  378. -moz-border-radius: 2px;
  379. -webkit-border-radius: 2px;
  380. padding: 4px;
  381. }
  382. #userbase-usernamepass-edit-form input:focus {
  383. background: #fff;
  384. }
  385. #userbase-usernamepass-edit-form abbr {
  386. cursor: help;
  387. font-style: normal;
  388. border: 0;
  389. color: red;
  390. font-size: 1.2em;
  391. font-weight: bold;
  392. }
  393. #userbase-usernamepass-edit-form .userbase-usernamepass-edit-section {
  394. font-size: 1.5em;
  395. font-weight: bold;
  396. margin-top: 1em;
  397. }
  398. </style>
  399. <form id="userbase-usernamepass-edit-form" action="<?php echo $action?>" method="POST">
  400. <fieldset>
  401. <legend>Update your name, email and password</legend>
  402. <ul>
  403. <?php
  404. $username = $user->getUsername();
  405. if (is_null($username)) {
  406. ?>
  407. <li><label>Username</label><input name="username" type="text" size="25" maxlength="25" value="<?php echo array_key_exists('username', $data) ? UserTools::escape($data['username']) : ''?>"/><?php echo array_key_exists('username', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['username'])).'">*</span>' : ''?></li>
  408. <?php }
  409. else
  410. {?>
  411. <li><label>Username</label><b title="Sorry, you can't change your username">&nbsp;<?php echo UserTools::escape($username)?></b></li>
  412. <?php }?>
  413. <li class="userbase-usernamepass-edit-section">Name and email</li>
  414. <li><label>Name</label><input name="name" type="test" size="40" value="<?php echo UserTools::escape(array_key_exists('name', $data) ? $data['name'] : $user->getName())?>"/><?php echo array_key_exists('name', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['name'])).'">*</span>' : ''?></li>
  415. <li><label>E-mail</label><input name="email" type="text" size="40" value="<?php echo UserTools::escape(array_key_exists('email', $data) ? $data['email'] : $user->getEmail())?>"/><?php echo array_key_exists('email', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['email'])).'">*</span>' : ''?></li>
  416. <li class="userbase-usernamepass-edit-section">Change password</li>
  417. <?php if (!is_null($user->getUsername())) {?>
  418. <li><label>Current password</label><input name="currentpass" type="password" size="25" autocomplete="off"/><?php echo array_key_exists('currentpass', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['currentpass'])).'">*</span>' : ''?></li>
  419. <?php } ?>
  420. <li><label><?php if (is_null($user->getUsername())) {?>Set a<?php } else {?>New<?php } ?> password</label><input name="pass" type="password" size="25" autocomplete="off"/><?php echo array_key_exists('pass', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['pass'])).'">*</span>' : ''?></li>
  421. <li><label>Repeat new password</label><input name="repeatpass" type="password" size="25" autocomplete="off"/><?php array_key_exists('repeatpass', $errors) ? ' <span style="color:red" title="'.UserTools::escape(implode("\n", $errors['repeatpass'])).'">*</span>' : ''?></li>
  422. <li><button id="userbase-usernamepass-edit-button" type="submit" name="save">Save</button></li>
  423. </ul>
  424. </fieldset>
  425. <?php UserTools::renderCSRFNonce(); ?>
  426. </form>
  427. <?php
  428. }
  429. public function processLogin($data, &$remember)
  430. {
  431. $remember = UserConfig::$allowRememberMe && array_key_exists('remember', $data);
  432. $db = UserConfig::getDB();
  433. $user = User::getUserByUsernamePassword($data['username'], $data['pass']);
  434. if (!is_null($user))
  435. {
  436. $user->recordActivity(USERBASE_ACTIVITY_LOGIN_UPASS);
  437. }
  438. return $user;
  439. }
  440. public function processRegistration($data, &$remember)
  441. {
  442. $remember = UserConfig::$allowRememberMe && UserConfig::$rememberUserOnRegistration;
  443. $errors = array();
  444. if (array_key_exists('pass', $data) && array_key_exists('repeatpass', $data) && $data['pass'] !== $data['repeatpass'])
  445. {
  446. $errors['repeatpass'][] = 'Passwords don\'t match';
  447. }
  448. if (array_key_exists('pass', $data) && strlen($data['pass']) < 6)
  449. {
  450. $errors['pass'][] = 'Passwords must be at least 6 characters long';
  451. }
  452. if (array_key_exists('username', $data))
  453. {
  454. $username = strtolower(trim(mb_convert_encoding($data['username'], 'UTF-8')));
  455. if (strlen($username) < 2)
  456. {
  457. $errors['username'][] = 'Username must be at least 2 characters long';
  458. }
  459. if (strlen($username) > 25)
  460. {
  461. $errors['username'][] = 'Username must be no more then 25 characters long';
  462. }
  463. if (preg_match('/^[a-z][a-z0-9.]*[a-z0-9]$/', $username) !== 1)
  464. {
  465. $errors['username'][] = "Username must start with the letter and contain only latin letters, digits or '.' symbols";
  466. }
  467. }
  468. else
  469. {
  470. $errors['username'][] = "No username passed";
  471. }
  472. if (array_key_exists('name', $data))
  473. {
  474. $name = trim(mb_convert_encoding($data['name'], 'UTF-8'));
  475. if ($name == '')
  476. {
  477. $errors['name'][] = "Name can't be empty";
  478. }
  479. }
  480. else
  481. {
  482. $errors['name'][] = 'No name specified';
  483. }
  484. if (array_key_exists('email', $data))
  485. {
  486. $email = trim(mb_convert_encoding($data['email'], 'UTF-8'));
  487. if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
  488. {
  489. $errors['email'][] = 'Invalid email address';
  490. }
  491. }
  492. else
  493. {
  494. $errors['email'][] = 'No email specified';
  495. }
  496. if (count($errors) > 0)
  497. {
  498. throw new InputValidationException('Validation failed', 0, $errors);
  499. }
  500. if (count(User::getUsersByEmailOrUsername($username)) > 0 ) {
  501. $errors['username'][] = "This username is already used, please pick another one";
  502. }
  503. if (count(User::getUsersByEmailOrUsername($email)) > 0 ) {
  504. $errors['email'][] = "This email is already used by another user, please enter another email address.";
  505. }
  506. if (count($errors) > 0)
  507. {
  508. throw new ExistingUserException('User already exists', 0, $errors);
  509. }
  510. // ok, let's create a user
  511. $user = User::createNew($name, $username, $email, $data['pass']);
  512. $user->recordActivity(USERBASE_ACTIVITY_REGISTER_UPASS);
  513. return $user;
  514. }
  515. /*
  516. * Updates user information
  517. *
  518. * returns true if successful and false if unsuccessful
  519. *
  520. * throws InputValidationException if there are problems with input data
  521. */
  522. public function processEditUser($user, $data)
  523. {
  524. $errors = array();
  525. $has_username = !is_null($user->getUsername());
  526. // don't change password if username was already set and no password fields are edited
  527. $changepass = false;
  528. // Force password setup when user sets username for the first time
  529. if (!$has_username)
  530. {
  531. $changepass = true;
  532. }
  533. else if (array_key_exists('currentpass', $data) &&
  534. array_key_exists('pass', $data) &&
  535. array_key_exists('repeatpass', $data) &&
  536. ($data['currentpass'] != '' || $data['pass'] != '' || $data['repeatpass'] != ''))
  537. {
  538. $changepass = true;
  539. if (!$user->checkPass($data['currentpass']))
  540. {
  541. $errors['currentpass'][] = 'You entered wrong current password';
  542. }
  543. }
  544. if ($changepass)
  545. {
  546. // both passwords must be passed and non-empty
  547. if (array_key_exists('pass', $data) && array_key_exists('repeatpass', $data) &&
  548. ($data['pass'] != '' || $data['repeatpass'] != '')
  549. )
  550. {
  551. if (strlen($data['pass']) < 6)
  552. {
  553. $errors['pass'][] = 'Passwords must be at least 6 characters long';
  554. }
  555. if ($data['pass'] !== $data['repeatpass'])
  556. {
  557. $errors['repeatpass'][] = 'Passwords don\'t match';
  558. }
  559. }
  560. else
  561. {
  562. if ($has_username)
  563. {
  564. $errors['pass'][] = 'You must specify new password';
  565. }
  566. else
  567. {
  568. $errors['pass'][] = 'You must set password when setting username and email';
  569. }
  570. }
  571. }
  572. // only validate username if user didn't specify it yet
  573. if (!$has_username)
  574. {
  575. if (array_key_exists('username', $data))
  576. {
  577. $username = strtolower(trim(mb_convert_encoding($data['username'], 'UTF-8')));
  578. if (strlen($username) < 2)
  579. {
  580. $errors['username'][] = 'Username must be at least 2 characters long';
  581. }
  582. if (strlen($username) > 25)
  583. {
  584. $errors['username'][] = 'Username must be no more then 25 characters long';
  585. }
  586. if (preg_match('/^[a-z][a-z0-9.]*[a-z0-9]$/', $username) !== 1)
  587. {
  588. $errors['username'][] = "Username must start with the letter and contain only latin letters, digits or '.' symbols";
  589. }
  590. }
  591. else
  592. {
  593. $errors['username'][] = "No username passed";
  594. }
  595. }
  596. if (array_key_exists('name', $data))
  597. {
  598. $name = trim(mb_convert_encoding($data['name'], 'UTF-8'));
  599. if ($name == '')
  600. {
  601. $errors['name'][] = "Name can't be empty";
  602. }
  603. }
  604. else
  605. {
  606. $errors['name'][] = 'No name specified';
  607. }
  608. if (array_key_exists('email', $data))
  609. {
  610. $email = trim(mb_convert_encoding($data['email'], 'UTF-8'));
  611. if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
  612. {
  613. $errors['email'][] = 'Invalid email address';
  614. }
  615. }
  616. else
  617. {
  618. $errors['email'][] = 'No email specified';
  619. }
  620. if (!$has_username)
  621. {
  622. $existing_users = User::getUsersByEmailOrUsername($username);
  623. if (!array_key_exists('username', $errors) &&
  624. (count($existing_users) > 0 && !$existing_users[0]->isTheSameAs($user))
  625. ) {
  626. $errors['username'][] = "This username is already used, please pick another one";
  627. }
  628. }
  629. $existing_users = User::getUsersByEmailOrUsername($email);
  630. if (!array_key_exists('email', $errors) &&
  631. (count($existing_users) > 0 && !$existing_users[0]->isTheSameAs($user))
  632. ) {
  633. $errors['email'][] = "This email is already used by another user, please enter another email address.";
  634. }
  635. if (count($errors) > 0)
  636. {
  637. throw new InputValidationException('Validation failed', 0, $errors);
  638. }
  639. if ($changepass)
  640. {
  641. $user->setPass($data['pass']);
  642. if ($has_username) {
  643. $user->recordActivity(USERBASE_ACTIVITY_UPDATEPASS);
  644. }
  645. }
  646. if (!$has_username)
  647. {
  648. $user->setUsername($username);
  649. $user->recordActivity(USERBASE_ACTIVITY_ADDED_UPASS);
  650. }
  651. $user->setName($name);
  652. $user->setEmail($email);
  653. $user->save();
  654. $user->recordActivity(USERBASE_ACTIVITY_UPDATEUSERINFO);
  655. return true;
  656. }
  657. /*
  658. * Updates user's password
  659. *
  660. * returns true if successful and false if unsuccessful
  661. *
  662. * throws InputValidationException if there are problems with input data
  663. */
  664. public function processUpdatePassword($user, $data)
  665. {
  666. $errors = array();
  667. if (array_key_exists('pass', $data) ||
  668. array_key_exists('repeatpass', $data))
  669. {
  670. if (array_key_exists('pass', $data) && array_key_exists('repeatpass', $data) && $data['pass'] !== $data['repeatpass'])
  671. {
  672. $errors['repeatpass'] = 'Passwords don\'t match';
  673. }
  674. if (array_key_exists('pass', $data) && strlen($data['pass']) < 6)
  675. {
  676. $errors['pass'] = 'Passwords must be at least 6 characters long';
  677. }
  678. }
  679. else
  680. {
  681. $errors['pass'] = 'Passwords must be specified';
  682. }
  683. if (count($errors) > 0)
  684. {
  685. throw new InputValidationException('Validation failed', 0, $errors);
  686. }
  687. $user->setPass($data['pass']);
  688. $user->setRequiresPasswordReset(false);
  689. $user->save();
  690. $user->resetTemporaryPassword();
  691. $user->recordActivity(USERBASE_ACTIVITY_RESETPASS);
  692. return true;
  693. }
  694. // THIS SHOULD ONLY BE SET ON PASSWORD RESET PAGE
  695. // SETTING THIS ON OTHER PAGES CAN RESULT IN SECURITY BREACH
  696. public static $IGNORE_PASSWORD_RESET = false;
  697. }
  698. class UsernamePassUserCredentials extends UserCredentials {
  699. private $username;
  700. public function __construct($username) {
  701. $this->username = $username;
  702. }
  703. public function getUsername() {
  704. return $this->username;
  705. }
  706. public function getHTML() {
  707. return $this->username;
  708. }
  709. }