mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-06 11:50:48 +02:00
Initial commit
Functional, without SSO
This commit is contained in:
216
sources/lib/Http/Client/Abstract.php
Normal file
216
sources/lib/Http/Client/Abstract.php
Normal file
@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
abstract class HttpClientAbstract
|
||||
{
|
||||
protected $_proxy_type;
|
||||
protected $_proxy_ip;
|
||||
protected $_proxy_port;
|
||||
protected $_proxy_user;
|
||||
protected $_proxy_password;
|
||||
protected $_url;
|
||||
protected $_method;
|
||||
protected $_user_agent;
|
||||
protected $_body;
|
||||
protected $_download_body = true;
|
||||
protected $_respond_code;
|
||||
|
||||
const METHOD_GET = "get";
|
||||
const METHOD_POST = "post";
|
||||
|
||||
const PROXY_TYPE_HTTP = 1;
|
||||
const PROXY_TYPE_SOCKS5 = 2;
|
||||
const PROXY_TYPE_WEB = 3;
|
||||
|
||||
/**
|
||||
* @param string $ip
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setProxyType($type)
|
||||
{
|
||||
if ($type != self::PROXY_TYPE_HTTP && $type != self::PROXY_TYPE_SOCKS5 && $type != self::PROXY_TYPE_WEB) {
|
||||
throw new Exception("Type de proxy invalide.");
|
||||
}
|
||||
$this->_proxy_type = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProxyType()
|
||||
{
|
||||
return $this->_proxy_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ip
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setProxyIp($ip)
|
||||
{
|
||||
if (0 === strpos($ip, "http://") || 0 === strpos($ip, "https://")) {
|
||||
$this->setProxyType(self::PROXY_TYPE_WEB);
|
||||
} else {
|
||||
$this->setProxyType(self::PROXY_TYPE_HTTP);
|
||||
}
|
||||
$this->_proxy_ip = $ip;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProxyIp()
|
||||
{
|
||||
return $this->_proxy_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $proxy_port
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setProxyPort($proxy_port)
|
||||
{
|
||||
$this->_proxy_port = $proxy_port;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getProxyPort()
|
||||
{
|
||||
return $this->_proxy_port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $proxy_user
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setProxyUser($proxy_user)
|
||||
{
|
||||
$this->_proxy_user = $proxy_user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProxyUser()
|
||||
{
|
||||
return $this->_proxy_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasProxy()
|
||||
{
|
||||
return !empty($this->_proxy_ip) && !empty($this->_proxy_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $proxy_password
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setProxyPassword($proxy_password)
|
||||
{
|
||||
$this->_proxy_password = $proxy_password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProxyPassword()
|
||||
{
|
||||
return $this->_proxy_password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $user_agent
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setUserAgent($user_agent)
|
||||
{
|
||||
$this->_user_agent = $user_agent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUserAgent()
|
||||
{
|
||||
return $this->_user_agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @throws Exception
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setMethod($method)
|
||||
{
|
||||
if ($method != self::METHOD_GET && $method != self::METHOD_POST) {
|
||||
throw new Exception("Méthode invalide.");
|
||||
}
|
||||
$this->_method = $method;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Définit l'URL à appeler.
|
||||
* @param string $url
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->_url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $download_body
|
||||
* @return HttpClientAbstract
|
||||
*/
|
||||
public function setDownloadBody($download_body)
|
||||
{
|
||||
$this->_download_body = (bool)$download_body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->_body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRespondCode()
|
||||
{
|
||||
return $this->_respond_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne la dernière erreur générée.
|
||||
* @return string
|
||||
*/
|
||||
abstract function getError();
|
||||
|
||||
abstract function request();
|
||||
}
|
95
sources/lib/Http/Client/Curl.php
Normal file
95
sources/lib/Http/Client/Curl.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/Abstract.php";
|
||||
|
||||
class HttpClientCurl extends HttpClientAbstract
|
||||
{
|
||||
protected $_resource;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->_resource = curl_init();
|
||||
curl_setopt($this->_resource, CURLOPT_HEADER, false);
|
||||
curl_setopt($this->_resource, CURLOPT_RETURNTRANSFER, true);
|
||||
if (!ini_get("safe_mode") && !ini_get("open_basedir")) {
|
||||
curl_setopt($this->_resource, CURLOPT_FOLLOWLOCATION, true);
|
||||
}
|
||||
curl_setopt($this->_resource, CURLOPT_CONNECTTIMEOUT, 5);
|
||||
}
|
||||
|
||||
public function request($url = null)
|
||||
{
|
||||
if (!$this->_url && !$url) {
|
||||
throw new Exception("Aucune URL à appeler.");
|
||||
}
|
||||
if ($url) {
|
||||
$this->setUrl($url);
|
||||
}
|
||||
if (!isset($this->_method) || $this->_method == self::METHOD_GET) {
|
||||
curl_setopt($this->_resource, CURLOPT_HTTPGET, true);
|
||||
} else {
|
||||
curl_setopt($this->_resource, CURLOPT_POST, true);
|
||||
}
|
||||
if ($this->_proxy_ip) {
|
||||
if ($this->getProxyType() == self::PROXY_TYPE_WEB) {
|
||||
$url = $this->_proxy_ip.urlencode($this->getUrl());
|
||||
} else {
|
||||
curl_setopt($this->_resource, CURLOPT_PROXY, $this->_proxy_ip);
|
||||
if (!$this->_proxy_type || $this->_proxy_type == self::PROXY_TYPE_HTTP) {
|
||||
curl_setopt($this->_resource, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||
} else {
|
||||
curl_setopt($this->_resource, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||
}
|
||||
if ($this->_proxy_port) {
|
||||
curl_setopt($this->_resource, CURLOPT_PROXYPORT, $this->_proxy_port);
|
||||
}
|
||||
if ($this->_proxy_user) {
|
||||
curl_setopt($this->_resource, CURLOPT_PROXYUSERPWD, $this->_proxy_user.":".$this->_proxy_password);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($userAgent = $this->getUserAgent()) {
|
||||
curl_setopt($this->_resource, CURLOPT_USERAGENT, $userAgent);
|
||||
}
|
||||
curl_setopt($this->_resource, CURLOPT_NOBODY, !$this->_download_body);
|
||||
curl_setopt($this->_resource, CURLOPT_URL, $url);
|
||||
$body = curl_exec($this->_resource);
|
||||
$this->_respond_code = curl_getinfo($this->_resource, CURLINFO_HTTP_CODE);
|
||||
|
||||
if ($this->_respond_code == 301 || $this->_respond_code == 302) {
|
||||
$redirect = curl_getinfo($this->_resource, CURLINFO_REDIRECT_URL);
|
||||
if ($redirect) {
|
||||
return $this->request($redirect);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->getProxyType() == self::PROXY_TYPE_WEB) {
|
||||
// restaure les vrai URL
|
||||
if (preg_match_all("#<(?:img|a)[^>]+(?:src|href)=(?:'|\")(https?://.*(http[^'\"]+))(?:'|\")[^>]*/?>#ismU", $body, $matches, PREG_SET_ORDER)) {
|
||||
$replaceFrom = array();
|
||||
$replaceTo = array();
|
||||
foreach ($matches AS $m) {
|
||||
$replaceFrom[] = $m[1];
|
||||
$replaceTo[] = urldecode($m[2]);
|
||||
}
|
||||
$body = str_replace($replaceFrom, $replaceTo, $body);
|
||||
}
|
||||
}
|
||||
$this->_body = $body;
|
||||
return $this->_body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne la dernière erreur générée par cURL.
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return curl_error($this->_resource);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
curl_close($this->_resource);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user