]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
cb865a2a976ae60bf3ffbf4fe03900720d1421af
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 ( file_exists ( './install/update.php' ) && ! DEBUG_POCHE
) {
46 $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.' ;
49 else if ( file_exists ( './install' ) && ! DEBUG_POCHE
) {
50 $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.' ;
53 else if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
54 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
55 $msg = 'You don \' t have write access on sqlite file.' ;
58 else if (! is_writable ( CACHE
)) {
59 Tools
:: logm ( 'you don \' t have write access on cache directory' );
60 $msg = 'You don \' t have write access on cache directory.' ;
65 echo $this- > tpl
-> render ( 'error.twig' , array (
73 private function initTpl ()
76 $loader = new Twig_Loader_Filesystem ( TPL
);
78 $twig_params = array ();
81 $twig_params = array ( 'cache' => CACHE
);
83 $this- > tpl
= new Twig_Environment ( $loader , $twig_params );
84 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
85 # filter to display domain name of an url
86 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
87 $this- > tpl
-> addFilter ( $filter );
89 # filter for reading time
90 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
91 $this- > tpl
-> addFilter ( $filter );
94 private function init ()
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 if ( $parametres_url = $url- > fetchContent ()) {
161 if ( $this- > store
-> add ( $url- > getUrl (), $parametres_url [ 'title' ], $parametres_url [ 'content' ], $this- > user
-> getId ())) {
162 Tools
:: logm ( 'add link ' . $url- > getUrl ());
164 if ( STORAGE
== 'postgres' ) {
165 $sequence = 'entries_id_seq' ;
167 $last_id = $this- > store
-> getLastId ( $sequence );
168 if ( DOWNLOAD_PICTURES
) {
169 $content = filtre_picture ( $parametres_url [ 'content' ], $url- > getUrl (), $last_id );
170 Tools
:: logm ( 'updating content article' );
171 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
174 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
179 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
180 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
186 $this- > messages
-> add ( 'e' , _ ( 'error during fetching content : the link wasn \' t added' ));
187 Tools
:: logm ( 'error during content fetch ' . $url- > getUrl ());
195 $msg = 'delete link #' . $id ;
196 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
197 if ( DOWNLOAD_PICTURES
) {
198 remove_directory ( ABS_PATH
. $id );
200 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
203 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
204 $msg = 'error : can \' t delete link #' . $id ;
207 Tools
:: redirect ( '?' );
210 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
211 Tools
:: logm ( 'mark as favorite link #' . $id );
216 case 'toggle_archive' :
217 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
218 Tools
:: logm ( 'archive link #' . $id );
224 Tools
:: logm ( 'action ' . $action . 'doesn \' t exist' );
229 function displayView ( $view , $id = 0 )
236 $dev = $this- > getPocheVersion ( 'dev' );
237 $prod = $this- > getPocheVersion ( 'prod' );
238 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
239 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
243 'compare_dev' => $compare_dev ,
244 'compare_prod' => $compare_prod ,
246 Tools
:: logm ( 'config view' );
249 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
250 if ( $entry != NULL ) {
251 Tools
:: logm ( 'view link #' . $id );
252 $content = $entry [ 'content' ];
253 if ( function_exists ( 'tidy_parse_string' )) {
254 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
255 $tidy- > cleanRepair ();
256 $content = $tidy- > value
;
260 'content' => $content ,
264 Tools
:: logm ( 'error in view call : entry is null' );
268 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
269 $this- > pagination
-> set_total ( count ( $entries ));
270 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
271 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
274 'page_links' => $page_links ,
276 Tools
:: logm ( 'display ' . $view . ' view' );
284 * update the password of the current user.
285 * if MODE_DEMO is TRUE, the password can't be updated.
286 * @todo add the return value
287 * @todo set the new password in function header like this updatePassword($newPassword)
290 public function updatePassword ()
293 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
294 Tools
:: logm ( 'in demo mode, you can \' t do this' );
295 Tools
:: redirect ( '?view=config' );
298 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
299 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
300 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
301 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
303 Tools
:: logm ( 'password updated' );
307 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
308 Tools
:: redirect ( '?view=config' );
315 * checks if login & password are correct and save the user in session.
316 * it redirects the user to the $referer link
317 * @param string $referer the url to redirect after login
318 * @todo add the return value
321 public function login ( $referer )
323 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
324 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
325 if ( $user != array ()) {
326 # Save login into Session
327 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
329 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
330 if (! empty ( $_POST [ 'longlastingsession' ])) {
331 $_SESSION [ 'longlastingsession' ] = 31536000 ;
332 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
333 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
335 session_set_cookie_params ( 0 );
337 session_regenerate_id ( true );
338 Tools
:: logm ( 'login successful' );
339 Tools
:: redirect ( $referer );
341 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
342 Tools
:: logm ( 'login failed' );
345 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
346 Tools
:: logm ( 'login failed' );
352 * log out the poche user. It cleans the session.
353 * @todo add the return value
356 public function logout ()
358 $this- > user
= array ();
360 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
361 Tools
:: logm ( 'logout' );
366 * import from Instapaper. poche needs a ./instapaper-export.html file
367 * @todo add the return value
370 private function importFromInstapaper ()
372 # TODO gestion des articles favs
373 $html = new simple_html_dom ();
374 $html- > load_file ( './instapaper-export.html' );
375 Tools
:: logm ( 'starting import from instapaper' );
379 foreach ( $html- > find ( 'ol' ) as $ul )
381 foreach ( $ul- > find ( 'li' ) as $li )
384 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
385 $this- > action ( 'add' , $url , 0 , TRUE );
388 if ( STORAGE
== 'postgres' ) {
389 $sequence = 'entries_id_seq' ;
391 $last_id = $this- > store
-> getLastId ( $sequence );
392 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
396 # the second <ol> is for read links
399 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
400 Tools
:: logm ( 'import from instapaper completed' );
405 * import from Pocket. poche needs a ./ril_export.html file
406 * @todo add the return value
409 private function importFromPocket ()
411 # TODO gestion des articles favs
412 $html = new simple_html_dom ();
413 $html- > load_file ( './ril_export.html' );
414 Tools
:: logm ( 'starting import from pocket' );
418 foreach ( $html- > find ( 'ul' ) as $ul )
420 foreach ( $ul- > find ( 'li' ) as $li )
423 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
424 $this- > action ( 'add' , $url , 0 , TRUE );
427 if ( STORAGE
== 'postgres' ) {
428 $sequence = 'entries_id_seq' ;
430 $last_id = $this- > store
-> getLastId ( $sequence );
431 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
435 # the second <ul> is for read links
438 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
439 Tools
:: logm ( 'import from pocket completed' );
444 * import from Readability. poche needs a ./readability file
445 * @todo add the return value
448 private function importFromReadability ()
450 # TODO gestion des articles lus / favs
451 $str_data = file_get_contents ( "./readability" );
452 $data = json_decode ( $str_data , true );
453 Tools
:: logm ( 'starting import from Readability' );
455 foreach ( $data as $key => $value ) {
459 foreach ( $value as $attr => $attr_value ) {
460 if ( $attr == 'article__url' ) {
461 $url = new Url ( base64_encode ( $attr_value ));
464 if ( STORAGE
== 'postgres' ) {
465 $sequence = 'entries_id_seq' ;
467 if ( $attr_value == 'true' ) {
468 if ( $attr == 'favorite' ) {
471 if ( $attr == 'archive' ) {
477 if (! is_null ( $url ) && $url- > isCorrect ()) {
478 $this- > action ( 'add' , $url , 0 , TRUE );
481 $last_id = $this- > store
-> getLastId ( $sequence );
482 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
485 $last_id = $this- > store
-> getLastId ( $sequence );
486 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
490 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
491 Tools
:: logm ( 'import from Readability completed' );
496 * import datas into your poche
497 * @param string $from name of the service to import : pocket, instapaper or readability
498 * @todo add the return value
501 public function import ( $from )
503 if ( $from == 'pocket' ) {
504 return $this- > importFromPocket ();
506 else if ( $from == 'readability' ) {
507 return $this- > importFromReadability ();
509 else if ( $from == 'instapaper' ) {
510 return $this- > importFromInstapaper ();
515 * export poche entries in json
516 * @return json all poche entries
518 public function export ()
520 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
521 echo $this- > tpl
-> render ( 'export.twig' , array (
522 'export' => Tools
:: renderJson ( $entries ),
524 Tools
:: logm ( 'export view' );
528 * Checks online the latest version of poche and cache it
529 * @param string $which 'prod' or 'dev'
530 * @return string latest $which version
532 private function getPocheVersion ( $which = 'prod' )
534 $cache_file = CACHE
. '/' . $which ;
536 # checks if the cached version file exists
537 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
538 $version = file_get_contents ( $cache_file );
540 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
541 file_put_contents ( $cache_file , $version , LOCK_EX
);