1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-22 17:18:37 +00:00
wallabag/src/Wallabag/CoreBundle/Service/Extractor.php

97 lines
2.8 KiB
PHP
Raw Normal View History

2015-01-26 22:15:19 +01:00
<?php
namespace Wallabag\CoreBundle\Service;
use Wallabag\CoreBundle\Helper\Content;
2015-01-27 13:08:02 +01:00
use Wallabag\CoreBundle\Helper\Url;
2015-01-26 22:15:19 +01:00
final class Extractor
{
2015-01-31 19:09:34 +01:00
public static function extract($url)
{
2015-05-30 13:52:26 +02:00
$pageContent = self::getPageContent(new Url(base64_encode($url)));
$title = $pageContent['rss']['channel']['item']['title'] ?: parse_url($url, PHP_URL_HOST);
2015-01-26 22:15:19 +01:00
$body = $pageContent['rss']['channel']['item']['description'];
$content = new Content();
$content->setTitle($title);
2015-01-31 19:09:34 +01:00
$content->setBody($body);
2015-01-26 22:15:19 +01:00
return $content;
}
2015-02-07 18:29:03 +01:00
2015-01-26 22:15:19 +01:00
/**
2015-05-30 13:52:26 +02:00
* Get the content for a given URL (by a call to FullTextFeed).
*
* @param Url $url
2015-01-26 22:15:19 +01:00
*
* @return mixed
*/
public static function getPageContent(Url $url)
{
// Saving and clearing context
$REAL = array();
2015-01-31 19:09:34 +01:00
foreach ($GLOBALS as $key => $value) {
if ($key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS') {
$GLOBALS[$key] = array();
$REAL[$key] = $value;
2015-01-26 22:15:19 +01:00
}
}
// Saving and clearing session
if (isset($_SESSION)) {
$REAL_SESSION = array();
2015-01-31 19:09:34 +01:00
foreach ($_SESSION as $key => $value) {
2015-01-26 22:15:19 +01:00
$REAL_SESSION[$key] = $value;
unset($_SESSION[$key]);
}
}
// Running code in different context
2015-01-31 19:09:34 +01:00
$scope = function () {
extract(func_get_arg(1));
2015-01-26 22:15:19 +01:00
$_GET = $_REQUEST = array(
2015-05-30 13:52:26 +02:00
'url' => $url->getUrl(),
'max' => 5,
'links' => 'preserve',
'exc' => '',
'format' => 'json',
'submit' => 'Create Feed',
2015-01-26 22:15:19 +01:00
);
ob_start();
require func_get_arg(0);
$json = ob_get_contents();
ob_end_clean();
2015-01-31 19:09:34 +01:00
2015-01-26 22:15:19 +01:00
return $json;
};
// Silence $scope function to avoid
// issues with FTRSS when error_reporting is to high
// FTRSS generates PHP warnings which break output
2015-05-30 13:52:26 +02:00
$json = @$scope(__DIR__.'/../../../../vendor/wallabag/Fivefilters_Libraries/makefulltextfeed.php', array('url' => $url));
2015-01-26 22:15:19 +01:00
// Clearing and restoring context
foreach ($GLOBALS as $key => $value) {
2015-05-30 13:52:26 +02:00
if ($key != 'GLOBALS' && $key != '_SESSION') {
2015-01-26 22:15:19 +01:00
unset($GLOBALS[$key]);
}
}
foreach ($REAL as $key => $value) {
$GLOBALS[$key] = $value;
}
// Clearing and restoring session
if (isset($REAL_SESSION)) {
2015-01-31 19:09:34 +01:00
foreach ($_SESSION as $key => $value) {
2015-01-26 22:15:19 +01:00
unset($_SESSION[$key]);
}
2015-01-31 19:09:34 +01:00
foreach ($REAL_SESSION as $key => $value) {
2015-01-26 22:15:19 +01:00
$_SESSION[$key] = $value;
}
}
return json_decode($json, true);
}
2015-01-31 19:09:34 +01:00
}