PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/admin/ajax.user.php

http://github.com/thorsten/phpMyFAQ
PHP | 219 lines | 174 code | 29 blank | 16 comment | 29 complexity | 871a4cc1572a64afef5f0202b8529019 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * AJAX: handling of Ajax user calls.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ
  10. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  11. * @copyright 2009-2021 phpMyFAQ Team
  12. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  13. * @link https://www.phpmyfaq.de
  14. * @since 2009-04-04
  15. */
  16. use phpMyFAQ\Auth;
  17. use phpMyFAQ\Category;
  18. use phpMyFAQ\Filter;
  19. use phpMyFAQ\Helper\HttpHelper;
  20. use phpMyFAQ\Helper\MailHelper;
  21. use phpMyFAQ\Permission;
  22. use phpMyFAQ\User;
  23. if (!defined('IS_VALID_PHPMYFAQ')) {
  24. http_response_code(400);
  25. exit();
  26. }
  27. $ajaxAction = Filter::filterInput(INPUT_GET, 'ajaxaction', FILTER_UNSAFE_RAW);
  28. $userId = Filter::filterInput(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
  29. $userSearch = Filter::filterInput(INPUT_GET, 'q', FILTER_UNSAFE_RAW);
  30. $csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_UNSAFE_RAW);
  31. // Send headers
  32. $http = new HttpHelper();
  33. $http->setContentType('application/json');
  34. $http->addHeader();
  35. if (
  36. $user->perm->hasPermission($user->getUserId(), 'add_user') ||
  37. $user->perm->hasPermission($user->getUserId(), 'edit_user') ||
  38. $user->perm->hasPermission($user->getUserId(), 'delete_user')
  39. ) {
  40. $user = new User($faqConfig);
  41. switch ($ajaxAction) {
  42. case 'get_user_list':
  43. $allUsers = [];
  44. foreach ($user->searchUsers($userSearch) as $singleUser) {
  45. $users = new \stdClass();
  46. $users->user_id = (int)$singleUser['user_id'];
  47. $users->name = $singleUser['login'];
  48. $allUsers[] = $users;
  49. }
  50. $http->sendJsonWithHeaders($allUsers);
  51. break;
  52. case 'get_user_data':
  53. $user->getUserById($userId, true);
  54. $userdata = [];
  55. $userdata = $user->userdata->get('*');
  56. $userdata['status'] = $user->getStatus();
  57. $userdata['login'] = $user->getLogin();
  58. $userdata['is_superadmin'] = $user->isSuperAdmin();
  59. $http->sendJsonWithHeaders($userdata);
  60. break;
  61. case 'get_all_user_data':
  62. $allUsers = $user->getAllUsers(false);
  63. $userData = [];
  64. foreach ($allUsers as $userId) {
  65. $user->getUserById($userId, true);
  66. $userObject = new \stdClass();
  67. $userObject->id = $user->getUserId();
  68. $userObject->status = $user->getStatus();
  69. $userObject->isSuperAdmin = $user->isSuperAdmin();
  70. $userObject->isVisible = $user->getUserData('is_visible');
  71. $userObject->displayName = $user->getUserData('display_name');
  72. $userObject->userName = $user->getLogin();
  73. $userObject->email = $user->getUserData('email');
  74. $userData[] = $userObject;
  75. }
  76. $http->setStatus(200);
  77. $http->sendJsonWithHeaders($userData);
  78. break;
  79. case 'get_user_rights':
  80. $user->getUserById($userId, true);
  81. $http->sendJsonWithHeaders($user->perm->getUserRights($userId));
  82. break;
  83. case 'activate_user':
  84. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  85. $http->setStatus(400);
  86. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  87. exit(1);
  88. }
  89. $user->getUserById($userId, true);
  90. $user->activateUser();
  91. $http->sendJsonWithHeaders($user->getStatus());
  92. break;
  93. case 'add_user':
  94. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  95. $http->setStatus(400);
  96. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  97. exit(1);
  98. }
  99. $errorMessage = [];
  100. $successMessage = '';
  101. $postData = json_decode(file_get_contents('php://input'), true);
  102. $userName = Filter::filterVar($postData['userName'], FILTER_UNSAFE_RAW);
  103. $userRealName = Filter::filterVar($postData['realName'], FILTER_UNSAFE_RAW);
  104. $userEmail = Filter::filterVar($postData['email'], FILTER_VALIDATE_EMAIL);
  105. $userPassword = Filter::filterVar($postData['password'], FILTER_UNSAFE_RAW);
  106. $userPasswordConfirm = Filter::filterVar($postData['passwordConfirm'], FILTER_UNSAFE_RAW);
  107. $userIsSuperAdmin = Filter::filterVar($postData['isSuperAdmin'], FILTER_VALIDATE_BOOLEAN);
  108. $newUser = new User($faqConfig);
  109. if (!$newUser->isValidLogin($userName)) {
  110. $errorMessage[] = $PMF_LANG['ad_user_error_loginInvalid'];
  111. }
  112. if ($newUser->getUserByLogin($userName)) {
  113. $errorMessage[] = $PMF_LANG['ad_adus_exerr'];
  114. }
  115. if ($userRealName === '') {
  116. $errorMessage[] = $PMF_LANG['ad_user_error_noRealName'];
  117. }
  118. if (is_null($userEmail)) {
  119. $errorMessage[] = $PMF_LANG['ad_user_error_noEmail'];
  120. }
  121. if (count($errorMessage) === 0) {
  122. if (!$newUser->createUser($userName, $userPassword)) {
  123. $errorMessage[] = $newUser->error();
  124. } else {
  125. $newUser->userdata->set(['display_name', 'email', 'is_visible'], [$userRealName, $userEmail, 0]);
  126. $newUser->setStatus('active');
  127. $newUser->setSuperAdmin($userIsSuperAdmin);
  128. $mailHelper = new MailHelper($faqConfig);
  129. $mailHelper->sendMailToNewUser($newUser, $userPassword);
  130. $successMessage = [ 'data' => $PMF_LANG['ad_adus_suc'] ];
  131. }
  132. $http->setStatus(201);
  133. $http->sendJsonWithHeaders($successMessage);
  134. exit(1);
  135. }
  136. $http->setStatus(400);
  137. $http->sendJsonWithHeaders($errorMessage);
  138. break;
  139. case 'delete_user':
  140. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  141. $http->setStatus(400);
  142. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  143. exit(1);
  144. }
  145. $user->getUserById($userId, true);
  146. if ($user->getStatus() == 'protected' || $userId == 1) {
  147. $message = '<p class="alert alert-error">' . $PMF_LANG['ad_user_error_protectedAccount'] . '</p>';
  148. } else {
  149. if (!$user->deleteUser()) {
  150. $message = $PMF_LANG['ad_user_error_delete'];
  151. } else {
  152. $category = new Category($faqConfig, [], false);
  153. $category->moveOwnership((int) $userId, 1);
  154. // Remove the user from groups
  155. if ('basic' !== $faqConfig->get('security.permLevel')) {
  156. $permissions = Permission::selectPerm('medium', $faqConfig);
  157. $permissions->removeFromAllGroups($userId);
  158. }
  159. $message = '<p class="alert alert-success">' . $PMF_LANG['ad_user_deleted'] . '</p>';
  160. }
  161. }
  162. $http->sendJsonWithHeaders($message);
  163. break;
  164. case 'overwrite_password':
  165. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  166. $http->setStatus(400);
  167. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  168. exit(1);
  169. }
  170. $userId = Filter::filterInput(INPUT_POST, 'user_id', FILTER_VALIDATE_INT);
  171. $csrfToken = Filter::filterInput(INPUT_POST, 'csrf', FILTER_UNSAFE_RAW);
  172. $newPassword = Filter::filterInput(INPUT_POST, 'npass', FILTER_UNSAFE_RAW);
  173. $retypedPassword = Filter::filterInput(INPUT_POST, 'bpass', FILTER_UNSAFE_RAW);
  174. $user->getUserById($userId, true);
  175. $auth = new Auth($faqConfig);
  176. $authSource = $auth->selectAuth($user->getAuthSource('name'));
  177. $authSource->selectEncType($user->getAuthData('encType'));
  178. if ($newPassword === $retypedPassword) {
  179. if (!$user->changePassword($newPassword)) {
  180. $http->setStatus(400);
  181. $http->sendJsonWithHeaders(['error' => $PMF_LANG['ad_passwd_fail']]);
  182. }
  183. $http->sendJsonWithHeaders(['success' => $PMF_LANG['ad_passwdsuc']]);
  184. } else {
  185. $http->setStatus(400);
  186. $http->sendJsonWithHeaders(['error' => $PMF_LANG['ad_passwd_fail']]);
  187. }
  188. break;
  189. }
  190. }