]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
1d8aaca2c6dcc8bb18e5e32e839325e5236a77a6
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 );
218 function displayView ( $view , $id = 0 )
225 $dev = $this- > getPocheVersion ( 'dev' );
226 $prod = $this- > getPocheVersion ( 'prod' );
227 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
228 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
232 'compare_dev' => $compare_dev ,
233 'compare_prod' => $compare_prod ,
235 Tools
:: logm ( 'config view' );
238 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
239 if ( $entry != NULL ) {
240 Tools
:: logm ( 'view link #' . $id );
241 $content = $entry [ 'content' ];
242 if ( function_exists ( 'tidy_parse_string' )) {
243 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
244 $tidy- > cleanRepair ();
245 $content = $tidy- > value
;
249 'content' => $content ,
253 Tools
:: logm ( 'error in view call : entry is null' );
257 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
258 $this- > pagination
-> set_total ( count ( $entries ));
259 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
260 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
263 'page_links' => $page_links ,
265 Tools
:: logm ( 'display ' . $view . ' view' );
273 * update the password of the current user.
274 * if MODE_DEMO is TRUE, the password can't be updated.
275 * @todo add the return value
276 * @todo set the new password in function header like this updatePassword($newPassword)
279 public function updatePassword ()
282 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
283 Tools
:: logm ( 'in demo mode, you can \' t do this' );
284 Tools
:: redirect ( '?view=config' );
287 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
288 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
289 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
290 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
292 Tools
:: logm ( 'password updated' );
296 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
297 Tools
:: redirect ( '?view=config' );
304 * checks if login & password are correct and save the user in session.
305 * it redirects the user to the $referer link
306 * @param string $referer the url to redirect after login
307 * @todo add the return value
310 public function login ( $referer )
312 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
313 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
314 if ( $user != array ()) {
315 # Save login into Session
316 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
318 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
319 if (! empty ( $_POST [ 'longlastingsession' ])) {
320 $_SESSION [ 'longlastingsession' ] = 31536000 ;
321 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
322 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
324 session_set_cookie_params ( 0 );
326 session_regenerate_id ( true );
327 Tools
:: logm ( 'login successful' );
328 Tools
:: redirect ( $referer );
330 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
331 Tools
:: logm ( 'login failed' );
334 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
335 Tools
:: logm ( 'login failed' );
341 * log out the poche user. It cleans the session.
342 * @todo add the return value
345 public function logout ()
347 $this- > user
= array ();
349 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
350 Tools
:: logm ( 'logout' );
355 * import from Instapaper. poche needs a ./instapaper-export.html file
356 * @todo add the return value
359 private function importFromInstapaper ()
361 # TODO gestion des articles favs
362 $html = new simple_html_dom ();
363 $html- > load_file ( './instapaper-export.html' );
364 Tools
:: logm ( 'starting import from instapaper' );
368 foreach ( $html- > find ( 'ol' ) as $ul )
370 foreach ( $ul- > find ( 'li' ) as $li )
373 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
374 $this- > action ( 'add' , $url , 0 , TRUE );
377 if ( STORAGE
== 'postgres' ) {
378 $sequence = 'entries_id_seq' ;
380 $last_id = $this- > store
-> getLastId ( $sequence );
381 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
385 # the second <ol> is for read links
388 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
389 Tools
:: logm ( 'import from instapaper completed' );
394 * import from Pocket. poche needs a ./ril_export.html file
395 * @todo add the return value
398 private function importFromPocket ()
400 # TODO gestion des articles favs
401 $html = new simple_html_dom ();
402 $html- > load_file ( './ril_export.html' );
403 Tools
:: logm ( 'starting import from pocket' );
407 foreach ( $html- > find ( 'ul' ) as $ul )
409 foreach ( $ul- > find ( 'li' ) as $li )
412 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
413 $this- > action ( 'add' , $url , 0 , TRUE );
416 if ( STORAGE
== 'postgres' ) {
417 $sequence = 'entries_id_seq' ;
419 $last_id = $this- > store
-> getLastId ( $sequence );
420 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
424 # the second <ul> is for read links
427 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
428 Tools
:: logm ( 'import from pocket completed' );
433 * import from Readability. poche needs a ./readability file
434 * @todo add the return value
437 private function importFromReadability ()
439 # TODO gestion des articles lus / favs
440 $str_data = file_get_contents ( "./readability" );
441 $data = json_decode ( $str_data , true );
442 Tools
:: logm ( 'starting import from Readability' );
444 foreach ( $data as $key => $value ) {
446 foreach ( $value as $attr => $attr_value ) {
447 if ( $attr == 'article__url' ) {
448 $url = new Url ( base64_encode ( $attr_value ));
451 if ( STORAGE
== 'postgres' ) {
452 $sequence = 'entries_id_seq' ;
454 // if ($attr_value == 'favorite' && $attr_value == 'true') {
455 // $last_id = $this->store->getLastId($sequence);
456 // $this->store->favoriteById($last_id);
457 // $this->action('toogle_fav', $url, $last_id, TRUE);
459 if ( $attr_value == 'archive' && $attr_value == 'true' ) {
460 $last_id = $this- > store
-> getLastId ( $sequence );
461 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
464 if ( $url- > isCorrect ())
465 $this- > action ( 'add' , $url , 0 , TRUE );
467 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed' ));
468 Tools
:: logm ( 'import from Readability completed' );
473 * import datas into your poche
474 * @param string $from name of the service to import : pocket, instapaper or readability
475 * @todo add the return value
478 public function import ( $from )
480 if ( $from == 'pocket' ) {
481 return $this- > importFromPocket ();
483 else if ( $from == 'readability' ) {
484 return $this- > importFromReadability ();
486 else if ( $from == 'instapaper' ) {
487 return $this- > importFromInstapaper ();
492 * export poche entries in json
493 * @return json all poche entries
495 public function export ()
497 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
498 echo $this- > tpl
-> render ( 'export.twig' , array (
499 'export' => Tools
:: renderJson ( $entries ),
501 Tools
:: logm ( 'export view' );
505 * Checks online the latest version of poche and cache it
506 * @param string $which 'prod' or 'dev'
507 * @return string latest $which version
509 private function getPocheVersion ( $which = 'prod' )
511 $cache_file = CACHE
. '/' . $which ;
513 # checks if the cached version file exists
514 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
515 $version = file_get_contents ( $cache_file );
517 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
518 file_put_contents ( $cache_file , $version , LOCK_EX
);