]>
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'])) {
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)
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());
113 $last_id = $this->store
->getLastId();
114 if (DOWNLOAD_PICTURES
) {
115 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
117 $this->messages
->add('s', _('the link has been added successfully'));
120 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
121 Tools
::logm('error during insertion : the link wasn\'t added');
125 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
126 Tools
::logm('error during content fetch');
131 $msg = 'delete link #' . $id;
132 if ($this->store
->deleteById($id, $this->user
->getId())) {
133 if (DOWNLOAD_PICTURES
) {
134 remove_directory(ABS_PATH
. $id);
136 $this->messages
->add('s', _('the link has been deleted successfully'));
139 $this->messages
->add('e', _('the link wasn\'t deleted'));
140 $msg = 'error : can\'t delete link #' . $id;
143 Tools
::redirect('?');
146 $this->store
->favoriteById($id, $this->user
->getId());
147 Tools
::logm('mark as favorite link #' . $id);
150 case 'toggle_archive' :
151 $this->store
->archiveById($id, $this->user
->getId());
152 Tools
::logm('archive link #' . $id);
160 function displayView($view, $id = 0)
167 $dev = $this->getPocheVersion('dev');
168 $prod = $this->getPocheVersion('prod');
169 $compare_dev = version_compare(POCHE_VERSION
, $dev);
170 $compare_prod = version_compare(POCHE_VERSION
, $prod);
174 'compare_dev' => $compare_dev,
175 'compare_prod' => $compare_prod,
177 Tools
::logm('config view');
180 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
181 if ($entry != NULL) {
182 Tools
::logm('view link #' . $id);
183 $content = $entry['content'];
184 if (function_exists('tidy_parse_string')) {
185 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
186 $tidy->cleanRepair();
187 $content = $tidy->value
;
191 'content' => $content,
195 Tools
::logm('error in view call : entry is NULL');
199 $entries = $this->store
->getEntriesByView($view, $this->user
->getId());
200 $this->pagination
->set_total(count($entries));
201 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
202 $datas = $this->store
->getEntriesByView($view, $this->user
->getId(), $this->pagination
->get_limit());
205 'page_links' => $page_links,
207 Tools
::logm('display ' . $view . ' view');
214 public function updatePassword()
217 $this->messages
->add('i', _('in demo mode, you can\'t update your password'));
218 Tools
::logm('in demo mode, you can\'t do this');
219 Tools
::redirect('?view=config');
222 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
223 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
224 $this->messages
->add('s', _('your password has been updated'));
225 $this->store
->updatePassword($this->user
->getId(), Tools
::encodeString($_POST['password'] . $this->user
->getUsername()));
227 Tools
::logm('password updated');
231 $this->messages
->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
232 Tools
::redirect('?view=config');
238 public function login($referer)
240 if (!empty($_POST['login']) && !empty($_POST['password'])) {
241 $user = $this->store
->login($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
242 if ($user != array()) {
243 # Save login into Session
244 Session
::login($user['username'], $user['password'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
246 $this->messages
->add('s', _('welcome to your poche'));
247 if (!empty($_POST['longlastingsession'])) {
248 $_SESSION['longlastingsession'] = 31536000;
249 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
250 session_set_cookie_params($_SESSION['longlastingsession']);
252 session_set_cookie_params(0);
254 session_regenerate_id(true);
255 Tools
::logm('login successful');
256 Tools
::redirect($referer);
258 $this->messages
->add('e', _('login failed: bad login or password'));
259 Tools
::logm('login failed');
262 $this->messages
->add('e', _('login failed: you have to fill all fields'));
263 Tools
::logm('login failed');
268 public function logout()
270 $this->messages
->add('s', _('see you soon!'));
271 Tools
::logm('logout');
272 $this->user
= array();
277 private function importFromInstapaper()
279 # TODO gestion des articles favs
280 $html = new simple_html_dom();
281 $html->load_file('./instapaper-export.html');
285 foreach($html->find('ol') as $ul)
287 foreach($ul->find('li') as $li)
290 $url = new Url(base64_encode($a[0]->href
));
291 $this->action('add', $url);
293 $last_id = $this->store
->getLastId();
294 $this->action('toggle_archive', $url, $last_id);
298 # the second <ol> is for read links
301 $this->messages
->add('s', _('import from instapaper completed'));
302 Tools
::logm('import from instapaper completed');
306 private function importFromPocket()
308 # TODO gestion des articles favs
309 $html = new simple_html_dom();
310 $html->load_file('./ril_export.html');
314 foreach($html->find('ul') as $ul)
316 foreach($ul->find('li') as $li)
319 $url = new Url(base64_encode($a[0]->href
));
320 $this->action('add', $url);
322 $last_id = $this->store
->getLastId();
323 $this->action('toggle_archive', $url, $last_id);
327 # the second <ul> is for read links
330 $this->messages
->add('s', _('import from pocket completed'));
331 Tools
::logm('import from pocket completed');
335 private function importFromReadability()
337 # TODO gestion des articles lus / favs
338 $str_data = file_get_contents("./readability");
339 $data = json_decode($str_data,true);
341 foreach ($data as $key => $value) {
343 foreach ($value as $attr => $attr_value) {
344 if ($attr == 'article__url') {
345 $url = new Url(base64_encode($attr_value));
347 // if ($attr_value == 'favorite' && $attr_value == 'true') {
348 // $last_id = $this->store->getLastId();
349 // $this->store->favoriteById($last_id);
350 // $this->action('toogle_fav', $url, $last_id);
352 // if ($attr_value == 'archive' && $attr_value == 'true') {
353 // $last_id = $this->store->getLastId();
354 // $this->action('toggle_archive', $url, $last_id);
357 if ($url->isCorrect())
358 $this->action('add', $url);
360 $this->messages
->add('s', _('import from Readability completed'));
361 Tools
::logm('import from Readability completed');
365 public function import($from)
367 if ($from == 'pocket') {
368 $this->importFromPocket();
370 else if ($from == 'readability') {
371 $this->importFromReadability();
373 else if ($from == 'instapaper') {
374 $this->importFromInstapaper();
378 public function export()
380 $entries = $this->store
->retrieveAll($this->user
->getId());
381 echo $this->tpl
->render('export.twig', array(
382 'export' => Tools
::renderJson($entries),
384 Tools
::logm('export view');
387 private function getPocheVersion($which = 'prod')
389 $cache_file = CACHE
. '/' . $which;
390 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
391 $version = file_get_contents($cache_file);
393 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
394 file_put_contents($cache_file, $version, LOCK_EX
);