Ajout du FR

Ajout du FR + correction du "functions.php"
This commit is contained in:
Gauvain Boiché
2020-03-30 14:52:34 +02:00
commit 5e4c5f9418
4541 changed files with 608941 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils;
use s9e\TextFormatter\Utils\Http\Clients\Cached;
use s9e\TextFormatter\Utils\Http\Clients\Curl;
use s9e\TextFormatter\Utils\Http\Clients\Native;
abstract class Http
{
public static function getClient()
{
return (\extension_loaded('curl')) ? new Curl : new Native;
}
public static function getCachingClient($cacheDir = \null)
{
$client = new Cached(self::getClient());
$client->cacheDir = (isset($cacheDir)) ? $cacheDir : \sys_get_temp_dir();
return $client;
}
}

View File

@@ -0,0 +1,15 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils\Http;
abstract class Client
{
public $sslVerifyPeer = \false;
public $timeout = 10;
abstract public function get($url, $headers = []);
abstract public function post($url, $headers = [], $body = '');
}

View File

@@ -0,0 +1,54 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils\Http\Clients;
use s9e\TextFormatter\Utils\Http\Client;
class Cached extends Client
{
public $client;
public $cacheDir;
public function __construct(Client $client)
{
$this->client = $client;
$this->timeout = $client->timeout;
$this->sslVerifyPeer = $client->sslVerifyPeer;
}
public function get($url, $headers = [])
{
$filepath = $this->getCachedFilepath([$url, $headers]);
if (isset($filepath) && \file_exists(\preg_replace('(^compress\\.zlib://)', '', $filepath)))
return \file_get_contents($filepath);
$content = $this->getClient()->get($url, $headers);
if (isset($filepath) && $content !== \false)
\file_put_contents($filepath, $content);
return $content;
}
public function post($url, $headers = [], $body = '')
{
return $this->getClient()->post($url, $headers, $body);
}
protected function getCachedFilepath(array $vars)
{
if (!isset($this->cacheDir))
return \null;
$filepath = $this->cacheDir . '/http.' . $this->getCacheKey($vars);
if (\extension_loaded('zlib'))
$filepath = 'compress.zlib://' . $filepath . '.gz';
return $filepath;
}
protected function getCacheKey(array $vars)
{
return \strtr(\base64_encode(\sha1(\serialize($vars), \true)), '/', '_');
}
protected function getClient()
{
$this->client->timeout = $this->timeout;
$this->client->sslVerifyPeer = $this->sslVerifyPeer;
return $this->client;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils\Http\Clients;
use s9e\TextFormatter\Utils\Http\Client;
class Curl extends Client
{
protected static $handle;
public function get($url, $headers = [])
{
$handle = $this->getHandle();
\curl_setopt($handle, \CURLOPT_HTTPGET, \true);
\curl_setopt($handle, \CURLOPT_HTTPHEADER, $headers);
\curl_setopt($handle, \CURLOPT_URL, $url);
return \curl_exec($handle);
}
public function post($url, $headers = [], $body = '')
{
$headers[] = 'Content-Length: ' . \strlen($body);
$handle = $this->getHandle();
\curl_setopt($handle, \CURLOPT_HTTPHEADER, $headers);
\curl_setopt($handle, \CURLOPT_POST, \true);
\curl_setopt($handle, \CURLOPT_POSTFIELDS, $body);
\curl_setopt($handle, \CURLOPT_URL, $url);
return \curl_exec($handle);
}
protected function getHandle()
{
if (!isset(self::$handle))
self::$handle = $this->getNewHandle();
\curl_setopt(self::$handle, \CURLOPT_SSL_VERIFYPEER, $this->sslVerifyPeer);
\curl_setopt(self::$handle, \CURLOPT_TIMEOUT, $this->timeout);
return self::$handle;
}
protected function getNewHandle()
{
$handle = \curl_init();
\curl_setopt($handle, \CURLOPT_ENCODING, '');
\curl_setopt($handle, \CURLOPT_FAILONERROR, \true);
\curl_setopt($handle, \CURLOPT_FOLLOWLOCATION, \true);
\curl_setopt($handle, \CURLOPT_RETURNTRANSFER, \true);
return $handle;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils\Http\Clients;
use s9e\TextFormatter\Utils\Http\Client;
class Native extends Client
{
public $gzipEnabled;
public function __construct()
{
$this->gzipEnabled = \extension_loaded('zlib');
}
public function get($url, $headers = [])
{
return $this->request('GET', $url, $headers);
}
public function post($url, $headers = [], $body = '')
{
return $this->request('POST', $url, $headers, $body);
}
protected function createContext($method, array $headers, $body)
{
$contextOptions = [
'ssl' => ['verify_peer' => $this->sslVerifyPeer],
'http' => [
'method' => $method,
'timeout' => $this->timeout,
'header' => $this->generateHeaders($headers, $body),
'content' => $body
]
];
return \stream_context_create($contextOptions);
}
protected function decompress($content)
{
if ($this->gzipEnabled && \substr($content, 0, 2) === "\x1f\x8b")
return \gzdecode($content);
return $content;
}
protected function generateHeaders(array $headers, $body)
{
if ($this->gzipEnabled)
$headers[] = 'Accept-Encoding: gzip';
$headers[] = 'Content-Length: ' . \strlen($body);
return $headers;
}
protected function request($method, $url, $headers, $body = '')
{
$response = @\file_get_contents($url, \false, $this->createContext($method, $headers, $body));
return (\is_string($response)) ? $this->decompress($response) : $response;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2019 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Utils;
use InvalidArgumentException;
abstract class XPath
{
public static function export($value)
{
if (!\is_scalar($value))
throw new InvalidArgumentException(__METHOD__ . '() cannot export non-scalar values');
if (\is_int($value))
return (string) $value;
if (\is_float($value))
return \preg_replace('(\\.?0+$)', '', \sprintf('%F', $value));
return self::exportString((string) $value);
}
protected static function exportString($str)
{
if (\strpos($str, "'") === \false)
return "'" . $str . "'";
if (\strpos($str, '"') === \false)
return '"' . $str . '"';
$toks = [];
$c = '"';
$pos = 0;
while ($pos < \strlen($str))
{
$spn = \strcspn($str, $c, $pos);
if ($spn)
{
$toks[] = $c . \substr($str, $pos, $spn) . $c;
$pos += $spn;
}
$c = ($c === '"') ? "'" : '"';
}
return 'concat(' . \implode(',', $toks) . ')';
}
}