diff options
Diffstat (limited to 'inc/poche')
-rw-r--r-- | inc/poche/pocheCore.php | 257 | ||||
-rw-r--r-- | inc/poche/pochePictures.php | 114 | ||||
-rw-r--r-- | inc/poche/pocheTool.class.php | 124 |
3 files changed, 495 insertions, 0 deletions
diff --git a/inc/poche/pocheCore.php b/inc/poche/pocheCore.php new file mode 100644 index 00000000..52c603ac --- /dev/null +++ b/inc/poche/pocheCore.php | |||
@@ -0,0 +1,257 @@ | |||
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 (pocheTool::isUrl($url)) { | ||
97 | $url = pocheTool::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 | pocheTool::logm($url . ' is not a valid url'); | ||
132 | } | ||
133 | |||
134 | return FALSE; | ||
135 | } | ||
136 | |||
137 | function display_view($view, $id = 0, $full_head = 'yes') | ||
138 | { | ||
139 | global $tpl, $store, $msg; | ||
140 | |||
141 | switch ($view) | ||
142 | { | ||
143 | case 'export': | ||
144 | $entries = $store->retrieveAll(); | ||
145 | $tpl->assign('export', pocheTool::renderJson($entries)); | ||
146 | $tpl->draw('export'); | ||
147 | pocheTool::logm('export view'); | ||
148 | break; | ||
149 | case 'config': | ||
150 | $tpl->assign('load_all_js', 0); | ||
151 | $tpl->draw('head'); | ||
152 | $tpl->draw('home'); | ||
153 | $tpl->draw('config'); | ||
154 | $tpl->draw('js'); | ||
155 | $tpl->draw('footer'); | ||
156 | pocheTool::logm('config view'); | ||
157 | break; | ||
158 | case 'view': | ||
159 | $entry = $store->retrieveOneById($id); | ||
160 | |||
161 | if ($entry != NULL) { | ||
162 | $tpl->assign('id', $entry['id']); | ||
163 | $tpl->assign('url', $entry['url']); | ||
164 | $tpl->assign('title', $entry['title']); | ||
165 | $content = $entry['content']; | ||
166 | if (function_exists('tidy_parse_string')) { | ||
167 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | ||
168 | $tidy->cleanRepair(); | ||
169 | $content = $tidy->value; | ||
170 | } | ||
171 | $tpl->assign('content', $content); | ||
172 | $tpl->assign('is_fav', $entry['is_fav']); | ||
173 | $tpl->assign('is_read', $entry['is_read']); | ||
174 | $tpl->assign('load_all_js', 0); | ||
175 | $tpl->draw('view'); | ||
176 | } | ||
177 | else { | ||
178 | pocheTool::logm('error in view call : entry is NULL'); | ||
179 | } | ||
180 | |||
181 | pocheTool::logm('view link #' . $id); | ||
182 | break; | ||
183 | default: # home view | ||
184 | $entries = $store->getEntriesByView($view); | ||
185 | |||
186 | $tpl->assign('entries', $entries); | ||
187 | |||
188 | if ($full_head == 'yes') { | ||
189 | $tpl->assign('load_all_js', 1); | ||
190 | $tpl->draw('head'); | ||
191 | $tpl->draw('home'); | ||
192 | } | ||
193 | |||
194 | $tpl->draw('entries'); | ||
195 | |||
196 | if ($full_head == 'yes') { | ||
197 | $tpl->draw('js'); | ||
198 | $tpl->draw('footer'); | ||
199 | } | ||
200 | break; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Appel d'une action (mark as fav, archive, delete) | ||
206 | */ | ||
207 | function action_to_do($action, $url, $id = 0) | ||
208 | { | ||
209 | global $store, $msg; | ||
210 | |||
211 | switch ($action) | ||
212 | { | ||
213 | case 'add': | ||
214 | if($parametres_url = fetch_url_content($url)) { | ||
215 | if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) { | ||
216 | pocheTool::logm('add link ' . $url); | ||
217 | $last_id = $store->getLastId(); | ||
218 | if (DOWNLOAD_PICTURES) { | ||
219 | $content = filtre_picture($parametres_url['content'], $url, $last_id); | ||
220 | } | ||
221 | #$msg->add('s', _('the link has been added successfully')); | ||
222 | } | ||
223 | else { | ||
224 | #$msg->add('e', _('error during insertion : the link wasn\'t added')); | ||
225 | pocheTool::logm('error during insertion : the link wasn\'t added'); | ||
226 | } | ||
227 | } | ||
228 | else { | ||
229 | #$msg->add('e', _('error during url preparation : the link wasn\'t added')); | ||
230 | pocheTool::logm('error during content fetch'); | ||
231 | } | ||
232 | break; | ||
233 | case 'delete': | ||
234 | if ($store->deleteById($id)) { | ||
235 | if (DOWNLOAD_PICTURES) { | ||
236 | remove_directory(ABS_PATH . $id); | ||
237 | } | ||
238 | #$msg->add('s', _('the link has been deleted successfully')); | ||
239 | pocheTool::logm('delete link #' . $id); | ||
240 | } | ||
241 | else { | ||
242 | #$msg->add('e', _('the link wasn\'t deleted')); | ||
243 | pocheTool::logm('error : can\'t delete link #' . $id); | ||
244 | } | ||
245 | break; | ||
246 | case 'toggle_fav' : | ||
247 | $store->favoriteById($id); | ||
248 | pocheTool::logm('mark as favorite link #' . $id); | ||
249 | break; | ||
250 | case 'toggle_archive' : | ||
251 | $store->archiveById($id); | ||
252 | pocheTool::logm('archive link #' . $id); | ||
253 | break; | ||
254 | default: | ||
255 | break; | ||
256 | } | ||
257 | } | ||
diff --git a/inc/poche/pochePictures.php b/inc/poche/pochePictures.php new file mode 100644 index 00000000..bfc80657 --- /dev/null +++ b/inc/poche/pochePictures.php | |||
@@ -0,0 +1,114 @@ | |||
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 | /** | ||
12 | * On modifie les URLS des images dans le corps de l'article | ||
13 | */ | ||
14 | function filtre_picture($content, $url, $id) | ||
15 | { | ||
16 | $matches = array(); | ||
17 | preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); | ||
18 | foreach($matches as $i => $link) | ||
19 | { | ||
20 | $link[1] = trim($link[1]); | ||
21 | if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) | ||
22 | { | ||
23 | $absolute_path = get_absolute_link($link[2],$url); | ||
24 | $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); | ||
25 | $directory = create_assets_directory($id); | ||
26 | $fullpath = $directory . '/' . $filename; | ||
27 | download_pictures($absolute_path, $fullpath); | ||
28 | $content = str_replace($matches[$i][2], $fullpath, $content); | ||
29 | } | ||
30 | |||
31 | } | ||
32 | |||
33 | return $content; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Retourne le lien absolu | ||
38 | */ | ||
39 | function get_absolute_link($relative_link, $url) | ||
40 | { | ||
41 | /* return if already absolute URL */ | ||
42 | if (parse_url($relative_link, PHP_URL_SCHEME) != '') return $relative_link; | ||
43 | |||
44 | /* queries and anchors */ | ||
45 | if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link; | ||
46 | |||
47 | /* parse base URL and convert to local variables: | ||
48 | $scheme, $host, $path */ | ||
49 | extract(parse_url($url)); | ||
50 | |||
51 | /* remove non-directory element from path */ | ||
52 | $path = preg_replace('#/[^/]*$#', '', $path); | ||
53 | |||
54 | /* destroy path if relative url points to root */ | ||
55 | if ($relative_link[0] == '/') $path = ''; | ||
56 | |||
57 | /* dirty absolute URL */ | ||
58 | $abs = $host . $path . '/' . $relative_link; | ||
59 | |||
60 | /* replace '//' or '/./' or '/foo/../' with '/' */ | ||
61 | $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); | ||
62 | for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} | ||
63 | |||
64 | /* absolute URL is ready! */ | ||
65 | return $scheme.'://'.$abs; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Téléchargement des images | ||
70 | */ | ||
71 | |||
72 | function download_pictures($absolute_path, $fullpath) | ||
73 | { | ||
74 | $rawdata = get_external_file($absolute_path); | ||
75 | |||
76 | if(file_exists($fullpath)) { | ||
77 | unlink($fullpath); | ||
78 | } | ||
79 | $fp = fopen($fullpath, 'x'); | ||
80 | fwrite($fp, $rawdata); | ||
81 | fclose($fp); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Crée un répertoire de médias pour l'article | ||
86 | */ | ||
87 | function create_assets_directory($id) | ||
88 | { | ||
89 | $assets_path = ABS_PATH; | ||
90 | if(!is_dir($assets_path)) { | ||
91 | mkdir($assets_path, 0705); | ||
92 | } | ||
93 | |||
94 | $article_directory = $assets_path . $id; | ||
95 | if(!is_dir($article_directory)) { | ||
96 | mkdir($article_directory, 0705); | ||
97 | } | ||
98 | |||
99 | return $article_directory; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Suppression du répertoire d'images | ||
104 | */ | ||
105 | function remove_directory($directory) | ||
106 | { | ||
107 | if(is_dir($directory)) { | ||
108 | $files = array_diff(scandir($directory), array('.','..')); | ||
109 | foreach ($files as $file) { | ||
110 | (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file"); | ||
111 | } | ||
112 | return rmdir($directory); | ||
113 | } | ||
114 | } | ||
diff --git a/inc/poche/pocheTool.class.php b/inc/poche/pocheTool.class.php new file mode 100644 index 00000000..cade115e --- /dev/null +++ b/inc/poche/pocheTool.class.php | |||
@@ -0,0 +1,124 @@ | |||
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 pocheTools | ||
12 | { | ||
13 | public static function initPhp() | ||
14 | { | ||
15 | define('START_TIME', microtime(true)); | ||
16 | |||
17 | if (phpversion() < 5) { | ||
18 | die(_('Oops, it seems you don\'t have PHP 5.')); | ||
19 | } | ||
20 | |||
21 | error_reporting(E_ALL); | ||
22 | |||
23 | function stripslashesDeep($value) { | ||
24 | return is_array($value) | ||
25 | ? array_map('stripslashesDeep', $value) | ||
26 | : stripslashes($value); | ||
27 | } | ||
28 | |||
29 | if (get_magic_quotes_gpc()) { | ||
30 | $_POST = array_map('stripslashesDeep', $_POST); | ||
31 | $_GET = array_map('stripslashesDeep', $_GET); | ||
32 | $_COOKIE = array_map('stripslashesDeep', $_COOKIE); | ||
33 | } | ||
34 | |||
35 | ob_start(); | ||
36 | register_shutdown_function('ob_end_flush'); | ||
37 | } | ||
38 | |||
39 | public static function isUrl($url) | ||
40 | { | ||
41 | $pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i'; | ||
42 | |||
43 | return preg_match($pattern, $url); | ||
44 | } | ||
45 | |||
46 | public static function getUrl() | ||
47 | { | ||
48 | $https = (!empty($_SERVER['HTTPS']) | ||
49 | && (strtolower($_SERVER['HTTPS']) == 'on')) | ||
50 | || (isset($_SERVER["SERVER_PORT"]) | ||
51 | && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection. | ||
52 | $serverport = (!isset($_SERVER["SERVER_PORT"]) | ||
53 | || $_SERVER["SERVER_PORT"] == '80' | ||
54 | || ($https && $_SERVER["SERVER_PORT"] == '443') | ||
55 | ? '' : ':' . $_SERVER["SERVER_PORT"]); | ||
56 | |||
57 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | ||
58 | |||
59 | if (!isset($_SERVER["SERVER_NAME"])) { | ||
60 | return $scriptname; | ||
61 | } | ||
62 | |||
63 | return 'http' . ($https ? 's' : '') . '://' | ||
64 | . $_SERVER["SERVER_NAME"] . $serverport . $scriptname; | ||
65 | } | ||
66 | |||
67 | public static function redirect($url = '') | ||
68 | { | ||
69 | if ($url === '') { | ||
70 | $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); | ||
71 | if (isset($_POST['returnurl'])) { | ||
72 | $url = $_POST['returnurl']; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | # prevent loop | ||
77 | if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { | ||
78 | $url = pocheTool::getUrl(); | ||
79 | } | ||
80 | |||
81 | if (substr($url, 0, 1) !== '?') { | ||
82 | $ref = pocheTool::getUrl(); | ||
83 | if (substr($url, 0, strlen($ref)) !== $ref) { | ||
84 | $url = $ref; | ||
85 | } | ||
86 | } | ||
87 | header('Location: '.$url); | ||
88 | exit(); | ||
89 | } | ||
90 | |||
91 | public static function cleanURL($url) | ||
92 | { | ||
93 | |||
94 | $url = html_entity_decode(trim($url)); | ||
95 | |||
96 | $stuff = strpos($url,'&utm_source='); | ||
97 | if ($stuff !== FALSE) | ||
98 | $url = substr($url, 0, $stuff); | ||
99 | $stuff = strpos($url,'?utm_source='); | ||
100 | if ($stuff !== FALSE) | ||
101 | $url = substr($url, 0, $stuff); | ||
102 | $stuff = strpos($url,'#xtor=RSS-'); | ||
103 | if ($stuff !== FALSE) | ||
104 | $url = substr($url, 0, $stuff); | ||
105 | |||
106 | return $url; | ||
107 | } | ||
108 | |||
109 | public static function renderJson($data) | ||
110 | { | ||
111 | header('Cache-Control: no-cache, must-revalidate'); | ||
112 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | ||
113 | header('Content-type: application/json; charset=UTF-8'); | ||
114 | |||
115 | echo json_encode($data); | ||
116 | exit(); | ||
117 | } | ||
118 | |||
119 | public static function logm($message) | ||
120 | { | ||
121 | $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n"; | ||
122 | file_put_contents('./log.txt',$t,FILE_APPEND); | ||
123 | } | ||
124 | } \ No newline at end of file | ||