]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
new design, pagination & more
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
1 <?php
2 /**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11 class Poche
12 {
13 public $store;
14 public $tpl;
15 public $messages;
16 public $pagination;
17
18 function __construct($storage_type)
19 {
20 $this->store = new $storage_type();
21 $this->init();
22 $this->messages = new Messages();
23
24 # installation
25 if(!$this->store->isInstalled())
26 {
27 $this->install();
28 }
29
30 $this->saveUser();
31 }
32
33 private function init()
34 {
35 # l10n
36 putenv('LC_ALL=' . LANG);
37 setlocale(LC_ALL, LANG);
38 bindtextdomain(LANG, LOCALE);
39 textdomain(LANG);
40
41 # template engine
42 $loader = new Twig_Loader_Filesystem(TPL);
43 $this->tpl = new Twig_Environment($loader, array(
44 'cache' => CACHE,
45 ));
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);
50
51 $this->pagination = new Paginator(PAGINATION, 'p');
52
53 Tools::initPhp();
54 Session::init();
55 }
56
57 private function install()
58 {
59 Tools::logm('poche still not installed');
60 echo $this->tpl->render('install.twig', array(
61 'token' => Session::getToken()
62 ));
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']));
68 Session::logout();
69 Tools::logm('poche is now installed');
70 Tools::redirect();
71 }
72 else {
73 Tools::logm('error during installation');
74 Tools::redirect();
75 }
76 }
77 exit();
78 }
79
80 private function saveUser()
81 {
82 $_SESSION['login'] = (isset ($_SESSION['login'])) ? $_SESSION['login'] : $this->store->getLogin();
83 $_SESSION['pass'] = (isset ($_SESSION['pass'])) ? $_SESSION['pass'] : $this->store->getPassword();
84 }
85
86 /**
87 * Call action (mark as fav, archive, delete, etc.)
88 */
89 public function action($action, Url $url, $id = 0)
90 {
91 switch ($action)
92 {
93 case 'add':
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);
100 }
101 $this->messages->add('s', _('the link has been added successfully'));
102 }
103 else {
104 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
105 Tools::logm('error during insertion : the link wasn\'t added');
106 }
107 }
108 else {
109 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
110 Tools::logm('error during content fetch');
111 }
112 Tools::redirect();
113 break;
114 case 'delete':
115 if ($this->store->deleteById($id)) {
116 if (DOWNLOAD_PICTURES) {
117 remove_directory(ABS_PATH . $id);
118 }
119 $this->messages->add('s', _('the link has been deleted successfully'));
120 Tools::logm('delete link #' . $id);
121 }
122 else {
123 $this->messages->add('e', _('the link wasn\'t deleted'));
124 Tools::logm('error : can\'t delete link #' . $id);
125 }
126 Tools::redirect();
127 break;
128 case 'toggle_fav' :
129 $this->store->favoriteById($id);
130 Tools::logm('mark as favorite link #' . $id);
131 Tools::redirect();
132 break;
133 case 'toggle_archive' :
134 $this->store->archiveById($id);
135 Tools::logm('archive link #' . $id);
136 Tools::redirect();
137 break;
138 default:
139 break;
140 }
141 }
142
143 function displayView($view, $id = 0)
144 {
145 $tpl_vars = array();
146
147 switch ($view)
148 {
149 case 'config':
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);
154 $tpl_vars = array(
155 'dev' => $dev,
156 'prod' => $prod,
157 'compare_dev' => $compare_dev,
158 'compare_prod' => $compare_prod,
159 );
160 Tools::logm('config view');
161 break;
162 case '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;
171 }
172 $tpl_vars = array(
173 'entry' => $entry,
174 'content' => $content,
175 );
176 }
177 else {
178 Tools::logm('error in view call : entry is NULL');
179 }
180 break;
181 default: # home view
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());
186 $tpl_vars = array(
187 'entries' => $datas,
188 'page_links' => $page_links,
189 );
190 Tools::logm('display ' . $view . ' view');
191 break;
192 }
193
194 return $tpl_vars;
195 }
196
197 public function updatePassword()
198 {
199 if (MODE_DEMO) {
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');
203 }
204 else {
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']));
210 Session::logout();
211 Tools::redirect();
212 }
213 else {
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');
216 }
217 }
218 }
219 }
220
221 public function login($referer)
222 {
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']);
231 } else {
232 session_set_cookie_params(0);
233 }
234 session_regenerate_id(true);
235 Tools::redirect($referer);
236 }
237 $this->messages->add('e', 'login failed: bad login or password');
238 Tools::logm('login failed');
239 Tools::redirect();
240 } else {
241 $this->messages->add('e', 'login failed: you have to fill all fields');
242 Tools::logm('login failed');
243 Tools::redirect();
244 }
245 }
246
247 public function logout()
248 {
249 $this->messages->add('s', 'see you soon!');
250 Tools::logm('logout');
251 Session::logout();
252 Tools::redirect();
253 }
254
255 private function importFromInstapaper()
256 {
257 # TODO gestion des articles favs
258 $html = new simple_html_dom();
259 $html->load_file('./instapaper-export.html');
260
261 $read = 0;
262 $errors = array();
263 foreach($html->find('ol') as $ul)
264 {
265 foreach($ul->find('li') as $li)
266 {
267 $a = $li->find('a');
268 $url = new Url(base64_encode($a[0]->href));
269 $this->action('add', $url);
270 if ($read == '1') {
271 $last_id = $this->store->getLastId();
272 $this->store->archiveById($last_id);
273 }
274 }
275
276 # the second <ol> is for read links
277 $read = 1;
278 }
279 $this->messages->add('s', 'import from instapaper completed');
280 Tools::logm('import from instapaper completed');
281 Tools::redirect();
282 }
283
284 private function importFromPocket()
285 {
286 # TODO gestion des articles favs
287 $html = new simple_html_dom();
288 $html->load_file('./ril_export.html');
289
290 $read = 0;
291 $errors = array();
292 foreach($html->find('ul') as $ul)
293 {
294 foreach($ul->find('li') as $li)
295 {
296 $a = $li->find('a');
297 $url = new Url(base64_encode($a[0]->href));
298 $this->action('add', $url);
299 if ($read == '1') {
300 $last_id = $this->store->getLastId();
301 $this->store->archiveById($last_id);
302 }
303 }
304
305 # the second <ul> is for read links
306 $read = 1;
307 }
308 $this->messages->add('s', 'import from pocket completed');
309 Tools::logm('import from pocket completed');
310 Tools::redirect();
311 }
312
313 private function importFromReadability()
314 {
315 # TODO gestion des articles lus / favs
316 $str_data = file_get_contents("./readability");
317 $data = json_decode($str_data,true);
318
319 foreach ($data as $key => $value) {
320 $url = '';
321 foreach ($value as $attr => $attr_value) {
322 if ($attr == 'article__url') {
323 $url = new Url(base64_encode($attr_value));
324 }
325 // if ($attr_value == 'favorite' && $attr_value == 'true') {
326 // $last_id = $this->store->getLastId();
327 // $this->store->favoriteById($last_id);
328 // }
329 // if ($attr_value == 'archive' && $attr_value == 'true') {
330 // $last_id = $this->store->getLastId();
331 // $this->store->archiveById($last_id);
332 // }
333 }
334 if ($url->isCorrect())
335 $this->action('add', $url);
336 }
337 $this->messages->add('s', 'import from Readability completed');
338 Tools::logm('import from Readability completed');
339 Tools::redirect();
340 }
341
342 public function import($from)
343 {
344 if ($from == 'pocket') {
345 $this->importFromPocket();
346 }
347 else if ($from == 'readability') {
348 $this->importFromReadability();
349 }
350 else if ($from == 'instapaper') {
351 $this->importFromInstapaper();
352 }
353 }
354
355 public function export()
356 {
357 $entries = $this->store->retrieveAll();
358 echo $this->tpl->render('export.twig', array(
359 'export' => Tools::renderJson($entries),
360 ));
361 Tools::logm('export view');
362 }
363
364 private function getPocheVersion($which = 'prod')
365 {
366 $cache_file = CACHE . '/' . $which;
367 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
368 $version = file_get_contents($cache_file);
369 } else {
370 $version = file_get_contents('http://www.inthepoche.com/' . $which);
371 file_put_contents($cache_file, $version, LOCK_EX);
372 }
373 return $version;
374 }
375 }