prepare to multi users

This commit is contained in:
Nicolas Lœuillet
2013-08-06 14:18:03 +02:00
parent 17a9cb9608
commit 7ce7ec4c94
10 changed files with 436 additions and 143 deletions

View File

@ -10,6 +10,7 @@
class Poche
{
public $user;
public $store;
public $tpl;
public $messages;
@ -26,17 +27,20 @@ class Poche
{
$this->install();
}
$this->saveUser();
}
private function init()
{
Tools::initPhp();
Session::init();
$this->user = isset($_SESSION['poche_user']) ? $_SESSION['poche_user'] : array();
# l10n
putenv('LC_ALL=' . LANG);
setlocale(LC_ALL, LANG);
bindtextdomain(LANG, LOCALE);
textdomain(LANG);
$language = ($this->user->getConfigValue('language')) ? $this->user->getConfigValue('language') : LANG;
putenv('LC_ALL=' . $language);
setlocale(LC_ALL, $language);
bindtextdomain($language, LOCALE);
textdomain($language);
# template engine
$loader = new Twig_Loader_Filesystem(TPL);
@ -48,10 +52,9 @@ class Poche
$filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
$this->tpl->addFilter($filter);
$this->pagination = new Paginator(PAGINATION, 'p');
Tools::initPhp();
Session::init();
# Pagination
$pager = ($this->user->getConfigValue('pager')) ? $this->user->getConfigValue('pager') : PAGINATION;
$this->pagination = new Paginator($pager, 'p');
}
private function install()
@ -77,12 +80,6 @@ class Poche
exit();
}
private function saveUser()
{
$_SESSION['login'] = (isset ($_SESSION['login'])) ? $_SESSION['login'] : $this->store->getLogin();
$_SESSION['pass'] = (isset ($_SESSION['pass'])) ? $_SESSION['pass'] : $this->store->getPassword();
}
/**
* Call action (mark as fav, archive, delete, etc.)
*/
@ -221,7 +218,11 @@ class Poche
public function login($referer)
{
if (!empty($_POST['login']) && !empty($_POST['password'])) {
if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) {
$user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
if ($user != array()) {
# Save login into Session
Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
Tools::logm('login successful');
$this->messages->add('s', 'welcome to your poche');
if (!empty($_POST['longlastingsession'])) {
@ -248,6 +249,7 @@ class Poche
{
$this->messages->add('s', 'see you soon!');
Tools::logm('logout');
$this->user = array();
Session::logout();
Tools::redirect();
}

33
inc/poche/User.class.php Normal file
View File

@ -0,0 +1,33 @@
<?php
/**
* poche, a read it later open source system
*
* @category poche
* @author Nicolas Lœuillet <support@inthepoche.com>
* @copyright 2013
* @license http://www.wtfpl.net/ see COPYING file
*/
class User
{
public $id;
public $username;
public $name;
public $password;
public $email;
public $config;
function __construct($user)
{
$this->id = $user['id'];
$this->username = $user['username'];
$this->name = $user['name'];
$this->password = $user['password'];
$this->email = $user['email'];
$this->config = $user['config'];
}
function getConfigValue($name) {
return (isset($this->config[$name])) ? $this->config[$name] : FALSE;
}
}

View File

@ -21,12 +21,13 @@ define ('ABS_PATH', 'assets/');
define ('TPL', './tpl');
define ('LOCALE', './locale');
define ('CACHE', './cache');
define ('LANG', 'fr_FR.UTF8');
define ('LANG', 'en_EN.UTF8');
define ('PAGINATION', '10');
define ('THEME', 'light');
$storage_type = 'sqlite'; # sqlite, mysql, (file, not yet)
# /!\ Be careful if you change the lines below /!\
require_once './inc/poche/User.class.php';
require_once './inc/poche/Tools.class.php';
require_once './inc/poche/Url.class.php';
require_once './inc/3rdparty/class.messages.php';

View File

@ -25,59 +25,59 @@ class Sqlite extends Store {
}
public function isInstalled() {
$sql = "SELECT name FROM sqlite_sequence WHERE name=?";
$query = $this->executeQuery($sql, array('config'));
$hasConfig = $query->fetchAll();
$sql = "SELECT username FROM users WHERE id=?";
$query = $this->executeQuery($sql, array('1'));
$hasAdmin = $query->fetchAll();
if (count($hasConfig) == 0)
return FALSE;
if (!$this->getLogin() || !$this->getPassword())
if (count($hasAdmin) == 0)
return FALSE;
return TRUE;
}
public function install($login, $password) {
$this->getHandle()->exec('CREATE TABLE IF NOT EXISTS "config" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "name" VARCHAR UNIQUE, "value" BLOB)');
$this->handle->exec('CREATE TABLE IF NOT EXISTS "entries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "title" VARCHAR, "url" VARCHAR UNIQUE , "is_read" INTEGER DEFAULT 0, "is_fav" INTEGER DEFAULT 0, "content" BLOB)');
if (!$this->getLogin()) {
$sql_login = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
$params_login = array('login', $login);
$query = $this->executeQuery($sql_login, $params_login);
}
if (!$this->getPassword()) {
$sql_pass = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
$params_pass = array('password', $password);
$query = $this->executeQuery($sql_pass, $params_pass);
}
$sql = 'INSERT INTO users ( username, password ) VALUES (?, ?)';
$params = array($login, $password);
$query = $this->executeQuery($sql, $params);
return TRUE;
}
public function getLogin() {
$sql = "SELECT value FROM config WHERE name=?";
$query = $this->executeQuery($sql, array('login'));
private function getConfigUser($id) {
$sql = "SELECT * FROM users_config WHERE user_id = ?";
$query = $this->executeQuery($sql, array($id));
$result = $query->fetchAll();
$user_config = array();
foreach ($result as $key => $value) {
$user_config[$value['name']] = $value['value'];
}
return $user_config;
}
public function login($username, $password) {
$sql = "SELECT * FROM users WHERE username=? AND password=?";
$query = $this->executeQuery($sql, array($username, $password));
$login = $query->fetchAll();
return isset($login[0]['value']) ? $login[0]['value'] : FALSE;
$user = array();
if (isset($login[0])) {
$user['id'] = $login[0]['id'];
$user['username'] = $login[0]['username'];
$user['password'] = $login[0]['password'];
$user['name'] = $login[0]['name'];
$user['email'] = $login[0]['email'];
$user['config'] = $this->getConfigUser($login[0]['id']);
}
return $user;
}
public function getPassword() {
$sql = "SELECT value FROM config WHERE name=?";
$query = $this->executeQuery($sql, array('password'));
$pass = $query->fetchAll();
return isset($pass[0]['value']) ? $pass[0]['value'] : FALSE;
}
public function updatePassword($password)
public function updatePassword($id, $password)
{
$sql_update = "UPDATE config SET value=? WHERE name='password'";
$params_update = array($password);
$sql_update = "UPDATE users SET password=? WHERE id=?";
$params_update = array($password, $id);
$query = $this->executeQuery($sql_update, $params_update);
}

View File

@ -13,14 +13,10 @@ class Store {
}
public function getLogin() {
public function login() {
}
public function getPassword() {
}
public function add() {
}