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