]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
test if /install exists
[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 $user;
14 public $store;
15 public $tpl;
16 public $messages;
17 public $pagination;
18
19 function __construct()
20 {
21 $this->store = new Database();
22 $this->init();
23 $this->messages = new Messages();
24
25 # installation
26 if(!$this->store->isInstalled())
27 {
28 $this->install();
29 }
30 }
31
32 private function init()
33 {
34 if (file_exists('./install') && !DEBUG_POCHE) {
35 Tools::logm('folder /install exists');
36 die('the folder /install exists, you have to delete it before using poche.');
37 }
38
39 Tools::initPhp();
40 Session::init();
41
42 if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
43 $this->user = $_SESSION['poche_user'];
44 }
45 else {
46 # fake user, just for install & login screens
47 $this->user = new User();
48 $this->user->setConfig($this->getDefaultConfig());
49 }
50
51 # l10n
52 $language = $this->user->getConfigValue('language');
53 putenv('LC_ALL=' . $language);
54 setlocale(LC_ALL, $language);
55 bindtextdomain($language, LOCALE);
56 textdomain($language);
57
58 # template engine
59 $loader = new Twig_Loader_Filesystem(TPL);
60 if (DEBUG_POCHE) {
61 $twig_params = array();
62 }
63 else {
64 $twig_params = array('cache' => CACHE);
65 }
66 $this->tpl = new Twig_Environment($loader, $twig_params);
67 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
68 # filter to display domain name of an url
69 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
70 $this->tpl->addFilter($filter);
71
72 # Pagination
73 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
74 }
75
76 private function install()
77 {
78 Tools::logm('poche still not installed');
79 echo $this->tpl->render('install.twig', array(
80 'token' => Session::getToken()
81 ));
82 if (isset($_GET['install'])) {
83 if (($_POST['password'] == $_POST['password_repeat'])
84 && $_POST['password'] != "" && $_POST['login'] != "") {
85 # let's rock, install poche baby !
86 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
87 Session::logout();
88 Tools::logm('poche is now installed');
89 Tools::redirect();
90 }
91 else {
92 Tools::logm('error during installation');
93 Tools::redirect();
94 }
95 }
96 exit();
97 }
98
99 public function getDefaultConfig()
100 {
101 return array(
102 'pager' => PAGINATION,
103 'language' => LANG,
104 );
105 }
106
107 /**
108 * Call action (mark as fav, archive, delete, etc.)
109 */
110 public function action($action, Url $url, $id = 0, $import = FALSE)
111 {
112 switch ($action)
113 {
114 case 'add':
115 if($parametres_url = $url->fetchContent()) {
116 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
117 Tools::logm('add link ' . $url->getUrl());
118 $sequence = '';
119 if (STORAGE == 'postgres') {
120 $sequence = 'entries_id_seq';
121 }
122 $last_id = $this->store->getLastId($sequence);
123 if (DOWNLOAD_PICTURES) {
124 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
125 }
126 if (!$import) {
127 $this->messages->add('s', _('the link has been added successfully'));
128 }
129 }
130 else {
131 if (!$import) {
132 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
133 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
134 }
135 }
136 }
137 else {
138 if (!$import) {
139 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
140 Tools::logm('error during content fetch ' . $url->getUrl());
141 }
142 }
143 if (!$import) {
144 Tools::redirect();
145 }
146 break;
147 case 'delete':
148 $msg = 'delete link #' . $id;
149 if ($this->store->deleteById($id, $this->user->getId())) {
150 if (DOWNLOAD_PICTURES) {
151 remove_directory(ABS_PATH . $id);
152 }
153 $this->messages->add('s', _('the link has been deleted successfully'));
154 }
155 else {
156 $this->messages->add('e', _('the link wasn\'t deleted'));
157 $msg = 'error : can\'t delete link #' . $id;
158 }
159 Tools::logm($msg);
160 Tools::redirect('?');
161 break;
162 case 'toggle_fav' :
163 $this->store->favoriteById($id, $this->user->getId());
164 Tools::logm('mark as favorite link #' . $id);
165 if (!$import) {
166 Tools::redirect();
167 }
168 break;
169 case 'toggle_archive' :
170 $this->store->archiveById($id, $this->user->getId());
171 Tools::logm('archive link #' . $id);
172 if (!$import) {
173 Tools::redirect();
174 }
175 break;
176 default:
177 break;
178 }
179 }
180
181 function displayView($view, $id = 0)
182 {
183 $tpl_vars = array();
184
185 switch ($view)
186 {
187 case 'config':
188 $dev = $this->getPocheVersion('dev');
189 $prod = $this->getPocheVersion('prod');
190 $compare_dev = version_compare(POCHE_VERSION, $dev);
191 $compare_prod = version_compare(POCHE_VERSION, $prod);
192 $tpl_vars = array(
193 'dev' => $dev,
194 'prod' => $prod,
195 'compare_dev' => $compare_dev,
196 'compare_prod' => $compare_prod,
197 );
198 Tools::logm('config view');
199 break;
200 case 'view':
201 $entry = $this->store->retrieveOneById($id, $this->user->getId());
202 if ($entry != NULL) {
203 Tools::logm('view link #' . $id);
204 $content = $entry['content'];
205 if (function_exists('tidy_parse_string')) {
206 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
207 $tidy->cleanRepair();
208 $content = $tidy->value;
209 }
210 $tpl_vars = array(
211 'entry' => $entry,
212 'content' => $content,
213 );
214 }
215 else {
216 Tools::logm('error in view call : entry is NULL');
217 }
218 break;
219 default: # home view
220 $entries = $this->store->getEntriesByView($view, $this->user->getId());
221 $this->pagination->set_total(count($entries));
222 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
223 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
224 $tpl_vars = array(
225 'entries' => $datas,
226 'page_links' => $page_links,
227 );
228 Tools::logm('display ' . $view . ' view');
229 break;
230 }
231
232 return $tpl_vars;
233 }
234
235 public function updatePassword()
236 {
237 if (MODE_DEMO) {
238 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
239 Tools::logm('in demo mode, you can\'t do this');
240 Tools::redirect('?view=config');
241 }
242 else {
243 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
244 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
245 $this->messages->add('s', _('your password has been updated'));
246 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
247 Session::logout();
248 Tools::logm('password updated');
249 Tools::redirect();
250 }
251 else {
252 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
253 Tools::redirect('?view=config');
254 }
255 }
256 }
257 }
258
259 public function login($referer)
260 {
261 if (!empty($_POST['login']) && !empty($_POST['password'])) {
262 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
263 if ($user != array()) {
264 # Save login into Session
265 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
266
267 $this->messages->add('s', _('welcome to your poche'));
268 if (!empty($_POST['longlastingsession'])) {
269 $_SESSION['longlastingsession'] = 31536000;
270 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
271 session_set_cookie_params($_SESSION['longlastingsession']);
272 } else {
273 session_set_cookie_params(0);
274 }
275 session_regenerate_id(true);
276 Tools::logm('login successful');
277 Tools::redirect($referer);
278 }
279 $this->messages->add('e', _('login failed: bad login or password'));
280 Tools::logm('login failed');
281 Tools::redirect();
282 } else {
283 $this->messages->add('e', _('login failed: you have to fill all fields'));
284 Tools::logm('login failed');
285 Tools::redirect();
286 }
287 }
288
289 public function logout()
290 {
291 $this->user = array();
292 Session::logout();
293 $this->messages->add('s', _('see you soon!'));
294 Tools::logm('logout');
295 Tools::redirect();
296 }
297
298 private function importFromInstapaper()
299 {
300 # TODO gestion des articles favs
301 $html = new simple_html_dom();
302 $html->load_file('./instapaper-export.html');
303 Tools::logm('starting import from instapaper');
304
305 $read = 0;
306 $errors = array();
307 foreach($html->find('ol') as $ul)
308 {
309 foreach($ul->find('li') as $li)
310 {
311 $a = $li->find('a');
312 $url = new Url(base64_encode($a[0]->href));
313 $this->action('add', $url, 0, TRUE);
314 if ($read == '1') {
315 $sequence = '';
316 if (STORAGE == 'postgres') {
317 $sequence = 'entries_id_seq';
318 }
319 $last_id = $this->store->getLastId($sequence);
320 $this->action('toggle_archive', $url, $last_id, TRUE);
321 }
322 }
323
324 # the second <ol> is for read links
325 $read = 1;
326 }
327 $this->messages->add('s', _('import from instapaper completed'));
328 Tools::logm('import from instapaper completed');
329 Tools::redirect();
330 }
331
332 private function importFromPocket()
333 {
334 # TODO gestion des articles favs
335 $html = new simple_html_dom();
336 $html->load_file('./ril_export.html');
337 Tools::logm('starting import from pocket');
338
339 $read = 0;
340 $errors = array();
341 foreach($html->find('ul') as $ul)
342 {
343 foreach($ul->find('li') as $li)
344 {
345 $a = $li->find('a');
346 $url = new Url(base64_encode($a[0]->href));
347 $this->action('add', $url, 0, TRUE);
348 if ($read == '1') {
349 $sequence = '';
350 if (STORAGE == 'postgres') {
351 $sequence = 'entries_id_seq';
352 }
353 $last_id = $this->store->getLastId($sequence);
354 $this->action('toggle_archive', $url, $last_id, TRUE);
355 }
356 }
357
358 # the second <ul> is for read links
359 $read = 1;
360 }
361 $this->messages->add('s', _('import from pocket completed'));
362 Tools::logm('import from pocket completed');
363 Tools::redirect();
364 }
365
366 private function importFromReadability()
367 {
368 # TODO gestion des articles lus / favs
369 $str_data = file_get_contents("./readability");
370 $data = json_decode($str_data,true);
371 Tools::logm('starting import from Readability');
372
373 foreach ($data as $key => $value) {
374 $url = '';
375 foreach ($value as $attr => $attr_value) {
376 if ($attr == 'article__url') {
377 $url = new Url(base64_encode($attr_value));
378 }
379 $sequence = '';
380 if (STORAGE == 'postgres') {
381 $sequence = 'entries_id_seq';
382 }
383 // if ($attr_value == 'favorite' && $attr_value == 'true') {
384 // $last_id = $this->store->getLastId($sequence);
385 // $this->store->favoriteById($last_id);
386 // $this->action('toogle_fav', $url, $last_id, TRUE);
387 // }
388 if ($attr_value == 'archive' && $attr_value == 'true') {
389 $last_id = $this->store->getLastId($sequence);
390 $this->action('toggle_archive', $url, $last_id, TRUE);
391 }
392 }
393 if ($url->isCorrect())
394 $this->action('add', $url, 0, TRUE);
395 }
396 $this->messages->add('s', _('import from Readability completed'));
397 Tools::logm('import from Readability completed');
398 Tools::redirect();
399 }
400
401 public function import($from)
402 {
403 if ($from == 'pocket') {
404 $this->importFromPocket();
405 }
406 else if ($from == 'readability') {
407 $this->importFromReadability();
408 }
409 else if ($from == 'instapaper') {
410 $this->importFromInstapaper();
411 }
412 }
413
414 public function export()
415 {
416 $entries = $this->store->retrieveAll($this->user->getId());
417 echo $this->tpl->render('export.twig', array(
418 'export' => Tools::renderJson($entries),
419 ));
420 Tools::logm('export view');
421 }
422
423 private function getPocheVersion($which = 'prod')
424 {
425 $cache_file = CACHE . '/' . $which;
426 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
427 $version = file_get_contents($cache_file);
428 } else {
429 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
430 file_put_contents($cache_file, $version, LOCK_EX);
431 }
432 return $version;
433 }
434 }