]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
multi user
authorNicolas Lœuillet <nicolas.loeuillet@gmail.com>
Tue, 6 Aug 2013 13:51:48 +0000 (15:51 +0200)
committerNicolas Lœuillet <nicolas.loeuillet@gmail.com>
Tue, 6 Aug 2013 13:51:48 +0000 (15:51 +0200)
inc/3rdparty/Session.class.php
inc/poche/Poche.class.php
inc/poche/User.class.php
inc/poche/config.inc.php
inc/store/file.class.php
inc/store/mysql.class.php
inc/store/sqlite.class.php
inc/store/store.class.php

index eff924ccdb06dddf8c68bcfab1a15fc967bfc000..3162f5074d82700f5970e0611fed2161613ac0f2 100644 (file)
@@ -93,7 +93,7 @@ class Session
     // Force 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.
index 2c0c73f92ebc9683cba5cb5d65890a8fb361b522..ce5bb54a64b229136896642377ba516e0d69808f 100644 (file)
@@ -33,10 +33,18 @@ class Poche
     {
         Tools::initPhp();
         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
-        $language = ($this->user->getConfigValue('language')) ? $this->user->getConfigValue('language') : LANG;
+        $language = $this->user->getConfigValue('language');
         putenv('LC_ALL=' . $language);
         setlocale(LC_ALL, $language);
         bindtextdomain($language, LOCALE); 
@@ -53,8 +61,7 @@ class Poche
         $this->tpl->addFilter($filter);
 
         # Pagination
-        $pager = ($this->user->getConfigValue('pager')) ? $this->user->getConfigValue('pager') : PAGINATION;
-        $this->pagination = new Paginator($pager, 'p');
+        $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
     }
 
     private function install() 
@@ -80,6 +87,14 @@ class Poche
         exit();
     }
 
+    public function getDefaultConfig()
+    {
+        return array(
+            'pager' => PAGINATION,
+            'language' => LANG,
+            );
+    }
+
     /**
      * Call action (mark as fav, archive, delete, etc.)
      */
@@ -89,7 +104,7 @@ class Poche
         {
             case 'add':
                 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());
                         $last_id = $this->store->getLastId();
                         if (DOWNLOAD_PICTURES) {
@@ -109,7 +124,7 @@ class Poche
                 Tools::redirect();
                 break;
             case 'delete':
-                if ($this->store->deleteById($id)) {
+                if ($this->store->deleteById($id, $this->user->getId())) {
                     if (DOWNLOAD_PICTURES) {
                         remove_directory(ABS_PATH . $id);
                     }
@@ -123,12 +138,12 @@ class Poche
                 Tools::redirect();
                 break;
             case 'toggle_fav' :
-                $this->store->favoriteById($id);
+                $this->store->favoriteById($id, $this->user->getId());
                 Tools::logm('mark as favorite link #' . $id);
                 Tools::redirect();
                 break;
             case 'toggle_archive' :
-                $this->store->archiveById($id);
+                $this->store->archiveById($id, $this->user->getId());
                 Tools::logm('archive link #' . $id);
                 Tools::redirect();
                 break;
@@ -157,7 +172,7 @@ class Poche
                 Tools::logm('config view');
                 break;
             case 'view':
-                $entry = $this->store->retrieveOneById($id);
+                $entry = $this->store->retrieveOneById($id, $this->user->getId());
                 if ($entry != NULL) {
                     Tools::logm('view link #' . $id);
                     $content = $entry['content'];
@@ -176,10 +191,10 @@ class Poche
                 }
                 break;
             default: # home view
-                $entries = $this->store->getEntriesByView($view);
+                $entries = $this->store->getEntriesByView($view, $this->user->getId());
                 $this->pagination->set_total(count($entries));
                 $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(
                     'entries' => $datas,
                     'page_links' => $page_links,
@@ -194,21 +209,21 @@ class Poche
     public function updatePassword()
     {
         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::redirect('?view=config');
         }
         else {
             if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
                 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
-                    Tools::logm('password updated');
-                    $this->messages->add('s', 'your password has been updated');
-                    $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login']));
+                    $this->messages->add('s', _('your password has been updated'));
+                    $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
                     Session::logout();
+                    Tools::logm('password updated');
                     Tools::redirect();
                 }
                 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');
                 }
             }
@@ -223,8 +238,7 @@ class Poche
                 # 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');
+                $this->messages->add('s', _('welcome to your poche'));
                 if (!empty($_POST['longlastingsession'])) {
                     $_SESSION['longlastingsession'] = 31536000;
                     $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
@@ -233,13 +247,14 @@ class Poche
                     session_set_cookie_params(0);
                 }
                 session_regenerate_id(true);
+                Tools::logm('login successful');
                 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::redirect();
         } 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::redirect();
         }
@@ -247,7 +262,7 @@ class Poche
 
     public function logout()
     {
-        $this->messages->add('s', 'see you soon!');
+        $this->messages->add('s', _('see you soon!'));
         Tools::logm('logout');
         $this->user = array();
         Session::logout();
@@ -271,14 +286,14 @@ class Poche
                 $this->action('add', $url);
                 if ($read == '1') {
                     $last_id = $this->store->getLastId();
-                    $this->store->archiveById($last_id);
+                    $this->action('toggle_archive', $url, $last_id);
                 }
             }
 
             # the second <ol> is for read links
             $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::redirect();
     }
@@ -300,14 +315,14 @@ class Poche
                 $this->action('add', $url);
                 if ($read == '1') {
                     $last_id = $this->store->getLastId();
-                    $this->store->archiveById($last_id);
+                    $this->action('toggle_archive', $url, $last_id);
                 }
             }
             
             # the second <ul> is for read links
             $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::redirect();
     }
@@ -327,16 +342,17 @@ class Poche
                 // if ($attr_value == 'favorite' && $attr_value == 'true') {
                 //     $last_id = $this->store->getLastId();
                 //     $this->store->favoriteById($last_id);
+                //     $this->action('toogle_fav', $url, $last_id);
                 // }
                 // if ($attr_value == 'archive' && $attr_value == 'true') {
                 //     $last_id = $this->store->getLastId();
-                //     $this->store->archiveById($last_id);
+                //     $this->action('toggle_archive', $url, $last_id);
                 // }
             }
             if ($url->isCorrect())
                 $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::redirect();
     }
@@ -356,7 +372,7 @@ class Poche
 
     public function export()
     {
-        $entries = $this->store->retrieveAll();
+        $entries = $this->store->retrieveAll($this->user->getId());
         echo $this->tpl->render('export.twig', array(
             'export' => Tools::renderJson($entries),
         ));
index ef47730f46a401c2fc2060aecde55eeaf9c7a2ed..6dac7839719472e952b7ae6e356d3da1d310f3fa 100644 (file)
@@ -17,17 +17,34 @@ class User
     public $email;
     public $config;
 
-    function __construct($user)
+    function __construct($user = array())
     {
-        $this->id = $user['id'];
-        $this->username = $user['username'];
-        $this->name = $user['name'];
-        $this->password = $user['password'];
-        $this->email = $user['email'];
-        $this->config = $user['config'];
+        if ($user != array()) {
+            $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) {
+    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;
     }
 }
\ No newline at end of file
index d0c686f0e4754368ab72329f67b869fecef42731..a8a9c032f0c57c3853ed4841465b7204587f1a10 100644 (file)
@@ -10,7 +10,7 @@
 
 define ('POCHE_VERSION', '1.0-beta');
 define ('MODE_DEMO', FALSE);
-define ('DEBUG_POCHE', FALSE);
+define ('DEBUG_POCHE', TRUE);
 define ('CONVERT_LINKS_FOOTNOTES', FALSE);
 define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE);
 define ('DOWNLOAD_PICTURES', FALSE);
index ad20937d4335208a0905034c1439ef218d9ab5fc..c9d85dcc2e436463603268b3f081a3bf7f1d5d64 100644 (file)
@@ -44,8 +44,4 @@ class File extends Store {
     public function getLastId() {
 
     }
-
-    public function updateContentById($id) {
-
-    }
 }
index 78254a5f192901d3ac03d582fcac625faf827c19..8b7f83dafb222476eb78760c5738d9f4a5ad8322 100644 (file)
@@ -192,11 +192,4 @@ class Mysql extends Store {
         parent::__construct();
         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);
-    }
 }
index 3cc5276d13c69a40b13f8052758004641113db58..4c628dc1602f605971a2c208e84abf77a0a64280 100644 (file)
@@ -57,9 +57,9 @@ class Sqlite extends Store {
     }
 
     public function login($username, $password) {
-        $sql    = "SELECT * FROM users WHERE username=? AND password=?";
-        $query  = $this->executeQuery($sql, array($username, $password));
-        $login  = $query->fetchAll();
+        $sql = "SELECT * FROM users WHERE username=? AND password=?";
+        $query = $this->executeQuery($sql, array($username, $password));
+        $login = $query->fetchAll();
 
         $user = array();
         if (isset($login[0])) {
@@ -76,9 +76,9 @@ class Sqlite extends Store {
 
     public function updatePassword($id, $password)
     {
-        $sql_update     = "UPDATE users SET password=? WHERE id=?";
-        $params_update  = array($password, $id);
-        $query          = $this->executeQuery($sql_update, $params_update);
+        $sql_update = "UPDATE users SET password=? WHERE id=?";
+        $params_update = array($password, $id);
+        $query = $this->executeQuery($sql_update, $params_update);
     }
 
     private function executeQuery($sql, $params) {
@@ -94,27 +94,27 @@ class Sqlite extends Store {
         }
     }
 
-    public function retrieveAll() {
-        $sql        = "SELECT * FROM entries ORDER BY id";
-        $query      = $this->executeQuery($sql, array());
+    public function retrieveAll($user_id) {
+        $sql        = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
+        $query      = $this->executeQuery($sql, array($user_id));
         $entries    = $query->fetchAll();
 
         return $entries;
     }
 
-    public function retrieveOneById($id) {
+    public function retrieveOneById($id, $user_id) {
         parent::__construct();
 
         $entry  = NULL;
-        $sql    = "SELECT * FROM entries WHERE id=?";
-        $params = array(intval($id));
+        $sql    = "SELECT * FROM entries WHERE id=? AND user_id=?";
+        $params = array(intval($id), $user_id);
         $query  = $this->executeQuery($sql, $params);
         $entry  = $query->fetchAll();
 
         return $entry[0];
     }
 
-    public function getEntriesByView($view, $limit = '') {
+    public function getEntriesByView($view, $user_id, $limit = '') {
         parent::__construct();
 
         switch ($_SESSION['sort'])
@@ -139,54 +139,54 @@ class Sqlite extends Store {
         switch ($view)
         {
             case 'archive':
-                $sql    = "SELECT * FROM entries WHERE is_read=? " . $order;
-                $params = array(-1);
+                $sql    = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
+                $params = array($user_id, -1);
                 break;
             case 'fav' :
-                $sql    = "SELECT * FROM entries WHERE is_fav=? " . $order;
-                $params = array(-1);
+                $sql    = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
+                $params = array($user_id, -1);
                 break;
             default:
-                $sql    = "SELECT * FROM entries WHERE is_read=? " . $order;
-                $params = array(0);
+                $sql    = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
+                $params = array($user_id, 0);
                 break;
         }
 
         $sql .= ' ' . $limit;
 
-        $query      = $this->executeQuery($sql, $params);
-        $entries    = $query->fetchAll();
+        $query = $this->executeQuery($sql, $params);
+        $entries = $query->fetchAll();
 
         return $entries;
     }
 
-    public function add($url, $title, $content) {
+    public function add($url, $title, $content, $user_id) {
         parent::__construct();
-        $sql_action     = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
-        $params_action  = array($url, $title, $content);
-        $query          = $this->executeQuery($sql_action, $params_action);
+        $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
+        $params_action = array($url, $title, $content, $user_id);
+        $query = $this->executeQuery($sql_action, $params_action);
         return $query;
     }
 
-    public function deleteById($id) {
+    public function deleteById($id, $user_id) {
         parent::__construct();
-        $sql_action     = "DELETE FROM entries WHERE id=?";
-        $params_action  = array($id);
+        $sql_action     = "DELETE FROM entries WHERE id=? AND user_id=?";
+        $params_action  = array($id, $user_id);
         $query          = $this->executeQuery($sql_action, $params_action);
         return $query;
     }
 
-    public function favoriteById($id) {
+    public function favoriteById($id, $user_id) {
         parent::__construct();
-        $sql_action     = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
-        $params_action  = array($id);
+        $sql_action     = "UPDATE entries SET is_fav=~is_fav WHERE id=? AND user_id=?";
+        $params_action  = array($id, $user_id);
         $query          = $this->executeQuery($sql_action, $params_action);
     }
 
-    public function archiveById($id) {
+    public function archiveById($id, $user_id) {
         parent::__construct();
-        $sql_action     = "UPDATE entries SET is_read=~is_read WHERE id=?";
-        $params_action  = array($id);
+        $sql_action     = "UPDATE entries SET is_read=~is_read WHERE id=? AND user_id=?";
+        $params_action  = array($id, $user_id);
         $query          = $this->executeQuery($sql_action, $params_action);
     }
 
@@ -194,11 +194,4 @@ class Sqlite extends Store {
         parent::__construct();
         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);
-    }
 }
index 5f8939b9ba4733d31a56de3378ef67cbc47b3518..d6e630143cef763f854aff3adb6cc947db8f68bf 100644 (file)
@@ -52,8 +52,4 @@ class Store {
     public function getLastId() {
 
     }
-
-    public function updateContentById($id) {
-
-    }
 }