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