]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
19 function __construct ()
22 if (! $this- > checkBeforeInstall ()) {
25 $this- > store
= new Database ();
27 $this- > messages
= new Messages ();
30 if (! $this- > store
-> isInstalled ())
37 * all checks before installation.
40 private function checkBeforeInstall ()
44 if ( file_exists ( './install' ) && ! DEBUG_POCHE
) {
45 Tools
:: logm ( 'folder /install exists' );
46 $msg = 'If you want to update your poche, you just have to delete /install folder. <br />To install your poche with sqlite, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.' ;
50 if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
51 Tools
:: logm ( 'you don \' t have write access on db file' );
52 $msg = 'You don \' t have write access on ' . STORAGE_SQLITE
. ' file.' ;
57 echo $this- > tpl
-> render ( 'error.twig' , array (
65 private function initTpl ()
68 $loader = new Twig_Loader_Filesystem ( TPL
);
70 $twig_params = array ();
73 $twig_params = array ( 'cache' => CACHE
);
75 $this- > tpl
= new Twig_Environment ( $loader , $twig_params );
76 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
77 # filter to display domain name of an url
78 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
79 $this- > tpl
-> addFilter ( $filter );
81 # filter for reading time
82 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
83 $this- > tpl
-> addFilter ( $filter );
86 private function init ()
91 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
92 $this- > user
= $_SESSION [ 'poche_user' ];
95 # fake user, just for install & login screens
96 $this- > user
= new User ();
97 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
101 $language = $this- > user
-> getConfigValue ( 'language' );
102 putenv ( 'LC_ALL=' . $language );
103 setlocale ( LC_ALL
, $language );
104 bindtextdomain ( $language , LOCALE
);
105 textdomain ( $language );
108 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
111 private function install ()
113 Tools
:: logm ( 'poche still not installed' );
114 echo $this- > tpl
-> render ( 'install.twig' , array (
115 'token' => Session
:: getToken ()
117 if ( isset ( $_GET [ 'install' ])) {
118 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
119 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
120 # let's rock, install poche baby !
121 $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
123 Tools
:: logm ( 'poche is now installed' );
127 Tools
:: logm ( 'error during installation' );
134 public function getDefaultConfig ()
137 'pager' => PAGINATION
,
143 * Call action (mark as fav, archive, delete, etc.)
145 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
150 if ( $parametres_url = $url- > fetchContent ()) {
151 if ( $this- > store
-> add ( $url- > getUrl (), $parametres_url [ 'title' ], $parametres_url [ 'content' ], $this- > user
-> getId ())) {
152 Tools
:: logm ( 'add link ' . $url- > getUrl ());
154 if ( STORAGE
== 'postgres' ) {
155 $sequence = 'entries_id_seq' ;
157 $last_id = $this- > store
-> getLastId ( $sequence );
158 if ( DOWNLOAD_PICTURES
) {
159 $content = filtre_picture ( $parametres_url [ 'content' ], $url- > getUrl (), $last_id );
160 Tools
:: logm ( 'updating content article' );
161 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
164 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
169 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
170 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
176 $this- > messages
-> add ( 'e' , _ ( 'error during fetching content : the link wasn \' t added' ));
177 Tools
:: logm ( 'error during content fetch ' . $url- > getUrl ());
185 $msg = 'delete link #' . $id ;
186 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
187 if ( DOWNLOAD_PICTURES
) {
188 remove_directory ( ABS_PATH
. $id );
190 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
193 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
194 $msg = 'error : can \' t delete link #' . $id ;
197 Tools
:: redirect ( '?' );
200 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
201 Tools
:: logm ( 'mark as favorite link #' . $id );
206 case 'toggle_archive' :
207 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
208 Tools
:: logm ( 'archive link #' . $id );
214 Tools
:: logm ( 'action ' . $action . 'doesn \' t exist' );
219 function displayView ( $view , $id = 0 )
226 $dev = $this- > getPocheVersion ( 'dev' );
227 $prod = $this- > getPocheVersion ( 'prod' );
228 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
229 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
233 'compare_dev' => $compare_dev ,
234 'compare_prod' => $compare_prod ,
236 Tools
:: logm ( 'config view' );
239 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
240 if ( $entry != NULL ) {
241 Tools
:: logm ( 'view link #' . $id );
242 $content = $entry [ 'content' ];
243 if ( function_exists ( 'tidy_parse_string' )) {
244 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
245 $tidy- > cleanRepair ();
246 $content = $tidy- > value
;
250 'content' => $content ,
254 Tools
:: logm ( 'error in view call : entry is null' );
258 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
259 $this- > pagination
-> set_total ( count ( $entries ));
260 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
261 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
264 'page_links' => $page_links ,
266 Tools
:: logm ( 'display ' . $view . ' view' );
274 * update the password of the current user.
275 * if MODE_DEMO is TRUE, the password can't be updated.
276 * @todo add the return value
277 * @todo set the new password in function header like this updatePassword($newPassword)
280 public function updatePassword ()
283 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
284 Tools
:: logm ( 'in demo mode, you can \' t do this' );
285 Tools
:: redirect ( '?view=config' );
288 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
289 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
290 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
291 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
293 Tools
:: logm ( 'password updated' );
297 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
298 Tools
:: redirect ( '?view=config' );
305 * checks if login & password are correct and save the user in session.
306 * it redirects the user to the $referer link
307 * @param string $referer the url to redirect after login
308 * @todo add the return value
311 public function login ( $referer )
313 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
314 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
315 if ( $user != array ()) {
316 # Save login into Session
317 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
319 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
320 if (! empty ( $_POST [ 'longlastingsession' ])) {
321 $_SESSION [ 'longlastingsession' ] = 31536000 ;
322 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
323 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
325 session_set_cookie_params ( 0 );
327 session_regenerate_id ( true );
328 Tools
:: logm ( 'login successful' );
329 Tools
:: redirect ( $referer );
331 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
332 Tools
:: logm ( 'login failed' );
335 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
336 Tools
:: logm ( 'login failed' );
342 * log out the poche user. It cleans the session.
343 * @todo add the return value
346 public function logout ()
348 $this- > user
= array ();
350 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
351 Tools
:: logm ( 'logout' );
356 * import from Instapaper. poche needs a ./instapaper-export.html file
357 * @todo add the return value
360 private function importFromInstapaper ()
362 # TODO gestion des articles favs
363 $html = new simple_html_dom ();
364 $html- > load_file ( './instapaper-export.html' );
365 Tools
:: logm ( 'starting import from instapaper' );
369 foreach ( $html- > find ( 'ol' ) as $ul )
371 foreach ( $ul- > find ( 'li' ) as $li )
374 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
375 $this- > action ( 'add' , $url , 0 , TRUE );
378 if ( STORAGE
== 'postgres' ) {
379 $sequence = 'entries_id_seq' ;
381 $last_id = $this- > store
-> getLastId ( $sequence );
382 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
386 # the second <ol> is for read links
389 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
390 Tools
:: logm ( 'import from instapaper completed' );
395 * import from Pocket. poche needs a ./ril_export.html file
396 * @todo add the return value
399 private function importFromPocket ()
401 # TODO gestion des articles favs
402 $html = new simple_html_dom ();
403 $html- > load_file ( './ril_export.html' );
404 Tools
:: logm ( 'starting import from pocket' );
408 foreach ( $html- > find ( 'ul' ) as $ul )
410 foreach ( $ul- > find ( 'li' ) as $li )
413 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
414 $this- > action ( 'add' , $url , 0 , TRUE );
417 if ( STORAGE
== 'postgres' ) {
418 $sequence = 'entries_id_seq' ;
420 $last_id = $this- > store
-> getLastId ( $sequence );
421 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
425 # the second <ul> is for read links
428 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
429 Tools
:: logm ( 'import from pocket completed' );
434 * import from Readability. poche needs a ./readability file
435 * @todo add the return value
438 private function importFromReadability ()
440 # TODO gestion des articles lus / favs
441 $str_data = file_get_contents ( "./readability" );
442 $data = json_decode ( $str_data , true );
443 Tools
:: logm ( 'starting import from Readability' );
445 foreach ( $data as $key => $value ) {
449 foreach ( $value as $attr => $attr_value ) {
450 if ( $attr == 'article__url' ) {
451 $url = new Url ( base64_encode ( $attr_value ));
454 if ( STORAGE
== 'postgres' ) {
455 $sequence = 'entries_id_seq' ;
457 if ( $attr_value == 'true' ) {
458 if ( $attr == 'favorite' ) {
461 if ( $attr == 'archive' ) {
467 if (! is_null ( $url ) && $url- > isCorrect ()) {
468 $this- > action ( 'add' , $url , 0 , TRUE );
471 $last_id = $this- > store
-> getLastId ( $sequence );
472 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
475 $last_id = $this- > store
-> getLastId ( $sequence );
476 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
480 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
481 Tools
:: logm ( 'import from Readability completed' );
486 * import datas into your poche
487 * @param string $from name of the service to import : pocket, instapaper or readability
488 * @todo add the return value
491 public function import ( $from )
493 if ( $from == 'pocket' ) {
494 return $this- > importFromPocket ();
496 else if ( $from == 'readability' ) {
497 return $this- > importFromReadability ();
499 else if ( $from == 'instapaper' ) {
500 return $this- > importFromInstapaper ();
505 * export poche entries in json
506 * @return json all poche entries
508 public function export ()
510 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
511 echo $this- > tpl
-> render ( 'export.twig' , array (
512 'export' => Tools
:: renderJson ( $entries ),
514 Tools
:: logm ( 'export view' );
518 * Checks online the latest version of poche and cache it
519 * @param string $which 'prod' or 'dev'
520 * @return string latest $which version
522 private function getPocheVersion ( $which = 'prod' )
524 $cache_file = CACHE
. '/' . $which ;
526 # checks if the cached version file exists
527 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
528 $version = file_get_contents ( $cache_file );
530 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
531 file_put_contents ( $cache_file , $version , LOCK_EX
);