]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
789d6647e8558a9f22774a3fd93c1a4f34b1ccbb
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
18 function __construct($storage_type)
20 $this->store
= new $storage_type();
22 $this->messages
= new Messages();
25 if(!$this->store
->isInstalled())
33 private function init()
36 putenv('LC_ALL=' . LANG
);
37 setlocale(LC_ALL
, LANG
);
38 bindtextdomain(LANG
, LOCALE
);
42 $loader = new Twig_Loader_Filesystem(TPL
);
43 $this->tpl
= new Twig_Environment($loader, array(
46 $this->tpl
->addExtension(new Twig_Extensions_Extension_I18n());
47 # filter to display domain name of an url
48 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
49 $this->tpl
->addFilter($filter);
51 $this->pagination
= new Paginator(PAGINATION
, 'p');
57 private function install()
59 Tools
::logm('poche still not installed');
60 echo $this->tpl
->render('install.twig', array(
61 'token' => Session
::getToken()
63 if (isset($_GET['install'])) {
64 if (($_POST['password'] == $_POST['password_repeat'])
65 && $_POST['password'] != "" && $_POST['login'] != "") {
66 # let's rock, install poche baby !
67 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
69 Tools
::logm('poche is now installed');
73 Tools
::logm('error during installation');
80 private function saveUser()
82 $_SESSION['login'] = (isset ($_SESSION['login'])) ? $_SESSION['login'] : $this->store
->getLogin();
83 $_SESSION['pass'] = (isset ($_SESSION['pass'])) ? $_SESSION['pass'] : $this->store
->getPassword();
87 * Call action (mark as fav, archive, delete, etc.)
89 public function action($action, Url
$url, $id = 0)
94 if($parametres_url = $url->fetchContent()) {
95 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'])) {
96 Tools
::logm('add link ' . $url->getUrl());
97 $last_id = $this->store
->getLastId();
98 if (DOWNLOAD_PICTURES
) {
99 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
101 $this->messages
->add('s', _('the link has been added successfully'));
104 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
105 Tools
::logm('error during insertion : the link wasn\'t added');
109 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
110 Tools
::logm('error during content fetch');
115 if ($this->store
->deleteById($id)) {
116 if (DOWNLOAD_PICTURES
) {
117 remove_directory(ABS_PATH
. $id);
119 $this->messages
->add('s', _('the link has been deleted successfully'));
120 Tools
::logm('delete link #' . $id);
123 $this->messages
->add('e', _('the link wasn\'t deleted'));
124 Tools
::logm('error : can\'t delete link #' . $id);
129 $this->store
->favoriteById($id);
130 Tools
::logm('mark as favorite link #' . $id);
133 case 'toggle_archive' :
134 $this->store
->archiveById($id);
135 Tools
::logm('archive link #' . $id);
143 function displayView($view, $id = 0)
150 $dev = $this->getPocheVersion('dev');
151 $prod = $this->getPocheVersion('prod');
152 $compare_dev = version_compare(POCHE_VERSION
, $dev);
153 $compare_prod = version_compare(POCHE_VERSION
, $prod);
157 'compare_dev' => $compare_dev,
158 'compare_prod' => $compare_prod,
160 Tools
::logm('config view');
163 $entry = $this->store
->retrieveOneById($id);
164 if ($entry != NULL) {
165 Tools
::logm('view link #' . $id);
166 $content = $entry['content'];
167 if (function_exists('tidy_parse_string')) {
168 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
169 $tidy->cleanRepair();
170 $content = $tidy->value
;
174 'content' => $content,
178 Tools
::logm('error in view call : entry is NULL');
182 $entries = $this->store
->getEntriesByView($view);
183 $this->pagination
->set_total(count($entries));
184 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
185 $datas = $this->store
->getEntriesByView($view, $this->pagination
->get_limit());
188 'page_links' => $page_links,
190 Tools
::logm('display ' . $view . ' view');
197 public function updatePassword()
200 $this->messages
->add('i', 'in demo mode, you can\'t update your password');
201 Tools
::logm('in demo mode, you can\'t do this');
202 Tools
::redirect('?view=config');
205 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
206 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
207 Tools
::logm('password updated');
208 $this->messages
->add('s', 'your password has been updated');
209 $this->store
->updatePassword(Tools
::encodeString($_POST['password'] . $_SESSION['login']));
214 $this->messages
->add('e', 'the two fields have to be filled & the password must be the same in the two fields');
215 Tools
::redirect('?view=config');
221 public function login($referer)
223 if (!empty($_POST['login']) && !empty($_POST['password'])) {
224 if (Session
::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']))) {
225 Tools
::logm('login successful');
226 $this->messages
->add('s', 'welcome to your poche');
227 if (!empty($_POST['longlastingsession'])) {
228 $_SESSION['longlastingsession'] = 31536000;
229 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
230 session_set_cookie_params($_SESSION['longlastingsession']);
232 session_set_cookie_params(0);
234 session_regenerate_id(true);
235 Tools
::redirect($referer);
237 $this->messages
->add('e', 'login failed: bad login or password');
238 Tools
::logm('login failed');
241 $this->messages
->add('e', 'login failed: you have to fill all fields');
242 Tools
::logm('login failed');
247 public function logout()
249 $this->messages
->add('s', 'see you soon!');
250 Tools
::logm('logout');
255 private function importFromInstapaper()
257 # TODO gestion des articles favs
258 $html = new simple_html_dom();
259 $html->load_file('./instapaper-export.html');
263 foreach($html->find('ol') as $ul)
265 foreach($ul->find('li') as $li)
268 $url = new Url(base64_encode($a[0]->href
));
269 $this->action('add', $url);
271 $last_id = $this->store
->getLastId();
272 $this->store
->archiveById($last_id);
276 # the second <ol> is for read links
279 $this->messages
->add('s', 'import from instapaper completed');
280 Tools
::logm('import from instapaper completed');
284 private function importFromPocket()
286 # TODO gestion des articles favs
287 $html = new simple_html_dom();
288 $html->load_file('./ril_export.html');
292 foreach($html->find('ul') as $ul)
294 foreach($ul->find('li') as $li)
297 $url = new Url(base64_encode($a[0]->href
));
298 $this->action('add', $url);
300 $last_id = $this->store
->getLastId();
301 $this->store
->archiveById($last_id);
305 # the second <ul> is for read links
308 $this->messages
->add('s', 'import from pocket completed');
309 Tools
::logm('import from pocket completed');
313 private function importFromReadability()
315 # TODO gestion des articles lus / favs
316 $str_data = file_get_contents("./readability");
317 $data = json_decode($str_data,true);
319 foreach ($data as $key => $value) {
321 foreach ($value as $attr => $attr_value) {
322 if ($attr == 'article__url') {
323 $url = new Url(base64_encode($attr_value));
325 // if ($attr_value == 'favorite' && $attr_value == 'true') {
326 // $last_id = $this->store->getLastId();
327 // $this->store->favoriteById($last_id);
329 // if ($attr_value == 'archive' && $attr_value == 'true') {
330 // $last_id = $this->store->getLastId();
331 // $this->store->archiveById($last_id);
334 if ($url->isCorrect())
335 $this->action('add', $url);
337 $this->messages
->add('s', 'import from Readability completed');
338 Tools
::logm('import from Readability completed');
342 public function import($from)
344 if ($from == 'pocket') {
345 $this->importFromPocket();
347 else if ($from == 'readability') {
348 $this->importFromReadability();
350 else if ($from == 'instapaper') {
351 $this->importFromInstapaper();
355 public function export()
357 $entries = $this->store
->retrieveAll();
358 echo $this->tpl
->render('export.twig', array(
359 'export' => Tools
::renderJson($entries),
361 Tools
::logm('export view');
364 private function getPocheVersion($which = 'prod')
366 $cache_file = CACHE
. '/' . $which;
367 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
368 $version = file_get_contents($cache_file);
370 $version = file_get_contents('http://www.inthepoche.com/' . $which);
371 file_put_contents($cache_file, $version, LOCK_EX
);