aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rw-r--r--inc/3rdparty/paginator.php198
-rw-r--r--inc/poche/Poche.class.php40
-rw-r--r--inc/poche/Tools.class.php2
-rw-r--r--inc/poche/config.inc.php12
-rw-r--r--inc/store/sqlite.class.php4
5 files changed, 238 insertions, 18 deletions
diff --git a/inc/3rdparty/paginator.php b/inc/3rdparty/paginator.php
new file mode 100644
index 00000000..e8801555
--- /dev/null
+++ b/inc/3rdparty/paginator.php
@@ -0,0 +1,198 @@
1<?php
2/*
3 * PHP Pagination Class
4 *
5 * @author David Carr - dave@daveismyname.com - http://www.daveismyname.com
6 * @version 1.0
7 * @date October 20, 2013
8 */
9class Paginator{
10
11 /**
12 * set the number of items per page.
13 *
14 * @var numeric
15 */
16 private $_perPage;
17
18 /**
19 * set get parameter for fetching the page number
20 *
21 * @var string
22 */
23 private $_instance;
24
25 /**
26 * sets the page number.
27 *
28 * @var numeric
29 */
30 private $_page;
31
32 /**
33 * set the limit for the data source
34 *
35 * @var string
36 */
37 private $_limit;
38
39 /**
40 * set the total number of records/items.
41 *
42 * @var numeric
43 */
44 private $_totalRows = 0;
45
46
47
48 /**
49 * __construct
50 *
51 * pass values when class is istantiated
52 *
53 * @param numeric $_perPage sets the number of iteems per page
54 * @param numeric $_instance sets the instance for the GET parameter
55 */
56 public function __construct($perPage,$instance){
57 $this->_instance = $instance;
58 $this->_perPage = $perPage;
59 $this->set_instance();
60 }
61
62 /**
63 * get_start
64 *
65 * creates the starting point for limiting the dataset
66 * @return numeric
67 */
68 private function get_start(){
69 return ($this->_page * $this->_perPage) - $this->_perPage;
70 }
71
72 /**
73 * set_instance
74 *
75 * sets the instance parameter, if numeric value is 0 then set to 1
76 *
77 * @var numeric
78 */
79 private function set_instance(){
80 $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]);
81 $this->_page = ($this->_page == 0 ? 1 : $this->_page);
82 }
83
84 /**
85 * set_total
86 *
87 * collect a numberic value and assigns it to the totalRows
88 *
89 * @var numeric
90 */
91 public function set_total($_totalRows){
92 $this->_totalRows = $_totalRows;
93 }
94
95 /**
96 * get_limit
97 *
98 * returns the limit for the data source, calling the get_start method and passing in the number of items perp page
99 *
100 * @return string
101 */
102 public function get_limit(){
103 return "LIMIT ".$this->get_start().",$this->_perPage";
104 }
105
106 /**
107 * page_links
108 *
109 * create the html links for navigating through the dataset
110 *
111 * @var sting $path optionally set the path for the link
112 * @var sting $ext optionally pass in extra parameters to the GET
113 * @return string returns the html menu
114 */
115 public function page_links($path='?',$ext=null)
116 {
117 $adjacents = "2";
118 $prev = $this->_page - 1;
119 $next = $this->_page + 1;
120 $lastpage = ceil($this->_totalRows/$this->_perPage);
121 $lpm1 = $lastpage - 1;
122
123 $pagination = "";
124 if($lastpage > 1)
125 {
126 $pagination .= "<div class='pagination'>";
127 if ($this->_page > 1)
128 $pagination.= "<a href='".$path."$this->_instance=$prev"."$ext'>« previous</a>";
129 else
130 $pagination.= "<span class='disabled'>« previous</span>";
131
132 if ($lastpage < 7 + ($adjacents * 2))
133 {
134 for ($counter = 1; $counter <= $lastpage; $counter++)
135 {
136 if ($counter == $this->_page)
137 $pagination.= "<span class='current'>$counter</span>";
138 else
139 $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";
140 }
141 }
142 elseif($lastpage > 5 + ($adjacents * 2))
143 {
144 if($this->_page < 1 + ($adjacents * 2))
145 {
146 for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
147 {
148 if ($counter == $this->_page)
149 $pagination.= "<span class='current'>$counter</span>";
150 else
151 $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";
152 }
153 $pagination.= "...";
154 $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>";
155 $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>";
156 }
157 elseif($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2))
158 {
159 $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>";
160 $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>";
161 $pagination.= "...";
162 for ($counter = $this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++)
163 {
164 if ($counter == $this->_page)
165 $pagination.= "<span class='current'>$counter</span>";
166 else
167 $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";
168 }
169 $pagination.= "..";
170 $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>";
171 $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>";
172 }
173 else
174 {
175 $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>";
176 $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>";
177 $pagination.= "..";
178 for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
179 {
180 if ($counter == $this->_page)
181 $pagination.= "<span class='current'>$counter</span>";
182 else
183 $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";
184 }
185 }
186 }
187
188 if ($this->_page < $counter - 1)
189 $pagination.= "<a href='".$path."$this->_instance=$next"."$ext'>next »</a>";
190 else
191 $pagination.= "<span class='disabled'>next »</span>";
192 $pagination.= "</div>\n";
193 }
194
195
196 return $pagination;
197 }
198}
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 80bf6919..789d6647 100644
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -13,11 +13,13 @@ class Poche
13 public $store; 13 public $store;
14 public $tpl; 14 public $tpl;
15 public $messages; 15 public $messages;
16 public $pagination;
16 17
17 function __construct($storage_type) 18 function __construct($storage_type)
18 { 19 {
19 $this->store = new $storage_type(); 20 $this->store = new $storage_type();
20 $this->init(); 21 $this->init();
22 $this->messages = new Messages();
21 23
22 # installation 24 # installation
23 if(!$this->store->isInstalled()) 25 if(!$this->store->isInstalled())
@@ -46,6 +48,8 @@ class Poche
46 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); 48 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
47 $this->tpl->addFilter($filter); 49 $this->tpl->addFilter($filter);
48 50
51 $this->pagination = new Paginator(PAGINATION, 'p');
52
49 Tools::initPhp(); 53 Tools::initPhp();
50 Session::init(); 54 Session::init();
51 } 55 }
@@ -54,7 +58,7 @@ class Poche
54 { 58 {
55 Tools::logm('poche still not installed'); 59 Tools::logm('poche still not installed');
56 echo $this->tpl->render('install.twig', array( 60 echo $this->tpl->render('install.twig', array(
57 'token' => Session::getToken(), 61 'token' => Session::getToken()
58 )); 62 ));
59 if (isset($_GET['install'])) { 63 if (isset($_GET['install'])) {
60 if (($_POST['password'] == $_POST['password_repeat']) 64 if (($_POST['password'] == $_POST['password_repeat'])
@@ -62,6 +66,11 @@ class Poche
62 # let's rock, install poche baby ! 66 # let's rock, install poche baby !
63 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])); 67 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
64 Session::logout(); 68 Session::logout();
69 Tools::logm('poche is now installed');
70 Tools::redirect();
71 }
72 else {
73 Tools::logm('error during installation');
65 Tools::redirect(); 74 Tools::redirect();
66 } 75 }
67 } 76 }
@@ -89,30 +98,32 @@ class Poche
89 if (DOWNLOAD_PICTURES) { 98 if (DOWNLOAD_PICTURES) {
90 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id); 99 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
91 } 100 }
92 #$msg->add('s', _('the link has been added successfully')); 101 $this->messages->add('s', _('the link has been added successfully'));
93 } 102 }
94 else { 103 else {
95 #$msg->add('e', _('error during insertion : the link wasn\'t added')); 104 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
96 Tools::logm('error during insertion : the link wasn\'t added'); 105 Tools::logm('error during insertion : the link wasn\'t added');
97 } 106 }
98 } 107 }
99 else { 108 else {
100 #$msg->add('e', _('error during url preparation : the link wasn\'t added')); 109 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
101 Tools::logm('error during content fetch'); 110 Tools::logm('error during content fetch');
102 } 111 }
112 Tools::redirect();
103 break; 113 break;
104 case 'delete': 114 case 'delete':
105 if ($this->store->deleteById($id)) { 115 if ($this->store->deleteById($id)) {
106 if (DOWNLOAD_PICTURES) { 116 if (DOWNLOAD_PICTURES) {
107 remove_directory(ABS_PATH . $id); 117 remove_directory(ABS_PATH . $id);
108 } 118 }
109 #$msg->add('s', _('the link has been deleted successfully')); 119 $this->messages->add('s', _('the link has been deleted successfully'));
110 Tools::logm('delete link #' . $id); 120 Tools::logm('delete link #' . $id);
111 } 121 }
112 else { 122 else {
113 #$msg->add('e', _('the link wasn\'t deleted')); 123 $this->messages->add('e', _('the link wasn\'t deleted'));
114 Tools::logm('error : can\'t delete link #' . $id); 124 Tools::logm('error : can\'t delete link #' . $id);
115 } 125 }
126 Tools::redirect();
116 break; 127 break;
117 case 'toggle_fav' : 128 case 'toggle_fav' :
118 $this->store->favoriteById($id); 129 $this->store->favoriteById($id);
@@ -169,9 +180,14 @@ class Poche
169 break; 180 break;
170 default: # home view 181 default: # home view
171 $entries = $this->store->getEntriesByView($view); 182 $entries = $this->store->getEntriesByView($view);
183 $this->pagination->set_total(count($entries));
184 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
185 $datas = $this->store->getEntriesByView($view, $this->pagination->get_limit());
172 $tpl_vars = array( 186 $tpl_vars = array(
173 'entries' => $entries, 187 'entries' => $datas,
188 'page_links' => $page_links,
174 ); 189 );
190 Tools::logm('display ' . $view . ' view');
175 break; 191 break;
176 } 192 }
177 193
@@ -183,6 +199,7 @@ class Poche
183 if (MODE_DEMO) { 199 if (MODE_DEMO) {
184 $this->messages->add('i', 'in demo mode, you can\'t update your password'); 200 $this->messages->add('i', 'in demo mode, you can\'t update your password');
185 Tools::logm('in demo mode, you can\'t do this'); 201 Tools::logm('in demo mode, you can\'t do this');
202 Tools::redirect('?view=config');
186 } 203 }
187 else { 204 else {
188 if (isset($_POST['password']) && isset($_POST['password_repeat'])) { 205 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
@@ -195,6 +212,7 @@ class Poche
195 } 212 }
196 else { 213 else {
197 $this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields'); 214 $this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields');
215 Tools::redirect('?view=config');
198 } 216 }
199 } 217 }
200 } 218 }
@@ -205,7 +223,7 @@ class Poche
205 if (!empty($_POST['login']) && !empty($_POST['password'])) { 223 if (!empty($_POST['login']) && !empty($_POST['password'])) {
206 if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) { 224 if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) {
207 Tools::logm('login successful'); 225 Tools::logm('login successful');
208 $this->messages->add('s', 'login successful, welcome to your poche'); 226 $this->messages->add('s', 'welcome to your poche');
209 if (!empty($_POST['longlastingsession'])) { 227 if (!empty($_POST['longlastingsession'])) {
210 $_SESSION['longlastingsession'] = 31536000; 228 $_SESSION['longlastingsession'] = 31536000;
211 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; 229 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
@@ -216,11 +234,11 @@ class Poche
216 session_regenerate_id(true); 234 session_regenerate_id(true);
217 Tools::redirect($referer); 235 Tools::redirect($referer);
218 } 236 }
219 $this->messages->add('e', 'login failed, bad login or password'); 237 $this->messages->add('e', 'login failed: bad login or password');
220 Tools::logm('login failed'); 238 Tools::logm('login failed');
221 Tools::redirect(); 239 Tools::redirect();
222 } else { 240 } else {
223 $this->messages->add('e', 'login failed, you have to fill all fields'); 241 $this->messages->add('e', 'login failed: you have to fill all fields');
224 Tools::logm('login failed'); 242 Tools::logm('login failed');
225 Tools::redirect(); 243 Tools::redirect();
226 } 244 }
@@ -228,7 +246,7 @@ class Poche
228 246
229 public function logout() 247 public function logout()
230 { 248 {
231 $this->messages->add('s', 'logout successful, see you soon!'); 249 $this->messages->add('s', 'see you soon!');
232 Tools::logm('logout'); 250 Tools::logm('logout');
233 Session::logout(); 251 Session::logout();
234 Tools::redirect(); 252 Tools::redirect();
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php
index 7bc8830a..8b339ea5 100644
--- a/inc/poche/Tools.class.php
+++ b/inc/poche/Tools.class.php
@@ -197,7 +197,7 @@ class Tools
197 { 197 {
198 if (DEBUG_POCHE) { 198 if (DEBUG_POCHE) {
199 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; 199 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
200 file_put_contents('./log.txt', $t, FILE_APPEND); 200 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
201 } 201 }
202 } 202 }
203 203
diff --git a/inc/poche/config.inc.php b/inc/poche/config.inc.php
index d49df190..d91a44be 100644
--- a/inc/poche/config.inc.php
+++ b/inc/poche/config.inc.php
@@ -8,7 +8,7 @@
8 * @license http://www.wtfpl.net/ see COPYING file 8 * @license http://www.wtfpl.net/ see COPYING file
9 */ 9 */
10 10
11define ('POCHE_VERSION', '1.0-alpha'); 11define ('POCHE_VERSION', '1.0-beta');
12define ('MODE_DEMO', FALSE); 12define ('MODE_DEMO', FALSE);
13define ('DEBUG_POCHE', FALSE); 13define ('DEBUG_POCHE', FALSE);
14define ('CONVERT_LINKS_FOOTNOTES', FALSE); 14define ('CONVERT_LINKS_FOOTNOTES', FALSE);
@@ -22,24 +22,26 @@ define ('TPL', './tpl');
22define ('LOCALE', './locale'); 22define ('LOCALE', './locale');
23define ('CACHE', './cache'); 23define ('CACHE', './cache');
24define ('LANG', 'fr_FR.UTF8'); 24define ('LANG', 'fr_FR.UTF8');
25define ('PAGINATION', '10');
26define ('THEME', 'light');
25$storage_type = 'sqlite'; # sqlite, mysql, (file, not yet) 27$storage_type = 'sqlite'; # sqlite, mysql, (file, not yet)
26 28
27# /!\ Be careful if you change the lines below /!\ 29# /!\ Be careful if you change the lines below /!\
28require_once './inc/poche/Tools.class.php'; 30require_once './inc/poche/Tools.class.php';
29require_once './inc/poche/Url.class.php'; 31require_once './inc/poche/Url.class.php';
32require_once './inc/3rdparty/Session.class.php';
33require_once './inc/3rdparty/class.messages.php';
30require_once './inc/poche/Poche.class.php'; 34require_once './inc/poche/Poche.class.php';
31require_once './inc/3rdparty/Readability.php'; 35require_once './inc/3rdparty/Readability.php';
32require_once './inc/3rdparty/Encoding.php'; 36require_once './inc/3rdparty/Encoding.php';
33require_once './inc/3rdparty/Session.class.php';
34require_once './inc/store/store.class.php'; 37require_once './inc/store/store.class.php';
35require_once './inc/store/' . $storage_type . '.class.php'; 38require_once './inc/store/' . $storage_type . '.class.php';
36require_once './vendor/autoload.php'; 39require_once './vendor/autoload.php';
37require_once './inc/3rdparty/simple_html_dom.php'; 40require_once './inc/3rdparty/simple_html_dom.php';
38require_once './inc/3rdparty/class.messages.php'; 41require_once './inc/3rdparty/paginator.php';
39 42
40if (DOWNLOAD_PICTURES) { 43if (DOWNLOAD_PICTURES) {
41 require_once './inc/poche/pochePictures.php'; 44 require_once './inc/poche/pochePictures.php';
42} 45}
43 46
44$poche = new Poche($storage_type); 47$poche = new Poche($storage_type); \ No newline at end of file
45$poche->messages = new Messages(); \ No newline at end of file
diff --git a/inc/store/sqlite.class.php b/inc/store/sqlite.class.php
index a15bc095..3e391e40 100644
--- a/inc/store/sqlite.class.php
+++ b/inc/store/sqlite.class.php
@@ -114,7 +114,7 @@ class Sqlite extends Store {
114 return $entry[0]; 114 return $entry[0];
115 } 115 }
116 116
117 public function getEntriesByView($view) { 117 public function getEntriesByView($view, $limit = '') {
118 parent::__construct(); 118 parent::__construct();
119 119
120 switch ($_SESSION['sort']) 120 switch ($_SESSION['sort'])
@@ -152,6 +152,8 @@ class Sqlite extends Store {
152 break; 152 break;
153 } 153 }
154 154
155 $sql .= ' ' . $limit;
156
155 $query = $this->executeQuery($sql, $params); 157 $query = $this->executeQuery($sql, $params);
156 $entries = $query->fetchAll(); 158 $entries = $query->fetchAll();
157 159