]>
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 | { | |
13 | public $store; | |
14 | public $tpl; | |
55821e04 | 15 | public $messages; |
6a361945 | 16 | public $pagination; |
eb1af592 NL |
17 | |
18 | function __construct($storage_type) | |
19 | { | |
20 | $this->store = new $storage_type(); | |
21 | $this->init(); | |
6a361945 | 22 | $this->messages = new Messages(); |
eb1af592 NL |
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()); | |
55821e04 NL |
47 | # filter to display domain name of an url |
48 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | |
49 | $this->tpl->addFilter($filter); | |
eb1af592 | 50 | |
6a361945 NL |
51 | $this->pagination = new Paginator(PAGINATION, 'p'); |
52 | ||
eb1af592 NL |
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( | |
6a361945 | 61 | 'token' => Session::getToken() |
eb1af592 NL |
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(); | |
6a361945 NL |
69 | Tools::logm('poche is now installed'); |
70 | Tools::redirect(); | |
71 | } | |
72 | else { | |
73 | Tools::logm('error during installation'); | |
eb1af592 NL |
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 | */ | |
c765c367 | 89 | public function action($action, Url $url, $id = 0) |
eb1af592 NL |
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 | } | |
6a361945 | 101 | $this->messages->add('s', _('the link has been added successfully')); |
eb1af592 NL |
102 | } |
103 | else { | |
6a361945 | 104 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
eb1af592 NL |
105 | Tools::logm('error during insertion : the link wasn\'t added'); |
106 | } | |
107 | } | |
108 | else { | |
6a361945 | 109 | $this->messages->add('e', _('error during fetching content : the link wasn\'t added')); |
eb1af592 NL |
110 | Tools::logm('error during content fetch'); |
111 | } | |
6a361945 | 112 | Tools::redirect(); |
eb1af592 NL |
113 | break; |
114 | case 'delete': | |
115 | if ($this->store->deleteById($id)) { | |
116 | if (DOWNLOAD_PICTURES) { | |
117 | remove_directory(ABS_PATH . $id); | |
118 | } | |
6a361945 | 119 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
120 | Tools::logm('delete link #' . $id); |
121 | } | |
122 | else { | |
6a361945 | 123 | $this->messages->add('e', _('the link wasn\'t deleted')); |
eb1af592 NL |
124 | Tools::logm('error : can\'t delete link #' . $id); |
125 | } | |
6a361945 | 126 | Tools::redirect(); |
eb1af592 NL |
127 | break; |
128 | case 'toggle_fav' : | |
129 | $this->store->favoriteById($id); | |
130 | Tools::logm('mark as favorite link #' . $id); | |
55821e04 | 131 | Tools::redirect(); |
eb1af592 NL |
132 | break; |
133 | case 'toggle_archive' : | |
134 | $this->store->archiveById($id); | |
135 | Tools::logm('archive link #' . $id); | |
55821e04 | 136 | Tools::redirect(); |
eb1af592 NL |
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 | { | |
eb1af592 | 149 | case 'config': |
32520785 NL |
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 | ); | |
eb1af592 NL |
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); | |
6a361945 NL |
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()); | |
eb1af592 | 186 | $tpl_vars = array( |
6a361945 NL |
187 | 'entries' => $datas, |
188 | 'page_links' => $page_links, | |
eb1af592 | 189 | ); |
6a361945 | 190 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
191 | break; |
192 | } | |
193 | ||
194 | return $tpl_vars; | |
195 | } | |
c765c367 NL |
196 | |
197 | public function updatePassword() | |
198 | { | |
55821e04 NL |
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'); | |
6a361945 | 202 | Tools::redirect('?view=config'); |
55821e04 NL |
203 | } |
204 | else { | |
205 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
206 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
c765c367 | 207 | Tools::logm('password updated'); |
55821e04 | 208 | $this->messages->add('s', 'your password has been updated'); |
c765c367 NL |
209 | $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login'])); |
210 | Session::logout(); | |
211 | Tools::redirect(); | |
212 | } | |
213 | else { | |
55821e04 | 214 | $this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields'); |
6a361945 | 215 | Tools::redirect('?view=config'); |
c765c367 NL |
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'); | |
6a361945 | 226 | $this->messages->add('s', 'welcome to your poche'); |
c765c367 NL |
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 | } | |
6a361945 | 237 | $this->messages->add('e', 'login failed: bad login or password'); |
c765c367 NL |
238 | Tools::logm('login failed'); |
239 | Tools::redirect(); | |
240 | } else { | |
6a361945 | 241 | $this->messages->add('e', 'login failed: you have to fill all fields'); |
c765c367 NL |
242 | Tools::logm('login failed'); |
243 | Tools::redirect(); | |
244 | } | |
245 | } | |
246 | ||
247 | public function logout() | |
248 | { | |
6a361945 | 249 | $this->messages->add('s', 'see you soon!'); |
c765c367 NL |
250 | Tools::logm('logout'); |
251 | Session::logout(); | |
252 | Tools::redirect(); | |
253 | } | |
254 | ||
63c35580 | 255 | private function importFromInstapaper() |
c765c367 | 256 | { |
7f959169 | 257 | # TODO gestion des articles favs |
a62788c6 NL |
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 | } | |
7f959169 NL |
275 | |
276 | # the second <ol> is for read links | |
a62788c6 NL |
277 | $read = 1; |
278 | } | |
55821e04 | 279 | $this->messages->add('s', 'import from instapaper completed'); |
63c35580 NL |
280 | Tools::logm('import from instapaper completed'); |
281 | Tools::redirect(); | |
282 | } | |
c765c367 | 283 | |
63c35580 NL |
284 | private function importFromPocket() |
285 | { | |
7f959169 | 286 | # TODO gestion des articles favs |
63c35580 NL |
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) | |
c765c367 | 295 | { |
63c35580 NL |
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); | |
c765c367 | 302 | } |
c765c367 | 303 | } |
7f959169 NL |
304 | |
305 | # the second <ul> is for read links | |
63c35580 | 306 | $read = 1; |
c765c367 | 307 | } |
55821e04 | 308 | $this->messages->add('s', 'import from pocket completed'); |
63c35580 NL |
309 | Tools::logm('import from pocket completed'); |
310 | Tools::redirect(); | |
311 | } | |
c765c367 | 312 | |
63c35580 NL |
313 | private function importFromReadability() |
314 | { | |
7f959169 | 315 | # TODO gestion des articles lus / favs |
63c35580 NL |
316 | $str_data = file_get_contents("./readability"); |
317 | $data = json_decode($str_data,true); | |
318 | ||
319 | foreach ($data as $key => $value) { | |
320 | $url = ''; | |
7f959169 NL |
321 | foreach ($value as $attr => $attr_value) { |
322 | if ($attr == 'article__url') { | |
323 | $url = new Url(base64_encode($attr_value)); | |
c765c367 | 324 | } |
7f959169 NL |
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 | // } | |
c765c367 | 333 | } |
63c35580 NL |
334 | if ($url->isCorrect()) |
335 | $this->action('add', $url); | |
c765c367 | 336 | } |
55821e04 | 337 | $this->messages->add('s', 'import from Readability completed'); |
63c35580 NL |
338 | Tools::logm('import from Readability completed'); |
339 | Tools::redirect(); | |
c765c367 NL |
340 | } |
341 | ||
63c35580 | 342 | public function import($from) |
c765c367 | 343 | { |
63c35580 NL |
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 | } | |
c765c367 | 354 | |
63c35580 NL |
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'); | |
c765c367 | 362 | } |
32520785 NL |
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 | } | |
eb1af592 | 375 | } |