userController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $languages = FreshRSS_Context::$conf->availableLanguages();
  97. if (!isset($languages[$new_user_language])) {
  98. $new_user_language = FreshRSS_Context::$conf->language;
  99. }
  100. $new_user_name = Minz_Request::param('new_user_name');
  101. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  102. if ($ok) {
  103. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  104. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  105. $configPath = join_path(DATA_PATH, 'users', $new_user_name, 'config.php');
  106. $ok &= !file_exists($configPath);
  107. }
  108. if ($ok) {
  109. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  110. $passwordHash = '';
  111. if ($passwordPlain != '') {
  112. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  113. $_POST['new_user_passwordPlain'] = '';
  114. if (!function_exists('password_hash')) {
  115. include_once(LIB_PATH . '/password_compat.php');
  116. }
  117. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  118. $passwordPlain = '';
  119. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  120. $ok &= ($passwordHash != '');
  121. }
  122. if (empty($passwordHash)) {
  123. $passwordHash = '';
  124. }
  125. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  126. if (empty($new_user_email)) {
  127. $new_user_email = '';
  128. } else {
  129. $personaFile = join_path(DATA_PATH, 'persona', $new_user_email . '.txt');
  130. @unlink($personaFile);
  131. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  132. }
  133. }
  134. if ($ok) {
  135. mkdir(join_path(DATA_PATH, 'users', $new_user_name));
  136. $config_array = array(
  137. 'language' => $new_user_language,
  138. 'passwordHash' => $passwordHash,
  139. 'mail_login' => $new_user_email,
  140. );
  141. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  142. }
  143. if ($ok) {
  144. $userDAO = new FreshRSS_UserDAO();
  145. $ok &= $userDAO->createUser($new_user_name);
  146. }
  147. invalidateHttpCache();
  148. $notif = array(
  149. 'type' => $ok ? 'good' : 'bad',
  150. 'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
  151. );
  152. Minz_Session::_param('notification', $notif);
  153. }
  154. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  155. }
  156. public function deleteAction() {
  157. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  158. $db = Minz_Configuration::dataBase();
  159. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  160. $username = Minz_Request::param('username');
  161. $ok = ctype_alnum($username);
  162. $user_data = join_path(DATA_PATH, 'users', $username);
  163. if ($ok) {
  164. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  165. }
  166. if ($ok) {
  167. $ok &= is_dir($user_data);
  168. }
  169. if ($ok) {
  170. $userDAO = new FreshRSS_UserDAO();
  171. $ok &= $userDAO->deleteUser($username);
  172. $ok &= recursive_unlink($user_data);
  173. //TODO: delete Persona file
  174. }
  175. invalidateHttpCache();
  176. $notif = array(
  177. 'type' => $ok ? 'good' : 'bad',
  178. 'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
  179. );
  180. Minz_Session::_param('notification', $notif);
  181. }
  182. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  183. }
  184. }