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