]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Service/Extractor.php
CS
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Service / Extractor.php
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'] != '') ? $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 * Get the content for a given URL (by a call to FullTextFeed)
24 *
25 * @param Url $url
26 * @return mixed
27 */
28 public static function getPageContent(Url $url)
29 {
30 // Saving and clearing context
31 $REAL = array();
32 foreach ($GLOBALS as $key => $value) {
33 if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') {
34 $GLOBALS[$key] = array();
35 $REAL[$key] = $value;
36 }
37 }
38 // Saving and clearing session
39 if (isset($_SESSION)) {
40 $REAL_SESSION = array();
41 foreach ($_SESSION as $key => $value) {
42 $REAL_SESSION[$key] = $value;
43 unset($_SESSION[$key]);
44 }
45 }
46
47 // Running code in different context
48 $scope = function () {
49 extract(func_get_arg(1));
50 $_GET = $_REQUEST = array(
51 "url" => $url->getUrl(),
52 "max" => 5,
53 "links" => "preserve",
54 "exc" => "",
55 "format" => "json",
56 "submit" => "Create Feed",
57 );
58 ob_start();
59 require func_get_arg(0);
60 $json = ob_get_contents();
61 ob_end_clean();
62
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
69 $json = @$scope(__DIR__."/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url));
70
71 // Clearing and restoring context
72 foreach ($GLOBALS as $key => $value) {
73 if ($key != "GLOBALS" && $key != "_SESSION") {
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)) {
83 foreach ($_SESSION as $key => $value) {
84 unset($_SESSION[$key]);
85 }
86
87 foreach ($REAL_SESSION as $key => $value) {
88 $_SESSION[$key] = $value;
89 }
90 }
91
92 return json_decode($json, true);
93 }
94 }