]>
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; | |
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 | */ | |
c765c367 | 76 | public function action($action, Url $url, $id = 0) |
eb1af592 NL |
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 | { | |
eb1af592 NL |
132 | case 'config': |
133 | Tools::logm('config view'); | |
134 | break; | |
135 | case 'view': | |
136 | $entry = $this->store->retrieveOneById($id); | |
137 | if ($entry != NULL) { | |
138 | Tools::logm('view link #' . $id); | |
139 | $content = $entry['content']; | |
140 | if (function_exists('tidy_parse_string')) { | |
141 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
142 | $tidy->cleanRepair(); | |
143 | $content = $tidy->value; | |
144 | } | |
145 | $tpl_vars = array( | |
146 | 'entry' => $entry, | |
147 | 'content' => $content, | |
148 | ); | |
149 | } | |
150 | else { | |
151 | Tools::logm('error in view call : entry is NULL'); | |
152 | } | |
153 | break; | |
154 | default: # home view | |
155 | $entries = $this->store->getEntriesByView($view); | |
156 | $tpl_vars = array( | |
157 | 'entries' => $entries, | |
158 | ); | |
159 | break; | |
160 | } | |
161 | ||
162 | return $tpl_vars; | |
163 | } | |
c765c367 NL |
164 | |
165 | public function updatePassword() | |
166 | { | |
167 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
168 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
169 | if (!MODE_DEMO) { | |
170 | Tools::logm('password updated'); | |
171 | $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login'])); | |
172 | Session::logout(); | |
173 | Tools::redirect(); | |
174 | } | |
175 | else { | |
176 | Tools::logm('in demo mode, you can\'t do this'); | |
177 | } | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | public function login($referer) | |
183 | { | |
184 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | |
185 | if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) { | |
186 | Tools::logm('login successful'); | |
187 | ||
188 | if (!empty($_POST['longlastingsession'])) { | |
189 | $_SESSION['longlastingsession'] = 31536000; | |
190 | $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; | |
191 | session_set_cookie_params($_SESSION['longlastingsession']); | |
192 | } else { | |
193 | session_set_cookie_params(0); | |
194 | } | |
195 | session_regenerate_id(true); | |
196 | Tools::redirect($referer); | |
197 | } | |
198 | Tools::logm('login failed'); | |
199 | Tools::redirect(); | |
200 | } else { | |
201 | Tools::logm('login failed'); | |
202 | Tools::redirect(); | |
203 | } | |
204 | } | |
205 | ||
206 | public function logout() | |
207 | { | |
208 | Tools::logm('logout'); | |
209 | Session::logout(); | |
210 | Tools::redirect(); | |
211 | } | |
212 | ||
63c35580 | 213 | private function importFromInstapaper() |
c765c367 | 214 | { |
a62788c6 NL |
215 | $html = new simple_html_dom(); |
216 | $html->load_file('./instapaper-export.html'); | |
217 | ||
218 | $read = 0; | |
219 | $errors = array(); | |
220 | foreach($html->find('ol') as $ul) | |
221 | { | |
222 | foreach($ul->find('li') as $li) | |
223 | { | |
224 | $a = $li->find('a'); | |
225 | $url = new Url(base64_encode($a[0]->href)); | |
226 | $this->action('add', $url); | |
227 | if ($read == '1') { | |
228 | $last_id = $this->store->getLastId(); | |
229 | $this->store->archiveById($last_id); | |
230 | } | |
231 | } | |
232 | # Instapaper génère un fichier HTML avec deux <ol> | |
233 | # Le premier concerne les éléments non lus | |
234 | # Le second concerne les éléments archivés | |
235 | $read = 1; | |
236 | } | |
63c35580 NL |
237 | Tools::logm('import from instapaper completed'); |
238 | Tools::redirect(); | |
239 | } | |
c765c367 | 240 | |
63c35580 NL |
241 | private function importFromPocket() |
242 | { | |
243 | $html = new simple_html_dom(); | |
244 | $html->load_file('./ril_export.html'); | |
245 | ||
246 | $read = 0; | |
247 | $errors = array(); | |
248 | foreach($html->find('ul') as $ul) | |
249 | { | |
250 | foreach($ul->find('li') as $li) | |
c765c367 | 251 | { |
63c35580 NL |
252 | $a = $li->find('a'); |
253 | $url = new Url(base64_encode($a[0]->href)); | |
254 | $this->action('add', $url); | |
255 | if ($read == '1') { | |
256 | $last_id = $this->store->getLastId(); | |
257 | $this->store->archiveById($last_id); | |
c765c367 | 258 | } |
c765c367 | 259 | } |
63c35580 NL |
260 | # Pocket génère un fichier HTML avec deux <ul> |
261 | # Le premier concerne les éléments non lus | |
262 | # Le second concerne les éléments archivés | |
263 | $read = 1; | |
c765c367 | 264 | } |
63c35580 NL |
265 | Tools::logm('import from pocket completed'); |
266 | Tools::redirect(); | |
267 | } | |
c765c367 | 268 | |
63c35580 NL |
269 | private function importFromReadability() |
270 | { | |
271 | # TODO finaliser tout ça ici | |
272 | # noms des variables + gestion des articles lus | |
273 | $str_data = file_get_contents("./readability"); | |
274 | $data = json_decode($str_data,true); | |
275 | ||
276 | foreach ($data as $key => $value) { | |
277 | $url = ''; | |
278 | foreach ($value as $key2 => $value2) { | |
279 | if ($key2 == 'article__url') { | |
280 | $url = new Url(base64_encode($value2)); | |
c765c367 | 281 | } |
c765c367 | 282 | } |
63c35580 NL |
283 | if ($url->isCorrect()) |
284 | $this->action('add', $url); | |
c765c367 | 285 | } |
63c35580 NL |
286 | Tools::logm('import from Readability completed'); |
287 | Tools::redirect(); | |
c765c367 NL |
288 | } |
289 | ||
63c35580 | 290 | public function import($from) |
c765c367 | 291 | { |
63c35580 NL |
292 | if ($from == 'pocket') { |
293 | $this->importFromPocket(); | |
294 | } | |
295 | else if ($from == 'readability') { | |
296 | $this->importFromReadability(); | |
297 | } | |
298 | else if ($from == 'instapaper') { | |
299 | $this->importFromInstapaper(); | |
300 | } | |
301 | } | |
c765c367 | 302 | |
63c35580 NL |
303 | public function export() |
304 | { | |
305 | $entries = $this->store->retrieveAll(); | |
306 | echo $this->tpl->render('export.twig', array( | |
307 | 'export' => Tools::renderJson($entries), | |
308 | )); | |
309 | Tools::logm('export view'); | |
c765c367 | 310 | } |
eb1af592 | 311 | } |