]>
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 NL |
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 | } | |
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 | */ | |
c765c367 | 105 | public function action($action, Url $url, $id = 0) |
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 NL |
112 | Tools::logm('add link ' . $url->getUrl()); |
113 | $last_id = $this->store->getLastId(); | |
114 | if (DOWNLOAD_PICTURES) { | |
115 | $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id); | |
116 | } | |
6a361945 | 117 | $this->messages->add('s', _('the link has been added successfully')); |
eb1af592 NL |
118 | } |
119 | else { | |
6a361945 | 120 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
eb1af592 NL |
121 | Tools::logm('error during insertion : the link wasn\'t added'); |
122 | } | |
123 | } | |
124 | else { | |
6a361945 | 125 | $this->messages->add('e', _('error during fetching content : the link wasn\'t added')); |
eb1af592 NL |
126 | Tools::logm('error during content fetch'); |
127 | } | |
6a361945 | 128 | Tools::redirect(); |
eb1af592 NL |
129 | break; |
130 | case 'delete': | |
bc1ee852 | 131 | $msg = 'delete link #' . $id; |
8d3275be | 132 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
133 | if (DOWNLOAD_PICTURES) { |
134 | remove_directory(ABS_PATH . $id); | |
135 | } | |
6a361945 | 136 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
137 | } |
138 | else { | |
6a361945 | 139 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 140 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 141 | } |
bc1ee852 NL |
142 | Tools::logm($msg); |
143 | Tools::redirect('?'); | |
eb1af592 NL |
144 | break; |
145 | case 'toggle_fav' : | |
8d3275be | 146 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 147 | Tools::logm('mark as favorite link #' . $id); |
55821e04 | 148 | Tools::redirect(); |
eb1af592 NL |
149 | break; |
150 | case 'toggle_archive' : | |
8d3275be | 151 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 152 | Tools::logm('archive link #' . $id); |
55821e04 | 153 | Tools::redirect(); |
eb1af592 NL |
154 | break; |
155 | default: | |
156 | break; | |
157 | } | |
158 | } | |
159 | ||
160 | function displayView($view, $id = 0) | |
161 | { | |
162 | $tpl_vars = array(); | |
163 | ||
164 | switch ($view) | |
165 | { | |
eb1af592 | 166 | case 'config': |
32520785 NL |
167 | $dev = $this->getPocheVersion('dev'); |
168 | $prod = $this->getPocheVersion('prod'); | |
169 | $compare_dev = version_compare(POCHE_VERSION, $dev); | |
170 | $compare_prod = version_compare(POCHE_VERSION, $prod); | |
171 | $tpl_vars = array( | |
172 | 'dev' => $dev, | |
173 | 'prod' => $prod, | |
174 | 'compare_dev' => $compare_dev, | |
175 | 'compare_prod' => $compare_prod, | |
176 | ); | |
eb1af592 NL |
177 | Tools::logm('config view'); |
178 | break; | |
179 | case 'view': | |
8d3275be | 180 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
181 | if ($entry != NULL) { |
182 | Tools::logm('view link #' . $id); | |
183 | $content = $entry['content']; | |
184 | if (function_exists('tidy_parse_string')) { | |
185 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
186 | $tidy->cleanRepair(); | |
187 | $content = $tidy->value; | |
188 | } | |
189 | $tpl_vars = array( | |
190 | 'entry' => $entry, | |
191 | 'content' => $content, | |
192 | ); | |
193 | } | |
194 | else { | |
195 | Tools::logm('error in view call : entry is NULL'); | |
196 | } | |
197 | break; | |
198 | default: # home view | |
8d3275be | 199 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); |
6a361945 NL |
200 | $this->pagination->set_total(count($entries)); |
201 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
8d3275be | 202 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); |
eb1af592 | 203 | $tpl_vars = array( |
6a361945 NL |
204 | 'entries' => $datas, |
205 | 'page_links' => $page_links, | |
eb1af592 | 206 | ); |
6a361945 | 207 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
208 | break; |
209 | } | |
210 | ||
211 | return $tpl_vars; | |
212 | } | |
c765c367 NL |
213 | |
214 | public function updatePassword() | |
215 | { | |
55821e04 | 216 | if (MODE_DEMO) { |
8d3275be | 217 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 218 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 219 | Tools::redirect('?view=config'); |
55821e04 NL |
220 | } |
221 | else { | |
222 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
223 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
224 | $this->messages->add('s', _('your password has been updated')); |
225 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 226 | Session::logout(); |
8d3275be | 227 | Tools::logm('password updated'); |
c765c367 NL |
228 | Tools::redirect(); |
229 | } | |
230 | else { | |
8d3275be | 231 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 232 | Tools::redirect('?view=config'); |
c765c367 NL |
233 | } |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | public function login($referer) | |
239 | { | |
240 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | |
7ce7ec4c NL |
241 | $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])); |
242 | if ($user != array()) { | |
243 | # Save login into Session | |
244 | Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user))); | |
245 | ||
8d3275be | 246 | $this->messages->add('s', _('welcome to your poche')); |
c765c367 NL |
247 | if (!empty($_POST['longlastingsession'])) { |
248 | $_SESSION['longlastingsession'] = 31536000; | |
249 | $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; | |
250 | session_set_cookie_params($_SESSION['longlastingsession']); | |
251 | } else { | |
252 | session_set_cookie_params(0); | |
253 | } | |
254 | session_regenerate_id(true); | |
8d3275be | 255 | Tools::logm('login successful'); |
c765c367 NL |
256 | Tools::redirect($referer); |
257 | } | |
8d3275be | 258 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
259 | Tools::logm('login failed'); |
260 | Tools::redirect(); | |
261 | } else { | |
8d3275be | 262 | $this->messages->add('e', _('login failed: you have to fill all fields')); |
c765c367 NL |
263 | Tools::logm('login failed'); |
264 | Tools::redirect(); | |
265 | } | |
266 | } | |
267 | ||
268 | public function logout() | |
269 | { | |
8d3275be | 270 | $this->messages->add('s', _('see you soon!')); |
c765c367 | 271 | Tools::logm('logout'); |
7ce7ec4c | 272 | $this->user = array(); |
c765c367 NL |
273 | Session::logout(); |
274 | Tools::redirect(); | |
275 | } | |
276 | ||
63c35580 | 277 | private function importFromInstapaper() |
c765c367 | 278 | { |
7f959169 | 279 | # TODO gestion des articles favs |
a62788c6 NL |
280 | $html = new simple_html_dom(); |
281 | $html->load_file('./instapaper-export.html'); | |
282 | ||
283 | $read = 0; | |
284 | $errors = array(); | |
285 | foreach($html->find('ol') as $ul) | |
286 | { | |
287 | foreach($ul->find('li') as $li) | |
288 | { | |
289 | $a = $li->find('a'); | |
290 | $url = new Url(base64_encode($a[0]->href)); | |
291 | $this->action('add', $url); | |
292 | if ($read == '1') { | |
293 | $last_id = $this->store->getLastId(); | |
8d3275be | 294 | $this->action('toggle_archive', $url, $last_id); |
a62788c6 NL |
295 | } |
296 | } | |
7f959169 NL |
297 | |
298 | # the second <ol> is for read links | |
a62788c6 NL |
299 | $read = 1; |
300 | } | |
8d3275be | 301 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
302 | Tools::logm('import from instapaper completed'); |
303 | Tools::redirect(); | |
304 | } | |
c765c367 | 305 | |
63c35580 NL |
306 | private function importFromPocket() |
307 | { | |
7f959169 | 308 | # TODO gestion des articles favs |
63c35580 NL |
309 | $html = new simple_html_dom(); |
310 | $html->load_file('./ril_export.html'); | |
311 | ||
312 | $read = 0; | |
313 | $errors = array(); | |
314 | foreach($html->find('ul') as $ul) | |
315 | { | |
316 | foreach($ul->find('li') as $li) | |
c765c367 | 317 | { |
63c35580 NL |
318 | $a = $li->find('a'); |
319 | $url = new Url(base64_encode($a[0]->href)); | |
320 | $this->action('add', $url); | |
321 | if ($read == '1') { | |
322 | $last_id = $this->store->getLastId(); | |
8d3275be | 323 | $this->action('toggle_archive', $url, $last_id); |
c765c367 | 324 | } |
c765c367 | 325 | } |
7f959169 NL |
326 | |
327 | # the second <ul> is for read links | |
63c35580 | 328 | $read = 1; |
c765c367 | 329 | } |
8d3275be | 330 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
331 | Tools::logm('import from pocket completed'); |
332 | Tools::redirect(); | |
333 | } | |
c765c367 | 334 | |
63c35580 NL |
335 | private function importFromReadability() |
336 | { | |
7f959169 | 337 | # TODO gestion des articles lus / favs |
63c35580 NL |
338 | $str_data = file_get_contents("./readability"); |
339 | $data = json_decode($str_data,true); | |
340 | ||
341 | foreach ($data as $key => $value) { | |
342 | $url = ''; | |
7f959169 NL |
343 | foreach ($value as $attr => $attr_value) { |
344 | if ($attr == 'article__url') { | |
345 | $url = new Url(base64_encode($attr_value)); | |
c765c367 | 346 | } |
7f959169 NL |
347 | // if ($attr_value == 'favorite' && $attr_value == 'true') { |
348 | // $last_id = $this->store->getLastId(); | |
349 | // $this->store->favoriteById($last_id); | |
8d3275be | 350 | // $this->action('toogle_fav', $url, $last_id); |
7f959169 NL |
351 | // } |
352 | // if ($attr_value == 'archive' && $attr_value == 'true') { | |
353 | // $last_id = $this->store->getLastId(); | |
8d3275be | 354 | // $this->action('toggle_archive', $url, $last_id); |
7f959169 | 355 | // } |
c765c367 | 356 | } |
63c35580 NL |
357 | if ($url->isCorrect()) |
358 | $this->action('add', $url); | |
c765c367 | 359 | } |
8d3275be | 360 | $this->messages->add('s', _('import from Readability completed')); |
63c35580 NL |
361 | Tools::logm('import from Readability completed'); |
362 | Tools::redirect(); | |
c765c367 NL |
363 | } |
364 | ||
63c35580 | 365 | public function import($from) |
c765c367 | 366 | { |
63c35580 NL |
367 | if ($from == 'pocket') { |
368 | $this->importFromPocket(); | |
369 | } | |
370 | else if ($from == 'readability') { | |
371 | $this->importFromReadability(); | |
372 | } | |
373 | else if ($from == 'instapaper') { | |
374 | $this->importFromInstapaper(); | |
375 | } | |
376 | } | |
c765c367 | 377 | |
63c35580 NL |
378 | public function export() |
379 | { | |
8d3275be | 380 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
381 | echo $this->tpl->render('export.twig', array( |
382 | 'export' => Tools::renderJson($entries), | |
383 | )); | |
384 | Tools::logm('export view'); | |
c765c367 | 385 | } |
32520785 NL |
386 | |
387 | private function getPocheVersion($which = 'prod') | |
388 | { | |
389 | $cache_file = CACHE . '/' . $which; | |
390 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | |
391 | $version = file_get_contents($cache_file); | |
392 | } else { | |
bc1ee852 | 393 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); |
32520785 NL |
394 | file_put_contents($cache_file, $version, LOCK_EX); |
395 | } | |
396 | return $version; | |
397 | } | |
eb1af592 | 398 | } |