]>
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 = '<h1>setup</h1><p><strong>It \' s your first time here?</strong> Please copy /install/poche.sqlite in db folder. Then, delete install folder.<br /><strong>If you have already installed poche</strong>, an update is needed <a href="install/update.php">by clicking here</a>.</p>' ;
53 else if ( file_exists ( './install' ) && ! DEBUG_POCHE
) {
54 $msg = '<h1>setup</h1><p><strong>If you want to update your poche</strong>, you just have to delete /install folder. <br /><strong>To install your poche with sqlite</strong>, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.</p>' ;
57 else if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
58 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
59 $msg = '<h1>error</h1><p>You don \' t have write access on sqlite file.</p>' ;
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 ()
96 Session
:: $sessionName = 'poche' ;
99 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
100 $this- > user
= $_SESSION [ 'poche_user' ];
103 # fake user, just for install & login screens
104 $this- > user
= new User ();
105 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
109 $language = $this- > user
-> getConfigValue ( 'language' );
110 putenv ( 'LC_ALL=' . $language );
111 setlocale ( LC_ALL
, $language );
112 bindtextdomain ( $language , LOCALE
);
113 textdomain ( $language );
116 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
119 private function install ()
121 Tools
:: logm ( 'poche still not installed' );
122 echo $this- > tpl
-> render ( 'install.twig' , array (
123 'token' => Session
:: getToken ()
125 if ( isset ( $_GET [ 'install' ])) {
126 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
127 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
128 # let's rock, install poche baby !
129 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
132 Tools
:: logm ( 'poche is now installed' );
137 Tools
:: logm ( 'error during installation' );
144 public function getDefaultConfig ()
147 'pager' => PAGINATION
,
153 * Call action (mark as fav, archive, delete, etc.)
155 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
160 $content = $url- > extract ();
162 if ( $this- > store
-> add ( $url- > getUrl (), $content [ 'title' ], $content [ 'body' ], $this- > user
-> getId ())) {
163 Tools
:: logm ( 'add link ' . $url- > getUrl ());
165 if ( STORAGE
== 'postgres' ) {
166 $sequence = 'entries_id_seq' ;
168 $last_id = $this- > store
-> getLastId ( $sequence );
169 if ( DOWNLOAD_PICTURES
) {
170 $content = filtre_picture ( $content [ 'body' ], $url- > getUrl (), $last_id );
171 Tools
:: logm ( 'updating content article' );
172 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
175 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
180 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
181 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
186 Tools
:: redirect ( '?view=home' );
190 $msg = 'delete link #' . $id ;
191 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
192 if ( DOWNLOAD_PICTURES
) {
193 remove_directory ( ABS_PATH
. $id );
195 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
198 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
199 $msg = 'error : can \' t delete link #' . $id ;
202 Tools
:: redirect ( '?' );
205 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
206 Tools
:: logm ( 'mark as favorite link #' . $id );
211 case 'toggle_archive' :
212 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
213 Tools
:: logm ( 'archive link #' . $id );
223 function displayView ( $view , $id = 0 )
230 $dev = $this- > getPocheVersion ( 'dev' );
231 $prod = $this- > getPocheVersion ( 'prod' );
232 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
233 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
237 'compare_dev' => $compare_dev ,
238 'compare_prod' => $compare_prod ,
240 Tools
:: logm ( 'config view' );
243 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
244 if ( $entry != NULL ) {
245 Tools
:: logm ( 'view link #' . $id );
246 $content = $entry [ 'content' ];
247 if ( function_exists ( 'tidy_parse_string' )) {
248 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
249 $tidy- > cleanRepair ();
250 $content = $tidy- > value
;
253 $flattr = new FlattrItem ();
254 $flattr- > checkItem ( $entry [ 'url' ]);
258 'content' => $content ,
264 Tools
:: logm ( 'error in view call : entry is null' );
267 default : # home, favorites and archive views
268 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
274 if ( count ( $entries ) > 0 ) {
275 $this- > pagination
-> set_total ( count ( $entries ));
276 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
277 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
278 $tpl_vars [ 'entries' ] = $datas ;
279 $tpl_vars [ 'page_links' ] = $page_links ;
280 $tpl_vars [ 'nb_results' ] = count ( $entries );
282 Tools
:: logm ( 'display ' . $view . ' view' );
290 * update the password of the current user.
291 * if MODE_DEMO is TRUE, the password can't be updated.
292 * @todo add the return value
293 * @todo set the new password in function header like this updatePassword($newPassword)
296 public function updatePassword ()
299 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
300 Tools
:: logm ( 'in demo mode, you can \' t do this' );
301 Tools
:: redirect ( '?view=config' );
304 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
305 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
306 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
307 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
309 Tools
:: logm ( 'password updated' );
313 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
314 Tools
:: redirect ( '?view=config' );
321 * checks if login & password are correct and save the user in session.
322 * it redirects the user to the $referer link
323 * @param string $referer the url to redirect after login
324 * @todo add the return value
327 public function login ( $referer )
329 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
330 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
331 if ( $user != array ()) {
332 # Save login into Session
333 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
335 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
336 if (! empty ( $_POST [ 'longlastingsession' ])) {
337 $_SESSION [ 'longlastingsession' ] = 31536000 ;
338 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
339 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
341 session_set_cookie_params ( 0 );
343 session_regenerate_id ( true );
344 Tools
:: logm ( 'login successful' );
345 Tools
:: redirect ( $referer );
347 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
348 Tools
:: logm ( 'login failed' );
351 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
352 Tools
:: logm ( 'login failed' );
358 * log out the poche user. It cleans the session.
359 * @todo add the return value
362 public function logout ()
364 $this- > user
= array ();
366 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
367 Tools
:: logm ( 'logout' );
372 * import from Instapaper. poche needs a ./instapaper-export.html file
373 * @todo add the return value
374 * @param string $targetFile the file used for importing
377 private function importFromInstapaper ( $targetFile )
379 # TODO gestion des articles favs
380 $html = new simple_html_dom ();
381 $html- > load_file ( $targetFile );
382 Tools
:: logm ( 'starting import from instapaper' );
386 foreach ( $html- > find ( 'ol' ) as $ul )
388 foreach ( $ul- > find ( 'li' ) as $li )
391 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
392 $this- > action ( 'add' , $url , 0 , TRUE );
395 if ( STORAGE
== 'postgres' ) {
396 $sequence = 'entries_id_seq' ;
398 $last_id = $this- > store
-> getLastId ( $sequence );
399 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
403 # the second <ol> is for read links
406 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
407 Tools
:: logm ( 'import from instapaper completed' );
412 * import from Pocket. poche needs a ./ril_export.html file
413 * @todo add the return value
414 * @param string $targetFile the file used for importing
417 private function importFromPocket ( $targetFile )
419 # TODO gestion des articles favs
420 $html = new simple_html_dom ();
421 $html- > load_file ( $targetFile );
422 Tools
:: logm ( 'starting import from pocket' );
426 foreach ( $html- > find ( 'ul' ) as $ul )
428 foreach ( $ul- > find ( 'li' ) as $li )
431 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
432 $this- > action ( 'add' , $url , 0 , TRUE );
435 if ( STORAGE
== 'postgres' ) {
436 $sequence = 'entries_id_seq' ;
438 $last_id = $this- > store
-> getLastId ( $sequence );
439 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
443 # the second <ul> is for read links
446 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
447 Tools
:: logm ( 'import from pocket completed' );
452 * import from Readability. poche needs a ./readability file
453 * @todo add the return value
454 * @param string $targetFile the file used for importing
457 private function importFromReadability ( $targetFile )
459 # TODO gestion des articles lus / favs
460 $str_data = file_get_contents ( $targetFile );
461 $data = json_decode ( $str_data , true );
462 Tools
:: logm ( 'starting import from Readability' );
464 foreach ( $data as $key => $value ) {
468 foreach ( $value as $attr => $attr_value ) {
469 if ( $attr == 'article__url' ) {
470 $url = new Url ( base64_encode ( $attr_value ));
473 if ( STORAGE
== 'postgres' ) {
474 $sequence = 'entries_id_seq' ;
476 if ( $attr_value == 'true' ) {
477 if ( $attr == 'favorite' ) {
480 if ( $attr == 'archive' ) {
486 if (! is_null ( $url ) && $url- > isCorrect ()) {
487 $this- > action ( 'add' , $url , 0 , TRUE );
490 $last_id = $this- > store
-> getLastId ( $sequence );
491 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
494 $last_id = $this- > store
-> getLastId ( $sequence );
495 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
499 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
500 Tools
:: logm ( 'import from Readability completed' );
505 * import datas into your poche
506 * @param string $from name of the service to import : pocket, instapaper or readability
507 * @todo add the return value
510 public function import ( $from )
513 'pocket' => 'importFromPocket' ,
514 'readability' => 'importFromReadability' ,
515 'instapaper' => 'importFromInstapaper'
518 if (! isset ( $providers [ $from ])) {
519 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
523 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
524 $targetFile = constant ( $targetDefinition );
526 if (! defined ( $targetDefinition )) {
527 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
531 if (! file_exists ( $targetFile )) {
532 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
536 $this- > $providers [ $from ]( $targetFile );
540 * export poche entries in json
541 * @return json all poche entries
543 public function export ()
545 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
546 echo $this- > tpl
-> render ( 'export.twig' , array (
547 'export' => Tools
:: renderJson ( $entries ),
549 Tools
:: logm ( 'export view' );
553 * Checks online the latest version of poche and cache it
554 * @param string $which 'prod' or 'dev'
555 * @return string latest $which version
557 private function getPocheVersion ( $which = 'prod' )
559 $cache_file = CACHE
. '/' . $which ;
561 # checks if the cached version file exists
562 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
563 $version = file_get_contents ( $cache_file );
565 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
566 file_put_contents ( $cache_file , $version , LOCK_EX
);