]>
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($storage_type)
21 $this->store
= new $storage_type();
23 $this->messages
= new Messages();
26 if(!$this->store
->isInstalled())
32 private function init()
37 if (isset($_SESSION['poche_user'])) {
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
);
55 $this->tpl
= new Twig_Environment($loader, array(
58 $this->tpl
->addExtension(new Twig_Extensions_Extension_I18n());
59 # filter to display domain name of an url
60 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
61 $this->tpl
->addFilter($filter);
64 $this->pagination
= new Paginator($this->user
->getConfigValue('pager'), 'p');
67 private function install()
69 Tools
::logm('poche still not installed');
70 echo $this->tpl
->render('install.twig', array(
71 'token' => Session
::getToken()
73 if (isset($_GET['install'])) {
74 if (($_POST['password'] == $_POST['password_repeat'])
75 && $_POST['password'] != "" && $_POST['login'] != "") {
76 # let's rock, install poche baby !
77 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
79 Tools
::logm('poche is now installed');
83 Tools
::logm('error during installation');
90 public function getDefaultConfig()
93 'pager' => PAGINATION
,
99 * Call action (mark as fav, archive, delete, etc.)
101 public function action($action, Url
$url, $id = 0)
106 if($parametres_url = $url->fetchContent()) {
107 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user
->getId())) {
108 Tools
::logm('add link ' . $url->getUrl());
109 $last_id = $this->store
->getLastId();
110 if (DOWNLOAD_PICTURES
) {
111 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
113 $this->messages
->add('s', _('the link has been added successfully'));
116 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
117 Tools
::logm('error during insertion : the link wasn\'t added');
121 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
122 Tools
::logm('error during content fetch');
127 if ($this->store
->deleteById($id, $this->user
->getId())) {
128 if (DOWNLOAD_PICTURES
) {
129 remove_directory(ABS_PATH
. $id);
131 $this->messages
->add('s', _('the link has been deleted successfully'));
132 Tools
::logm('delete link #' . $id);
135 $this->messages
->add('e', _('the link wasn\'t deleted'));
136 Tools
::logm('error : can\'t delete link #' . $id);
141 $this->store
->favoriteById($id, $this->user
->getId());
142 Tools
::logm('mark as favorite link #' . $id);
145 case 'toggle_archive' :
146 $this->store
->archiveById($id, $this->user
->getId());
147 Tools
::logm('archive link #' . $id);
155 function displayView($view, $id = 0)
162 $dev = $this->getPocheVersion('dev');
163 $prod = $this->getPocheVersion('prod');
164 $compare_dev = version_compare(POCHE_VERSION
, $dev);
165 $compare_prod = version_compare(POCHE_VERSION
, $prod);
169 'compare_dev' => $compare_dev,
170 'compare_prod' => $compare_prod,
172 Tools
::logm('config view');
175 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
176 if ($entry != NULL) {
177 Tools
::logm('view link #' . $id);
178 $content = $entry['content'];
179 if (function_exists('tidy_parse_string')) {
180 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
181 $tidy->cleanRepair();
182 $content = $tidy->value
;
186 'content' => $content,
190 Tools
::logm('error in view call : entry is NULL');
194 $entries = $this->store
->getEntriesByView($view, $this->user
->getId());
195 $this->pagination
->set_total(count($entries));
196 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
197 $datas = $this->store
->getEntriesByView($view, $this->user
->getId(), $this->pagination
->get_limit());
200 'page_links' => $page_links,
202 Tools
::logm('display ' . $view . ' view');
209 public function updatePassword()
212 $this->messages
->add('i', _('in demo mode, you can\'t update your password'));
213 Tools
::logm('in demo mode, you can\'t do this');
214 Tools
::redirect('?view=config');
217 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
218 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
219 $this->messages
->add('s', _('your password has been updated'));
220 $this->store
->updatePassword($this->user
->getId(), Tools
::encodeString($_POST['password'] . $this->user
->getUsername()));
222 Tools
::logm('password updated');
226 $this->messages
->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
227 Tools
::redirect('?view=config');
233 public function login($referer)
235 if (!empty($_POST['login']) && !empty($_POST['password'])) {
236 $user = $this->store
->login($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
237 if ($user != array()) {
238 # Save login into Session
239 Session
::login($user['username'], $user['password'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
241 $this->messages
->add('s', _('welcome to your poche'));
242 if (!empty($_POST['longlastingsession'])) {
243 $_SESSION['longlastingsession'] = 31536000;
244 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
245 session_set_cookie_params($_SESSION['longlastingsession']);
247 session_set_cookie_params(0);
249 session_regenerate_id(true);
250 Tools
::logm('login successful');
251 Tools
::redirect($referer);
253 $this->messages
->add('e', _('login failed: bad login or password'));
254 Tools
::logm('login failed');
257 $this->messages
->add('e', _('login failed: you have to fill all fields'));
258 Tools
::logm('login failed');
263 public function logout()
265 $this->messages
->add('s', _('see you soon!'));
266 Tools
::logm('logout');
267 $this->user
= array();
272 private function importFromInstapaper()
274 # TODO gestion des articles favs
275 $html = new simple_html_dom();
276 $html->load_file('./instapaper-export.html');
280 foreach($html->find('ol') as $ul)
282 foreach($ul->find('li') as $li)
285 $url = new Url(base64_encode($a[0]->href
));
286 $this->action('add', $url);
288 $last_id = $this->store
->getLastId();
289 $this->action('toggle_archive', $url, $last_id);
293 # the second <ol> is for read links
296 $this->messages
->add('s', _('import from instapaper completed'));
297 Tools
::logm('import from instapaper completed');
301 private function importFromPocket()
303 # TODO gestion des articles favs
304 $html = new simple_html_dom();
305 $html->load_file('./ril_export.html');
309 foreach($html->find('ul') as $ul)
311 foreach($ul->find('li') as $li)
314 $url = new Url(base64_encode($a[0]->href
));
315 $this->action('add', $url);
317 $last_id = $this->store
->getLastId();
318 $this->action('toggle_archive', $url, $last_id);
322 # the second <ul> is for read links
325 $this->messages
->add('s', _('import from pocket completed'));
326 Tools
::logm('import from pocket completed');
330 private function importFromReadability()
332 # TODO gestion des articles lus / favs
333 $str_data = file_get_contents("./readability");
334 $data = json_decode($str_data,true);
336 foreach ($data as $key => $value) {
338 foreach ($value as $attr => $attr_value) {
339 if ($attr == 'article__url') {
340 $url = new Url(base64_encode($attr_value));
342 // if ($attr_value == 'favorite' && $attr_value == 'true') {
343 // $last_id = $this->store->getLastId();
344 // $this->store->favoriteById($last_id);
345 // $this->action('toogle_fav', $url, $last_id);
347 // if ($attr_value == 'archive' && $attr_value == 'true') {
348 // $last_id = $this->store->getLastId();
349 // $this->action('toggle_archive', $url, $last_id);
352 if ($url->isCorrect())
353 $this->action('add', $url);
355 $this->messages
->add('s', _('import from Readability completed'));
356 Tools
::logm('import from Readability completed');
360 public function import($from)
362 if ($from == 'pocket') {
363 $this->importFromPocket();
365 else if ($from == 'readability') {
366 $this->importFromReadability();
368 else if ($from == 'instapaper') {
369 $this->importFromInstapaper();
373 public function export()
375 $entries = $this->store
->retrieveAll($this->user
->getId());
376 echo $this->tpl
->render('export.twig', array(
377 'export' => Tools
::renderJson($entries),
379 Tools
::logm('export view');
382 private function getPocheVersion($which = 'prod')
384 $cache_file = CACHE
. '/' . $which;
385 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
386 $version = file_get_contents($cache_file);
388 $version = file_get_contents('http://www.inthepoche.com/' . $which);
389 file_put_contents($cache_file, $version, LOCK_EX
);