]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/pocheCore.php
twig implementation
[github/wallabag/wallabag.git] / inc / poche / pocheCore.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 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);
96 if (pocheTools::isUrl($url)) {
97 $url = pocheTools::cleanURL($url);
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'));
131 pocheTools::logm($url . ' is not a valid url');
132 }
133
134 return FALSE;
135 }
136
137 function display_view($view, $id = 0)
138 {
139 global $tpl, $store;
140
141 $tpl_vars = array();
142
143 switch ($view)
144 {
145 case 'install':
146 pocheTools::logm('install mode');
147 break;
148 case 'import';
149 pocheTools::logm('import mode');
150 break;
151 case 'export':
152 $entries = $store->retrieveAll();
153 $tpl->assign('export', pocheTools::renderJson($entries));
154 $tpl->draw('export');
155 pocheTools::logm('export view');
156 break;
157 case 'config':
158 $tpl->assign('load_all_js', 0);
159 $tpl->draw('head');
160 $tpl->draw('home');
161 $tpl->draw('config');
162 $tpl->draw('js');
163 $tpl->draw('footer');
164 pocheTools::logm('config view');
165 break;
166 case 'view':
167 $entry = $store->retrieveOneById($id);
168 if ($entry != NULL) {
169 pocheTools::logm('view link #' . $id);
170 $tpl->assign('id', $entry['id']);
171 $tpl->assign('url', $entry['url']);
172 $tpl->assign('title', $entry['title']);
173 $content = $entry['content'];
174 if (function_exists('tidy_parse_string')) {
175 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
176 $tidy->cleanRepair();
177 $content = $tidy->value;
178 }
179 $tpl->assign('content', $content);
180 $tpl->assign('is_fav', $entry['is_fav']);
181 $tpl->assign('is_read', $entry['is_read']);
182 $tpl->assign('load_all_js', 0);
183 $tpl->draw('view');
184 }
185 else {
186 pocheTools::logm('error in view call : entry is NULL');
187 }
188 break;
189 default: # home view
190 $entries = $store->getEntriesByView($view);
191 $tpl_vars = array(
192 'entries' => $entries,
193 );
194
195 // if ($full_head == 'yes') {
196 // $tpl->assign('load_all_js', 1);
197 // $tpl->draw('head');
198 // $tpl->draw('home');
199 // }
200
201 // $tpl->draw('entries');
202 // if ($full_head == 'yes') {
203 // $tpl->draw('js');
204 // $tpl->draw('footer');
205 // }
206 break;
207 }
208
209 return $tpl_vars;
210 }
211
212 /**
213 * Call action (mark as fav, archive, delete, etc.)
214 */
215 function action_to_do($action, $url, $id = 0)
216 {
217 global $store;
218
219 switch ($action)
220 {
221 case 'add':
222 if($parametres_url = fetch_url_content($url)) {
223 if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) {
224 pocheTools::logm('add link ' . $url);
225 $last_id = $store->getLastId();
226 if (DOWNLOAD_PICTURES) {
227 $content = filtre_picture($parametres_url['content'], $url, $last_id);
228 }
229 #$msg->add('s', _('the link has been added successfully'));
230 }
231 else {
232 #$msg->add('e', _('error during insertion : the link wasn\'t added'));
233 pocheTools::logm('error during insertion : the link wasn\'t added');
234 }
235 }
236 else {
237 #$msg->add('e', _('error during url preparation : the link wasn\'t added'));
238 pocheTools::logm('error during content fetch');
239 }
240 break;
241 case 'delete':
242 if ($store->deleteById($id)) {
243 if (DOWNLOAD_PICTURES) {
244 remove_directory(ABS_PATH . $id);
245 }
246 #$msg->add('s', _('the link has been deleted successfully'));
247 pocheTools::logm('delete link #' . $id);
248 }
249 else {
250 #$msg->add('e', _('the link wasn\'t deleted'));
251 pocheTools::logm('error : can\'t delete link #' . $id);
252 }
253 break;
254 case 'toggle_fav' :
255 $store->favoriteById($id);
256 pocheTools::logm('mark as favorite link #' . $id);
257 break;
258 case 'toggle_archive' :
259 $store->archiveById($id);
260 pocheTools::logm('archive link #' . $id);
261 break;
262 default:
263 break;
264 }
265 }