]>
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 ()
21 if ( file_exists ( './install' ) && ! DEBUG_POCHE
) {
22 Tools
:: logm ( 'folder /install exists' );
23 die ( '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.' );
26 $this- > store
= new Database ();
28 $this- > messages
= new Messages ();
31 if (! $this- > store
-> isInstalled ())
37 private function init ()
42 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
43 $this- > user
= $_SESSION [ 'poche_user' ];
46 # fake user, just for install & login screens
47 $this- > user
= new User ();
48 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
52 $language = $this- > user
-> getConfigValue ( 'language' );
53 putenv ( 'LC_ALL=' . $language );
54 setlocale ( LC_ALL
, $language );
55 bindtextdomain ( $language , LOCALE
);
56 textdomain ( $language );
59 $loader = new Twig_Loader_Filesystem ( TPL
);
61 $twig_params = array ();
64 $twig_params = array ( 'cache' => CACHE
);
66 $this- > tpl
= new Twig_Environment ( $loader , $twig_params );
67 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
68 # filter to display domain name of an url
69 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
70 $this- > tpl
-> addFilter ( $filter );
72 # filter for reading time
73 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
74 $this- > tpl
-> addFilter ( $filter );
77 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
80 private function install ()
82 Tools
:: logm ( 'poche still not installed' );
83 echo $this- > tpl
-> render ( 'install.twig' , array (
84 'token' => Session
:: getToken ()
86 if ( isset ( $_GET [ 'install' ])) {
87 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
88 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
89 # let's rock, install poche baby !
90 $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
92 Tools
:: logm ( 'poche is now installed' );
96 Tools
:: logm ( 'error during installation' );
103 public function getDefaultConfig ()
106 'pager' => PAGINATION
,
112 * Call action (mark as fav, archive, delete, etc.)
114 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
119 if ( $parametres_url = $url- > fetchContent ()) {
120 if ( $this- > store
-> add ( $url- > getUrl (), $parametres_url [ 'title' ], $parametres_url [ 'content' ], $this- > user
-> getId ())) {
121 Tools
:: logm ( 'add link ' . $url- > getUrl ());
123 if ( STORAGE
== 'postgres' ) {
124 $sequence = 'entries_id_seq' ;
126 $last_id = $this- > store
-> getLastId ( $sequence );
127 if ( DOWNLOAD_PICTURES
) {
128 $content = filtre_picture ( $parametres_url [ 'content' ], $url- > getUrl (), $last_id );
131 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
136 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
137 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
143 $this- > messages
-> add ( 'e' , _ ( 'error during fetching content : the link wasn \' t added' ));
144 Tools
:: logm ( 'error during content fetch ' . $url- > getUrl ());
152 $msg = 'delete link #' . $id ;
153 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
154 if ( DOWNLOAD_PICTURES
) {
155 remove_directory ( ABS_PATH
. $id );
157 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
160 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
161 $msg = 'error : can \' t delete link #' . $id ;
164 Tools
:: redirect ( '?' );
167 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
168 Tools
:: logm ( 'mark as favorite link #' . $id );
173 case 'toggle_archive' :
174 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
175 Tools
:: logm ( 'archive link #' . $id );
185 function displayView ( $view , $id = 0 )
192 $dev = $this- > getPocheVersion ( 'dev' );
193 $prod = $this- > getPocheVersion ( 'prod' );
194 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
195 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
199 'compare_dev' => $compare_dev ,
200 'compare_prod' => $compare_prod ,
202 Tools
:: logm ( 'config view' );
205 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
206 if ( $entry != NULL ) {
207 Tools
:: logm ( 'view link #' . $id );
208 $content = $entry [ 'content' ];
209 if ( function_exists ( 'tidy_parse_string' )) {
210 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
211 $tidy- > cleanRepair ();
212 $content = $tidy- > value
;
216 'content' => $content ,
220 Tools
:: logm ( 'error in view call : entry is null' );
224 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
225 $this- > pagination
-> set_total ( count ( $entries ));
226 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
227 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
230 'page_links' => $page_links ,
232 Tools
:: logm ( 'display ' . $view . ' view' );
240 * update the password of the current user.
241 * if MODE_DEMO is TRUE, the password can't be updated.
242 * @todo add the return value
243 * @todo set the new password in function header like this updatePassword($newPassword)
246 public function updatePassword ()
249 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
250 Tools
:: logm ( 'in demo mode, you can \' t do this' );
251 Tools
:: redirect ( '?view=config' );
254 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
255 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
256 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
257 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
259 Tools
:: logm ( 'password updated' );
263 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
264 Tools
:: redirect ( '?view=config' );
271 * checks if login & password are correct and save the user in session.
272 * it redirects the user to the $referer link
273 * @param string $referer the url to redirect after login
274 * @todo add the return value
277 public function login ( $referer )
279 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
280 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
281 if ( $user != array ()) {
282 # Save login into Session
283 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
285 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
286 if (! empty ( $_POST [ 'longlastingsession' ])) {
287 $_SESSION [ 'longlastingsession' ] = 31536000 ;
288 $_SESSION [ 'expires_on' ] = time () +
$_SESSION [ 'longlastingsession' ];
289 session_set_cookie_params ( $_SESSION [ 'longlastingsession' ]);
291 session_set_cookie_params ( 0 );
293 session_regenerate_id ( true );
294 Tools
:: logm ( 'login successful' );
295 Tools
:: redirect ( $referer );
297 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
298 Tools
:: logm ( 'login failed' );
301 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
302 Tools
:: logm ( 'login failed' );
308 * log out the poche user. It cleans the session.
309 * @todo add the return value
312 public function logout ()
314 $this- > user
= array ();
316 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
317 Tools
:: logm ( 'logout' );
322 * import from Instapaper. poche needs a ./instapaper-export.html file
323 * @todo add the return value
326 private function importFromInstapaper ()
328 # TODO gestion des articles favs
329 $html = new simple_html_dom ();
330 $html- > load_file ( './instapaper-export.html' );
331 Tools
:: logm ( 'starting import from instapaper' );
335 foreach ( $html- > find ( 'ol' ) as $ul )
337 foreach ( $ul- > find ( 'li' ) as $li )
340 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
341 $this- > action ( 'add' , $url , 0 , TRUE );
344 if ( STORAGE
== 'postgres' ) {
345 $sequence = 'entries_id_seq' ;
347 $last_id = $this- > store
-> getLastId ( $sequence );
348 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
352 # the second <ol> is for read links
355 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
356 Tools
:: logm ( 'import from instapaper completed' );
361 * import from Pocket. poche needs a ./ril_export.html file
362 * @todo add the return value
365 private function importFromPocket ()
367 # TODO gestion des articles favs
368 $html = new simple_html_dom ();
369 $html- > load_file ( './ril_export.html' );
370 Tools
:: logm ( 'starting import from pocket' );
374 foreach ( $html- > find ( 'ul' ) as $ul )
376 foreach ( $ul- > find ( 'li' ) as $li )
379 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
380 $this- > action ( 'add' , $url , 0 , TRUE );
383 if ( STORAGE
== 'postgres' ) {
384 $sequence = 'entries_id_seq' ;
386 $last_id = $this- > store
-> getLastId ( $sequence );
387 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
391 # the second <ul> is for read links
394 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
395 Tools
:: logm ( 'import from pocket completed' );
400 * import from Readability. poche needs a ./readability file
401 * @todo add the return value
404 private function importFromReadability ()
406 # TODO gestion des articles lus / favs
407 $str_data = file_get_contents ( "./readability" );
408 $data = json_decode ( $str_data , true );
409 Tools
:: logm ( 'starting import from Readability' );
411 foreach ( $data as $key => $value ) {
413 foreach ( $value as $attr => $attr_value ) {
414 if ( $attr == 'article__url' ) {
415 $url = new Url ( base64_encode ( $attr_value ));
418 if ( STORAGE
== 'postgres' ) {
419 $sequence = 'entries_id_seq' ;
421 // if ($attr_value == 'favorite' && $attr_value == 'true') {
422 // $last_id = $this->store->getLastId($sequence);
423 // $this->store->favoriteById($last_id);
424 // $this->action('toogle_fav', $url, $last_id, TRUE);
426 if ( $attr_value == 'archive' && $attr_value == 'true' ) {
427 $last_id = $this- > store
-> getLastId ( $sequence );
428 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
431 if ( $url- > isCorrect ())
432 $this- > action ( 'add' , $url , 0 , TRUE );
434 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed' ));
435 Tools
:: logm ( 'import from Readability completed' );
440 * import datas into your poche
441 * @param string $from name of the service to import : pocket, instapaper or readability
442 * @todo add the return value
445 public function import ( $from )
447 if ( $from == 'pocket' ) {
448 return $this- > importFromPocket ();
450 else if ( $from == 'readability' ) {
451 return $this- > importFromReadability ();
453 else if ( $from == 'instapaper' ) {
454 return $this- > importFromInstapaper ();
459 * export poche entries in json
460 * @return json all poche entries
462 public function export ()
464 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
465 echo $this- > tpl
-> render ( 'export.twig' , array (
466 'export' => Tools
:: renderJson ( $entries ),
468 Tools
:: logm ( 'export view' );
472 * Checks online the latest version of poche and cache it
473 * @param string $which 'prod' or 'dev'
474 * @return string latest $which version
476 private function getPocheVersion ( $which = 'prod' )
478 $cache_file = CACHE
. '/' . $which ;
480 # checks if the cached version file exists
481 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
482 $version = file_get_contents ( $cache_file );
484 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
485 file_put_contents ( $cache_file , $version , LOCK_EX
);