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