userController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 profil page.
  24. */
  25. public function profilAction() {
  26. Minz_View::prependTitle(_t('users.profil') . ' · ');
  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. $this->view->conf->_passwordHash($passwordHash);
  41. }
  42. Minz_Session::_param('passwordHash', $this->view->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. $this->view->conf->_apiPasswordHash($passwordHash);
  53. }
  54. // TODO: why do we need of hasAccess here?
  55. if (FreshRSS_Auth::hasAccess('admin')) {
  56. $this->view->conf->_mail_login(Minz_Request::param('mail_login', '', true));
  57. }
  58. $email = $this->view->conf->mail_login;
  59. Minz_Session::_param('mail', $email);
  60. $ok &= $this->view->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('users.profil.updated',
  68. array('c' => 'user', 'a' => 'profil'));
  69. } else {
  70. Minz_Request::bad('error_occurred',
  71. array('c' => 'user', 'a' => 'profil'));
  72. }
  73. }
  74. }
  75. /**
  76. * This action displays the user management page.
  77. */
  78. public function manageAction() {
  79. Minz_View::prependTitle(_t('users.manage') . ' · ');
  80. }
  81. public function createAction() {
  82. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  83. $db = Minz_Configuration::dataBase();
  84. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  85. $new_user_language = Minz_Request::param('new_user_language', $this->view->conf->language);
  86. if (!in_array($new_user_language, $this->view->conf->availableLanguages())) {
  87. $new_user_language = $this->view->conf->language;
  88. }
  89. $new_user_name = Minz_Request::param('new_user_name');
  90. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  91. if ($ok) {
  92. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  93. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  94. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  95. $ok &= !file_exists($configPath);
  96. }
  97. if ($ok) {
  98. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  99. $passwordHash = '';
  100. if ($passwordPlain != '') {
  101. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  102. $_POST['new_user_passwordPlain'] = '';
  103. if (!function_exists('password_hash')) {
  104. include_once(LIB_PATH . '/password_compat.php');
  105. }
  106. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  107. $passwordPlain = '';
  108. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  109. $ok &= ($passwordHash != '');
  110. }
  111. if (empty($passwordHash)) {
  112. $passwordHash = '';
  113. }
  114. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  115. if (empty($new_user_email)) {
  116. $new_user_email = '';
  117. } else {
  118. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  119. @unlink($personaFile);
  120. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  121. }
  122. }
  123. if ($ok) {
  124. $config_array = array(
  125. 'language' => $new_user_language,
  126. 'passwordHash' => $passwordHash,
  127. 'mail_login' => $new_user_email,
  128. );
  129. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  130. }
  131. if ($ok) {
  132. $userDAO = new FreshRSS_UserDAO();
  133. $ok &= $userDAO->createUser($new_user_name);
  134. }
  135. invalidateHttpCache();
  136. $notif = array(
  137. 'type' => $ok ? 'good' : 'bad',
  138. 'content' => _t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  139. );
  140. Minz_Session::_param('notification', $notif);
  141. }
  142. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  143. }
  144. public function deleteAction() {
  145. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  146. $db = Minz_Configuration::dataBase();
  147. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  148. $username = Minz_Request::param('username');
  149. $ok = ctype_alnum($username);
  150. if ($ok) {
  151. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  152. }
  153. if ($ok) {
  154. $configPath = DATA_PATH . '/' . $username . '_user.php';
  155. $ok &= file_exists($configPath);
  156. }
  157. if ($ok) {
  158. $userDAO = new FreshRSS_UserDAO();
  159. $ok &= $userDAO->deleteUser($username);
  160. $ok &= unlink($configPath);
  161. //TODO: delete Persona file
  162. }
  163. invalidateHttpCache();
  164. $notif = array(
  165. 'type' => $ok ? 'good' : 'bad',
  166. 'content' => _t($ok ? 'user_deleted' : 'error_occurred', $username)
  167. );
  168. Minz_Session::_param('notification', $notif);
  169. }
  170. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  171. }
  172. }