]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Service/Extractor.php
Merge pull request #1395 from wallabag/v2-fix-1378
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Service / Extractor.php
CommitLineData
6b767d1c
NL
1<?php
2
3namespace Wallabag\CoreBundle\Service;
4
5use Wallabag\CoreBundle\Helper\Content;
b9ec99e2 6use Wallabag\CoreBundle\Helper\Url;
6b767d1c
NL
7
8final class Extractor
9{
7df80cb3
J
10 public static function extract($url)
11 {
4346a860 12 $pageContent = self::getPageContent(new Url(base64_encode($url)));
0bc2baa6 13 $title = $pageContent['rss']['channel']['item']['title'] ?: parse_url($url, PHP_URL_HOST);
6b767d1c
NL
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 }
94f2364c 22
6b767d1c 23 /**
4346a860
JB
24 * Get the content for a given URL (by a call to FullTextFeed).
25 *
26 * @param Url $url
6b767d1c 27 *
6b767d1c
NL
28 * @return mixed
29 */
30 public static function getPageContent(Url $url)
31 {
32 // Saving and clearing context
33 $REAL = array();
7df80cb3
J
34 foreach ($GLOBALS as $key => $value) {
35 if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') {
8ce32af6
JB
36 $GLOBALS[$key] = array();
37 $REAL[$key] = $value;
6b767d1c
NL
38 }
39 }
40 // Saving and clearing session
41 if (isset($_SESSION)) {
42 $REAL_SESSION = array();
7df80cb3 43 foreach ($_SESSION as $key => $value) {
6b767d1c
NL
44 $REAL_SESSION[$key] = $value;
45 unset($_SESSION[$key]);
46 }
47 }
48
49 // Running code in different context
7df80cb3
J
50 $scope = function () {
51 extract(func_get_arg(1));
6b767d1c 52 $_GET = $_REQUEST = array(
4346a860
JB
53 'url' => $url->getUrl(),
54 'max' => 5,
55 'links' => 'preserve',
56 'exc' => '',
57 'format' => 'json',
58 'submit' => 'Create Feed',
6b767d1c
NL
59 );
60 ob_start();
61 require func_get_arg(0);
62 $json = ob_get_contents();
63 ob_end_clean();
7df80cb3 64
6b767d1c
NL
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
4346a860 71 $json = @$scope(__DIR__.'/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php', array('url' => $url));
6b767d1c
NL
72
73 // Clearing and restoring context
74 foreach ($GLOBALS as $key => $value) {
4346a860 75 if ($key != 'GLOBALS' && $key != '_SESSION') {
6b767d1c
NL
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)) {
7df80cb3 85 foreach ($_SESSION as $key => $value) {
6b767d1c
NL
86 unset($_SESSION[$key]);
87 }
88
7df80cb3 89 foreach ($REAL_SESSION as $key => $value) {
6b767d1c
NL
90 $_SESSION[$key] = $value;
91 }
92 }
93
94 return json_decode($json, true);
95 }
7df80cb3 96}