2014-07-23 15:52:50 +02:00
|
|
|
<?php
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* Controller to handle user actions.
|
|
|
|
*/
|
|
|
|
class FreshRSS_user_Controller extends Minz_ActionController {
|
|
|
|
// Will also have to be computed client side on mobile devices,
|
|
|
|
// so do not use a too high cost
|
|
|
|
const BCRYPT_COST = 9;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This action is called before every other action in that class. It is
|
|
|
|
* the common boiler plate for every action. It is triggered by the
|
|
|
|
* underlying framework.
|
2015-11-06 16:54:20 +01:00
|
|
|
*
|
|
|
|
* @todo clean up the access condition.
|
2015-02-08 18:55:48 +01:00
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function firstAction() {
|
2015-11-06 16:54:20 +01:00
|
|
|
if (!FreshRSS_Auth::hasAccess() && !(
|
|
|
|
Minz_Request::actionName() === 'create' &&
|
|
|
|
!max_registrations_reached()
|
|
|
|
)) {
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Error::error(403);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action displays the user profile page.
|
|
|
|
*/
|
|
|
|
public function profileAction() {
|
|
|
|
Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
|
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
Minz_View::appendScript(Minz_Url::display(
|
|
|
|
'/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
|
|
|
|
));
|
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
if (Minz_Request::isPost()) {
|
|
|
|
$ok = true;
|
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
$passwordPlain = Minz_Request::param('newPasswordPlain', '', true);
|
2014-07-23 15:52:50 +02:00
|
|
|
if ($passwordPlain != '') {
|
2015-11-06 16:54:20 +01:00
|
|
|
Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
|
|
|
|
$_POST['newPasswordPlain'] = '';
|
2014-07-23 15:52:50 +02:00
|
|
|
if (!function_exists('password_hash')) {
|
|
|
|
include_once(LIB_PATH . '/password_compat.php');
|
|
|
|
}
|
|
|
|
$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
|
|
|
|
$passwordPlain = '';
|
|
|
|
$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
|
|
|
|
$ok &= ($passwordHash != '');
|
2015-02-08 18:55:48 +01:00
|
|
|
FreshRSS_Context::$user_conf->passwordHash = $passwordHash;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Session::_param('passwordHash', FreshRSS_Context::$user_conf->passwordHash);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
$passwordPlain = Minz_Request::param('apiPasswordPlain', '', true);
|
|
|
|
if ($passwordPlain != '') {
|
|
|
|
if (!function_exists('password_hash')) {
|
|
|
|
include_once(LIB_PATH . '/password_compat.php');
|
|
|
|
}
|
|
|
|
$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
|
|
|
|
$passwordPlain = '';
|
|
|
|
$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
|
|
|
|
$ok &= ($passwordHash != '');
|
2015-02-08 18:55:48 +01:00
|
|
|
FreshRSS_Context::$user_conf->apiPasswordHash = $passwordHash;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
// TODO: why do we need of hasAccess here?
|
|
|
|
if (FreshRSS_Auth::hasAccess('admin')) {
|
|
|
|
FreshRSS_Context::$user_conf->mail_login = Minz_Request::param('mail_login', '', true);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
$email = FreshRSS_Context::$user_conf->mail_login;
|
2014-07-23 15:52:50 +02:00
|
|
|
Minz_Session::_param('mail', $email);
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$ok &= FreshRSS_Context::$user_conf->save();
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
if ($email != '') {
|
|
|
|
$personaFile = DATA_PATH . '/persona/' . $email . '.txt';
|
|
|
|
@unlink($personaFile);
|
|
|
|
$ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
if ($ok) {
|
|
|
|
Minz_Request::good(_t('feedback.profile.updated'),
|
|
|
|
array('c' => 'user', 'a' => 'profile'));
|
|
|
|
} else {
|
|
|
|
Minz_Request::bad(_t('feedback.profile.error'),
|
|
|
|
array('c' => 'user', 'a' => 'profile'));
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action displays the user management page.
|
|
|
|
*/
|
|
|
|
public function manageAction() {
|
|
|
|
if (!FreshRSS_Auth::hasAccess('admin')) {
|
|
|
|
Minz_Error::error(403);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_View::prependTitle(_t('admin.user.title') . ' · ');
|
|
|
|
|
|
|
|
// Get the correct current user.
|
|
|
|
$username = Minz_Request::param('u', Minz_Session::param('currentUser'));
|
|
|
|
if (!FreshRSS_UserDAO::exist($username)) {
|
|
|
|
$username = Minz_Session::param('currentUser');
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
$this->view->current_user = $username;
|
|
|
|
|
|
|
|
// Get information about the current user.
|
|
|
|
$entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
|
|
|
|
$this->view->nb_articles = $entryDAO->count();
|
|
|
|
$this->view->size_user = $entryDAO->size();
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
/**
|
|
|
|
* This action creates a new user.
|
|
|
|
*
|
|
|
|
* Request parameters are:
|
|
|
|
* - new_user_language
|
|
|
|
* - new_user_name
|
|
|
|
* - new_user_passwordPlain
|
|
|
|
* - new_user_email
|
|
|
|
* - r (i.e. a redirection url, optional)
|
|
|
|
*
|
|
|
|
* @todo clean up this method. Idea: write a method to init a user with basic information.
|
|
|
|
* @todo handle r redirection in Minz_Request::forward directly?
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function createAction() {
|
2015-11-06 16:54:20 +01:00
|
|
|
if (Minz_Request::isPost() && (
|
|
|
|
FreshRSS_Auth::hasAccess('admin') ||
|
|
|
|
!max_registrations_reached()
|
|
|
|
)) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$db = FreshRSS_Context::$system_conf->db;
|
2014-09-21 12:12:35 +02:00
|
|
|
require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
|
|
|
|
$languages = Minz_Translate::availableLanguages();
|
|
|
|
if (!isset($languages[$new_user_language])) {
|
|
|
|
$new_user_language = FreshRSS_Context::$user_conf->language;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$new_user_name = Minz_Request::param('new_user_name');
|
|
|
|
$ok = ($new_user_name != '') && ctype_alnum($new_user_name);
|
|
|
|
|
|
|
|
if ($ok) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$default_user = FreshRSS_Context::$system_conf->default_user;
|
|
|
|
$ok &= (strcasecmp($new_user_name, $default_user) !== 0); //It is forbidden to alter the default user
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
$ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$configPath = join_path(DATA_PATH, 'users', $new_user_name, 'config.php');
|
2014-07-23 15:52:50 +02:00
|
|
|
$ok &= !file_exists($configPath);
|
|
|
|
}
|
|
|
|
if ($ok) {
|
|
|
|
$passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
|
|
|
|
$passwordHash = '';
|
|
|
|
if ($passwordPlain != '') {
|
|
|
|
Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
|
|
|
|
$_POST['new_user_passwordPlain'] = '';
|
|
|
|
if (!function_exists('password_hash')) {
|
|
|
|
include_once(LIB_PATH . '/password_compat.php');
|
|
|
|
}
|
|
|
|
$passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
|
|
|
|
$passwordPlain = '';
|
|
|
|
$passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
|
|
|
|
$ok &= ($passwordHash != '');
|
|
|
|
}
|
|
|
|
if (empty($passwordHash)) {
|
|
|
|
$passwordHash = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
|
|
|
|
if (empty($new_user_email)) {
|
|
|
|
$new_user_email = '';
|
|
|
|
} else {
|
2015-02-08 18:55:48 +01:00
|
|
|
$personaFile = join_path(DATA_PATH, 'persona', $new_user_email . '.txt');
|
2014-07-23 15:52:50 +02:00
|
|
|
@unlink($personaFile);
|
|
|
|
$ok &= (file_put_contents($personaFile, $new_user_name) !== false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($ok) {
|
2015-02-08 18:55:48 +01:00
|
|
|
mkdir(join_path(DATA_PATH, 'users', $new_user_name));
|
2014-07-23 15:52:50 +02:00
|
|
|
$config_array = array(
|
|
|
|
'language' => $new_user_language,
|
|
|
|
'passwordHash' => $passwordHash,
|
|
|
|
'mail_login' => $new_user_email,
|
|
|
|
);
|
|
|
|
$ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
|
|
|
|
}
|
|
|
|
if ($ok) {
|
|
|
|
$userDAO = new FreshRSS_UserDAO();
|
|
|
|
$ok &= $userDAO->createUser($new_user_name);
|
|
|
|
}
|
|
|
|
invalidateHttpCache();
|
|
|
|
|
|
|
|
$notif = array(
|
|
|
|
'type' => $ok ? 'good' : 'bad',
|
2015-02-08 18:55:48 +01:00
|
|
|
'content' => _t('feedback.user.created' . (!$ok ? '.error' : ''), $new_user_name)
|
2014-07-23 15:52:50 +02:00
|
|
|
);
|
|
|
|
Minz_Session::_param('notification', $notif);
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
$redirect_url = urldecode(Minz_Request::param('r', false, true));
|
|
|
|
if (!$redirect_url) {
|
|
|
|
$redirect_url = array('c' => 'user', 'a' => 'manage');
|
|
|
|
}
|
|
|
|
Minz_Request::forward($redirect_url, true);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
/**
|
|
|
|
* This action delete an existing user.
|
|
|
|
*
|
|
|
|
* Request parameter is:
|
|
|
|
* - username
|
|
|
|
*
|
|
|
|
* @todo clean up this method. Idea: create a User->clean() method.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function deleteAction() {
|
2015-11-06 16:54:20 +01:00
|
|
|
$username = Minz_Request::param('username');
|
|
|
|
$redirect_url = urldecode(Minz_Request::param('r', false, true));
|
|
|
|
if (!$redirect_url) {
|
|
|
|
$redirect_url = array('c' => 'user', 'a' => 'manage');
|
|
|
|
}
|
|
|
|
|
|
|
|
$self_deletion = Minz_Session::param('currentUser', '_') === $username;
|
|
|
|
|
|
|
|
if (Minz_Request::isPost() && (
|
|
|
|
FreshRSS_Auth::hasAccess('admin') ||
|
|
|
|
$self_deletion
|
|
|
|
)) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$db = FreshRSS_Context::$system_conf->db;
|
2014-09-21 12:12:35 +02:00
|
|
|
require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
$ok = ctype_alnum($username);
|
2015-02-08 18:55:48 +01:00
|
|
|
$user_data = join_path(DATA_PATH, 'users', $username);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
if ($ok) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$default_user = FreshRSS_Context::$system_conf->default_user;
|
|
|
|
$ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2015-11-06 16:54:20 +01:00
|
|
|
if ($ok && $self_deletion) {
|
|
|
|
// We check the password if it's a self-destruction
|
|
|
|
$nonce = Minz_Session::param('nonce');
|
|
|
|
$challenge = Minz_Request::param('challenge', '');
|
|
|
|
|
|
|
|
$ok &= FreshRSS_FormAuth::checkCredentials(
|
|
|
|
$username, FreshRSS_Context::$user_conf->passwordHash,
|
|
|
|
$nonce, $challenge
|
|
|
|
);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
if ($ok) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$ok &= is_dir($user_data);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
if ($ok) {
|
|
|
|
$userDAO = new FreshRSS_UserDAO();
|
|
|
|
$ok &= $userDAO->deleteUser($username);
|
2015-02-08 18:55:48 +01:00
|
|
|
$ok &= recursive_unlink($user_data);
|
2014-07-23 15:52:50 +02:00
|
|
|
//TODO: delete Persona file
|
|
|
|
}
|
2015-11-06 16:54:20 +01:00
|
|
|
if ($ok && $self_deletion) {
|
|
|
|
FreshRSS_Auth::removeAccess();
|
|
|
|
$redirect_url = array('c' => 'index', 'a' => 'index');
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
invalidateHttpCache();
|
|
|
|
|
|
|
|
$notif = array(
|
|
|
|
'type' => $ok ? 'good' : 'bad',
|
2015-02-08 18:55:48 +01:00
|
|
|
'content' => _t('feedback.user.deleted' . (!$ok ? '.error' : ''), $username)
|
2014-07-23 15:52:50 +02:00
|
|
|
);
|
|
|
|
Minz_Session::_param('notification', $notif);
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2015-11-06 16:54:20 +01:00
|
|
|
Minz_Request::forward($redirect_url, true);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|