4
0

userController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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('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. if (!FreshRSS_Auth::hasAccess('admin')) {
  80. Minz_Error::error(403,
  81. array('error' => array(_t('access_denied'))));
  82. }
  83. Minz_View::prependTitle(_t('users.manage') . ' · ');
  84. // Get the correct current user.
  85. $userDAO = new FreshRSS_UserDAO();
  86. $username = Minz_Request::param('u', Minz_Session::param('currentUser'));
  87. if (!$userDAO->exist($username)) {
  88. $username = Minz_Session::param('currentUser');
  89. }
  90. $this->view->current_user = $username;
  91. // Get information about the current user.
  92. $entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
  93. $this->view->nb_articles = $entryDAO->count();
  94. $this->view->size_user = $entryDAO->size();
  95. }
  96. public function createAction() {
  97. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  98. $db = Minz_Configuration::dataBase();
  99. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  100. $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$conf->language);
  101. if (!in_array($new_user_language, FreshRSS_Context::$conf->availableLanguages())) {
  102. $new_user_language = FreshRSS_Context::$conf->language;
  103. }
  104. $new_user_name = Minz_Request::param('new_user_name');
  105. $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
  106. if ($ok) {
  107. $ok &= (strcasecmp($new_user_name, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to alter the default user
  108. $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
  109. $configPath = DATA_PATH . '/' . $new_user_name . '_user.php';
  110. $ok &= !file_exists($configPath);
  111. }
  112. if ($ok) {
  113. $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
  114. $passwordHash = '';
  115. if ($passwordPlain != '') {
  116. Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
  117. $_POST['new_user_passwordPlain'] = '';
  118. if (!function_exists('password_hash')) {
  119. include_once(LIB_PATH . '/password_compat.php');
  120. }
  121. $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
  122. $passwordPlain = '';
  123. $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
  124. $ok &= ($passwordHash != '');
  125. }
  126. if (empty($passwordHash)) {
  127. $passwordHash = '';
  128. }
  129. $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
  130. if (empty($new_user_email)) {
  131. $new_user_email = '';
  132. } else {
  133. $personaFile = DATA_PATH . '/persona/' . $new_user_email . '.txt';
  134. @unlink($personaFile);
  135. $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
  136. }
  137. }
  138. if ($ok) {
  139. $config_array = array(
  140. 'language' => $new_user_language,
  141. 'passwordHash' => $passwordHash,
  142. 'mail_login' => $new_user_email,
  143. );
  144. $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
  145. }
  146. if ($ok) {
  147. $userDAO = new FreshRSS_UserDAO();
  148. $ok &= $userDAO->createUser($new_user_name);
  149. }
  150. invalidateHttpCache();
  151. $notif = array(
  152. 'type' => $ok ? 'good' : 'bad',
  153. 'content' => _t($ok ? 'user_created' : 'error_occurred', $new_user_name)
  154. );
  155. Minz_Session::_param('notification', $notif);
  156. }
  157. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  158. }
  159. public function deleteAction() {
  160. if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
  161. $db = Minz_Configuration::dataBase();
  162. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  163. $username = Minz_Request::param('username');
  164. $ok = ctype_alnum($username);
  165. if ($ok) {
  166. $ok &= (strcasecmp($username, Minz_Configuration::defaultUser()) !== 0); //It is forbidden to delete the default user
  167. }
  168. if ($ok) {
  169. $configPath = DATA_PATH . '/' . $username . '_user.php';
  170. $ok &= file_exists($configPath);
  171. }
  172. if ($ok) {
  173. $userDAO = new FreshRSS_UserDAO();
  174. $ok &= $userDAO->deleteUser($username);
  175. $ok &= unlink($configPath);
  176. //TODO: delete Persona file
  177. }
  178. invalidateHttpCache();
  179. $notif = array(
  180. 'type' => $ok ? 'good' : 'bad',
  181. 'content' => _t($ok ? 'user_deleted' : 'error_occurred', $username)
  182. );
  183. Minz_Session::_param('notification', $notif);
  184. }
  185. Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
  186. }
  187. }