aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Service/Extractor.php
blob: 1c6ff0ae851632fd80ef36a5a7c7ac21674806d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Wallabag\CoreBundle\Service;

use Wallabag\CoreBundle\Helper\Content;
use Wallabag\CoreBundle\Helper\Url;

final class Extractor
{
    public static function extract($url) {
        $pageContent = Extractor::getPageContent(new Url(base64_encode($url)));
        $title = ($pageContent['rss']['channel']['item']['title'] != '') ? $pageContent['rss']['channel']['item']['title'] : _('Untitled');
        $body = $pageContent['rss']['channel']['item']['description'];

        $content = new Content();
        $content->setTitle($title);
        $content->setBody($body);;

        return $content;
    }
    /**
     * Get the content for a given URL (by a call to FullTextFeed)
     *
     * @param Url $url
     * @return mixed
     */
    public static function getPageContent(Url $url)
    {
        // Saving and clearing context
        $REAL = array();
        foreach( $GLOBALS as $key => $value ) {
            if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
                $GLOBALS[$key]  = array();
                $REAL[$key]     = $value;
            }
        }
        // Saving and clearing session
        if (isset($_SESSION)) {
            $REAL_SESSION = array();
            foreach( $_SESSION as $key => $value ) {
                $REAL_SESSION[$key] = $value;
                unset($_SESSION[$key]);
            }
        }

        // Running code in different context
        $scope = function() {
            extract( func_get_arg(1) );
            $_GET = $_REQUEST = array(
                "url" => $url->getUrl(),
                "max" => 5,
                "links" => "preserve",
                "exc" => "",
                "format" => "json",
                "submit" => "Create Feed"
            );
            ob_start();
            require func_get_arg(0);
            $json = ob_get_contents();
            ob_end_clean();
            return $json;
        };

        // Silence $scope function to avoid
        // issues with FTRSS when error_reporting is to high
        // FTRSS generates PHP warnings which break output
        $json = @$scope(__DIR__ . "/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php", array("url" => $url));

        // Clearing and restoring context
        foreach ($GLOBALS as $key => $value) {
            if($key != "GLOBALS" && $key != "_SESSION" ) {
                unset($GLOBALS[$key]);
            }
        }
        foreach ($REAL as $key => $value) {
            $GLOBALS[$key] = $value;
        }

        // Clearing and restoring session
        if (isset($REAL_SESSION)) {
            foreach($_SESSION as $key => $value) {
                unset($_SESSION[$key]);
            }

            foreach($REAL_SESSION as $key => $value) {
                $_SESSION[$key] = $value;
            }
        }

        return json_decode($json, true);
    }
}