]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
share email +twitter / class messages
[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 $store;
14 public $tpl;
15 public $messages;
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());
45 # filter to display domain name of an url
46 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
47 $this->tpl->addFilter($filter);
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 */
80 public function action($action, Url $url, $id = 0)
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);
120 Tools::redirect();
121 break;
122 case 'toggle_archive' :
123 $this->store->archiveById($id);
124 Tools::logm('archive link #' . $id);
125 Tools::redirect();
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 {
138 case 'config':
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 );
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 }
180
181 public function updatePassword()
182 {
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'] != "") {
190 Tools::logm('password updated');
191 $this->messages->add('s', 'your password has been updated');
192 $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login']));
193 Session::logout();
194 Tools::redirect();
195 }
196 else {
197 $this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields');
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');
208 $this->messages->add('s', 'login successful, welcome to your poche');
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 }
219 $this->messages->add('e', 'login failed, bad login or password');
220 Tools::logm('login failed');
221 Tools::redirect();
222 } else {
223 $this->messages->add('e', 'login failed, you have to fill all fields');
224 Tools::logm('login failed');
225 Tools::redirect();
226 }
227 }
228
229 public function logout()
230 {
231 $this->messages->add('s', 'logout successful, see you soon!');
232 Tools::logm('logout');
233 Session::logout();
234 Tools::redirect();
235 }
236
237 private function importFromInstapaper()
238 {
239 # TODO gestion des articles favs
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 }
257
258 # the second <ol> is for read links
259 $read = 1;
260 }
261 $this->messages->add('s', 'import from instapaper completed');
262 Tools::logm('import from instapaper completed');
263 Tools::redirect();
264 }
265
266 private function importFromPocket()
267 {
268 # TODO gestion des articles favs
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)
277 {
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);
284 }
285 }
286
287 # the second <ul> is for read links
288 $read = 1;
289 }
290 $this->messages->add('s', 'import from pocket completed');
291 Tools::logm('import from pocket completed');
292 Tools::redirect();
293 }
294
295 private function importFromReadability()
296 {
297 # TODO gestion des articles lus / favs
298 $str_data = file_get_contents("./readability");
299 $data = json_decode($str_data,true);
300
301 foreach ($data as $key => $value) {
302 $url = '';
303 foreach ($value as $attr => $attr_value) {
304 if ($attr == 'article__url') {
305 $url = new Url(base64_encode($attr_value));
306 }
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 // }
315 }
316 if ($url->isCorrect())
317 $this->action('add', $url);
318 }
319 $this->messages->add('s', 'import from Readability completed');
320 Tools::logm('import from Readability completed');
321 Tools::redirect();
322 }
323
324 public function import($from)
325 {
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 }
336
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');
344 }
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 }
357 }