multi user

This commit is contained in:
Nicolas Lœuillet 2013-08-06 15:51:48 +02:00
parent 7ce7ec4c94
commit 8d3275bee4
8 changed files with 109 additions and 98 deletions

View file

@ -93,7 +93,7 @@ class Session
// Force logout // Force logout
public static function logout() public static function logout()
{ {
unset($_SESSION['uid'],$_SESSION['info'],$_SESSION['expires_on'],$_SESSION['tokens'], $_SESSION['login'], $_SESSION['pass']); unset($_SESSION['uid'],$_SESSION['info'],$_SESSION['expires_on'],$_SESSION['tokens'], $_SESSION['login'], $_SESSION['pass'], $_SESSION['poche_user']);
} }
// Make sure user is logged in. // Make sure user is logged in.

View file

@ -33,10 +33,18 @@ class Poche
{ {
Tools::initPhp(); Tools::initPhp();
Session::init(); Session::init();
$this->user = isset($_SESSION['poche_user']) ? $_SESSION['poche_user'] : array();
if (isset($_SESSION['poche_user'])) {
$this->user = $_SESSION['poche_user'];
}
else {
# fake user, just for install & login screens
$this->user = new User();
$this->user->setConfig($this->getDefaultConfig());
}
# l10n # l10n
$language = ($this->user->getConfigValue('language')) ? $this->user->getConfigValue('language') : LANG; $language = $this->user->getConfigValue('language');
putenv('LC_ALL=' . $language); putenv('LC_ALL=' . $language);
setlocale(LC_ALL, $language); setlocale(LC_ALL, $language);
bindtextdomain($language, LOCALE); bindtextdomain($language, LOCALE);
@ -53,8 +61,7 @@ class Poche
$this->tpl->addFilter($filter); $this->tpl->addFilter($filter);
# Pagination # Pagination
$pager = ($this->user->getConfigValue('pager')) ? $this->user->getConfigValue('pager') : PAGINATION; $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
$this->pagination = new Paginator($pager, 'p');
} }
private function install() private function install()
@ -80,6 +87,14 @@ class Poche
exit(); exit();
} }
public function getDefaultConfig()
{
return array(
'pager' => PAGINATION,
'language' => LANG,
);
}
/** /**
* Call action (mark as fav, archive, delete, etc.) * Call action (mark as fav, archive, delete, etc.)
*/ */
@ -89,7 +104,7 @@ class Poche
{ {
case 'add': case 'add':
if($parametres_url = $url->fetchContent()) { if($parametres_url = $url->fetchContent()) {
if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'])) { if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
Tools::logm('add link ' . $url->getUrl()); Tools::logm('add link ' . $url->getUrl());
$last_id = $this->store->getLastId(); $last_id = $this->store->getLastId();
if (DOWNLOAD_PICTURES) { if (DOWNLOAD_PICTURES) {
@ -109,7 +124,7 @@ class Poche
Tools::redirect(); Tools::redirect();
break; break;
case 'delete': case 'delete':
if ($this->store->deleteById($id)) { if ($this->store->deleteById($id, $this->user->getId())) {
if (DOWNLOAD_PICTURES) { if (DOWNLOAD_PICTURES) {
remove_directory(ABS_PATH . $id); remove_directory(ABS_PATH . $id);
} }
@ -123,12 +138,12 @@ class Poche
Tools::redirect(); Tools::redirect();
break; break;
case 'toggle_fav' : case 'toggle_fav' :
$this->store->favoriteById($id); $this->store->favoriteById($id, $this->user->getId());
Tools::logm('mark as favorite link #' . $id); Tools::logm('mark as favorite link #' . $id);
Tools::redirect(); Tools::redirect();
break; break;
case 'toggle_archive' : case 'toggle_archive' :
$this->store->archiveById($id); $this->store->archiveById($id, $this->user->getId());
Tools::logm('archive link #' . $id); Tools::logm('archive link #' . $id);
Tools::redirect(); Tools::redirect();
break; break;
@ -157,7 +172,7 @@ class Poche
Tools::logm('config view'); Tools::logm('config view');
break; break;
case 'view': case 'view':
$entry = $this->store->retrieveOneById($id); $entry = $this->store->retrieveOneById($id, $this->user->getId());
if ($entry != NULL) { if ($entry != NULL) {
Tools::logm('view link #' . $id); Tools::logm('view link #' . $id);
$content = $entry['content']; $content = $entry['content'];
@ -176,10 +191,10 @@ class Poche
} }
break; break;
default: # home view default: # home view
$entries = $this->store->getEntriesByView($view); $entries = $this->store->getEntriesByView($view, $this->user->getId());
$this->pagination->set_total(count($entries)); $this->pagination->set_total(count($entries));
$page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
$datas = $this->store->getEntriesByView($view, $this->pagination->get_limit()); $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
$tpl_vars = array( $tpl_vars = array(
'entries' => $datas, 'entries' => $datas,
'page_links' => $page_links, 'page_links' => $page_links,
@ -194,21 +209,21 @@ class Poche
public function updatePassword() public function updatePassword()
{ {
if (MODE_DEMO) { if (MODE_DEMO) {
$this->messages->add('i', 'in demo mode, you can\'t update your password'); $this->messages->add('i', _('in demo mode, you can\'t update your password'));
Tools::logm('in demo mode, you can\'t do this'); Tools::logm('in demo mode, you can\'t do this');
Tools::redirect('?view=config'); Tools::redirect('?view=config');
} }
else { else {
if (isset($_POST['password']) && isset($_POST['password_repeat'])) { if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
Tools::logm('password updated'); $this->messages->add('s', _('your password has been updated'));
$this->messages->add('s', 'your password has been updated'); $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
$this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login']));
Session::logout(); Session::logout();
Tools::logm('password updated');
Tools::redirect(); Tools::redirect();
} }
else { else {
$this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields'); $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
Tools::redirect('?view=config'); Tools::redirect('?view=config');
} }
} }
@ -223,8 +238,7 @@ class Poche
# Save login into Session # Save login into Session
Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user))); 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'));
$this->messages->add('s', 'welcome to your poche');
if (!empty($_POST['longlastingsession'])) { if (!empty($_POST['longlastingsession'])) {
$_SESSION['longlastingsession'] = 31536000; $_SESSION['longlastingsession'] = 31536000;
$_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
@ -233,13 +247,14 @@ class Poche
session_set_cookie_params(0); session_set_cookie_params(0);
} }
session_regenerate_id(true); session_regenerate_id(true);
Tools::logm('login successful');
Tools::redirect($referer); Tools::redirect($referer);
} }
$this->messages->add('e', 'login failed: bad login or password'); $this->messages->add('e', _('login failed: bad login or password'));
Tools::logm('login failed'); Tools::logm('login failed');
Tools::redirect(); Tools::redirect();
} else { } else {
$this->messages->add('e', 'login failed: you have to fill all fields'); $this->messages->add('e', _('login failed: you have to fill all fields'));
Tools::logm('login failed'); Tools::logm('login failed');
Tools::redirect(); Tools::redirect();
} }
@ -247,7 +262,7 @@ class Poche
public function logout() public function logout()
{ {
$this->messages->add('s', 'see you soon!'); $this->messages->add('s', _('see you soon!'));
Tools::logm('logout'); Tools::logm('logout');
$this->user = array(); $this->user = array();
Session::logout(); Session::logout();
@ -271,14 +286,14 @@ class Poche
$this->action('add', $url); $this->action('add', $url);
if ($read == '1') { if ($read == '1') {
$last_id = $this->store->getLastId(); $last_id = $this->store->getLastId();
$this->store->archiveById($last_id); $this->action('toggle_archive', $url, $last_id);
} }
} }
# the second <ol> is for read links # the second <ol> is for read links
$read = 1; $read = 1;
} }
$this->messages->add('s', 'import from instapaper completed'); $this->messages->add('s', _('import from instapaper completed'));
Tools::logm('import from instapaper completed'); Tools::logm('import from instapaper completed');
Tools::redirect(); Tools::redirect();
} }
@ -300,14 +315,14 @@ class Poche
$this->action('add', $url); $this->action('add', $url);
if ($read == '1') { if ($read == '1') {
$last_id = $this->store->getLastId(); $last_id = $this->store->getLastId();
$this->store->archiveById($last_id); $this->action('toggle_archive', $url, $last_id);
} }
} }
# the second <ul> is for read links # the second <ul> is for read links
$read = 1; $read = 1;
} }
$this->messages->add('s', 'import from pocket completed'); $this->messages->add('s', _('import from pocket completed'));
Tools::logm('import from pocket completed'); Tools::logm('import from pocket completed');
Tools::redirect(); Tools::redirect();
} }
@ -327,16 +342,17 @@ class Poche
// if ($attr_value == 'favorite' && $attr_value == 'true') { // if ($attr_value == 'favorite' && $attr_value == 'true') {
// $last_id = $this->store->getLastId(); // $last_id = $this->store->getLastId();
// $this->store->favoriteById($last_id); // $this->store->favoriteById($last_id);
// $this->action('toogle_fav', $url, $last_id);
// } // }
// if ($attr_value == 'archive' && $attr_value == 'true') { // if ($attr_value == 'archive' && $attr_value == 'true') {
// $last_id = $this->store->getLastId(); // $last_id = $this->store->getLastId();
// $this->store->archiveById($last_id); // $this->action('toggle_archive', $url, $last_id);
// } // }
} }
if ($url->isCorrect()) if ($url->isCorrect())
$this->action('add', $url); $this->action('add', $url);
} }
$this->messages->add('s', 'import from Readability completed'); $this->messages->add('s', _('import from Readability completed'));
Tools::logm('import from Readability completed'); Tools::logm('import from Readability completed');
Tools::redirect(); Tools::redirect();
} }
@ -356,7 +372,7 @@ class Poche
public function export() public function export()
{ {
$entries = $this->store->retrieveAll(); $entries = $this->store->retrieveAll($this->user->getId());
echo $this->tpl->render('export.twig', array( echo $this->tpl->render('export.twig', array(
'export' => Tools::renderJson($entries), 'export' => Tools::renderJson($entries),
)); ));

View file

@ -17,17 +17,34 @@ class User
public $email; public $email;
public $config; public $config;
function __construct($user) function __construct($user = array())
{ {
$this->id = $user['id']; if ($user != array()) {
$this->username = $user['username']; $this->id = $user['id'];
$this->name = $user['name']; $this->username = $user['username'];
$this->password = $user['password']; $this->name = $user['name'];
$this->email = $user['email']; $this->password = $user['password'];
$this->config = $user['config']; $this->email = $user['email'];
$this->config = $user['config'];
}
} }
function getConfigValue($name) { public function getId()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function setConfig($config)
{
$this->config = $config;
}
public function getConfigValue($name) {
return (isset($this->config[$name])) ? $this->config[$name] : FALSE; return (isset($this->config[$name])) ? $this->config[$name] : FALSE;
} }
} }

View file

@ -10,7 +10,7 @@
define ('POCHE_VERSION', '1.0-beta'); define ('POCHE_VERSION', '1.0-beta');
define ('MODE_DEMO', FALSE); define ('MODE_DEMO', FALSE);
define ('DEBUG_POCHE', FALSE); define ('DEBUG_POCHE', TRUE);
define ('CONVERT_LINKS_FOOTNOTES', FALSE); define ('CONVERT_LINKS_FOOTNOTES', FALSE);
define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE); define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE);
define ('DOWNLOAD_PICTURES', FALSE); define ('DOWNLOAD_PICTURES', FALSE);

View file

@ -44,8 +44,4 @@ class File extends Store {
public function getLastId() { public function getLastId() {
} }
public function updateContentById($id) {
}
} }

View file

@ -192,11 +192,4 @@ class Mysql extends Store {
parent::__construct(); parent::__construct();
return $this->getHandle()->lastInsertId(); return $this->getHandle()->lastInsertId();
} }
public function updateContentById($id) {
parent::__construct();
$sql_update = "UPDATE entries SET content=? WHERE id=?";
$params_update = array($content, $id);
$query = $this->executeQuery($sql_update, $params_update);
}
} }

View file

@ -57,9 +57,9 @@ class Sqlite extends Store {
} }
public function login($username, $password) { public function login($username, $password) {
$sql = "SELECT * FROM users WHERE username=? AND password=?"; $sql = "SELECT * FROM users WHERE username=? AND password=?";
$query = $this->executeQuery($sql, array($username, $password)); $query = $this->executeQuery($sql, array($username, $password));
$login = $query->fetchAll(); $login = $query->fetchAll();
$user = array(); $user = array();
if (isset($login[0])) { if (isset($login[0])) {
@ -76,9 +76,9 @@ class Sqlite extends Store {
public function updatePassword($id, $password) public function updatePassword($id, $password)
{ {
$sql_update = "UPDATE users SET password=? WHERE id=?"; $sql_update = "UPDATE users SET password=? WHERE id=?";
$params_update = array($password, $id); $params_update = array($password, $id);
$query = $this->executeQuery($sql_update, $params_update); $query = $this->executeQuery($sql_update, $params_update);
} }
private function executeQuery($sql, $params) { private function executeQuery($sql, $params) {
@ -94,27 +94,27 @@ class Sqlite extends Store {
} }
} }
public function retrieveAll() { public function retrieveAll($user_id) {
$sql = "SELECT * FROM entries ORDER BY id"; $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
$query = $this->executeQuery($sql, array()); $query = $this->executeQuery($sql, array($user_id));
$entries = $query->fetchAll(); $entries = $query->fetchAll();
return $entries; return $entries;
} }
public function retrieveOneById($id) { public function retrieveOneById($id, $user_id) {
parent::__construct(); parent::__construct();
$entry = NULL; $entry = NULL;
$sql = "SELECT * FROM entries WHERE id=?"; $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
$params = array(intval($id)); $params = array(intval($id), $user_id);
$query = $this->executeQuery($sql, $params); $query = $this->executeQuery($sql, $params);
$entry = $query->fetchAll(); $entry = $query->fetchAll();
return $entry[0]; return $entry[0];
} }
public function getEntriesByView($view, $limit = '') { public function getEntriesByView($view, $user_id, $limit = '') {
parent::__construct(); parent::__construct();
switch ($_SESSION['sort']) switch ($_SESSION['sort'])
@ -139,54 +139,54 @@ class Sqlite extends Store {
switch ($view) switch ($view)
{ {
case 'archive': case 'archive':
$sql = "SELECT * FROM entries WHERE is_read=? " . $order; $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
$params = array(-1); $params = array($user_id, -1);
break; break;
case 'fav' : case 'fav' :
$sql = "SELECT * FROM entries WHERE is_fav=? " . $order; $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
$params = array(-1); $params = array($user_id, -1);
break; break;
default: default:
$sql = "SELECT * FROM entries WHERE is_read=? " . $order; $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
$params = array(0); $params = array($user_id, 0);
break; break;
} }
$sql .= ' ' . $limit; $sql .= ' ' . $limit;
$query = $this->executeQuery($sql, $params); $query = $this->executeQuery($sql, $params);
$entries = $query->fetchAll(); $entries = $query->fetchAll();
return $entries; return $entries;
} }
public function add($url, $title, $content) { public function add($url, $title, $content, $user_id) {
parent::__construct(); parent::__construct();
$sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
$params_action = array($url, $title, $content); $params_action = array($url, $title, $content, $user_id);
$query = $this->executeQuery($sql_action, $params_action);
return $query;
}
public function deleteById($id, $user_id) {
parent::__construct();
$sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
$params_action = array($id, $user_id);
$query = $this->executeQuery($sql_action, $params_action); $query = $this->executeQuery($sql_action, $params_action);
return $query; return $query;
} }
public function deleteById($id) { public function favoriteById($id, $user_id) {
parent::__construct(); parent::__construct();
$sql_action = "DELETE FROM entries WHERE id=?"; $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=? AND user_id=?";
$params_action = array($id); $params_action = array($id, $user_id);
$query = $this->executeQuery($sql_action, $params_action);
return $query;
}
public function favoriteById($id) {
parent::__construct();
$sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
$params_action = array($id);
$query = $this->executeQuery($sql_action, $params_action); $query = $this->executeQuery($sql_action, $params_action);
} }
public function archiveById($id) { public function archiveById($id, $user_id) {
parent::__construct(); parent::__construct();
$sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=? AND user_id=?";
$params_action = array($id); $params_action = array($id, $user_id);
$query = $this->executeQuery($sql_action, $params_action); $query = $this->executeQuery($sql_action, $params_action);
} }
@ -194,11 +194,4 @@ class Sqlite extends Store {
parent::__construct(); parent::__construct();
return $this->getHandle()->lastInsertId(); return $this->getHandle()->lastInsertId();
} }
public function updateContentById($id) {
parent::__construct();
$sql_update = "UPDATE entries SET content=? WHERE id=?";
$params_update = array($content, $id);
$query = $this->executeQuery($sql_update, $params_update);
}
} }

View file

@ -52,8 +52,4 @@ class Store {
public function getLastId() { public function getLastId() {
} }
public function updateContentById($id) {
}
} }