openstamanager/src/Validate.php

143 lines
3.9 KiB
PHP
Raw Normal View History

<?php
use Mpociot\VatCalculator\VatCalculator;
use Respect\Validation\Validator as v;
/**
* 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-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
*
2018-06-26 15:53:10 +02:00
* @return bool
2018-03-01 22:05:54 +01:00
*/
public static function isValidVatNumber($vat_number)
{
2018-06-26 15:53:10 +02:00
if (empty($vat_number)) {
return true;
}
$vat_number = starts_with($vat_number, 'IT') ? $vat_number : 'IT'.$vat_number;
2018-06-27 10:50:41 +02:00
// Controllo con API europea ufficiale
if (extension_loaded('soap')) {
try {
$validator = new VatCalculator();
if (!$validator->isValidVATNumber($vat_number)) {
return false;
}
} catch (VATCheckUnavailableException $e) {
}
}
2018-06-27 10:50:41 +02:00
// Controllo attraverso apilayer
2018-07-09 10:44:54 +02:00
$access_key = setting('apilayer API key for VAT number');
if (!empty($access_key)) {
2018-06-27 10:50:41 +02:00
if (!extension_loaded('curl')) {
2018-07-19 17:29:21 +02:00
flash()->warning(tr('Estensione cURL non installata'));
2018-03-01 22:05:54 +01:00
return true;
2018-03-02 19:01:30 +01:00
}
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
return $data->valid;
2018-03-02 19:01:30 +01:00
}
2018-03-01 22:05:54 +01:00
return true;
2018-03-01 22:05:54 +01:00
}
/**
* Controlla se l'email inserita è valida.
*
* @param string $email
2018-03-02 19:01:30 +01:00
* @param bool $smtp
*
* @return bool|object
*/
public static function isValidEmail($email, $smtp = 0)
{
if (!v::email()->validate($email)) {
return false;
}
2018-06-27 10:50:41 +02:00
// Controllo attraverso apilayer
2018-07-09 10:44:54 +02:00
$access_key = setting('apilayer API key for Email');
if (!empty($access_key)) {
2018-06-27 10:50:41 +02:00
if (!extension_loaded('curl')) {
2018-07-19 17:29:21 +02:00
flash()->warning(tr('Estensione cURL non installata'));
return true;
}
2018-03-02 19:01:30 +01:00
$qs = '&email='.urlencode($email);
$qs .= "&smtp=$smtp";
$qs .= '&format=1';
$qs .= '&resource=check_email';
$url = "https://services.osmcloud.it/api/?token=$access_key".$qs;
$curl_options = [
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => 'gzip,deflate',
];
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$output = curl_exec($ch);
2018-03-02 19:01:30 +01:00
curl_close($ch);
$data = json_decode($output, false);
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) {
2018-03-02 19:01:30 +01:00
$data->format_valid = 0;
}
if ($data->mx_found == null && $smtp) {
2018-03-02 19:01:30 +01:00
$data->mx_found = 0;
}
if ($data->smtp_check == null && $smtp) {
2018-03-02 19:01:30 +01:00
$data->smtp_check = 0;
}
$data->smtp = $smtp;
2018-03-02 19:01:30 +01:00
$data->json_last_error = json_last_error();
$data->json_last_error_msg = json_last_error_msg();
return empty($data->format_valid) ? false : $data;
}
return true;
2018-03-02 19:01:30 +01:00
}
}