2018-03-01 20:35:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe per la gestione delle funzioni VALIDATE richiamabili del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
2018-03-02 19:01:30 +01:00
|
|
|
class Validate
|
2018-03-01 20:35:25 +01:00
|
|
|
{
|
2018-03-02 19:01:30 +01:00
|
|
|
/**
|
2018-03-01 22:05:54 +01:00
|
|
|
* Controlla se la partita iva inserita è valida.
|
|
|
|
*
|
|
|
|
* @param string $vat_number
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function isValidVatNumber($vat_number)
|
|
|
|
{
|
2018-03-02 19:01:30 +01:00
|
|
|
$access_key = Settings::get('apilayer API key for VAT number');
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
if ((!empty($vat_number)) && (!empty($access_key))) {
|
|
|
|
if (strpos($vat_number, 'IT') === false) {
|
|
|
|
$vat_number = 'IT'.$vat_number;
|
|
|
|
}
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$ch = curl_init();
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$qs = '&vat_number='.urlencode(strtoupper($vat_number));
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$url = "http://apilayer.net/api/validate?access_key=$access_key".$qs;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$data = json_decode(curl_exec($ch));
|
|
|
|
curl_close($ch);
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
/*se la riposta è null imposto la relativa proprietà dell'oggetto a 0*/
|
|
|
|
if ($data->valid == null) {
|
|
|
|
$data->valid = 0;
|
|
|
|
}
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
//$data->url = $url;
|
|
|
|
//$data->json_last_error = json_last_error();
|
|
|
|
//$data->json_last_error_msg = json_last_error_msg();
|
|
|
|
}
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
return $data;
|
2018-03-01 22:05:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:35:25 +01:00
|
|
|
/**
|
|
|
|
* Controlla se l'email inserita è valida.
|
|
|
|
*
|
|
|
|
* @param string $email
|
2018-03-02 19:01:30 +01:00
|
|
|
* @param bool $format
|
|
|
|
* @param bool $smtp
|
2018-03-01 20:35:25 +01:00
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function isValidEmail($email, $format = 1, $smtp = 0)
|
|
|
|
{
|
2018-03-02 19:01:30 +01:00
|
|
|
$access_key = Settings::get('apilayer API key for Email');
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
/*$data = (object) [
|
|
|
|
'format_valid' => NULL,
|
|
|
|
'mx_found' => NULL,
|
|
|
|
'smtp_check' => NULL,
|
|
|
|
];*/
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
if ((!empty($email)) && (!empty($access_key))) {
|
|
|
|
$ch = curl_init();
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$qs = '&email='.urlencode($email);
|
|
|
|
$qs .= "&smtp=$smtp";
|
|
|
|
$qs .= "&format=$format";
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$url = "http://apilayer.net/api/check?access_key=$access_key".$qs;
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$data = json_decode(curl_exec($ch));
|
|
|
|
curl_close($ch);
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
/*se la riposta è null verficando il formato, il record mx o il server smtp imposto la relativa proprietà dell'oggetto a 0*/
|
|
|
|
if (($data->format_valid == null) && ($format)) {
|
|
|
|
$data->format_valid = 0;
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
if (($data->mx_found == null) && ($smtp)) {
|
|
|
|
$data->mx_found = 0;
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
if (($data->smtp_check == null) && ($smtp)) {
|
|
|
|
$data->smtp_check = 0;
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
/*controllo o meno smtp
|
|
|
|
if ($data->smtp_check==false)
|
|
|
|
$data->smtp_check = 0;
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
if ($data->mx_found==false)
|
|
|
|
$data->mx_found = 0;
|
|
|
|
*/
|
|
|
|
/* --- */
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
$data->json_last_error = json_last_error();
|
|
|
|
$data->json_last_error_msg = json_last_error_msg();
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
|
2018-03-02 19:01:30 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
}
|