]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Poche.class.php
multi user
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
CommitLineData
eb1af592
NL
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Poche
12{
7ce7ec4c 13 public $user;
eb1af592
NL
14 public $store;
15 public $tpl;
55821e04 16 public $messages;
6a361945 17 public $pagination;
eb1af592
NL
18
19 function __construct($storage_type)
20 {
21 $this->store = new $storage_type();
22 $this->init();
6a361945 23 $this->messages = new Messages();
eb1af592
NL
24
25 # installation
26 if(!$this->store->isInstalled())
27 {
28 $this->install();
29 }
eb1af592
NL
30 }
31
32 private function init()
33 {
7ce7ec4c
NL
34 Tools::initPhp();
35 Session::init();
8d3275be
NL
36
37 if (isset($_SESSION['poche_user'])) {
38 $this->user = $_SESSION['poche_user'];
39 }
40 else {
41 # fake user, just for install & login screens
42 $this->user = new User();
43 $this->user->setConfig($this->getDefaultConfig());
44 }
7ce7ec4c 45
eb1af592 46 # l10n
8d3275be 47 $language = $this->user->getConfigValue('language');
7ce7ec4c
NL
48 putenv('LC_ALL=' . $language);
49 setlocale(LC_ALL, $language);
50 bindtextdomain($language, LOCALE);
51 textdomain($language);
eb1af592
NL
52
53 # template engine
54 $loader = new Twig_Loader_Filesystem(TPL);
55 $this->tpl = new Twig_Environment($loader, array(
56 'cache' => CACHE,
57 ));
58 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
55821e04
NL
59 # filter to display domain name of an url
60 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
61 $this->tpl->addFilter($filter);
eb1af592 62
7ce7ec4c 63 # Pagination
8d3275be 64 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
eb1af592
NL
65 }
66
67 private function install()
68 {
69 Tools::logm('poche still not installed');
70 echo $this->tpl->render('install.twig', array(
6a361945 71 'token' => Session::getToken()
eb1af592
NL
72 ));
73 if (isset($_GET['install'])) {
74 if (($_POST['password'] == $_POST['password_repeat'])
75 && $_POST['password'] != "" && $_POST['login'] != "") {
76 # let's rock, install poche baby !
77 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
78 Session::logout();
6a361945
NL
79 Tools::logm('poche is now installed');
80 Tools::redirect();
81 }
82 else {
83 Tools::logm('error during installation');
eb1af592
NL
84 Tools::redirect();
85 }
86 }
87 exit();
88 }
89
8d3275be
NL
90 public function getDefaultConfig()
91 {
92 return array(
93 'pager' => PAGINATION,
94 'language' => LANG,
95 );
96 }
97
eb1af592
NL
98 /**
99 * Call action (mark as fav, archive, delete, etc.)
100 */
c765c367 101 public function action($action, Url $url, $id = 0)
eb1af592
NL
102 {
103 switch ($action)
104 {
105 case 'add':
106 if($parametres_url = $url->fetchContent()) {
8d3275be 107 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
eb1af592
NL
108 Tools::logm('add link ' . $url->getUrl());
109 $last_id = $this->store->getLastId();
110 if (DOWNLOAD_PICTURES) {
111 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
112 }
6a361945 113 $this->messages->add('s', _('the link has been added successfully'));
eb1af592
NL
114 }
115 else {
6a361945 116 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
eb1af592
NL
117 Tools::logm('error during insertion : the link wasn\'t added');
118 }
119 }
120 else {
6a361945 121 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
eb1af592
NL
122 Tools::logm('error during content fetch');
123 }
6a361945 124 Tools::redirect();
eb1af592
NL
125 break;
126 case 'delete':
8d3275be 127 if ($this->store->deleteById($id, $this->user->getId())) {
eb1af592
NL
128 if (DOWNLOAD_PICTURES) {
129 remove_directory(ABS_PATH . $id);
130 }
6a361945 131 $this->messages->add('s', _('the link has been deleted successfully'));
eb1af592
NL
132 Tools::logm('delete link #' . $id);
133 }
134 else {
6a361945 135 $this->messages->add('e', _('the link wasn\'t deleted'));
eb1af592
NL
136 Tools::logm('error : can\'t delete link #' . $id);
137 }
6a361945 138 Tools::redirect();
eb1af592
NL
139 break;
140 case 'toggle_fav' :
8d3275be 141 $this->store->favoriteById($id, $this->user->getId());
eb1af592 142 Tools::logm('mark as favorite link #' . $id);
55821e04 143 Tools::redirect();
eb1af592
NL
144 break;
145 case 'toggle_archive' :
8d3275be 146 $this->store->archiveById($id, $this->user->getId());
eb1af592 147 Tools::logm('archive link #' . $id);
55821e04 148 Tools::redirect();
eb1af592
NL
149 break;
150 default:
151 break;
152 }
153 }
154
155 function displayView($view, $id = 0)
156 {
157 $tpl_vars = array();
158
159 switch ($view)
160 {
eb1af592 161 case 'config':
32520785
NL
162 $dev = $this->getPocheVersion('dev');
163 $prod = $this->getPocheVersion('prod');
164 $compare_dev = version_compare(POCHE_VERSION, $dev);
165 $compare_prod = version_compare(POCHE_VERSION, $prod);
166 $tpl_vars = array(
167 'dev' => $dev,
168 'prod' => $prod,
169 'compare_dev' => $compare_dev,
170 'compare_prod' => $compare_prod,
171 );
eb1af592
NL
172 Tools::logm('config view');
173 break;
174 case 'view':
8d3275be 175 $entry = $this->store->retrieveOneById($id, $this->user->getId());
eb1af592
NL
176 if ($entry != NULL) {
177 Tools::logm('view link #' . $id);
178 $content = $entry['content'];
179 if (function_exists('tidy_parse_string')) {
180 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
181 $tidy->cleanRepair();
182 $content = $tidy->value;
183 }
184 $tpl_vars = array(
185 'entry' => $entry,
186 'content' => $content,
187 );
188 }
189 else {
190 Tools::logm('error in view call : entry is NULL');
191 }
192 break;
193 default: # home view
8d3275be 194 $entries = $this->store->getEntriesByView($view, $this->user->getId());
6a361945
NL
195 $this->pagination->set_total(count($entries));
196 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
8d3275be 197 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
eb1af592 198 $tpl_vars = array(
6a361945
NL
199 'entries' => $datas,
200 'page_links' => $page_links,
eb1af592 201 );
6a361945 202 Tools::logm('display ' . $view . ' view');
eb1af592
NL
203 break;
204 }
205
206 return $tpl_vars;
207 }
c765c367
NL
208
209 public function updatePassword()
210 {
55821e04 211 if (MODE_DEMO) {
8d3275be 212 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
55821e04 213 Tools::logm('in demo mode, you can\'t do this');
6a361945 214 Tools::redirect('?view=config');
55821e04
NL
215 }
216 else {
217 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
218 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
8d3275be
NL
219 $this->messages->add('s', _('your password has been updated'));
220 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
c765c367 221 Session::logout();
8d3275be 222 Tools::logm('password updated');
c765c367
NL
223 Tools::redirect();
224 }
225 else {
8d3275be 226 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
6a361945 227 Tools::redirect('?view=config');
c765c367
NL
228 }
229 }
230 }
231 }
232
233 public function login($referer)
234 {
235 if (!empty($_POST['login']) && !empty($_POST['password'])) {
7ce7ec4c
NL
236 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
237 if ($user != array()) {
238 # Save login into Session
239 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
240
8d3275be 241 $this->messages->add('s', _('welcome to your poche'));
c765c367
NL
242 if (!empty($_POST['longlastingsession'])) {
243 $_SESSION['longlastingsession'] = 31536000;
244 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
245 session_set_cookie_params($_SESSION['longlastingsession']);
246 } else {
247 session_set_cookie_params(0);
248 }
249 session_regenerate_id(true);
8d3275be 250 Tools::logm('login successful');
c765c367
NL
251 Tools::redirect($referer);
252 }
8d3275be 253 $this->messages->add('e', _('login failed: bad login or password'));
c765c367
NL
254 Tools::logm('login failed');
255 Tools::redirect();
256 } else {
8d3275be 257 $this->messages->add('e', _('login failed: you have to fill all fields'));
c765c367
NL
258 Tools::logm('login failed');
259 Tools::redirect();
260 }
261 }
262
263 public function logout()
264 {
8d3275be 265 $this->messages->add('s', _('see you soon!'));
c765c367 266 Tools::logm('logout');
7ce7ec4c 267 $this->user = array();
c765c367
NL
268 Session::logout();
269 Tools::redirect();
270 }
271
63c35580 272 private function importFromInstapaper()
c765c367 273 {
7f959169 274 # TODO gestion des articles favs
a62788c6
NL
275 $html = new simple_html_dom();
276 $html->load_file('./instapaper-export.html');
277
278 $read = 0;
279 $errors = array();
280 foreach($html->find('ol') as $ul)
281 {
282 foreach($ul->find('li') as $li)
283 {
284 $a = $li->find('a');
285 $url = new Url(base64_encode($a[0]->href));
286 $this->action('add', $url);
287 if ($read == '1') {
288 $last_id = $this->store->getLastId();
8d3275be 289 $this->action('toggle_archive', $url, $last_id);
a62788c6
NL
290 }
291 }
7f959169
NL
292
293 # the second <ol> is for read links
a62788c6
NL
294 $read = 1;
295 }
8d3275be 296 $this->messages->add('s', _('import from instapaper completed'));
63c35580
NL
297 Tools::logm('import from instapaper completed');
298 Tools::redirect();
299 }
c765c367 300
63c35580
NL
301 private function importFromPocket()
302 {
7f959169 303 # TODO gestion des articles favs
63c35580
NL
304 $html = new simple_html_dom();
305 $html->load_file('./ril_export.html');
306
307 $read = 0;
308 $errors = array();
309 foreach($html->find('ul') as $ul)
310 {
311 foreach($ul->find('li') as $li)
c765c367 312 {
63c35580
NL
313 $a = $li->find('a');
314 $url = new Url(base64_encode($a[0]->href));
315 $this->action('add', $url);
316 if ($read == '1') {
317 $last_id = $this->store->getLastId();
8d3275be 318 $this->action('toggle_archive', $url, $last_id);
c765c367 319 }
c765c367 320 }
7f959169
NL
321
322 # the second <ul> is for read links
63c35580 323 $read = 1;
c765c367 324 }
8d3275be 325 $this->messages->add('s', _('import from pocket completed'));
63c35580
NL
326 Tools::logm('import from pocket completed');
327 Tools::redirect();
328 }
c765c367 329
63c35580
NL
330 private function importFromReadability()
331 {
7f959169 332 # TODO gestion des articles lus / favs
63c35580
NL
333 $str_data = file_get_contents("./readability");
334 $data = json_decode($str_data,true);
335
336 foreach ($data as $key => $value) {
337 $url = '';
7f959169
NL
338 foreach ($value as $attr => $attr_value) {
339 if ($attr == 'article__url') {
340 $url = new Url(base64_encode($attr_value));
c765c367 341 }
7f959169
NL
342 // if ($attr_value == 'favorite' && $attr_value == 'true') {
343 // $last_id = $this->store->getLastId();
344 // $this->store->favoriteById($last_id);
8d3275be 345 // $this->action('toogle_fav', $url, $last_id);
7f959169
NL
346 // }
347 // if ($attr_value == 'archive' && $attr_value == 'true') {
348 // $last_id = $this->store->getLastId();
8d3275be 349 // $this->action('toggle_archive', $url, $last_id);
7f959169 350 // }
c765c367 351 }
63c35580
NL
352 if ($url->isCorrect())
353 $this->action('add', $url);
c765c367 354 }
8d3275be 355 $this->messages->add('s', _('import from Readability completed'));
63c35580
NL
356 Tools::logm('import from Readability completed');
357 Tools::redirect();
c765c367
NL
358 }
359
63c35580 360 public function import($from)
c765c367 361 {
63c35580
NL
362 if ($from == 'pocket') {
363 $this->importFromPocket();
364 }
365 else if ($from == 'readability') {
366 $this->importFromReadability();
367 }
368 else if ($from == 'instapaper') {
369 $this->importFromInstapaper();
370 }
371 }
c765c367 372
63c35580
NL
373 public function export()
374 {
8d3275be 375 $entries = $this->store->retrieveAll($this->user->getId());
63c35580
NL
376 echo $this->tpl->render('export.twig', array(
377 'export' => Tools::renderJson($entries),
378 ));
379 Tools::logm('export view');
c765c367 380 }
32520785
NL
381
382 private function getPocheVersion($which = 'prod')
383 {
384 $cache_file = CACHE . '/' . $which;
385 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
386 $version = file_get_contents($cache_file);
387 } else {
388 $version = file_get_contents('http://www.inthepoche.com/' . $which);
389 file_put_contents($cache_file, $version, LOCK_EX);
390 }
391 return $version;
392 }
eb1af592 393}