]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
56910bc03deb94f238a2f8f2a3fb3dfeb861dc97
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 $this->store
= new Database();
23 $this->messages
= new Messages();
26 if(!$this->store
->isInstalled())
32 private function init()
34 if (file_exists('./install') && !DEBUG_POCHE
) {
35 Tools
::logm('folder /install exists');
36 die('the folder /install exists, you have to delete it before using poche.');
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);
73 $this->pagination
= new Paginator($this->user
->getConfigValue('pager'), 'p');
76 private function install()
78 Tools
::logm('poche still not installed');
79 echo $this->tpl
->render('install.twig', array(
80 'token' => Session
::getToken()
82 if (isset($_GET['install'])) {
83 if (($_POST['password'] == $_POST['password_repeat'])
84 && $_POST['password'] != "" && $_POST['login'] != "") {
85 # let's rock, install poche baby !
86 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
88 Tools
::logm('poche is now installed');
92 Tools
::logm('error during installation');
99 public function getDefaultConfig()
102 'pager' => PAGINATION
,
108 * Call action (mark as fav, archive, delete, etc.)
110 public function action($action, Url
$url, $id = 0, $import = FALSE)
115 if($parametres_url = $url->fetchContent()) {
116 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user
->getId())) {
117 Tools
::logm('add link ' . $url->getUrl());
119 if (STORAGE
== 'postgres') {
120 $sequence = 'entries_id_seq';
122 $last_id = $this->store
->getLastId($sequence);
123 if (DOWNLOAD_PICTURES
) {
124 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
127 $this->messages
->add('s', _('the link has been added successfully'));
132 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
133 Tools
::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
139 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
140 Tools
::logm('error during content fetch ' . $url->getUrl());
148 $msg = 'delete link #' . $id;
149 if ($this->store
->deleteById($id, $this->user
->getId())) {
150 if (DOWNLOAD_PICTURES
) {
151 remove_directory(ABS_PATH
. $id);
153 $this->messages
->add('s', _('the link has been deleted successfully'));
156 $this->messages
->add('e', _('the link wasn\'t deleted'));
157 $msg = 'error : can\'t delete link #' . $id;
160 Tools
::redirect('?');
163 $this->store
->favoriteById($id, $this->user
->getId());
164 Tools
::logm('mark as favorite link #' . $id);
169 case 'toggle_archive' :
170 $this->store
->archiveById($id, $this->user
->getId());
171 Tools
::logm('archive link #' . $id);
181 function displayView($view, $id = 0)
188 $dev = $this->getPocheVersion('dev');
189 $prod = $this->getPocheVersion('prod');
190 $compare_dev = version_compare(POCHE_VERSION
, $dev);
191 $compare_prod = version_compare(POCHE_VERSION
, $prod);
195 'compare_dev' => $compare_dev,
196 'compare_prod' => $compare_prod,
198 Tools
::logm('config view');
201 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
202 if ($entry != NULL) {
203 Tools
::logm('view link #' . $id);
204 $content = $entry['content'];
205 if (function_exists('tidy_parse_string')) {
206 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
207 $tidy->cleanRepair();
208 $content = $tidy->value
;
212 'content' => $content,
216 Tools
::logm('error in view call : entry is NULL');
220 $entries = $this->store
->getEntriesByView($view, $this->user
->getId());
221 $this->pagination
->set_total(count($entries));
222 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
223 $datas = $this->store
->getEntriesByView($view, $this->user
->getId(), $this->pagination
->get_limit());
226 'page_links' => $page_links,
228 Tools
::logm('display ' . $view . ' view');
236 * update the password of the current user.
237 * if MODE_DEMO is TRUE, the password can't be updated.
238 * @todo add the return value
239 * @todo set the new password in function header like this updatePassword($newPassword)
242 public function updatePassword()
245 $this->messages
->add('i', _('in demo mode, you can\'t update your password'));
246 Tools
::logm('in demo mode, you can\'t do this');
247 Tools
::redirect('?view=config');
250 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
251 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
252 $this->messages
->add('s', _('your password has been updated'));
253 $this->store
->updatePassword($this->user
->getId(), Tools
::encodeString($_POST['password'] . $this->user
->getUsername()));
255 Tools
::logm('password updated');
259 $this->messages
->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
260 Tools
::redirect('?view=config');
267 * checks if login & password are correct and save the user in session.
268 * it redirects the user to the $referer link
269 * @param string $referer the url to redirect after login
270 * @todo add the return value
273 public function login($referer)
275 if (!empty($_POST['login']) && !empty($_POST['password'])) {
276 $user = $this->store
->login($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
277 if ($user != array()) {
278 # Save login into Session
279 Session
::login($user['username'], $user['password'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
281 $this->messages
->add('s', _('welcome to your poche'));
282 if (!empty($_POST['longlastingsession'])) {
283 $_SESSION['longlastingsession'] = 31536000;
284 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
285 session_set_cookie_params($_SESSION['longlastingsession']);
287 session_set_cookie_params(0);
289 session_regenerate_id(true);
290 Tools
::logm('login successful');
291 Tools
::redirect($referer);
293 $this->messages
->add('e', _('login failed: bad login or password'));
294 Tools
::logm('login failed');
297 $this->messages
->add('e', _('login failed: you have to fill all fields'));
298 Tools
::logm('login failed');
304 * log out the poche user. It cleans the session.
305 * @todo add the return value
308 public function logout()
310 $this->user
= array();
312 $this->messages
->add('s', _('see you soon!'));
313 Tools
::logm('logout');
318 * import from Instapaper. poche needs a ./instapaper-export.html file
319 * @todo add the return value
322 private function importFromInstapaper()
324 # TODO gestion des articles favs
325 $html = new simple_html_dom();
326 $html->load_file('./instapaper-export.html');
327 Tools
::logm('starting import from instapaper');
331 foreach($html->find('ol') as $ul)
333 foreach($ul->find('li') as $li)
336 $url = new Url(base64_encode($a[0]->href
));
337 $this->action('add', $url, 0, TRUE);
340 if (STORAGE
== 'postgres') {
341 $sequence = 'entries_id_seq';
343 $last_id = $this->store
->getLastId($sequence);
344 $this->action('toggle_archive', $url, $last_id, TRUE);
348 # the second <ol> is for read links
351 $this->messages
->add('s', _('import from instapaper completed'));
352 Tools
::logm('import from instapaper completed');
357 * import from Pocket. poche needs a ./ril_export.html file
358 * @todo add the return value
361 private function importFromPocket()
363 # TODO gestion des articles favs
364 $html = new simple_html_dom();
365 $html->load_file('./ril_export.html');
366 Tools
::logm('starting import from pocket');
370 foreach($html->find('ul') as $ul)
372 foreach($ul->find('li') as $li)
375 $url = new Url(base64_encode($a[0]->href
));
376 $this->action('add', $url, 0, TRUE);
379 if (STORAGE
== 'postgres') {
380 $sequence = 'entries_id_seq';
382 $last_id = $this->store
->getLastId($sequence);
383 $this->action('toggle_archive', $url, $last_id, TRUE);
387 # the second <ul> is for read links
390 $this->messages
->add('s', _('import from pocket completed'));
391 Tools
::logm('import from pocket completed');
396 * import from Readability. poche needs a ./readability file
397 * @todo add the return value
400 private function importFromReadability()
402 # TODO gestion des articles lus / favs
403 $str_data = file_get_contents("./readability");
404 $data = json_decode($str_data,true);
405 Tools
::logm('starting import from Readability');
407 foreach ($data as $key => $value) {
409 foreach ($value as $attr => $attr_value) {
410 if ($attr == 'article__url') {
411 $url = new Url(base64_encode($attr_value));
414 if (STORAGE
== 'postgres') {
415 $sequence = 'entries_id_seq';
417 // if ($attr_value == 'favorite' && $attr_value == 'true') {
418 // $last_id = $this->store->getLastId($sequence);
419 // $this->store->favoriteById($last_id);
420 // $this->action('toogle_fav', $url, $last_id, TRUE);
422 if ($attr_value == 'archive' && $attr_value == 'true') {
423 $last_id = $this->store
->getLastId($sequence);
424 $this->action('toggle_archive', $url, $last_id, TRUE);
427 if ($url->isCorrect())
428 $this->action('add', $url, 0, TRUE);
430 $this->messages
->add('s', _('import from Readability completed'));
431 Tools
::logm('import from Readability completed');
436 * import datas into your poche
437 * @param string $from name of the service to import : pocket, instapaper or readability
438 * @todo add the return value
441 public function import($from)
443 if ($from == 'pocket') {
444 return $this->importFromPocket();
446 else if ($from == 'readability') {
447 return $this->importFromReadability();
449 else if ($from == 'instapaper') {
450 return $this->importFromInstapaper();
455 * export poche entries in json
456 * @return json all poche entries
458 public function export()
460 $entries = $this->store
->retrieveAll($this->user
->getId());
461 echo $this->tpl
->render('export.twig', array(
462 'export' => Tools
::renderJson($entries),
464 Tools
::logm('export view');
468 * Checks online the latest version of poche and cache it
469 * @param string $which 'prod' or 'dev'
470 * @return string latest $which version
472 private function getPocheVersion($which = 'prod')
474 $cache_file = CACHE
. '/' . $which;
476 # checks if the cached version file exists
477 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
478 $version = file_get_contents($cache_file);
480 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
481 file_put_contents($cache_file, $version, LOCK_EX
);