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