]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
2c0c73f92ebc9683cba5cb5d65890a8fb361b522
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()
36 $this->user
= isset($_SESSION['poche_user']) ? $_SESSION['poche_user'] : array();
39 $language = ($this->user
->getConfigValue('language')) ? $this->user
->getConfigValue('language') : LANG
;
40 putenv('LC_ALL=' . $language);
41 setlocale(LC_ALL
, $language);
42 bindtextdomain($language, LOCALE
);
43 textdomain($language);
46 $loader = new Twig_Loader_Filesystem(TPL
);
47 $this->tpl
= new Twig_Environment($loader, array(
50 $this->tpl
->addExtension(new Twig_Extensions_Extension_I18n());
51 # filter to display domain name of an url
52 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
53 $this->tpl
->addFilter($filter);
56 $pager = ($this->user
->getConfigValue('pager')) ? $this->user
->getConfigValue('pager') : PAGINATION
;
57 $this->pagination
= new Paginator($pager, 'p');
60 private function install()
62 Tools
::logm('poche still not installed');
63 echo $this->tpl
->render('install.twig', array(
64 'token' => Session
::getToken()
66 if (isset($_GET['install'])) {
67 if (($_POST['password'] == $_POST['password_repeat'])
68 && $_POST['password'] != "" && $_POST['login'] != "") {
69 # let's rock, install poche baby !
70 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
72 Tools
::logm('poche is now installed');
76 Tools
::logm('error during installation');
84 * Call action (mark as fav, archive, delete, etc.)
86 public function action($action, Url
$url, $id = 0)
91 if($parametres_url = $url->fetchContent()) {
92 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'])) {
93 Tools
::logm('add link ' . $url->getUrl());
94 $last_id = $this->store
->getLastId();
95 if (DOWNLOAD_PICTURES
) {
96 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
98 $this->messages
->add('s', _('the link has been added successfully'));
101 $this->messages
->add('e', _('error during insertion : the link wasn\'t added'));
102 Tools
::logm('error during insertion : the link wasn\'t added');
106 $this->messages
->add('e', _('error during fetching content : the link wasn\'t added'));
107 Tools
::logm('error during content fetch');
112 if ($this->store
->deleteById($id)) {
113 if (DOWNLOAD_PICTURES
) {
114 remove_directory(ABS_PATH
. $id);
116 $this->messages
->add('s', _('the link has been deleted successfully'));
117 Tools
::logm('delete link #' . $id);
120 $this->messages
->add('e', _('the link wasn\'t deleted'));
121 Tools
::logm('error : can\'t delete link #' . $id);
126 $this->store
->favoriteById($id);
127 Tools
::logm('mark as favorite link #' . $id);
130 case 'toggle_archive' :
131 $this->store
->archiveById($id);
132 Tools
::logm('archive link #' . $id);
140 function displayView($view, $id = 0)
147 $dev = $this->getPocheVersion('dev');
148 $prod = $this->getPocheVersion('prod');
149 $compare_dev = version_compare(POCHE_VERSION
, $dev);
150 $compare_prod = version_compare(POCHE_VERSION
, $prod);
154 'compare_dev' => $compare_dev,
155 'compare_prod' => $compare_prod,
157 Tools
::logm('config view');
160 $entry = $this->store
->retrieveOneById($id);
161 if ($entry != NULL) {
162 Tools
::logm('view link #' . $id);
163 $content = $entry['content'];
164 if (function_exists('tidy_parse_string')) {
165 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
166 $tidy->cleanRepair();
167 $content = $tidy->value
;
171 'content' => $content,
175 Tools
::logm('error in view call : entry is NULL');
179 $entries = $this->store
->getEntriesByView($view);
180 $this->pagination
->set_total(count($entries));
181 $page_links = $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
182 $datas = $this->store
->getEntriesByView($view, $this->pagination
->get_limit());
185 'page_links' => $page_links,
187 Tools
::logm('display ' . $view . ' view');
194 public function updatePassword()
197 $this->messages
->add('i', 'in demo mode, you can\'t update your password');
198 Tools
::logm('in demo mode, you can\'t do this');
199 Tools
::redirect('?view=config');
202 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
203 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
204 Tools
::logm('password updated');
205 $this->messages
->add('s', 'your password has been updated');
206 $this->store
->updatePassword(Tools
::encodeString($_POST['password'] . $_SESSION['login']));
211 $this->messages
->add('e', 'the two fields have to be filled & the password must be the same in the two fields');
212 Tools
::redirect('?view=config');
218 public function login($referer)
220 if (!empty($_POST['login']) && !empty($_POST['password'])) {
221 $user = $this->store
->login($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
222 if ($user != array()) {
223 # Save login into Session
224 Session
::login($user['username'], $user['password'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
226 Tools
::logm('login successful');
227 $this->messages
->add('s', 'welcome to your poche');
228 if (!empty($_POST['longlastingsession'])) {
229 $_SESSION['longlastingsession'] = 31536000;
230 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
231 session_set_cookie_params($_SESSION['longlastingsession']);
233 session_set_cookie_params(0);
235 session_regenerate_id(true);
236 Tools
::redirect($referer);
238 $this->messages
->add('e', 'login failed: bad login or password');
239 Tools
::logm('login failed');
242 $this->messages
->add('e', 'login failed: you have to fill all fields');
243 Tools
::logm('login failed');
248 public function logout()
250 $this->messages
->add('s', 'see you soon!');
251 Tools
::logm('logout');
252 $this->user
= array();
257 private function importFromInstapaper()
259 # TODO gestion des articles favs
260 $html = new simple_html_dom();
261 $html->load_file('./instapaper-export.html');
265 foreach($html->find('ol') as $ul)
267 foreach($ul->find('li') as $li)
270 $url = new Url(base64_encode($a[0]->href
));
271 $this->action('add', $url);
273 $last_id = $this->store
->getLastId();
274 $this->store
->archiveById($last_id);
278 # the second <ol> is for read links
281 $this->messages
->add('s', 'import from instapaper completed');
282 Tools
::logm('import from instapaper completed');
286 private function importFromPocket()
288 # TODO gestion des articles favs
289 $html = new simple_html_dom();
290 $html->load_file('./ril_export.html');
294 foreach($html->find('ul') as $ul)
296 foreach($ul->find('li') as $li)
299 $url = new Url(base64_encode($a[0]->href
));
300 $this->action('add', $url);
302 $last_id = $this->store
->getLastId();
303 $this->store
->archiveById($last_id);
307 # the second <ul> is for read links
310 $this->messages
->add('s', 'import from pocket completed');
311 Tools
::logm('import from pocket completed');
315 private function importFromReadability()
317 # TODO gestion des articles lus / favs
318 $str_data = file_get_contents("./readability");
319 $data = json_decode($str_data,true);
321 foreach ($data as $key => $value) {
323 foreach ($value as $attr => $attr_value) {
324 if ($attr == 'article__url') {
325 $url = new Url(base64_encode($attr_value));
327 // if ($attr_value == 'favorite' && $attr_value == 'true') {
328 // $last_id = $this->store->getLastId();
329 // $this->store->favoriteById($last_id);
331 // if ($attr_value == 'archive' && $attr_value == 'true') {
332 // $last_id = $this->store->getLastId();
333 // $this->store->archiveById($last_id);
336 if ($url->isCorrect())
337 $this->action('add', $url);
339 $this->messages
->add('s', 'import from Readability completed');
340 Tools
::logm('import from Readability completed');
344 public function import($from)
346 if ($from == 'pocket') {
347 $this->importFromPocket();
349 else if ($from == 'readability') {
350 $this->importFromReadability();
352 else if ($from == 'instapaper') {
353 $this->importFromInstapaper();
357 public function export()
359 $entries = $this->store
->retrieveAll();
360 echo $this->tpl
->render('export.twig', array(
361 'export' => Tools
::renderJson($entries),
363 Tools
::logm('export view');
366 private function getPocheVersion($which = 'prod')
368 $cache_file = CACHE
. '/' . $which;
369 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
370 $version = file_get_contents($cache_file);
372 $version = file_get_contents('http://www.inthepoche.com/' . $which);
373 file_put_contents($cache_file, $version, LOCK_EX
);