diff options
Diffstat (limited to 'inc/poche')
-rw-r--r-- | inc/poche/Poche.class.php | 91 | ||||
-rw-r--r-- | inc/poche/Tools.class.php | 37 | ||||
-rw-r--r-- | inc/poche/Url.class.php | 401 | ||||
-rwxr-xr-x | inc/poche/config.inc.php | 21 | ||||
-rw-r--r-- | inc/poche/define.inc.php | 10 |
5 files changed, 441 insertions, 119 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index e0dc0d20..646193f7 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -47,16 +47,16 @@ class Poche | |||
47 | die('You don\'t have write access on cache directory.'); | 47 | die('You don\'t have write access on cache directory.'); |
48 | } | 48 | } |
49 | else if (file_exists('./install/update.php') && !DEBUG_POCHE) { | 49 | else if (file_exists('./install/update.php') && !DEBUG_POCHE) { |
50 | $msg = 'A poche update is needed. Please execute this update <a href="install/update.php">by clicking here</a>. If you have already do the update, please delete /install folder.'; | 50 | $msg = '<h1>setup</h1><p><strong>It\'s your first time here?</strong> Please copy /install/poche.sqlite in db folder. Then, delete install folder.<br /><strong>If you have already installed poche</strong>, an update is needed <a href="install/update.php">by clicking here</a>.</p>'; |
51 | $allIsGood = FALSE; | 51 | $allIsGood = FALSE; |
52 | } | 52 | } |
53 | else if (file_exists('./install') && !DEBUG_POCHE) { | 53 | else if (file_exists('./install') && !DEBUG_POCHE) { |
54 | $msg = 'If you want to update your poche, you just have to delete /install folder. <br />To install your poche with sqlite, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.'; | 54 | $msg = '<h1>setup</h1><p><strong>If you want to update your poche</strong>, you just have to delete /install folder. <br /><strong>To install your poche with sqlite</strong>, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.</p>'; |
55 | $allIsGood = FALSE; | 55 | $allIsGood = FALSE; |
56 | } | 56 | } |
57 | else if (STORAGE == 'sqlite' && !is_writable(STORAGE_SQLITE)) { | 57 | else if (STORAGE == 'sqlite' && !is_writable(STORAGE_SQLITE)) { |
58 | Tools::logm('you don\'t have write access on sqlite file'); | 58 | Tools::logm('you don\'t have write access on sqlite file'); |
59 | $msg = 'You don\'t have write access on sqlite file.'; | 59 | $msg = '<h1>error</h1><p>You don\'t have write access on sqlite file.</p>'; |
60 | $allIsGood = FALSE; | 60 | $allIsGood = FALSE; |
61 | } | 61 | } |
62 | 62 | ||
@@ -156,36 +156,31 @@ class Poche | |||
156 | switch ($action) | 156 | switch ($action) |
157 | { | 157 | { |
158 | case 'add': | 158 | case 'add': |
159 | if($parametres_url = $url->fetchContent()) { | 159 | $content = $url->extract(); |
160 | if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) { | 160 | |
161 | Tools::logm('add link ' . $url->getUrl()); | 161 | if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) { |
162 | $sequence = ''; | 162 | Tools::logm('add link ' . $url->getUrl()); |
163 | if (STORAGE == 'postgres') { | 163 | $sequence = ''; |
164 | $sequence = 'entries_id_seq'; | 164 | if (STORAGE == 'postgres') { |
165 | } | 165 | $sequence = 'entries_id_seq'; |
166 | $last_id = $this->store->getLastId($sequence); | ||
167 | if (DOWNLOAD_PICTURES) { | ||
168 | $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id); | ||
169 | Tools::logm('updating content article'); | ||
170 | $this->store->updateContent($last_id, $content, $this->user->getId()); | ||
171 | } | ||
172 | if (!$import) { | ||
173 | $this->messages->add('s', _('the link has been added successfully')); | ||
174 | } | ||
175 | } | 166 | } |
176 | else { | 167 | $last_id = $this->store->getLastId($sequence); |
177 | if (!$import) { | 168 | if (DOWNLOAD_PICTURES) { |
178 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); | 169 | $content = filtre_picture($parametres_url['body'], $url->getUrl(), $last_id); |
179 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | 170 | Tools::logm('updating content article'); |
180 | } | 171 | $this->store->updateContent($last_id, $content, $this->user->getId()); |
172 | } | ||
173 | if (!$import) { | ||
174 | $this->messages->add('s', _('the link has been added successfully')); | ||
181 | } | 175 | } |
182 | } | 176 | } |
183 | else { | 177 | else { |
184 | if (!$import) { | 178 | if (!$import) { |
185 | $this->messages->add('e', _('error during fetching content : the link wasn\'t added')); | 179 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
186 | Tools::logm('error during content fetch ' . $url->getUrl()); | 180 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); |
187 | } | 181 | } |
188 | } | 182 | } |
183 | |||
189 | if (!$import) { | 184 | if (!$import) { |
190 | Tools::redirect(); | 185 | Tools::redirect(); |
191 | } | 186 | } |
@@ -220,7 +215,6 @@ class Poche | |||
220 | } | 215 | } |
221 | break; | 216 | break; |
222 | default: | 217 | default: |
223 | Tools::logm('action ' . $action . 'doesn\'t exist'); | ||
224 | break; | 218 | break; |
225 | } | 219 | } |
226 | } | 220 | } |
@@ -364,13 +358,14 @@ class Poche | |||
364 | /** | 358 | /** |
365 | * import from Instapaper. poche needs a ./instapaper-export.html file | 359 | * import from Instapaper. poche needs a ./instapaper-export.html file |
366 | * @todo add the return value | 360 | * @todo add the return value |
361 | * @param string $targetFile the file used for importing | ||
367 | * @return boolean | 362 | * @return boolean |
368 | */ | 363 | */ |
369 | private function importFromInstapaper() | 364 | private function importFromInstapaper($targetFile) |
370 | { | 365 | { |
371 | # TODO gestion des articles favs | 366 | # TODO gestion des articles favs |
372 | $html = new simple_html_dom(); | 367 | $html = new simple_html_dom(); |
373 | $html->load_file('./instapaper-export.html'); | 368 | $html->load_file($targetFile); |
374 | Tools::logm('starting import from instapaper'); | 369 | Tools::logm('starting import from instapaper'); |
375 | 370 | ||
376 | $read = 0; | 371 | $read = 0; |
@@ -403,13 +398,14 @@ class Poche | |||
403 | /** | 398 | /** |
404 | * import from Pocket. poche needs a ./ril_export.html file | 399 | * import from Pocket. poche needs a ./ril_export.html file |
405 | * @todo add the return value | 400 | * @todo add the return value |
401 | * @param string $targetFile the file used for importing | ||
406 | * @return boolean | 402 | * @return boolean |
407 | */ | 403 | */ |
408 | private function importFromPocket() | 404 | private function importFromPocket($targetFile) |
409 | { | 405 | { |
410 | # TODO gestion des articles favs | 406 | # TODO gestion des articles favs |
411 | $html = new simple_html_dom(); | 407 | $html = new simple_html_dom(); |
412 | $html->load_file('./ril_export.html'); | 408 | $html->load_file($targetFile); |
413 | Tools::logm('starting import from pocket'); | 409 | Tools::logm('starting import from pocket'); |
414 | 410 | ||
415 | $read = 0; | 411 | $read = 0; |
@@ -442,12 +438,13 @@ class Poche | |||
442 | /** | 438 | /** |
443 | * import from Readability. poche needs a ./readability file | 439 | * import from Readability. poche needs a ./readability file |
444 | * @todo add the return value | 440 | * @todo add the return value |
441 | * @param string $targetFile the file used for importing | ||
445 | * @return boolean | 442 | * @return boolean |
446 | */ | 443 | */ |
447 | private function importFromReadability() | 444 | private function importFromReadability($targetFile) |
448 | { | 445 | { |
449 | # TODO gestion des articles lus / favs | 446 | # TODO gestion des articles lus / favs |
450 | $str_data = file_get_contents("./readability"); | 447 | $str_data = file_get_contents($targetFile); |
451 | $data = json_decode($str_data,true); | 448 | $data = json_decode($str_data,true); |
452 | Tools::logm('starting import from Readability'); | 449 | Tools::logm('starting import from Readability'); |
453 | $count = 0; | 450 | $count = 0; |
@@ -499,15 +496,31 @@ class Poche | |||
499 | */ | 496 | */ |
500 | public function import($from) | 497 | public function import($from) |
501 | { | 498 | { |
502 | if ($from == 'pocket') { | 499 | $providers = array( |
503 | return $this->importFromPocket(); | 500 | 'pocket' => 'importFromPocket', |
501 | 'readability' => 'importFromReadability', | ||
502 | 'instapaper' => 'importFromInstapaper' | ||
503 | ); | ||
504 | |||
505 | if (! isset($providers[$from])) { | ||
506 | $this->messages->add('e', _('Unknown import provider.')); | ||
507 | Tools::redirect(); | ||
504 | } | 508 | } |
505 | else if ($from == 'readability') { | 509 | |
506 | return $this->importFromReadability(); | 510 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; |
511 | $targetFile = constant($targetDefinition); | ||
512 | |||
513 | if (! defined($targetDefinition)) { | ||
514 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | ||
515 | Tools::redirect(); | ||
507 | } | 516 | } |
508 | else if ($from == 'instapaper') { | 517 | |
509 | return $this->importFromInstapaper(); | 518 | if (! file_exists($targetFile)) { |
519 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | ||
520 | Tools::redirect(); | ||
510 | } | 521 | } |
522 | |||
523 | $this->$providers[$from]($targetFile); | ||
511 | } | 524 | } |
512 | 525 | ||
513 | /** | 526 | /** |
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php index 1baf745d..3a792d43 100644 --- a/inc/poche/Tools.class.php +++ b/inc/poche/Tools.class.php | |||
@@ -216,13 +216,7 @@ class Tools | |||
216 | 216 | ||
217 | public static function getDomain($url) | 217 | public static function getDomain($url) |
218 | { | 218 | { |
219 | $pieces = parse_url($url); | 219 | return parse_url($url, PHP_URL_HOST); |
220 | $domain = isset($pieces['host']) ? $pieces['host'] : ''; | ||
221 | if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { | ||
222 | return $regs['domain']; | ||
223 | } | ||
224 | |||
225 | return FALSE; | ||
226 | } | 220 | } |
227 | 221 | ||
228 | public static function getReadingTime($text) { | 222 | public static function getReadingTime($text) { |
@@ -239,24 +233,19 @@ class Tools | |||
239 | { | 233 | { |
240 | $myconfig_file = './inc/poche/myconfig.inc.php'; | 234 | $myconfig_file = './inc/poche/myconfig.inc.php'; |
241 | 235 | ||
242 | if (version_compare(POCHE_VERSION, '1.0-beta3') == 1) { | 236 | if (!is_writable('./inc/poche/')) { |
243 | # $myconfig_file is only created with poche > 1.0-beta3 | 237 | self::logm('you don\'t have write access to create ./inc/poche/myconfig.inc.php'); |
244 | # in 1.0-beta3, the update script creates $myconfig_file | 238 | die('You don\'t have write access to create ./inc/poche/myconfig.inc.php.'); |
245 | 239 | } | |
246 | if (!is_writable('./inc/poche/')) { | ||
247 | self::logm('you don\'t have write access to create ./inc/poche/myconfig.inc.php'); | ||
248 | die('You don\'t have write access to create ./inc/poche/myconfig.inc.php.'); | ||
249 | } | ||
250 | 240 | ||
251 | if (!file_exists($myconfig_file)) | 241 | if (!file_exists($myconfig_file)) |
252 | { | 242 | { |
253 | $fp = fopen($myconfig_file, 'w'); | 243 | $fp = fopen($myconfig_file, 'w'); |
254 | fwrite($fp, '<?php'."\r\n"); | 244 | fwrite($fp, '<?php'."\r\n"); |
255 | fwrite($fp, "define ('POCHE_VERSION', '1.0-beta3');" . "\r\n"); | 245 | fwrite($fp, "define ('POCHE_VERSION', '1.0-beta4');" . "\r\n"); |
256 | fwrite($fp, "define ('SALT', '" . md5(time() . $_SERVER['SCRIPT_FILENAME'] . rand()) . "');" . "\r\n"); | 246 | fwrite($fp, "define ('SALT', '" . md5(time() . $_SERVER['SCRIPT_FILENAME'] . rand()) . "');" . "\r\n"); |
257 | fwrite($fp, "define ('LANG', 'en_EN.utf8');" . "\r\n"); | 247 | fwrite($fp, "define ('LANG', 'en_EN.utf8');" . "\r\n"); |
258 | fclose($fp); | 248 | fclose($fp); |
259 | } | ||
260 | } | 249 | } |
261 | } | 250 | } |
262 | } \ No newline at end of file | 251 | } \ No newline at end of file |
diff --git a/inc/poche/Url.class.php b/inc/poche/Url.class.php index 00b0b257..5a893014 100644 --- a/inc/poche/Url.class.php +++ b/inc/poche/Url.class.php | |||
@@ -12,6 +12,45 @@ class Url | |||
12 | { | 12 | { |
13 | public $url; | 13 | public $url; |
14 | 14 | ||
15 | private $fingerprints = array( | ||
16 | // Posterous | ||
17 | '<meta name="generator" content="Posterous"' => array('hostname'=>'fingerprint.posterous.com', 'head'=>true), | ||
18 | // Blogger | ||
19 | '<meta content=\'blogger\' name=\'generator\'' => array('hostname'=>'fingerprint.blogspot.com', 'head'=>true), | ||
20 | '<meta name="generator" content="Blogger"' => array('hostname'=>'fingerprint.blogspot.com', 'head'=>true), | ||
21 | // WordPress (self-hosted and hosted) | ||
22 | '<meta name="generator" content="WordPress' => array('hostname'=>'fingerprint.wordpress.com', 'head'=>true) | ||
23 | ); | ||
24 | |||
25 | private $user_agents = array( 'lifehacker.com' => 'PHP/5.2', | ||
26 | 'gawker.com' => 'PHP/5.2', | ||
27 | 'deadspin.com' => 'PHP/5.2', | ||
28 | 'kotaku.com' => 'PHP/5.2', | ||
29 | 'jezebel.com' => 'PHP/5.2', | ||
30 | 'io9.com' => 'PHP/5.2', | ||
31 | 'jalopnik.com' => 'PHP/5.2', | ||
32 | 'gizmodo.com' => 'PHP/5.2', | ||
33 | '.wikipedia.org' => 'Mozilla/5.2' | ||
34 | ); | ||
35 | |||
36 | private $content_type_exc = array( | ||
37 | 'application/pdf' => array('action'=>'link', 'name'=>'PDF'), | ||
38 | 'image' => array('action'=>'link', 'name'=>'Image'), | ||
39 | 'audio' => array('action'=>'link', 'name'=>'Audio'), | ||
40 | 'video' => array('action'=>'link', 'name'=>'Video') | ||
41 | ); | ||
42 | |||
43 | private $rewrite_url = array( | ||
44 | // Rewrite public Google Docs URLs to point to HTML view: | ||
45 | // if a URL contains docs.google.com, replace /Doc? with /View? | ||
46 | 'docs.google.com' => array('/Doc?' => '/View?'), | ||
47 | 'tnr.com' => array('tnr.com/article/' => 'tnr.com/print/article/'), | ||
48 | '.m.wikipedia.org' => array('.m.wikipedia.org' => '.wikipedia.org') | ||
49 | ); | ||
50 | |||
51 | private $rewrite_relative_urls = true; | ||
52 | private $error_message = '[unable to retrieve full-text content]'; | ||
53 | |||
15 | function __construct($url) | 54 | function __construct($url) |
16 | { | 55 | { |
17 | $this->url = base64_decode($url); | 56 | $this->url = base64_decode($url); |
@@ -25,70 +64,332 @@ class Url | |||
25 | $this->url = $url; | 64 | $this->url = $url; |
26 | } | 65 | } |
27 | 66 | ||
28 | public function isCorrect() | 67 | public function isCorrect() { |
29 | { | 68 | return filter_var($this->url, FILTER_VALIDATE_URL) !== FALSE; |
30 | $pattern = '|^(.*:)//([a-z\-.]+)(:[0-9]+)?(.*)$|i'; | ||
31 | |||
32 | return preg_match($pattern, $this->url); | ||
33 | } | 69 | } |
34 | 70 | ||
35 | public function clean() | 71 | public function extract() { |
36 | { | 72 | global $http, $extractor; |
37 | $url = html_entity_decode(trim($this->url)); | 73 | $extractor = new ContentExtractor(dirname(__FILE__).'/../3rdparty/site_config/custom', dirname(__FILE__).'/../3rdparty/site_config/standard'); |
38 | 74 | $extractor->fingerprints = $this->fingerprints; | |
39 | $stuff = strpos($url,'&utm_source='); | ||
40 | if ($stuff !== FALSE) | ||
41 | $url = substr($url, 0, $stuff); | ||
42 | $stuff = strpos($url,'?utm_source='); | ||
43 | if ($stuff !== FALSE) | ||
44 | $url = substr($url, 0, $stuff); | ||
45 | $stuff = strpos($url,'#xtor=RSS-'); | ||
46 | if ($stuff !== FALSE) | ||
47 | $url = substr($url, 0, $stuff); | ||
48 | 75 | ||
49 | $this->url = $url; | 76 | $http = new HumbleHttpAgent(); |
50 | } | 77 | $http->userAgentMap = $this->user_agents; |
78 | $http->headerOnlyTypes = array_keys($this->content_type_exc); | ||
79 | $http->rewriteUrls = $this->rewrite_url; | ||
80 | $http->userAgentDefault = HumbleHttpAgent::UA_PHP; | ||
81 | // configure SimplePie HTTP extension class to use our HumbleHttpAgent instance | ||
82 | SimplePie_HumbleHttpAgent::set_agent($http); | ||
83 | $feed = new SimplePie(); | ||
84 | // some feeds use the text/html content type - force_feed tells SimplePie to process anyway | ||
85 | $feed->force_feed(true); | ||
86 | $feed->set_file_class('SimplePie_HumbleHttpAgent'); | ||
87 | $feed->feed_url = $this->url; | ||
88 | $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); | ||
89 | $feed->set_timeout(20); | ||
90 | $feed->enable_cache(false); | ||
91 | $feed->set_stupidly_fast(true); | ||
92 | $feed->enable_order_by_date(false); // we don't want to do anything to the feed | ||
93 | $feed->set_url_replacements(array()); | ||
94 | // initialise the feed | ||
95 | // the @ suppresses notices which on some servers causes a 500 internal server error | ||
96 | $result = @$feed->init(); | ||
97 | if ($result && (!is_array($feed->data) || count($feed->data) == 0)) { | ||
98 | die('Sorry, no feed items found'); | ||
99 | } | ||
100 | // from now on, we'll identify ourselves as a browser | ||
101 | $http->userAgentDefault = HumbleHttpAgent::UA_BROWSER; | ||
102 | unset($feed, $result); | ||
51 | 103 | ||
52 | public function fetchContent() | 104 | $feed = new DummySingleItemFeed($this->url); |
53 | { | 105 | |
54 | if ($this->isCorrect()) { | 106 | $items = $feed->get_items(0, 1); |
55 | $this->clean(); | 107 | // Request all feed items in parallel (if supported) |
56 | $html = Encoding::toUTF8(Tools::getFile($this->getUrl())); | 108 | $urls_sanitized = array(); |
57 | 109 | $urls = array(); | |
58 | # if Tools::getFile() if not able to retrieve HTTPS content, try the same URL with HTTP protocol | 110 | foreach ($items as $key => $item) { |
59 | if (!preg_match('!^https?://!i', $this->getUrl()) && (!isset($html) || strlen($html) <= 0)) { | 111 | $permalink = htmlspecialchars_decode($item->get_permalink()); |
60 | $this->setUrl('http://' . $this->getUrl()); | 112 | // Colons in URL path segments get encoded by SimplePie, yet some sites expect them unencoded |
61 | $html = Encoding::toUTF8(Tools::getFile($this->getUrl())); | 113 | $permalink = str_replace('%3A', ':', $permalink); |
114 | if ($permalink) { | ||
115 | $urls_sanitized[] = $permalink; | ||
62 | } | 116 | } |
117 | $urls[$key] = $permalink; | ||
118 | } | ||
119 | $http->fetchAll($urls_sanitized); | ||
120 | |||
121 | foreach ($items as $key => $item) { | ||
122 | $do_content_extraction = true; | ||
123 | $extract_result = false; | ||
124 | $permalink = $urls[$key]; | ||
125 | |||
126 | // TODO: Allow error codes - some sites return correct content with error status | ||
127 | // e.g. prospectmagazine.co.uk returns 403 | ||
128 | |||
129 | if ($permalink && ($response = $http->get($permalink, true)) && ($response['status_code'] < 300 || $response['status_code'] > 400)) { | ||
130 | $effective_url = $response['effective_url']; | ||
131 | // check if action defined for returned Content-Type | ||
132 | $type = null; | ||
133 | if (preg_match('!^Content-Type:\s*(([-\w]+)/([-\w\+]+))!im', $response['headers'], $match)) { | ||
134 | // look for full mime type (e.g. image/jpeg) or just type (e.g. image) | ||
135 | $match[1] = strtolower(trim($match[1])); | ||
136 | $match[2] = strtolower(trim($match[2])); | ||
137 | foreach (array($match[1], $match[2]) as $_mime) { | ||
138 | if (isset($this->content_type_exc[$_mime])) { | ||
139 | $type = $match[1]; | ||
140 | $_act = $this->content_type_exc[$_mime]['action']; | ||
141 | $_name = $this->content_type_exc[$_mime]['name']; | ||
142 | if ($_act == 'exclude') { | ||
143 | continue 2; // skip this feed item entry | ||
144 | } elseif ($_act == 'link') { | ||
145 | if ($match[2] == 'image') { | ||
146 | $html = "<a href=\"$effective_url\"><img src=\"$effective_url\" alt=\"$_name\" /></a>"; | ||
147 | } else { | ||
148 | $html = "<a href=\"$effective_url\">Download $_name</a>"; | ||
149 | } | ||
150 | $title = $_name; | ||
151 | $do_content_extraction = false; | ||
152 | break; | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | unset($_mime, $_act, $_name, $match); | ||
157 | } | ||
158 | if ($do_content_extraction) { | ||
159 | $html = $response['body']; | ||
160 | // remove strange things | ||
161 | $html = str_replace('</[>', '', $html); | ||
162 | $html = $this->convert_to_utf8($html, $response['headers']); | ||
63 | 163 | ||
64 | if (function_exists('tidy_parse_string')) { | 164 | // check site config for single page URL - fetch it if found |
65 | $tidy = tidy_parse_string($html, array(), 'UTF8'); | 165 | if ($single_page_response = $this->getSinglePage($item, $html, $effective_url)) { |
66 | $tidy->cleanRepair(); | 166 | $html = $single_page_response['body']; |
67 | $html = $tidy->value; | 167 | // remove strange things |
168 | $html = str_replace('</[>', '', $html); | ||
169 | $html = $this->convert_to_utf8($html, $single_page_response['headers']); | ||
170 | $effective_url = $single_page_response['effective_url']; | ||
171 | unset($single_page_response); | ||
172 | } | ||
173 | $extract_result = $extractor->process($html, $effective_url); | ||
174 | $readability = $extractor->readability; | ||
175 | $content_block = ($extract_result) ? $extractor->getContent() : null; | ||
176 | } | ||
177 | } | ||
178 | if ($do_content_extraction) { | ||
179 | // if we failed to extract content... | ||
180 | if (!$extract_result) { | ||
181 | $html = $this->error_message; | ||
182 | // keep the original item description | ||
183 | $html .= $item->get_description(); | ||
184 | } else { | ||
185 | $readability->clean($content_block, 'select'); | ||
186 | if ($this->rewrite_relative_urls) $this->makeAbsolute($effective_url, $content_block); | ||
187 | if ($content_block->childNodes->length == 1 && $content_block->firstChild->nodeType === XML_ELEMENT_NODE) { | ||
188 | $html = $content_block->firstChild->innerHTML; | ||
189 | } else { | ||
190 | $html = $content_block->innerHTML; | ||
191 | } | ||
192 | // post-processing cleanup | ||
193 | $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html); | ||
194 | } | ||
68 | } | 195 | } |
196 | } | ||
197 | |||
198 | $title = ($extractor->getTitle() != '' ? $extractor->getTitle() : _('Untitled')); | ||
199 | $content = array ('title' => $title, 'body' => $html); | ||
200 | |||
201 | return $content; | ||
202 | } | ||
69 | 203 | ||
70 | $parameters = array(); | 204 | private function convert_to_utf8($html, $header=null) |
71 | if (isset($html) and strlen($html) > 0) | 205 | { |
72 | { | 206 | $encoding = null; |
73 | $readability = new Readability($html, $this->getUrl()); | 207 | if ($html || $header) { |
74 | $readability->convertLinksToFootnotes = CONVERT_LINKS_FOOTNOTES; | 208 | if (is_array($header)) $header = implode("\n", $header); |
75 | $readability->revertForcedParagraphElements = REVERT_FORCED_PARAGRAPH_ELEMENTS; | 209 | if (!$header || !preg_match_all('/^Content-Type:\s+([^;]+)(?:;\s*charset=["\']?([^;"\'\n]*))?/im', $header, $match, PREG_SET_ORDER)) { |
210 | // error parsing the response | ||
211 | } else { | ||
212 | $match = end($match); // get last matched element (in case of redirects) | ||
213 | if (isset($match[2])) $encoding = trim($match[2], "\"' \r\n\0\x0B\t"); | ||
214 | } | ||
215 | // TODO: check to see if encoding is supported (can we convert it?) | ||
216 | // If it's not, result will be empty string. | ||
217 | // For now we'll check for invalid encoding types returned by some sites, e.g. 'none' | ||
218 | // Problem URL: http://facta.co.jp/blog/archives/20111026001026.html | ||
219 | if (!$encoding || $encoding == 'none') { | ||
220 | // search for encoding in HTML - only look at the first 35000 characters | ||
221 | $html_head = substr($html, 0, 40000); | ||
222 | if (preg_match('/^<\?xml\s+version=(?:"[^"]*"|\'[^\']*\')\s+encoding=("[^"]*"|\'[^\']*\')/s', $html_head, $match)) { | ||
223 | $encoding = trim($match[1], '"\''); | ||
224 | } elseif (preg_match('/<meta\s+http-equiv=["\']?Content-Type["\']? content=["\'][^;]+;\s*charset=["\']?([^;"\'>]+)/i', $html_head, $match)) { | ||
225 | $encoding = trim($match[1]); | ||
226 | } elseif (preg_match_all('/<meta\s+([^>]+)>/i', $html_head, $match)) { | ||
227 | foreach ($match[1] as $_test) { | ||
228 | if (preg_match('/charset=["\']?([^"\']+)/i', $_test, $_m)) { | ||
229 | $encoding = trim($_m[1]); | ||
230 | break; | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | } | ||
235 | if (isset($encoding)) $encoding = trim($encoding); | ||
236 | // trim is important here! | ||
237 | if (!$encoding || (strtolower($encoding) == 'iso-8859-1')) { | ||
238 | // replace MS Word smart qutoes | ||
239 | $trans = array(); | ||
240 | $trans[chr(130)] = '‚'; // Single Low-9 Quotation Mark | ||
241 | $trans[chr(131)] = 'ƒ'; // Latin Small Letter F With Hook | ||
242 | $trans[chr(132)] = '„'; // Double Low-9 Quotation Mark | ||
243 | $trans[chr(133)] = '…'; // Horizontal Ellipsis | ||
244 | $trans[chr(134)] = '†'; // Dagger | ||
245 | $trans[chr(135)] = '‡'; // Double Dagger | ||
246 | $trans[chr(136)] = 'ˆ'; // Modifier Letter Circumflex Accent | ||
247 | $trans[chr(137)] = '‰'; // Per Mille Sign | ||
248 | $trans[chr(138)] = 'Š'; // Latin Capital Letter S With Caron | ||
249 | $trans[chr(139)] = '‹'; // Single Left-Pointing Angle Quotation Mark | ||
250 | $trans[chr(140)] = 'Œ'; // Latin Capital Ligature OE | ||
251 | $trans[chr(145)] = '‘'; // Left Single Quotation Mark | ||
252 | $trans[chr(146)] = '’'; // Right Single Quotation Mark | ||
253 | $trans[chr(147)] = '“'; // Left Double Quotation Mark | ||
254 | $trans[chr(148)] = '”'; // Right Double Quotation Mark | ||
255 | $trans[chr(149)] = '•'; // Bullet | ||
256 | $trans[chr(150)] = '–'; // En Dash | ||
257 | $trans[chr(151)] = '—'; // Em Dash | ||
258 | $trans[chr(152)] = '˜'; // Small Tilde | ||
259 | $trans[chr(153)] = '™'; // Trade Mark Sign | ||
260 | $trans[chr(154)] = 'š'; // Latin Small Letter S With Caron | ||
261 | $trans[chr(155)] = '›'; // Single Right-Pointing Angle Quotation Mark | ||
262 | $trans[chr(156)] = 'œ'; // Latin Small Ligature OE | ||
263 | $trans[chr(159)] = 'Ÿ'; // Latin Capital Letter Y With Diaeresis | ||
264 | $html = strtr($html, $trans); | ||
265 | } | ||
266 | if (!$encoding) { | ||
267 | $encoding = 'utf-8'; | ||
268 | } else { | ||
269 | if (strtolower($encoding) != 'utf-8') { | ||
270 | $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8'); | ||
271 | /* | ||
272 | if (function_exists('iconv')) { | ||
273 | // iconv appears to handle certain character encodings better than mb_convert_encoding | ||
274 | $html = iconv($encoding, 'utf-8', $html); | ||
275 | } else { | ||
276 | $html = mb_convert_encoding($html, 'utf-8', $encoding); | ||
277 | } | ||
278 | */ | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | return $html; | ||
283 | } | ||
76 | 284 | ||
77 | if($readability->init()) | 285 | private function makeAbsolute($base, $elem) { |
78 | { | 286 | $base = new SimplePie_IRI($base); |
79 | $content = $readability->articleContent->innerHTML; | 287 | // remove '//' in URL path (used to prevent URLs from resolving properly) |
80 | $parameters['title'] = $readability->articleTitle->innerHTML; | 288 | // TODO: check if this is still the case |
81 | $parameters['content'] = $content; | 289 | if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path); |
290 | foreach(array('a'=>'href', 'img'=>'src') as $tag => $attr) { | ||
291 | $elems = $elem->getElementsByTagName($tag); | ||
292 | for ($i = $elems->length-1; $i >= 0; $i--) { | ||
293 | $e = $elems->item($i); | ||
294 | //$e->parentNode->replaceChild($articleContent->ownerDocument->createTextNode($e->textContent), $e); | ||
295 | $this->makeAbsoluteAttr($base, $e, $attr); | ||
296 | } | ||
297 | if (strtolower($elem->tagName) == $tag) $this->makeAbsoluteAttr($base, $elem, $attr); | ||
298 | } | ||
299 | } | ||
82 | 300 | ||
83 | return $parameters; | 301 | private function makeAbsoluteAttr($base, $e, $attr) { |
302 | if ($e->hasAttribute($attr)) { | ||
303 | // Trim leading and trailing white space. I don't really like this but | ||
304 | // unfortunately it does appear on some sites. e.g. <img src=" /path/to/image.jpg" /> | ||
305 | $url = trim(str_replace('%20', ' ', $e->getAttribute($attr))); | ||
306 | $url = str_replace(' ', '%20', $url); | ||
307 | if (!preg_match('!https?://!i', $url)) { | ||
308 | if ($absolute = SimplePie_IRI::absolutize($base, $url)) { | ||
309 | $e->setAttribute($attr, $absolute); | ||
84 | } | 310 | } |
85 | } | 311 | } |
86 | } | 312 | } |
87 | else { | 313 | } |
88 | #$msg->add('e', _('error during url preparation : the link is not valid')); | 314 | |
89 | Tools::logm($this->getUrl() . ' is not a valid url'); | 315 | private function makeAbsoluteStr($base, $url) { |
316 | $base = new SimplePie_IRI($base); | ||
317 | // remove '//' in URL path (causes URLs not to resolve properly) | ||
318 | if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path); | ||
319 | if (preg_match('!^https?://!i', $url)) { | ||
320 | // already absolute | ||
321 | return $url; | ||
322 | } else { | ||
323 | if ($absolute = SimplePie_IRI::absolutize($base, $url)) { | ||
324 | return $absolute; | ||
325 | } | ||
326 | return false; | ||
90 | } | 327 | } |
328 | } | ||
91 | 329 | ||
92 | return FALSE; | 330 | // returns single page response, or false if not found |
331 | private function getSinglePage($item, $html, $url) { | ||
332 | global $http, $extractor; | ||
333 | $host = @parse_url($url, PHP_URL_HOST); | ||
334 | $site_config = SiteConfig::build($host); | ||
335 | if ($site_config === false) { | ||
336 | // check for fingerprints | ||
337 | if (!empty($extractor->fingerprints) && ($_fphost = $extractor->findHostUsingFingerprints($html))) { | ||
338 | $site_config = SiteConfig::build($_fphost); | ||
339 | } | ||
340 | if ($site_config === false) $site_config = new SiteConfig(); | ||
341 | SiteConfig::add_to_cache($host, $site_config); | ||
342 | return false; | ||
343 | } else { | ||
344 | SiteConfig::add_to_cache($host, $site_config); | ||
345 | } | ||
346 | $splink = null; | ||
347 | if (!empty($site_config->single_page_link)) { | ||
348 | $splink = $site_config->single_page_link; | ||
349 | } elseif (!empty($site_config->single_page_link_in_feed)) { | ||
350 | // single page link xpath is targeted at feed | ||
351 | $splink = $site_config->single_page_link_in_feed; | ||
352 | // so let's replace HTML with feed item description | ||
353 | $html = $item->get_description(); | ||
354 | } | ||
355 | if (isset($splink)) { | ||
356 | // Build DOM tree from HTML | ||
357 | $readability = new Readability($html, $url); | ||
358 | $xpath = new DOMXPath($readability->dom); | ||
359 | // Loop through single_page_link xpath expressions | ||
360 | $single_page_url = null; | ||
361 | foreach ($splink as $pattern) { | ||
362 | $elems = @$xpath->evaluate($pattern, $readability->dom); | ||
363 | if (is_string($elems)) { | ||
364 | $single_page_url = trim($elems); | ||
365 | break; | ||
366 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
367 | foreach ($elems as $item) { | ||
368 | if ($item instanceof DOMElement && $item->hasAttribute('href')) { | ||
369 | $single_page_url = $item->getAttribute('href'); | ||
370 | break; | ||
371 | } elseif ($item instanceof DOMAttr && $item->value) { | ||
372 | $single_page_url = $item->value; | ||
373 | break; | ||
374 | } | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | // If we've got URL, resolve against $url | ||
379 | if (isset($single_page_url) && ($single_page_url = $this->makeAbsoluteStr($url, $single_page_url))) { | ||
380 | // check it's not what we have already! | ||
381 | if ($single_page_url != $url) { | ||
382 | // it's not, so let's try to fetch it... | ||
383 | $_prev_ref = $http->referer; | ||
384 | $http->referer = $single_page_url; | ||
385 | if (($response = $http->get($single_page_url, true)) && $response['status_code'] < 300) { | ||
386 | $http->referer = $_prev_ref; | ||
387 | return $response; | ||
388 | } | ||
389 | $http->referer = $_prev_ref; | ||
390 | } | ||
391 | } | ||
392 | } | ||
393 | return false; | ||
93 | } | 394 | } |
94 | } \ No newline at end of file | 395 | } \ No newline at end of file |
diff --git a/inc/poche/config.inc.php b/inc/poche/config.inc.php index 4122ff10..a1917295 100755 --- a/inc/poche/config.inc.php +++ b/inc/poche/config.inc.php | |||
@@ -15,9 +15,9 @@ if (!file_exists(__DIR__ . '/../../vendor/autoload.php')) { | |||
15 | die('Twig does not seem installed. Have a look at <a href="http://inthepoche.com/?pages/Documentation">the documentation.</a>'); | 15 | die('Twig does not seem installed. Have a look at <a href="http://inthepoche.com/?pages/Documentation">the documentation.</a>'); |
16 | } | 16 | } |
17 | 17 | ||
18 | if (file_exists(__DIR__ . '/../../inc/poche/myconfig.inc.php')) { | 18 | // if (file_exists(__DIR__ . '/../../inc/poche/myconfig.inc.php')) { |
19 | require_once __DIR__ . '/../../inc/poche/myconfig.inc.php'; | 19 | // require_once __DIR__ . '/../../inc/poche/myconfig.inc.php'; |
20 | } | 20 | // } |
21 | require_once __DIR__ . '/../../inc/poche/User.class.php'; | 21 | require_once __DIR__ . '/../../inc/poche/User.class.php'; |
22 | require_once __DIR__ . '/../../inc/poche/Url.class.php'; | 22 | require_once __DIR__ . '/../../inc/poche/Url.class.php'; |
23 | require_once __DIR__ . '/../../inc/3rdparty/class.messages.php'; | 23 | require_once __DIR__ . '/../../inc/3rdparty/class.messages.php'; |
@@ -30,10 +30,25 @@ require_once __DIR__ . '/../../inc/3rdparty/simple_html_dom.php'; | |||
30 | require_once __DIR__ . '/../../inc/3rdparty/paginator.php'; | 30 | require_once __DIR__ . '/../../inc/3rdparty/paginator.php'; |
31 | require_once __DIR__ . '/../../inc/3rdparty/Session.class.php'; | 31 | require_once __DIR__ . '/../../inc/3rdparty/Session.class.php'; |
32 | 32 | ||
33 | require_once __DIR__ . '/../../inc/3rdparty/simplepie/SimplePieAutoloader.php'; | ||
34 | require_once __DIR__ . '/../../inc/3rdparty/simplepie/SimplePie/Core.php'; | ||
35 | require_once __DIR__ . '/../../inc/3rdparty/content-extractor/ContentExtractor.php'; | ||
36 | require_once __DIR__ . '/../../inc/3rdparty/content-extractor/SiteConfig.php'; | ||
37 | require_once __DIR__ . '/../../inc/3rdparty/humble-http-agent/HumbleHttpAgent.php'; | ||
38 | require_once __DIR__ . '/../../inc/3rdparty/humble-http-agent/SimplePie_HumbleHttpAgent.php'; | ||
39 | require_once __DIR__ . '/../../inc/3rdparty/humble-http-agent/CookieJar.php'; | ||
40 | require_once __DIR__ . '/../../inc/3rdparty/feedwriter/FeedItem.php'; | ||
41 | require_once __DIR__ . '/../../inc/3rdparty/feedwriter/FeedWriter.php'; | ||
42 | require_once __DIR__ . '/../../inc/3rdparty/feedwriter/DummySingleItemFeed.php'; | ||
43 | |||
33 | if (DOWNLOAD_PICTURES) { | 44 | if (DOWNLOAD_PICTURES) { |
34 | require_once __DIR__ . '/../../inc/poche/pochePictures.php'; | 45 | require_once __DIR__ . '/../../inc/poche/pochePictures.php'; |
35 | } | 46 | } |
36 | 47 | ||
48 | if (!ini_get('date.timezone') || !@date_default_timezone_set(ini_get('date.timezone'))) { | ||
49 | date_default_timezone_set('UTC'); | ||
50 | } | ||
51 | |||
37 | $poche = new Poche(); | 52 | $poche = new Poche(); |
38 | #XSRF protection with token | 53 | #XSRF protection with token |
39 | // if (!empty($_POST)) { | 54 | // if (!empty($_POST)) { |
diff --git a/inc/poche/define.inc.php b/inc/poche/define.inc.php index c32ca098..3f667430 100644 --- a/inc/poche/define.inc.php +++ b/inc/poche/define.inc.php | |||
@@ -17,14 +17,18 @@ define ('STORAGE_PASSWORD', 'postgres'); # leave blank for sqlite | |||
17 | 17 | ||
18 | define ('MODE_DEMO', FALSE); | 18 | define ('MODE_DEMO', FALSE); |
19 | define ('DEBUG_POCHE', FALSE); | 19 | define ('DEBUG_POCHE', FALSE); |
20 | define ('CONVERT_LINKS_FOOTNOTES', FALSE); | ||
21 | define ('REVERT_FORCED_PARAGRAPH_ELEMENTS', FALSE); | ||
22 | define ('DOWNLOAD_PICTURES', FALSE); | 20 | define ('DOWNLOAD_PICTURES', FALSE); |
23 | define ('SHARE_TWITTER', TRUE); | 21 | define ('SHARE_TWITTER', TRUE); |
24 | define ('SHARE_MAIL', TRUE); | 22 | define ('SHARE_MAIL', TRUE); |
23 | define ('SHARE_SHAARLI', FALSE); | ||
24 | define ('SHAARLI_URL', 'http://myshaarliurl.com'); | ||
25 | define ('ABS_PATH', 'assets/'); | 25 | define ('ABS_PATH', 'assets/'); |
26 | define ('TPL', __DIR__ . '/../../tpl'); | 26 | define ('TPL', __DIR__ . '/../../tpl'); |
27 | define ('LOCALE', __DIR__ . '/../../locale'); | 27 | define ('LOCALE', __DIR__ . '/../../locale'); |
28 | define ('CACHE', __DIR__ . '/../../cache'); | 28 | define ('CACHE', __DIR__ . '/../../cache'); |
29 | define ('PAGINATION', '10'); | 29 | define ('PAGINATION', '10'); |
30 | define ('THEME', 'light'); \ No newline at end of file | 30 | define ('THEME', 'light'); |
31 | |||
32 | define ('IMPORT_POCKET_FILE', './ril_export.html'); | ||
33 | define ('IMPORT_READABILITY_FILE', './readability'); | ||
34 | define ('IMPORT_INSTAPAPER_FILE', './instapaper-export.html'); \ No newline at end of file | ||