3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
11 function encode_string($string)
13 return sha1($string . SALT
);
16 function get_external_file($url)
19 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
21 if (in_array ('curl', get_loaded_extensions())) {
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);
30 # for ssl, do not verified certificate
31 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
32 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
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);
39 $data = curl_exec($curl);
40 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
41 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
44 # create http context and add timeout and user-agent
45 $context = stream_context_create(
48 'timeout' => $timeout,
49 'header' => "User-Agent: " . $useragent,
50 'follow_location' => true
53 'verify_peer' => false,
54 'allow_self_signed' => true
59 # only download page lesser than 4MB
60 $data = @file_get_contents($url, false, $context, -1, 4000000);
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));
67 # if response is not empty and response is OK
68 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
70 # take charset of page and get it
71 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
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';
79 $html_charset = 'utf-8';
83 # replace charset of url to charset of page
84 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
93 function fetch_url_content($url)
95 $url = base64_decode($url);
96 if (pocheTools
::isUrl($url)) {
97 $url = pocheTools
::cleanURL($url);
98 $html = Encoding
::toUTF8(get_external_file($url));
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));
106 if (function_exists('tidy_parse_string')) {
107 $tidy = tidy_parse_string($html, array(), 'UTF8');
108 $tidy->cleanRepair();
109 $html = $tidy->value
;
112 $parameters = array();
113 if (isset($html) and strlen($html) > 0)
115 $readability = new Readability($html, $url);
116 $readability->convertLinksToFootnotes
= CONVERT_LINKS_FOOTNOTES
;
117 $readability->revertForcedParagraphElements
= REVERT_FORCED_PARAGRAPH_ELEMENTS
;
119 if($readability->init())
121 $content = $readability->articleContent
->innerHTML
;
122 $parameters['title'] = $readability->articleTitle
->innerHTML
;
123 $parameters['content'] = $content;
130 #$msg->add('e', _('error during url preparation : the link is not valid'));
131 pocheTools
::logm($url . ' is not a valid url');
137 function display_view($view, $id = 0, $full_head = 'yes')
144 pocheTools
::logm('install mode');
147 pocheTools
::logm('import mode');
150 $entries = $store->retrieveAll();
151 $tpl->assign('export', pocheTools
::renderJson($entries));
152 $tpl->draw('export');
153 pocheTools
::logm('export view');
156 $tpl->assign('load_all_js', 0);
159 $tpl->draw('config');
161 $tpl->draw('footer');
162 pocheTools
::logm('config view');
165 $entry = $store->retrieveOneById($id);
166 if ($entry != NULL) {
167 pocheTools
::logm('view link #' . $id);
168 $tpl->assign('id', $entry['id']);
169 $tpl->assign('url', $entry['url']);
170 $tpl->assign('title', $entry['title']);
171 $content = $entry['content'];
172 if (function_exists('tidy_parse_string')) {
173 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
174 $tidy->cleanRepair();
175 $content = $tidy->value
;
177 $tpl->assign('content', $content);
178 $tpl->assign('is_fav', $entry['is_fav']);
179 $tpl->assign('is_read', $entry['is_read']);
180 $tpl->assign('load_all_js', 0);
184 pocheTools
::logm('error in view call : entry is NULL');
188 $entries = $store->getEntriesByView($view);
189 $tpl->assign('entries', $entries);
191 if ($full_head == 'yes') {
192 $tpl->assign('load_all_js', 1);
197 $tpl->draw('entries');
198 if ($full_head == 'yes') {
200 $tpl->draw('footer');
207 * Call action (mark as fav, archive, delete, etc.)
209 function action_to_do($action, $url, $id = 0)
216 if($parametres_url = fetch_url_content($url)) {
217 if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) {
218 pocheTools
::logm('add link ' . $url);
219 $last_id = $store->getLastId();
220 if (DOWNLOAD_PICTURES
) {
221 $content = filtre_picture($parametres_url['content'], $url, $last_id);
223 #$msg->add('s', _('the link has been added successfully'));
226 #$msg->add('e', _('error during insertion : the link wasn\'t added'));
227 pocheTools
::logm('error during insertion : the link wasn\'t added');
231 #$msg->add('e', _('error during url preparation : the link wasn\'t added'));
232 pocheTools
::logm('error during content fetch');
236 if ($store->deleteById($id)) {
237 if (DOWNLOAD_PICTURES
) {
238 remove_directory(ABS_PATH
. $id);
240 #$msg->add('s', _('the link has been deleted successfully'));
241 pocheTools
::logm('delete link #' . $id);
244 #$msg->add('e', _('the link wasn\'t deleted'));
245 pocheTools
::logm('error : can\'t delete link #' . $id);
249 $store->favoriteById($id);
250 pocheTools
::logm('mark as favorite link #' . $id);
252 case 'toggle_archive' :
253 $store->archiveById($id);
254 pocheTools
::logm('archive link #' . $id);