2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2018-02-20 14:56:13 +01:00
|
|
|
* Classe per gestire la sanitarizzazione degli input, basata sul framework open-source HTMLPurifier.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class Filter
|
|
|
|
{
|
|
|
|
/** @var HTMLPurifier */
|
|
|
|
protected static $purifier;
|
|
|
|
|
2017-08-31 11:32:49 +02:00
|
|
|
/** @var array Elenco dei contenuti inviati via POST */
|
2018-07-02 15:41:38 +02:00
|
|
|
protected static $post = [];
|
2017-08-31 11:32:49 +02:00
|
|
|
/** @var array Elenco dei contenuti inviati via GET */
|
2018-07-02 15:41:38 +02:00
|
|
|
protected static $get = [];
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il valore presente nei dati ottenuti dall'input dell'utente.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* @param string $method
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-02 15:41:38 +02:00
|
|
|
public static function getValue($property, $method = null, $raw = false)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
|
|
|
$value = null;
|
|
|
|
|
|
|
|
if (empty($method)) {
|
2018-07-02 15:41:38 +02:00
|
|
|
$value = (self::post($property, $raw) !== null) ? self::post($property, $raw) : self::get($property, $raw);
|
2017-08-04 16:28:16 +02:00
|
|
|
} elseif (strtolower($method) == 'post') {
|
2018-07-02 15:41:38 +02:00
|
|
|
$value = self::post($property, $raw);
|
2017-08-04 16:28:16 +02:00
|
|
|
} elseif (strtolower($method) == 'get') {
|
2018-07-02 15:41:38 +02:00
|
|
|
$value = self::get($property, $raw);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i contenuti dalla sezione POST.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getPOST()
|
|
|
|
{
|
|
|
|
if (empty(self::$post)) {
|
2018-07-02 15:41:38 +02:00
|
|
|
self::$post['raw'] = self::sanitize($_POST);
|
|
|
|
self::$post['data'] = self::parse(self::$post['raw']);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 15:41:38 +02:00
|
|
|
return self::$post['data'];
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il valore presente nei dati ottenuti dalla sezione POST.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-02 15:41:38 +02:00
|
|
|
public static function post($property, $raw = false)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2018-07-02 15:41:38 +02:00
|
|
|
self::getPOST();
|
|
|
|
|
|
|
|
$category = empty($raw) ? 'data' : 'raw';
|
|
|
|
|
|
|
|
if (isset(self::$post[$category][$property])) {
|
|
|
|
return self::$post[$category][$property];
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i contenuti dalla sezione GET.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getGET()
|
|
|
|
{
|
|
|
|
if (empty(self::$get)) {
|
2018-07-02 15:41:38 +02:00
|
|
|
self::$get['raw'] = self::sanitize($_GET);
|
|
|
|
self::$get['data'] = self::parse(self::$get['raw']);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 15:41:38 +02:00
|
|
|
return self::$get['data'];
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il valore presente nei dati ottenuti dalla sezione GET.
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-02 15:41:38 +02:00
|
|
|
public static function get($property, $raw = false)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2018-07-02 15:41:38 +02:00
|
|
|
self::getGET();
|
|
|
|
|
|
|
|
$category = empty($raw) ? 'data' : 'raw';
|
|
|
|
|
|
|
|
if (isset(self::$get[$category][$property])) {
|
|
|
|
return self::$get[$category][$property];
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-28 17:57:01 +02:00
|
|
|
* Sanitarizza i contenuti dell'input.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2018-07-28 17:57:01 +02:00
|
|
|
* @param mixed $input Contenuti
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-07-28 17:57:01 +02:00
|
|
|
public static function sanitize($input)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
|
|
|
$output = null;
|
|
|
|
if (is_array($input)) {
|
|
|
|
foreach ($input as $key => $value) {
|
|
|
|
$output[$key] = self::sanitize($value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$output = trim(self::getPurifier()->purify($input));
|
2018-07-02 15:41:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-07-02 15:41:38 +02:00
|
|
|
/**
|
2018-07-28 17:57:01 +02:00
|
|
|
* Interpreta e formatta correttamente i contenuti dell'input.
|
|
|
|
*
|
|
|
|
* @param mixed $input Contenuti
|
|
|
|
*
|
|
|
|
* @return mixed
|
2018-07-02 15:41:38 +02:00
|
|
|
*/
|
|
|
|
public static function parse($input)
|
|
|
|
{
|
|
|
|
$output = null;
|
|
|
|
if (is_array($input)) {
|
|
|
|
foreach ($input as $key => $value) {
|
|
|
|
$output[$key] = self::parse($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
2018-07-02 15:41:38 +02:00
|
|
|
} elseif (!is_null($input)) {
|
2018-09-20 12:05:22 +02:00
|
|
|
$output = formatter()->parse($input);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'istanza di HTMLPurifier in utilizzo.
|
|
|
|
*
|
|
|
|
* @return \HTMLPurifier
|
|
|
|
*/
|
|
|
|
public static function getPurifier()
|
|
|
|
{
|
|
|
|
if (empty(self::$purifier)) {
|
|
|
|
$config = \HTMLPurifier_Config::createDefault();
|
2018-09-19 17:15:01 +02:00
|
|
|
|
|
|
|
$config->set('HTML.Allowed', 'br,p[style],b[style],strong[style],i[style],em[style],u[style],strike,a[style|href|title|target],ol[style],ul[style],li[style],hr[style],blockquote[style],img[style|alt|title|width|height|src|align],table[style|width|bgcolor|align|cellspacing|cellpadding|border],tr[style],td[style],th[style],tbody,thead,caption,col,colgroup,span[style],sup');
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
//$config->set('Cache.SerializerPath', realpath(__DIR__.'/cache/HTMLPurifier'));
|
|
|
|
$config->set('Cache.DefinitionImpl', null);
|
2019-09-23 11:10:37 +02:00
|
|
|
$config->set('URI.AllowedSchemes', [
|
|
|
|
'http' => true,
|
|
|
|
'https' => true,
|
|
|
|
'mailto' => true,
|
|
|
|
'ftp' => true,
|
|
|
|
'tel' => true,
|
|
|
|
'data' => true,
|
|
|
|
]);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
self::$purifier = new \HTMLPurifier($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$purifier;
|
|
|
|
}
|
2018-07-03 11:12:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Imposta una proprietà specifica a un valore personalizzato.
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public static function set($method, $property, $value)
|
|
|
|
{
|
|
|
|
if (strtolower($method) == 'post') {
|
|
|
|
self::$post['data'][$property] = $value;
|
|
|
|
} elseif (strtolower($method) == 'get') {
|
|
|
|
self::$get['data'][$property] = $value;
|
|
|
|
}
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|