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)) {
|
|
|
|
$output = Translator::getFormatter()->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();
|
|
|
|
$config->set('HTML.Allowed', 'a[href|target|title],img[class|src|border|alt|title|hspace|vspace|width|height|align|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|style],br,p[class]');
|
|
|
|
//$config->set('Cache.SerializerPath', realpath(__DIR__.'/cache/HTMLPurifier'));
|
|
|
|
$config->set('Cache.DefinitionImpl', null);
|
|
|
|
|
|
|
|
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
|
|
|
}
|