]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Service; | |
4 | ||
5 | use Wallabag\CoreBundle\Helper\Content; | |
6 | use Wallabag\CoreBundle\Helper\Url; | |
7 | ||
8 | final class Extractor | |
9 | { | |
10 | public static function extract($url) | |
11 | { | |
12 | $pageContent = self::getPageContent(new Url(base64_encode($url))); | |
13 | $title = $pageContent['rss']['channel']['item']['title'] ?: parse_url($url, PHP_URL_HOST); | |
14 | $body = $pageContent['rss']['channel']['item']['description']; | |
15 | ||
16 | $content = new Content(); | |
17 | $content->setTitle($title); | |
18 | $content->setBody($body); | |
19 | ||
20 | return $content; | |
21 | } | |
22 | ||
23 | /** | |
24 | * Get the content for a given URL (by a call to FullTextFeed). | |
25 | * | |
26 | * @param Url $url | |
27 | * | |
28 | * @return mixed | |
29 | */ | |
30 | public static function getPageContent(Url $url) | |
31 | { | |
32 | // Saving and clearing context | |
33 | $REAL = array(); | |
34 | foreach ($GLOBALS as $key => $value) { | |
35 | if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') { | |
36 | $GLOBALS[$key] = array(); | |
37 | $REAL[$key] = $value; | |
38 | } | |
39 | } | |
40 | // Saving and clearing session | |
41 | if (isset($_SESSION)) { | |
42 | $REAL_SESSION = array(); | |
43 | foreach ($_SESSION as $key => $value) { | |
44 | $REAL_SESSION[$key] = $value; | |
45 | unset($_SESSION[$key]); | |
46 | } | |
47 | } | |
48 | ||
49 | // Running code in different context | |
50 | $scope = function () { | |
51 | extract(func_get_arg(1)); | |
52 | $_GET = $_REQUEST = array( | |
53 | 'url' => $url->getUrl(), | |
54 | 'max' => 5, | |
55 | 'links' => 'preserve', | |
56 | 'exc' => '', | |
57 | 'format' => 'json', | |
58 | 'submit' => 'Create Feed', | |
59 | ); | |
60 | ob_start(); | |
61 | require func_get_arg(0); | |
62 | $json = ob_get_contents(); | |
63 | ob_end_clean(); | |
64 | ||
65 | return $json; | |
66 | }; | |
67 | ||
68 | // Silence $scope function to avoid | |
69 | // issues with FTRSS when error_reporting is to high | |
70 | // FTRSS generates PHP warnings which break output | |
71 | $json = @$scope(__DIR__.'/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php', array('url' => $url)); | |
72 | ||
73 | // Clearing and restoring context | |
74 | foreach ($GLOBALS as $key => $value) { | |
75 | if ($key != 'GLOBALS' && $key != '_SESSION') { | |
76 | unset($GLOBALS[$key]); | |
77 | } | |
78 | } | |
79 | foreach ($REAL as $key => $value) { | |
80 | $GLOBALS[$key] = $value; | |
81 | } | |
82 | ||
83 | // Clearing and restoring session | |
84 | if (isset($REAL_SESSION)) { | |
85 | foreach ($_SESSION as $key => $value) { | |
86 | unset($_SESSION[$key]); | |
87 | } | |
88 | ||
89 | foreach ($REAL_SESSION as $key => $value) { | |
90 | $_SESSION[$key] = $value; | |
91 | } | |
92 | } | |
93 | ||
94 | return json_decode($json, true); | |
95 | } | |
96 | } |