diff --git a/controllers/common.php b/controllers/common.php index ff39893..58c1fd9 100644 --- a/controllers/common.php +++ b/controllers/common.php @@ -72,16 +72,6 @@ Router\get_action('more', function() { Response\html(Template\layout('show_more', array('menu' => 'more'))); }); -// Select another database -Router\get_action('select-db', function() { - - if (ENABLE_MULTIPLE_DB) { - $_SESSION['database'] = \Model\Database\select(Request\param('database')); - } - - Response\redirect('?action=login'); -}); - // Image proxy (avoid SSL mixed content warnings) Router\get_action('proxy', function() { list($content, $type) = Model\Proxy\download(rawurldecode(Request\param('url'))); diff --git a/models/database.php b/models/database.php index f681af3..70ed0a8 100644 --- a/models/database.php +++ b/models/database.php @@ -21,11 +21,12 @@ function create($filename, $username, $password) )); if ($db->schema()->check(Schema\VERSION)) { - - $db->table('config')->update(array( + $credentials = array( 'username' => $username, 'password' => password_hash($password, PASSWORD_BCRYPT) - )); + ); + + $db->table('config')->update($credentials); return true; } @@ -48,7 +49,7 @@ function select($filename = '') // unset the authenticated flag if the database is changed if (empty($_SESSION['database']) || $_SESSION['database'] !== $filename) { if (isset($_SESSION)) { - unset($_SESSION['user']); + unset($_SESSION['loggedin']); } $_SESSION['database'] = $filename; diff --git a/models/feed.php b/models/feed.php index ac9ecde..9142b7c 100644 --- a/models/feed.php +++ b/models/feed.php @@ -56,10 +56,13 @@ function get_favicons(array $feed_ids) return array(); } - return Database::get('db') + $db = Database::get('db') ->hashtable('favicons') - ->in('feed_id', $feed_ids) - ->getAll('feed_id', 'icon'); + ->columnKey('feed_id') + ->columnValue('icon'); + + // pass $feeds_ids as argument list to hashtable::get(), use ... operator with php 5.6+ + return call_user_func_array(array($db, 'get'), $feed_ids); } // Get all favicons for a list of items @@ -68,7 +71,7 @@ function get_item_favicons(array $items) $feed_ids = array(); foreach ($items as $item) { - $feed_ids[] = $item['feed_id']; + $feed_ids[$item['feed_id']] = $item['feed_id']; } return get_favicons($feed_ids); diff --git a/models/remember_me.php b/models/remember_me.php index 5a86b02..430acca 100644 --- a/models/remember_me.php +++ b/models/remember_me.php @@ -4,7 +4,6 @@ namespace Model\RememberMe; use PicoDb\Database; use Model\Config; -use Model\User; use Model\Database as DatabaseModel; const TABLE = 'remember_me'; @@ -65,8 +64,8 @@ function authenticate() $record['expiration'] ); - // Create the session - $_SESSION['user'] = User\get($record['username']); + // mark user as sucessfull logged in + $_SESSION['loggedin'] = true; return true; } diff --git a/models/user.php b/models/user.php index af25ef7..2ae28e0 100644 --- a/models/user.php +++ b/models/user.php @@ -10,10 +10,10 @@ use Model\Config; use Model\RememberMe; use Model\Database as DatabaseModel; -// Check if the user is logged +// Check if the user is logged in function is_loggedin() { - return ! empty($_SESSION['user']); + return ! empty($_SESSION['loggedin']); } // Destroy the session and the rememberMe cookie @@ -23,13 +23,12 @@ function logout() Session\close(); } -// Get a user by username -function get($username) +// Get the credentials from the current selected database +function getCredentials() { return Database::get('db') ->table('config') - ->columns('username', 'password', 'language') - ->eq('username', $username) + ->columns('username', 'password') ->findOne(); } @@ -47,19 +46,17 @@ function validate_login(array $values) if ($result) { - $user = get($values['username']); + $credentials = getCredentials(); - if ($user && password_verify($values['password'], $user['password'])) { + if ($credentials && $credentials['username'] === $values['username'] && password_verify($values['password'], $credentials['password'])) { - unset($user['password']); - - $_SESSION['user'] = $user; + $_SESSION['loggedin'] = true; $_SESSION['config'] = Config\get_all(); // Setup the remember me feature if (! empty($values['remember_me'])) { - $credentials = RememberMe\create(DatabaseModel\select(), $values['username'], Config\get_ip_address(), Config\get_user_agent()); - RememberMe\write_cookie($credentials['token'], $credentials['sequence'], $credentials['expiration']); + $cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Config\get_ip_address(), Config\get_user_agent()); + RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']); } } else {