]>
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 $this->store
= new Database();
23 $this->messages
= new Messages();
26 if(!$this->store
->isInstalled())
32 private function init()
37 if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
38 $this->user
= $_SESSION['poche_user'];
41 # fake user, just for install & login screens
42 $this->user
= new User();
43 $this->user
->setConfig($this->getDefaultConfig());
47 $language = $this->user
->getConfigValue('language');
48 putenv('LC_ALL=' . $language);
49 setlocale(LC_ALL
, $language);
50 bindtextdomain($language, LOCALE
);
51 textdomain($language);
54 $loader = new Twig_Loader_Filesystem(TPL
);
56 $twig_params = array();
59 $twig_params = array('cache' => CACHE
);
61 $this->tpl
= new Twig_Environment($loader, $twig_params);
62 $this->tpl
->addExtension(new Twig_Extensions_Extension_I18n());
63 # filter to display domain name of an url
64 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
65 $this->tpl
->addFilter($filter);
68 $this->pagination
= new Paginator($this->user
->getConfigValue('pager'), 'p');
71 private function install()
73 Tools
::logm('poche still not installed');
74 echo $this->tpl
->render('install.twig', array(
75 'token' => Session
::getToken()
77 if (isset($_GET['install'])) {
78 if (($_POST['password'] == $_POST['password_repeat'])
79 && $_POST['password'] != "" && $_POST['login'] != "") {
80 # let's rock, install poche baby !
81 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
83 Tools
::logm('poche is now installed');
87 Tools
::logm('error during installation');
94 public function getDefaultConfig()
97 'pager' => PAGINATION
,
103 * Call action (mark as fav, archive, delete, etc.)
105 public function action($action, Url
$url, $id = 0, $import = FALSE)
110 if($parametres_url = $url->fetchContent()) {
111 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user
->getId())) {
112 Tools
::logm('add link ' . $url->getUrl());
114 if (STORAGE
== 'postgres') {
115 $sequence = 'entries_id_seq';
117 $last_id = $this->store
->getLastId($sequence);
118 if (DOWNLOAD_PICTURES
) {
119 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
122 $this->messages
->add('s', _('the link has been added successfully'));
127 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
128 Tools
::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
134 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
135 Tools
::logm('error during content fetch ' . $url->getUrl());
143 $msg = 'delete link #' . $id;
144 if ($this->store
->deleteById($id, $this->user
->getId())) {
145 if (DOWNLOAD_PICTURES
) {
146 remove_directory(ABS_PATH
. $id);
148 $this->messages
->add('s', _('the link has been deleted successfully'));
151 $this->messages
->add('e', _('the link wasn\'t deleted'));
152 $msg = 'error : can\'t delete link #' . $id;
155 Tools
::redirect('?');
158 $this->store
->favoriteById($id, $this->user
->getId());
159 Tools
::logm('mark as favorite link #' . $id);
164 case 'toggle_archive' :
165 $this->store
->archiveById($id, $this->user
->getId());
166 Tools
::logm('archive link #' . $id);
176 function displayView($view, $id = 0)
183 $dev = $this->getPocheVersion('dev');
184 $prod = $this->getPocheVersion('prod');
185 $compare_dev = version_compare(POCHE_VERSION
, $dev);
186 $compare_prod = version_compare(POCHE_VERSION
, $prod);
190 'compare_dev' => $compare_dev,
191 'compare_prod' => $compare_prod,
193 Tools
::logm('config view');
196 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
197 if ($entry != NULL) {
198 Tools
::logm('view link #' . $id);
199 $content = $entry['content'];
200 if (function_exists('tidy_parse_string')) {
201 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
202 $tidy->cleanRepair();
203 $content = $tidy->value
;
207 'content' => $content,
211 Tools
::logm('error in view call : entry is NULL');
215 $entries = $this->store
->getEntriesByView($view, $this->user
->getId());
216 $this->pagination
->set_total(count($entries));
217 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
218 $datas = $this->store
->getEntriesByView($view, $this->user
->getId(), $this->pagination
->get_limit());
221 'page_links' => $page_links,
223 Tools
::logm('display ' . $view . ' view');
230 public function updatePassword()
233 $this->messages
->add('i', _('in demo mode, you can\'t update your password'));
234 Tools
::logm('in demo mode, you can\'t do this');
235 Tools
::redirect('?view=config');
238 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
239 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
240 $this->messages
->add('s', _('your password has been updated'));
241 $this->store
->updatePassword($this->user
->getId(), Tools
::encodeString($_POST['password'] . $this->user
->getUsername()));
243 Tools
::logm('password updated');
247 $this->messages
->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
248 Tools
::redirect('?view=config');
254 public function login($referer)
256 if (!empty($_POST['login']) && !empty($_POST['password'])) {
257 $user = $this->store
->login($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
258 if ($user != array()) {
259 # Save login into Session
260 Session
::login($user['username'], $user['password'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
262 $this->messages
->add('s', _('welcome to your poche'));
263 if (!empty($_POST['longlastingsession'])) {
264 $_SESSION['longlastingsession'] = 31536000;
265 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
266 session_set_cookie_params($_SESSION['longlastingsession']);
268 session_set_cookie_params(0);
270 session_regenerate_id(true);
271 Tools
::logm('login successful');
272 Tools
::redirect($referer);
274 $this->messages
->add('e', _('login failed: bad login or password'));
275 Tools
::logm('login failed');
278 $this->messages
->add('e', _('login failed: you have to fill all fields'));
279 Tools
::logm('login failed');
284 public function logout()
286 $this->user
= array();
288 $this->messages
->add('s', _('see you soon!'));
289 Tools
::logm('logout');
293 private function importFromInstapaper()
295 # TODO gestion des articles favs
296 $html = new simple_html_dom();
297 $html->load_file('./instapaper-export.html');
298 Tools
::logm('starting import from instapaper');
302 foreach($html->find('ol') as $ul)
304 foreach($ul->find('li') as $li)
307 $url = new Url(base64_encode($a[0]->href
));
308 $this->action('add', $url, 0, TRUE);
311 if (STORAGE
== 'postgres') {
312 $sequence = 'entries_id_seq';
314 $last_id = $this->store
->getLastId($sequence);
315 $this->action('toggle_archive', $url, $last_id, TRUE);
319 # the second <ol> is for read links
322 $this->messages
->add('s', _('import from instapaper completed'));
323 Tools
::logm('import from instapaper completed');
327 private function importFromPocket()
329 # TODO gestion des articles favs
330 $html = new simple_html_dom();
331 $html->load_file('./ril_export.html');
332 Tools
::logm('starting import from pocket');
336 foreach($html->find('ul') as $ul)
338 foreach($ul->find('li') as $li)
341 $url = new Url(base64_encode($a[0]->href
));
342 $this->action('add', $url, 0, TRUE);
345 if (STORAGE
== 'postgres') {
346 $sequence = 'entries_id_seq';
348 $last_id = $this->store
->getLastId($sequence);
349 $this->action('toggle_archive', $url, $last_id, TRUE);
353 # the second <ul> is for read links
356 $this->messages
->add('s', _('import from pocket completed'));
357 Tools
::logm('import from pocket completed');
361 private function importFromReadability()
363 # TODO gestion des articles lus / favs
364 $str_data = file_get_contents("./readability");
365 $data = json_decode($str_data,true);
366 Tools
::logm('starting import from Readability');
368 foreach ($data as $key => $value) {
370 foreach ($value as $attr => $attr_value) {
371 if ($attr == 'article__url') {
372 $url = new Url(base64_encode($attr_value));
375 if (STORAGE
== 'postgres') {
376 $sequence = 'entries_id_seq';
378 // if ($attr_value == 'favorite' && $attr_value == 'true') {
379 // $last_id = $this->store->getLastId($sequence);
380 // $this->store->favoriteById($last_id);
381 // $this->action('toogle_fav', $url, $last_id, TRUE);
383 if ($attr_value == 'archive' && $attr_value == 'true') {
384 $last_id = $this->store
->getLastId($sequence);
385 $this->action('toggle_archive', $url, $last_id, TRUE);
388 if ($url->isCorrect())
389 $this->action('add', $url, 0, TRUE);
391 $this->messages
->add('s', _('import from Readability completed'));
392 Tools
::logm('import from Readability completed');
396 public function import($from)
398 if ($from == 'pocket') {
399 $this->importFromPocket();
401 else if ($from == 'readability') {
402 $this->importFromReadability();
404 else if ($from == 'instapaper') {
405 $this->importFromInstapaper();
409 public function export()
411 $entries = $this->store
->retrieveAll($this->user
->getId());
412 echo $this->tpl
->render('export.twig', array(
413 'export' => Tools
::renderJson($entries),
415 Tools
::logm('export view');
418 private function getPocheVersion($which = 'prod')
420 $cache_file = CACHE
. '/' . $which;
421 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
422 $version = file_get_contents($cache_file);
424 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
425 file_put_contents($cache_file, $version, LOCK_EX
);