]>
Commit | Line | Data |
---|---|---|
a4565e88 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 | function encode_string($string) | |
12 | { | |
13 | return sha1($string . SALT); | |
14 | } | |
15 | ||
16 | function get_external_file($url) | |
17 | { | |
18 | $timeout = 15; | |
19 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
20 | ||
21 | if (in_array ('curl', get_loaded_extensions())) { | |
22 | # Fetch feed from URL | |
23 | $curl = curl_init(); | |
24 | curl_setopt($curl, CURLOPT_URL, $url); | |
25 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
26 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
27 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
28 | curl_setopt($curl, CURLOPT_HEADER, false); | |
29 | ||
30 | # for ssl, do not verified certificate | |
31 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
32 | curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE ); | |
33 | ||
34 | # FeedBurner requires a proper USER-AGENT... | |
35 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
36 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | |
37 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
38 | ||
39 | $data = curl_exec($curl); | |
40 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
41 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
42 | curl_close($curl); | |
43 | } else { | |
44 | # create http context and add timeout and user-agent | |
45 | $context = stream_context_create( | |
46 | array( | |
47 | 'http' => array( | |
48 | 'timeout' => $timeout, | |
49 | 'header' => "User-Agent: " . $useragent, | |
50 | 'follow_location' => true | |
51 | ), | |
52 | 'ssl' => array( | |
53 | 'verify_peer' => false, | |
54 | 'allow_self_signed' => true | |
55 | ) | |
56 | ) | |
57 | ); | |
58 | ||
59 | # only download page lesser than 4MB | |
60 | $data = @file_get_contents($url, false, $context, -1, 4000000); | |
61 | ||
62 | if (isset($http_response_header) and isset($http_response_header[0])) { | |
63 | $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== FALSE) or (strpos($http_response_header[0], '301 Moved Permanently') !== FALSE)); | |
64 | } | |
65 | } | |
66 | ||
67 | # if response is not empty and response is OK | |
68 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | |
69 | ||
70 | # take charset of page and get it | |
71 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
72 | ||
73 | # if meta tag is found | |
74 | if (!empty($meta[0])) { | |
75 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | |
76 | # if charset is found set it otherwise, set it to utf-8 | |
77 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | |
78 | } else { | |
79 | $html_charset = 'utf-8'; | |
80 | $encoding[1] = ''; | |
81 | } | |
82 | ||
83 | # replace charset of url to charset of page | |
84 | $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); | |
85 | ||
86 | return $data; | |
87 | } | |
88 | else { | |
89 | return FALSE; | |
90 | } | |
91 | } | |
92 | ||
93 | function fetch_url_content($url) | |
94 | { | |
95 | $url = base64_decode($url); | |
161395d7 NL |
96 | if (pocheTools::isUrl($url)) { |
97 | $url = pocheTools::cleanURL($url); | |
a4565e88 NL |
98 | $html = Encoding::toUTF8(get_external_file($url)); |
99 | ||
100 | # if get_external_file if not able to retrieve HTTPS content, try the same URL with HTTP protocol | |
101 | if (!preg_match('!^https?://!i', $url) && (!isset($html) || strlen($html) <= 0)) { | |
102 | $url = 'http://' . $url; | |
103 | $html = Encoding::toUTF8(get_external_file($url)); | |
104 | } | |
105 | ||
106 | if (function_exists('tidy_parse_string')) { | |
107 | $tidy = tidy_parse_string($html, array(), 'UTF8'); | |
108 | $tidy->cleanRepair(); | |
109 | $html = $tidy->value; | |
110 | } | |
111 | ||
112 | $parameters = array(); | |
113 | if (isset($html) and strlen($html) > 0) | |
114 | { | |
115 | $readability = new Readability($html, $url); | |
116 | $readability->convertLinksToFootnotes = CONVERT_LINKS_FOOTNOTES; | |
117 | $readability->revertForcedParagraphElements = REVERT_FORCED_PARAGRAPH_ELEMENTS; | |
118 | ||
119 | if($readability->init()) | |
120 | { | |
121 | $content = $readability->articleContent->innerHTML; | |
122 | $parameters['title'] = $readability->articleTitle->innerHTML; | |
123 | $parameters['content'] = $content; | |
124 | ||
125 | return $parameters; | |
126 | } | |
127 | } | |
128 | } | |
129 | else { | |
130 | #$msg->add('e', _('error during url preparation : the link is not valid')); | |
161395d7 | 131 | pocheTools::logm($url . ' is not a valid url'); |
a4565e88 NL |
132 | } |
133 | ||
134 | return FALSE; | |
135 | } | |
136 | ||
2b840e0c NL |
137 | function get_tpl_file($view) |
138 | { | |
139 | $tpl_file = 'home.twig'; | |
140 | switch ($view) | |
141 | { | |
142 | case 'install': | |
143 | $tpl_file = 'install.twig'; | |
144 | break; | |
145 | case 'import'; | |
146 | $tpl_file = 'import.twig'; | |
147 | break; | |
148 | case 'export': | |
149 | $tpl_file = 'export.twig'; | |
150 | break; | |
151 | case 'config': | |
152 | $tpl_file = 'config.twig'; | |
153 | break; | |
154 | case 'view': | |
155 | $tpl_file = 'view.twig'; | |
156 | break; | |
157 | default: | |
158 | break; | |
159 | } | |
160 | return $tpl_file; | |
161 | } | |
162 | ||
8cbb2a88 | 163 | function display_view($view, $id = 0) |
a4565e88 | 164 | { |
2b840e0c | 165 | global $store; |
a4565e88 | 166 | |
8cbb2a88 NL |
167 | $tpl_vars = array(); |
168 | ||
a4565e88 NL |
169 | switch ($view) |
170 | { | |
5ffe5cf5 | 171 | case 'install': |
161395d7 | 172 | pocheTools::logm('install mode'); |
5ffe5cf5 NL |
173 | break; |
174 | case 'import'; | |
161395d7 | 175 | pocheTools::logm('import mode'); |
5ffe5cf5 | 176 | break; |
a4565e88 NL |
177 | case 'export': |
178 | $entries = $store->retrieveAll(); | |
161395d7 | 179 | $tpl->assign('export', pocheTools::renderJson($entries)); |
a4565e88 | 180 | $tpl->draw('export'); |
161395d7 | 181 | pocheTools::logm('export view'); |
a4565e88 NL |
182 | break; |
183 | case 'config': | |
161395d7 | 184 | pocheTools::logm('config view'); |
a4565e88 NL |
185 | break; |
186 | case 'view': | |
187 | $entry = $store->retrieveOneById($id); | |
a4565e88 | 188 | if ($entry != NULL) { |
161395d7 | 189 | pocheTools::logm('view link #' . $id); |
a4565e88 NL |
190 | $content = $entry['content']; |
191 | if (function_exists('tidy_parse_string')) { | |
192 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
193 | $tidy->cleanRepair(); | |
194 | $content = $tidy->value; | |
195 | } | |
3ba5f81b NL |
196 | $tpl_vars = array( |
197 | 'entry' => $entry, | |
198 | 'content' => $content, | |
199 | ); | |
a4565e88 NL |
200 | } |
201 | else { | |
161395d7 | 202 | pocheTools::logm('error in view call : entry is NULL'); |
a4565e88 | 203 | } |
a4565e88 NL |
204 | break; |
205 | default: # home view | |
206 | $entries = $store->getEntriesByView($view); | |
8cbb2a88 NL |
207 | $tpl_vars = array( |
208 | 'entries' => $entries, | |
209 | ); | |
a4565e88 NL |
210 | break; |
211 | } | |
8cbb2a88 NL |
212 | |
213 | return $tpl_vars; | |
a4565e88 NL |
214 | } |
215 | ||
216 | /** | |
5ffe5cf5 | 217 | * Call action (mark as fav, archive, delete, etc.) |
a4565e88 NL |
218 | */ |
219 | function action_to_do($action, $url, $id = 0) | |
220 | { | |
5ffe5cf5 | 221 | global $store; |
a4565e88 NL |
222 | |
223 | switch ($action) | |
224 | { | |
225 | case 'add': | |
226 | if($parametres_url = fetch_url_content($url)) { | |
227 | if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) { | |
161395d7 | 228 | pocheTools::logm('add link ' . $url); |
a4565e88 NL |
229 | $last_id = $store->getLastId(); |
230 | if (DOWNLOAD_PICTURES) { | |
231 | $content = filtre_picture($parametres_url['content'], $url, $last_id); | |
232 | } | |
233 | #$msg->add('s', _('the link has been added successfully')); | |
234 | } | |
235 | else { | |
236 | #$msg->add('e', _('error during insertion : the link wasn\'t added')); | |
161395d7 | 237 | pocheTools::logm('error during insertion : the link wasn\'t added'); |
a4565e88 NL |
238 | } |
239 | } | |
240 | else { | |
241 | #$msg->add('e', _('error during url preparation : the link wasn\'t added')); | |
161395d7 | 242 | pocheTools::logm('error during content fetch'); |
a4565e88 NL |
243 | } |
244 | break; | |
245 | case 'delete': | |
246 | if ($store->deleteById($id)) { | |
247 | if (DOWNLOAD_PICTURES) { | |
248 | remove_directory(ABS_PATH . $id); | |
249 | } | |
250 | #$msg->add('s', _('the link has been deleted successfully')); | |
161395d7 | 251 | pocheTools::logm('delete link #' . $id); |
a4565e88 NL |
252 | } |
253 | else { | |
254 | #$msg->add('e', _('the link wasn\'t deleted')); | |
161395d7 | 255 | pocheTools::logm('error : can\'t delete link #' . $id); |
a4565e88 NL |
256 | } |
257 | break; | |
258 | case 'toggle_fav' : | |
259 | $store->favoriteById($id); | |
161395d7 | 260 | pocheTools::logm('mark as favorite link #' . $id); |
a4565e88 NL |
261 | break; |
262 | case 'toggle_archive' : | |
263 | $store->archiveById($id); | |
161395d7 | 264 | pocheTools::logm('archive link #' . $id); |
a4565e88 NL |
265 | break; |
266 | default: | |
267 | break; | |
268 | } | |
269 | } |