]>
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 ()
45 if (! is_writable ( CACHE
)) {
46 Tools
:: logm ( 'you don \' t have write access on cache directory' );
47 die ( 'You don \' t have write access on cache directory.' );
49 else if ( file_exists ( './install/update.php' ) && ! DEBUG_POCHE
) {
50 $msg = 'A poche update is needed. Please execute this update <a href="install/update.php">by clicking here</a>. If you have already do the update, please delete /install folder.' ;
53 else if ( file_exists ( './install' ) && ! DEBUG_POCHE
) {
54 $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.' ;
57 else if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
58 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
59 $msg = 'You don \' t have write access on sqlite file.' ;
64 echo $this- > tpl
-> render ( 'error.twig' , array (
72 private function initTpl ()
75 $loader = new Twig_Loader_Filesystem ( TPL
);
77 $twig_params = array ();
80 $twig_params = array ( 'cache' => CACHE
);
82 $this- > tpl
= new Twig_Environment ( $loader , $twig_params );
83 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
84 # filter to display domain name of an url
85 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
86 $this- > tpl
-> addFilter ( $filter );
88 # filter for reading time
89 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
90 $this- > tpl
-> addFilter ( $filter );
93 private function init ()
98 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
99 $this- > user
= $_SESSION [ 'poche_user' ];
102 # fake user, just for install & login screens
103 $this- > user
= new User ();
104 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
108 $language = $this- > user
-> getConfigValue ( 'language' );
109 putenv ( 'LC_ALL=' . $language );
110 setlocale ( LC_ALL
, $language );
111 bindtextdomain ( $language , LOCALE
);
112 textdomain ( $language );
115 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
118 private function install ()
120 Tools
:: logm ( 'poche still not installed' );
121 echo $this- > tpl
-> render ( 'install.twig' , array (
122 'token' => Session
:: getToken ()
124 if ( isset ( $_GET [ 'install' ])) {
125 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
126 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
127 # let's rock, install poche baby !
128 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
131 Tools
:: logm ( 'poche is now installed' );
136 Tools
:: logm ( 'error during installation' );
143 public function getDefaultConfig ()
146 'pager' => PAGINATION
,
152 * Call action (mark as fav, archive, delete, etc.)
154 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
159 if ( $parametres_url = $url- > fetchContent ()) {
160 if ( $this- > store
-> add ( $url- > getUrl (), $parametres_url [ 'title' ], $parametres_url [ 'content' ], $this- > user
-> getId ())) {
161 Tools
:: logm ( 'add link ' . $url- > getUrl ());
163 if ( STORAGE
== 'postgres' ) {
164 $sequence = 'entries_id_seq' ;
166 $last_id = $this- > store
-> getLastId ( $sequence );
167 if ( DOWNLOAD_PICTURES
) {
168 $content = filtre_picture ( $parametres_url [ 'content' ], $url- > getUrl (), $last_id );
169 Tools
:: logm ( 'updating content article' );
170 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
173 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
178 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
179 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
185 $this- > messages
-> add ( 'e' , _ ( 'error during fetching content : the link wasn \' t added' ));
186 Tools
:: logm ( 'error during content fetch ' . $url- > getUrl ());
194 $msg = 'delete link #' . $id ;
195 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
196 if ( DOWNLOAD_PICTURES
) {
197 remove_directory ( ABS_PATH
. $id );
199 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
202 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
203 $msg = 'error : can \' t delete link #' . $id ;
206 Tools
:: redirect ( '?' );
209 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
210 Tools
:: logm ( 'mark as favorite link #' . $id );
215 case 'toggle_archive' :
216 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
217 Tools
:: logm ( 'archive link #' . $id );
223 Tools
:: logm ( 'action ' . $action . 'doesn \' t exist' );
228 function displayView ( $view , $id = 0 )
235 $dev = $this- > getPocheVersion ( 'dev' );
236 $prod = $this- > getPocheVersion ( 'prod' );
237 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
238 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
242 'compare_dev' => $compare_dev ,
243 'compare_prod' => $compare_prod ,
245 Tools
:: logm ( 'config view' );
248 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
249 if ( $entry != NULL ) {
250 Tools
:: logm ( 'view link #' . $id );
251 $content = $entry [ 'content' ];
252 if ( function_exists ( 'tidy_parse_string' )) {
253 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
254 $tidy- > cleanRepair ();
255 $content = $tidy- > value
;
259 'content' => $content ,
263 Tools
:: logm ( 'error in view call : entry is null' );
267 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
268 $this- > pagination
-> set_total ( count ( $entries ));
269 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
270 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
273 'page_links' => $page_links ,
275 Tools
:: logm ( 'display ' . $view . ' view' );
283 * update the password of the current user.
284 * if MODE_DEMO is TRUE, the password can't be updated.
285 * @todo add the return value
286 * @todo set the new password in function header like this updatePassword($newPassword)
289 public function updatePassword ()
292 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
293 Tools
:: logm ( 'in demo mode, you can \' t do this' );
294 Tools
:: redirect ( '?view=config' );
297 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
298 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
299 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
300 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
302 Tools
:: logm ( 'password updated' );
306 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
307 Tools
:: redirect ( '?view=config' );
314 * checks if login & password are correct and save the user in session.
315 * it redirects the user to the $referer link
316 * @param string $referer the url to redirect after login
317 * @todo add the return value
320 public function login ( $referer )
322 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
323 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
324 if ( $user != array ()) {
325 # Save login into Session
326 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
328 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
329 if (! empty ( $_POST [ 'longlastingsession' ])) {
330 $_SESSION [ 'longlastingsession' ] = 31536000 ;
331 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
332 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
334 session_set_cookie_params ( 0 );
336 session_regenerate_id ( true );
337 Tools
:: logm ( 'login successful' );
338 Tools
:: redirect ( $referer );
340 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
341 Tools
:: logm ( 'login failed' );
344 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
345 Tools
:: logm ( 'login failed' );
351 * log out the poche user. It cleans the session.
352 * @todo add the return value
355 public function logout ()
357 $this- > user
= array ();
359 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
360 Tools
:: logm ( 'logout' );
365 * import from Instapaper. poche needs a ./instapaper-export.html file
366 * @todo add the return value
369 private function importFromInstapaper ()
371 # TODO gestion des articles favs
372 $html = new simple_html_dom ();
373 $html- > load_file ( './instapaper-export.html' );
374 Tools
:: logm ( 'starting import from instapaper' );
378 foreach ( $html- > find ( 'ol' ) as $ul )
380 foreach ( $ul- > find ( 'li' ) as $li )
383 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
384 $this- > action ( 'add' , $url , 0 , TRUE );
387 if ( STORAGE
== 'postgres' ) {
388 $sequence = 'entries_id_seq' ;
390 $last_id = $this- > store
-> getLastId ( $sequence );
391 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
395 # the second <ol> is for read links
398 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
399 Tools
:: logm ( 'import from instapaper completed' );
404 * import from Pocket. poche needs a ./ril_export.html file
405 * @todo add the return value
408 private function importFromPocket ()
410 # TODO gestion des articles favs
411 $html = new simple_html_dom ();
412 $html- > load_file ( './ril_export.html' );
413 Tools
:: logm ( 'starting import from pocket' );
417 foreach ( $html- > find ( 'ul' ) as $ul )
419 foreach ( $ul- > find ( 'li' ) as $li )
422 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
423 $this- > action ( 'add' , $url , 0 , TRUE );
426 if ( STORAGE
== 'postgres' ) {
427 $sequence = 'entries_id_seq' ;
429 $last_id = $this- > store
-> getLastId ( $sequence );
430 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
434 # the second <ul> is for read links
437 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
438 Tools
:: logm ( 'import from pocket completed' );
443 * import from Readability. poche needs a ./readability file
444 * @todo add the return value
447 private function importFromReadability ()
449 # TODO gestion des articles lus / favs
450 $str_data = file_get_contents ( "./readability" );
451 $data = json_decode ( $str_data , true );
452 Tools
:: logm ( 'starting import from Readability' );
454 foreach ( $data as $key => $value ) {
458 foreach ( $value as $attr => $attr_value ) {
459 if ( $attr == 'article__url' ) {
460 $url = new Url ( base64_encode ( $attr_value ));
463 if ( STORAGE
== 'postgres' ) {
464 $sequence = 'entries_id_seq' ;
466 if ( $attr_value == 'true' ) {
467 if ( $attr == 'favorite' ) {
470 if ( $attr == 'archive' ) {
476 if (! is_null ( $url ) && $url- > isCorrect ()) {
477 $this- > action ( 'add' , $url , 0 , TRUE );
480 $last_id = $this- > store
-> getLastId ( $sequence );
481 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
484 $last_id = $this- > store
-> getLastId ( $sequence );
485 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
489 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
490 Tools
:: logm ( 'import from Readability completed' );
495 * import datas into your poche
496 * @param string $from name of the service to import : pocket, instapaper or readability
497 * @todo add the return value
500 public function import ( $from )
502 if ( $from == 'pocket' ) {
503 return $this- > importFromPocket ();
505 else if ( $from == 'readability' ) {
506 return $this- > importFromReadability ();
508 else if ( $from == 'instapaper' ) {
509 return $this- > importFromInstapaper ();
514 * export poche entries in json
515 * @return json all poche entries
517 public function export ()
519 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
520 echo $this- > tpl
-> render ( 'export.twig' , array (
521 'export' => Tools
:: renderJson ( $entries ),
523 Tools
:: logm ( 'export view' );
527 * Checks online the latest version of poche and cache it
528 * @param string $which 'prod' or 'dev'
529 * @return string latest $which version
531 private function getPocheVersion ( $which = 'prod' )
533 $cache_file = CACHE
. '/' . $which ;
535 # checks if the cached version file exists
536 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
537 $version = file_get_contents ( $cache_file );
539 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
540 file_put_contents ( $cache_file , $version , LOCK_EX
);