Skip to content
Snippets Groups Projects
Commit 47e277d3 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

wip

parent 80961d80
No related branches found
No related tags found
No related merge requests found
Pipeline #11281 failed
......@@ -43,6 +43,17 @@ class StartController extends AuthenticatedController
WidgetHelper::setInitialPositions();
}
dd(
url_get_headers('https://cataas.com/cat?json=true', true),
get_headers('https://cataas.com/cat?json=true', true)
);
$client = HttpFactory::get()->createHttpClient();
$response = $client->sendRequest(
$client->createRequest('GET', 'https://cataas.com/cat?json=true')
);
dd($response, $response->getBody()->getContents());
$this->left = WidgetHelper::getUserWidgets($GLOBALS['user']->id, 0);
$this->right = WidgetHelper::getUserWidgets($GLOBALS['user']->id, 1);
......
......@@ -18,7 +18,6 @@
},
"require": {
"php": "^7.2",
"guzzlehttp/psr7": "^2.3",
"neomerx/json-api": "4.0.1",
"spomky-labs/otphp": "^10",
"tuupola/cors-middleware": "^1.2.1",
......@@ -59,7 +58,10 @@
"symfony/polyfill-php73": "^1.27",
"symfony/polyfill-php74": "^1.27",
"symfony/polyfill-php80": "^1.27",
"symfony/polyfill-php81": "^1.27"
"symfony/polyfill-php81": "^1.27",
"symfony/http-client": "^5.4",
"nyholm/psr7": "^1.5",
"psr/http-client": "^1.0"
},
"replace": {
"symfony/polyfill-php54": "*",
......
This diff is collapsed.
<?php
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Slim\Psr7\Factory\RequestFactory;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Factory\UploadedFileFactory;
use Slim\Psr7\Factory\UriFactory;
use Symfony\Component\HttpClient\HttpClient as SymfonyHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
final class HttpFactory implements
RequestFactoryInterface,
ResponseFactoryInterface,
ServerRequestFactoryInterface,
StreamFactoryInterface,
UploadedFileFactoryInterface,
UriFactoryInterface
{
private static $instance = null;
public static function get(): HttpFactory
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function createRequest(string $method, $uri): RequestInterface
{
return $this->getRequestFactory()->createRequest($method, $uri);
}
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
$factory = $this->getResponseFactory();
return $factory->createResponse($code, $reasonPhrase);
}
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return $this->getServerRequestFactory()->createServerRequest($method, $uri, $serverParams);
}
public function createStream(string $content = ''): StreamInterface
{
return $this->getStreamFactory()->createStream($content);
}
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return $this->getStreamFactory()->createStreamFromFile($filename, $mode);
}
public function createStreamFromResource($resource): StreamInterface
{
return $this->getStreamFactory()->createStreamFromResource($resource);
}
public function createUploadedFile(
StreamInterface $stream,
int $size = null,
int $error = \UPLOAD_ERR_OK,
string $clientFilename = null,
string $clientMediaType = null
): UploadedFileInterface {
$factory = $this->getUploadedFileFactory();
return $factory->createUploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
}
public function createUri(string $uri = ''): UriInterface
{
return $this->getUriFactory()->createUri($uri);
}
public function createHttpClient(): ClientInterface
{
$options = [];
// Respect proxy
$proxy = Config::get()->getValue('HTTP_PROXY');
if ($proxy) {
$options['proxy'] = "http://{$proxy}";
$ignore = Config::get()->getValue('HTTP_PROXY_IGNORE');
$ignore = trim($ignore);
if ($ignore) {
$options['no_proxy'] = $ignore;
}
}
$client = SymfonyHttpClient::create($options);
return new Psr18Client(
$client,
$this->getResponseFactory(),
$this->getStreamFactory()
);
}
private function getRequestFactory(): RequestFactory
{
return new RequestFactory(
$this->getStreamFactory(),
$this->getUriFactory()
);
}
private function getResponseFactory(): ResponseFactory
{
return new ResponseFactory();
}
private function getServerRequestFactory(): ServerRequestFactory
{
return new ServerRequestFactory(
$this->getStreamFactory(),
$this->getUriFactory()
);
}
private function getStreamFactory(): StreamFactory
{
return new StreamFactory();
}
private function getUploadedFileFactory(): UploadedFileFactory
{
return new Slim\Psr7\Factory\UploadedFileFactory();
}
private function getUriFactory(): UriFactory
{
return new UriFactory();
}
}
......@@ -6,7 +6,6 @@ use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\InternalServerError;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\NonJsonApiController;
use GuzzleHttp\Psr7;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
......@@ -114,7 +113,8 @@ class FileRefsContentShow extends NonJsonApiController
$fileRef->incrementDownloadCounter();
$stream = Psr7\Utils::streamFor(fopen($pathFile, 'rb'));
$stream = \HttpFactory::get()->createStreamFromFile($pathFile, 'rb');
return $response->withBody($stream);
}
......
......@@ -353,7 +353,7 @@ class SiteinfoMarkupEngine {
function coregroup() {
$cache = StudipCacheFactory::getCache();
if (!($remotefile = $cache->read('coregroup'))) {
$remotefile = file_get_contents('https://develop.studip.de/studip/extern.php?module=Persons&config_id=8d1dafc3afca2bce6125d57d4119b631&range_id=4498a5bc62d7974d0a0ac3e97aca5296', false, get_default_http_stream_context('https://develop.studip.de'));
$remotefile = url_get_contents('https://develop.studip.de/studip/extern.php?module=Persons&config_id=8d1dafc3afca2bce6125d57d4119b631&range_id=4498a5bc62d7974d0a0ac3e97aca5296');
$cache->write('coregroup', $remotefile);
}
return str_replace(['class="normal"', 'align="left"'], ['', ''], $remotefile);
......
......@@ -112,10 +112,10 @@ class ExportPDF extends TCPDF implements ExportDocument {
// Fetch headers from url, handle possible redirects
do {
$headers = get_headers($url, true, get_default_http_stream_context($url));
$headers = url_get_headers($url, true);
list(, $status) = explode(' ', $headers[0]);
$url = $headers['Location'] ?: $headers['location'] ?: $url;
$url = $headers['Location'] ?? $headers['location'] ?? $url;
} while (in_array($status, [300, 301, 302, 303, 305, 307]));
$status = $status ?: 404;
......@@ -336,7 +336,7 @@ class ExportPDF extends TCPDF implements ExportDocument {
}
$src = 'src=""';
$file_content = @file_get_contents($convurl, false, get_default_http_stream_context($convurl));
$file_content = url_get_contents($convurl);
if ($file_content) {
$img_size = @getimagesizefromstring($file_content);
if (is_array($img_size) && $img_size[0] > 0) {
......
......@@ -114,13 +114,9 @@ abstract class LibrarySearch
if (!$base_url) {
return false;
}
$full_url = $base_url;
if ($url_parameters) {
$full_url .= '?' . http_build_query($url_parameters);
}
$data = file_get_contents($full_url, false, get_default_http_stream_context($base_url));
return $data;
$url = URLHelper::getURL($base_url, $url_parameters);
return url_get_contents($url);
}
......
......@@ -1834,6 +1834,93 @@ function get_default_http_stream_context($url = '')
return stream_context_get_default($opts);
}
/**
* @param string $uri
* @param array $options
* @return \Psr\Http\Message\ResponseInterface|false
*/
function fetch_url_response(string $uri, array $options = [])
{
try {
// Get client
$client = HttpFactory::get()->createHttpClient();
// Create request
$request = $client->createRequest('GET', $uri);
if (isset($options['headers']) && is_array($options['headers'])) {
foreach ($options['headers'] as $key => $value) {
$request = $request->withHeader($key, $value);
}
}
// Return response
return $client->sendRequest($request);
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
return false;
}
}
/**
* @param string $uri
* @param array $options
* @return string|false
*/
function url_get_contents(string $uri, array $options = [])
{
$response = fetch_url_response($uri, $options);
if ($response === false) {
return false;
}
return $response->getBody()->getContents();
}
/**
* @param string $uri
* @param bool $associative
* @param array $options
* @return array|false
*/
function url_get_headers(string $uri, bool $associative = false, array $options = [])
{
$response = fetch_url_response($uri, $options);
if ($response === false) {
return false;
}
$headers = [];
foreach ($response->getHeaders() as $key => $value) {
$key = ucwords($key, '-');
if (count($value) === 1) {
$value = array_values($value)[0];
}
$headers[$key] = $value;
}
array_unshift(
$headers,
"HTTP/{$response->getProtocolVersion()} {$response->getStatusCode()} {$response->getReasonPhrase()}",
);
if ($associative) {
return $headers;
}
$result = [];
array_map(
function ($key, $value) use (&$result) {
foreach ((array) $value as $line) {
$result[] = $key ? "{$key}: {$line}" : $line;
}
},
array_keys($headers),
array_values($headers)
);
return $result;
}
/**
* Encodes an uri just like encodeURI() in Javascript would do.
*
......
......@@ -166,16 +166,12 @@ class OpenGraphURL extends SimpleORMap
$currentEncoding = 'UTF-8';
}
$context = get_default_http_stream_context($this['url']);
stream_context_set_option($context, [
'http' => [
'method' => 'GET',
'header' => sprintf("User-Agent: Stud.IP v%s OpenGraph Parser\r\n", $GLOBALS['SOFTWARE_VERSION']),
$content = url_get_contents($this['url'], [
'headers' => [
'User-Agent' => "Stud.IP v{$GLOBALS['SOFTWARE_VERSION']} OpenGraph Parser",
],
]);
$content = @file_get_contents($this['url'], false, $context);
if ($content === false) {
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment