]>
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; | |
c765c367 NL |
121 | case 'import': |
122 | break; | |
eb1af592 NL |
123 | default: |
124 | break; | |
125 | } | |
126 | } | |
127 | ||
128 | function displayView($view, $id = 0) | |
129 | { | |
130 | $tpl_vars = array(); | |
131 | ||
132 | switch ($view) | |
133 | { | |
134 | case 'install': | |
135 | Tools::logm('install mode'); | |
136 | break; | |
137 | case 'import'; | |
138 | Tools::logm('import mode'); | |
139 | break; | |
140 | case 'export': | |
141 | $entries = $this->store->retrieveAll(); | |
142 | // $tpl->assign('export', Tools::renderJson($entries)); | |
143 | // $tpl->draw('export'); | |
144 | Tools::logm('export view'); | |
145 | break; | |
146 | case 'config': | |
147 | Tools::logm('config view'); | |
148 | break; | |
149 | case 'view': | |
150 | $entry = $this->store->retrieveOneById($id); | |
151 | if ($entry != NULL) { | |
152 | Tools::logm('view link #' . $id); | |
153 | $content = $entry['content']; | |
154 | if (function_exists('tidy_parse_string')) { | |
155 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
156 | $tidy->cleanRepair(); | |
157 | $content = $tidy->value; | |
158 | } | |
159 | $tpl_vars = array( | |
160 | 'entry' => $entry, | |
161 | 'content' => $content, | |
162 | ); | |
163 | } | |
164 | else { | |
165 | Tools::logm('error in view call : entry is NULL'); | |
166 | } | |
167 | break; | |
168 | default: # home view | |
169 | $entries = $this->store->getEntriesByView($view); | |
170 | $tpl_vars = array( | |
171 | 'entries' => $entries, | |
172 | ); | |
173 | break; | |
174 | } | |
175 | ||
176 | return $tpl_vars; | |
177 | } | |
c765c367 NL |
178 | |
179 | public function updatePassword() | |
180 | { | |
181 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
182 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
183 | if (!MODE_DEMO) { | |
184 | Tools::logm('password updated'); | |
185 | $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login'])); | |
186 | Session::logout(); | |
187 | Tools::redirect(); | |
188 | } | |
189 | else { | |
190 | Tools::logm('in demo mode, you can\'t do this'); | |
191 | } | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | public function login($referer) | |
197 | { | |
198 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | |
199 | if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) { | |
200 | Tools::logm('login successful'); | |
201 | ||
202 | if (!empty($_POST['longlastingsession'])) { | |
203 | $_SESSION['longlastingsession'] = 31536000; | |
204 | $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; | |
205 | session_set_cookie_params($_SESSION['longlastingsession']); | |
206 | } else { | |
207 | session_set_cookie_params(0); | |
208 | } | |
209 | session_regenerate_id(true); | |
210 | Tools::redirect($referer); | |
211 | } | |
212 | Tools::logm('login failed'); | |
213 | Tools::redirect(); | |
214 | } else { | |
215 | Tools::logm('login failed'); | |
216 | Tools::redirect(); | |
217 | } | |
218 | } | |
219 | ||
220 | public function logout() | |
221 | { | |
222 | Tools::logm('logout'); | |
223 | Session::logout(); | |
224 | Tools::redirect(); | |
225 | } | |
226 | ||
227 | public function import($from) | |
228 | { | |
229 | if ($from == 'pocket') { | |
230 | $html = new simple_html_dom(); | |
231 | $html->load_file('./ril_export.html'); | |
232 | ||
233 | $read = 0; | |
234 | $errors = array(); | |
235 | foreach($html->find('ul') as $ul) | |
236 | { | |
237 | foreach($ul->find('li') as $li) | |
238 | { | |
239 | $a = $li->find('a'); | |
240 | $url = new Url($a[0]->href); | |
241 | $this->action('add', $url); | |
242 | if ($read == '1') { | |
243 | $last_id = $this->store->lastInsertId(); | |
244 | $sql_update = "UPDATE entries SET is_read=~is_read WHERE id=?"; | |
245 | $params_update = array($last_id); | |
246 | $query_update = $this->store->prepare($sql_update); | |
247 | $query_update->execute($params_update); | |
248 | } | |
249 | } | |
250 | # Pocket génère un fichier HTML avec deux <ul> | |
251 | # Le premier concerne les éléments non lus | |
252 | # Le second concerne les éléments archivés | |
253 | $read = 1; | |
254 | } | |
255 | logm('import from pocket completed'); | |
256 | Tools::redirect(); | |
257 | } | |
258 | else if ($from == 'readability') { | |
259 | # TODO finaliser tout ça ici | |
260 | $str_data = file_get_contents("readability"); | |
261 | $data = json_decode($str_data,true); | |
262 | ||
263 | foreach ($data as $key => $value) { | |
264 | $url = ''; | |
265 | foreach ($value as $key2 => $value2) { | |
266 | if ($key2 == 'article__url') { | |
267 | $url = new Url($value2); | |
268 | } | |
269 | } | |
270 | if ($url != '') | |
271 | action_to_do('add', $url); | |
272 | } | |
273 | logm('import from Readability completed'); | |
274 | Tools::redirect(); | |
275 | } | |
276 | } | |
277 | ||
278 | public function export() | |
279 | { | |
280 | ||
281 | } | |
eb1af592 | 282 | } |