]>
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 = Extractor::getPageContent(new Url(base64_encode($url))); | |
13 | $title = $pageContent['rss']['channel']['item']['title'] ?: 'Untitled'; | |
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 | * @return mixed | |
28 | */ | |
29 | public static function getPageContent(Url $url) | |
30 | { | |
31 | // Saving and clearing context | |
32 | $REAL = array(); | |
33 | foreach ($GLOBALS as $key => $value) { | |
34 | if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') { | |
35 | $GLOBALS[$key] = array(); | |
36 | $REAL[$key] = $value; | |
37 | } | |
38 | } | |
39 | // Saving and clearing session | |
40 | if (isset($_SESSION)) { | |
41 | $REAL_SESSION = array(); | |
42 | foreach ($_SESSION as $key => $value) { | |
43 | $REAL_SESSION[$key] = $value; | |
44 | unset($_SESSION[$key]); | |
45 | } | |
46 | } | |
47 | ||
48 | // Running code in different context | |
49 | $scope = function () { | |
50 | extract(func_get_arg(1)); | |
51 | $_GET = $_REQUEST = array( | |
52 | "url" => $url->getUrl(), | |
53 | "max" => 5, | |
54 | "links" => "preserve", | |
55 | "exc" => "", | |
56 | "format" => "json", | |
57 | "submit" => "Create Feed", | |
58 | ); | |
59 | ob_start(); | |
60 | require func_get_arg(0); | |
61 | $json = ob_get_contents(); | |
62 | ob_end_clean(); | |
63 | ||
64 | return $json; | |
65 | }; | |
66 | ||
67 | // Silence $scope function to avoid | |
68 | // issues with FTRSS when error_reporting is to high | |
69 | // FTRSS generates PHP warnings which break output | |
70 | $json = @$scope(__DIR__."/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url)); | |
71 | ||
72 | // Clearing and restoring context | |
73 | foreach ($GLOBALS as $key => $value) { | |
74 | if ($key != "GLOBALS" && $key != "_SESSION") { | |
75 | unset($GLOBALS[$key]); | |
76 | } | |
77 | } | |
78 | foreach ($REAL as $key => $value) { | |
79 | $GLOBALS[$key] = $value; | |
80 | } | |
81 | ||
82 | // Clearing and restoring session | |
83 | if (isset($REAL_SESSION)) { | |
84 | foreach ($_SESSION as $key => $value) { | |
85 | unset($_SESSION[$key]); | |
86 | } | |
87 | ||
88 | foreach ($REAL_SESSION as $key => $value) { | |
89 | $_SESSION[$key] = $value; | |
90 | } | |
91 | } | |
92 | ||
93 | return json_decode($json, true); | |
94 | } | |
95 | } |