]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
80bf6919cbed0377de4bca50670e119a3937b5b5
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
17 function __construct($storage_type)
19 $this->store
= new $storage_type();
23 if(!$this->store
->isInstalled())
31 private function init()
34 putenv('LC_ALL=' . LANG
);
35 setlocale(LC_ALL
, LANG
);
36 bindtextdomain(LANG
, LOCALE
);
40 $loader = new Twig_Loader_Filesystem(TPL
);
41 $this->tpl
= new Twig_Environment($loader, array(
44 $this->tpl
->addExtension(new Twig_Extensions_Extension_I18n());
45 # filter to display domain name of an url
46 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
47 $this->tpl
->addFilter($filter);
53 private function install()
55 Tools
::logm('poche still not installed');
56 echo $this->tpl
->render('install.twig', array(
57 'token' => Session
::getToken(),
59 if (isset($_GET['install'])) {
60 if (($_POST['password'] == $_POST['password_repeat'])
61 && $_POST['password'] != "" && $_POST['login'] != "") {
62 # let's rock, install poche baby !
63 $this->store
->install($_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']));
71 private function saveUser()
73 $_SESSION['login'] = (isset ($_SESSION['login'])) ? $_SESSION['login'] : $this->store
->getLogin();
74 $_SESSION['pass'] = (isset ($_SESSION['pass'])) ? $_SESSION['pass'] : $this->store
->getPassword();
78 * Call action (mark as fav, archive, delete, etc.)
80 public function action($action, Url
$url, $id = 0)
85 if($parametres_url = $url->fetchContent()) {
86 if ($this->store
->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'])) {
87 Tools
::logm('add link ' . $url->getUrl());
88 $last_id = $this->store
->getLastId();
89 if (DOWNLOAD_PICTURES
) {
90 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
92 #$msg->add('s', _('the link has been added successfully'));
95 #$msg->add('e', _('error during insertion : the link wasn\'t added'));
96 Tools
::logm('error during insertion : the link wasn\'t added');
100 #$msg->add('e', _('error during url preparation : the link wasn\'t added'));
101 Tools
::logm('error during content fetch');
105 if ($this->store
->deleteById($id)) {
106 if (DOWNLOAD_PICTURES
) {
107 remove_directory(ABS_PATH
. $id);
109 #$msg->add('s', _('the link has been deleted successfully'));
110 Tools
::logm('delete link #' . $id);
113 #$msg->add('e', _('the link wasn\'t deleted'));
114 Tools
::logm('error : can\'t delete link #' . $id);
118 $this->store
->favoriteById($id);
119 Tools
::logm('mark as favorite link #' . $id);
122 case 'toggle_archive' :
123 $this->store
->archiveById($id);
124 Tools
::logm('archive link #' . $id);
132 function displayView($view, $id = 0)
139 $dev = $this->getPocheVersion('dev');
140 $prod = $this->getPocheVersion('prod');
141 $compare_dev = version_compare(POCHE_VERSION
, $dev);
142 $compare_prod = version_compare(POCHE_VERSION
, $prod);
146 'compare_dev' => $compare_dev,
147 'compare_prod' => $compare_prod,
149 Tools
::logm('config view');
152 $entry = $this->store
->retrieveOneById($id);
153 if ($entry != NULL) {
154 Tools
::logm('view link #' . $id);
155 $content = $entry['content'];
156 if (function_exists('tidy_parse_string')) {
157 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
158 $tidy->cleanRepair();
159 $content = $tidy->value
;
163 'content' => $content,
167 Tools
::logm('error in view call : entry is NULL');
171 $entries = $this->store
->getEntriesByView($view);
173 'entries' => $entries,
181 public function updatePassword()
184 $this->messages
->add('i', 'in demo mode, you can\'t update your password');
185 Tools
::logm('in demo mode, you can\'t do this');
188 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
189 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
190 Tools
::logm('password updated');
191 $this->messages
->add('s', 'your password has been updated');
192 $this->store
->updatePassword(Tools
::encodeString($_POST['password'] . $_SESSION['login']));
197 $this->messages
->add('e', 'the two fields have to be filled & the password must be the same in the two fields');
203 public function login($referer)
205 if (!empty($_POST['login']) && !empty($_POST['password'])) {
206 if (Session
::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools
::encodeString($_POST['password'] . $_POST['login']))) {
207 Tools
::logm('login successful');
208 $this->messages
->add('s', 'login successful, welcome to your poche');
209 if (!empty($_POST['longlastingsession'])) {
210 $_SESSION['longlastingsession'] = 31536000;
211 $_SESSION['expires_on'] = time() +
$_SESSION['longlastingsession'];
212 session_set_cookie_params($_SESSION['longlastingsession']);
214 session_set_cookie_params(0);
216 session_regenerate_id(true);
217 Tools
::redirect($referer);
219 $this->messages
->add('e', 'login failed, bad login or password');
220 Tools
::logm('login failed');
223 $this->messages
->add('e', 'login failed, you have to fill all fields');
224 Tools
::logm('login failed');
229 public function logout()
231 $this->messages
->add('s', 'logout successful, see you soon!');
232 Tools
::logm('logout');
237 private function importFromInstapaper()
239 # TODO gestion des articles favs
240 $html = new simple_html_dom();
241 $html->load_file('./instapaper-export.html');
245 foreach($html->find('ol') as $ul)
247 foreach($ul->find('li') as $li)
250 $url = new Url(base64_encode($a[0]->href
));
251 $this->action('add', $url);
253 $last_id = $this->store
->getLastId();
254 $this->store
->archiveById($last_id);
258 # the second <ol> is for read links
261 $this->messages
->add('s', 'import from instapaper completed');
262 Tools
::logm('import from instapaper completed');
266 private function importFromPocket()
268 # TODO gestion des articles favs
269 $html = new simple_html_dom();
270 $html->load_file('./ril_export.html');
274 foreach($html->find('ul') as $ul)
276 foreach($ul->find('li') as $li)
279 $url = new Url(base64_encode($a[0]->href
));
280 $this->action('add', $url);
282 $last_id = $this->store
->getLastId();
283 $this->store
->archiveById($last_id);
287 # the second <ul> is for read links
290 $this->messages
->add('s', 'import from pocket completed');
291 Tools
::logm('import from pocket completed');
295 private function importFromReadability()
297 # TODO gestion des articles lus / favs
298 $str_data = file_get_contents("./readability");
299 $data = json_decode($str_data,true);
301 foreach ($data as $key => $value) {
303 foreach ($value as $attr => $attr_value) {
304 if ($attr == 'article__url') {
305 $url = new Url(base64_encode($attr_value));
307 // if ($attr_value == 'favorite' && $attr_value == 'true') {
308 // $last_id = $this->store->getLastId();
309 // $this->store->favoriteById($last_id);
311 // if ($attr_value == 'archive' && $attr_value == 'true') {
312 // $last_id = $this->store->getLastId();
313 // $this->store->archiveById($last_id);
316 if ($url->isCorrect())
317 $this->action('add', $url);
319 $this->messages
->add('s', 'import from Readability completed');
320 Tools
::logm('import from Readability completed');
324 public function import($from)
326 if ($from == 'pocket') {
327 $this->importFromPocket();
329 else if ($from == 'readability') {
330 $this->importFromReadability();
332 else if ($from == 'instapaper') {
333 $this->importFromInstapaper();
337 public function export()
339 $entries = $this->store
->retrieveAll();
340 echo $this->tpl
->render('export.twig', array(
341 'export' => Tools
::renderJson($entries),
343 Tools
::logm('export view');
346 private function getPocheVersion($which = 'prod')
348 $cache_file = CACHE
. '/' . $which;
349 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
350 $version = file_get_contents($cache_file);
352 $version = file_get_contents('http://www.inthepoche.com/' . $which);
353 file_put_contents($cache_file, $version, LOCK_EX
);