userController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Controller to handle user actions.
  4. */
  5. class FreshRSS_user_Controller extends Minz_ActionController {
  6. // Will also have to be computed client side on mobile devices,
  7. // so do not use a too high cost
  8. const BCRYPT_COST = 9;
  9. /**
  10. * This action is called before every other action in that class. It is
  11. * the common boiler plate for every action. It is triggered by the
  12. * underlying framework.
  13. */
  14. public function firstAction() {
  15. if (!FreshRSS_Auth::hasAccess()) {
  16. Minz_Error::error(403);
  17. }
  18. }
  19. /**
  20. * This action displays the user profile page.
  21. */
  22. public function profileAction() {
  23. Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
  24. if (Minz_Request::isPost()) {
  25. $ok = true;
  26. $passwordPlain = Minz_Request::param('passwordPlain', '', true);
  27. if ($passwordPlain != '') {
  28. Minz_Request::_param('passwordPlain'); //Discard plain-text password ASAP
  29. $_POST['passwordPlain'] = '';
  30. if (!function_exists('password_hash')) {
  31. include_once(LIB_PATH . '/password_compat.php');
  32. }
  33. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  34. $passwordPlain = '';
  35. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  36. $ok &= ($passwordHash != '');
  37. FreshRSS_Context::$conf->_passwordHash($passwordHash);
  38. }
  39. Minz_Session::_param('passwordHash', FreshRSS_Context::$conf->passwordHash);
  40. $passwordPlain = Minz_Request::param('apiPasswordPlain', '', true);
  41. if ($passwordPlain != '') {
  42. if (!function_exists('password_hash')) {
  43. include_once(LIB_PATH . '/password_compat.php');
  44. }
  45. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  46. $passwordPlain = '';
  47. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  48. $ok &= ($passwordHash != '');
  49. FreshRSS_Context::$conf->_apiPasswordHash($passwordHash);
  50. }
  51. // TODO: why do we need of hasAccess here?
  52. if (FreshRSS_Auth::hasAccess('admin')) {
  53. FreshRSS_Context::$conf->_mail_login(Minz_Request::param('mail_login', '', true));
  54. }
  55. $email = FreshRSS_Context::$conf->mail_login;
  56. Minz_Session::_param('mail', $email);
  57. $ok &= FreshRSS_Context::$conf->save();
  58. if ($email != '') {
  59. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  60. @unlink($personaFile);
  61. $ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
  62. }
  63. if ($ok) {
  64. Minz_Request::good(_t('feedback.profile.updated'),
  65. array('c' => 'user', 'a' => 'profile'));
  66. } else {
  67. Minz_Request::bad(_t('feedback.profile.error'),
  68. array('c' => 'user', 'a' => 'profile'));
  69. }
  70. }
  71. }
  72. /**
  73. * This action displays the user management page.
  74. */
  75. public function manageAction() {
  76. if (!FreshRSS_Auth::hasAccess('admin')) {
  77. Minz_Error::error(403);
  78. }
  79. Minz_View::prependTitle(_t('admin.user.title') . ' · ');
  80. // Get the correct current user.
  81. $username = Minz_Request::param('u', Minz_Session::param('currentUser'));
  82. if (!FreshRSS_UserDAO::exist($username)) {
  83. $username = Minz_Session::param('currentUser');
  84. }
  85. $this->view->current_user = $username;
  86. // Get information about the current user.
  87. $entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
  88. $this->view->nb_articles = $entryDAO->count();
  89. $this->view->size_user = $entryDAO->size();
  90. }
  91. public function createAction() {
  92. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  93. $db = Minz_Configuration::dataBase();
  94. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  95. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$conf->language);
  96. if (!in_array($new_user_language, FreshRSS_Context::$conf->availableLanguages())) {
  97. $new_user_language = FreshRSS_Context::$conf->language;
  98. }
  99. $new_user_name = Minz_Request::param('new_user_name');
  100. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  101. if ($ok) {
  102. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  103. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  104. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  105. $ok &= !file_exists($configPath);
  106. }
  107. if ($ok) {
  108. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  109. $passwordHash = '';
  110. if ($passwordPlain != '') {
  111. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  112. $_POST['new_user_passwordPlain'] = '';
  113. if (!function_exists('password_hash')) {
  114. include_once(LIB_PATH . '/password_compat.php');
  115. }
  116. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  117. $passwordPlain = '';
  118. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  119. $ok &= ($passwordHash != '');
  120. }
  121. if (empty($passwordHash)) {
  122. $passwordHash = '';
  123. }
  124. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  125. if (empty($new_user_email)) {
  126. $new_user_email = '';
  127. } else {
  128. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  129. @unlink($personaFile);
  130. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  131. }
  132. }
  133. if ($ok) {
  134. $config_array = array(
  135. 'language' => $new_user_language,
  136. 'passwordHash' => $passwordHash,
  137. 'mail_login' => $new_user_email,
  138. );
  139. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  140. }
  141. if ($ok) {
  142. $userDAO = new FreshRSS_UserDAO();
  143. $ok &= $userDAO->createUser($new_user_name);
  144. }
  145. invalidateHttpCache();
  146. $notif = array(
  147. 'type' => $ok ? 'good' : 'bad',
  148. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  149. );
  150. Minz_Session::_param('notification', $notif);
  151. }
  152. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  153. }
  154. public function deleteAction() {
  155. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  156. $db = Minz_Configuration::dataBase();
  157. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  158. $username = Minz_Request::param('username');
  159. $ok = ctype_alnum($username);
  160. if ($ok) {
  161. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  162. }
  163. if ($ok) {
  164. $configPath = DATA_PATH . '/' . $username . '_user.php';
  165. $ok &= file_exists($configPath);
  166. }
  167. if ($ok) {
  168. $userDAO = new FreshRSS_UserDAO();
  169. $ok &= $userDAO->deleteUser($username);
  170. $ok &= unlink($configPath);
  171. //TODO: delete Persona file
  172. }
  173. invalidateHttpCache();
  174. $notif = array(
  175. 'type' => $ok ? 'good' : 'bad',
  176. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  177. );
  178. Minz_Session::_param('notification', $notif);
  179. }
  180. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  181. }
  182. }