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