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