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