userController.php 7.2 KB

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