openstamanager/src/Validate.php

192 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2018-10-01 18:43:01 +02:00
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;
2018-12-29 12:03:22 +01:00
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
{
2019-02-14 18:04:50 +01:00
public static function vatCheckIT($partita_iva)
2018-11-30 18:31:16 +01:00
{
if ($partita_iva === '') {
return true;
}
2018-12-12 18:42:41 +01:00
if (strlen($partita_iva) == 13) {
$partita_iva = substr($partita_iva, 2);
}
2018-11-30 18:31:16 +01:00
2018-12-23 14:01:59 +01:00
if (strlen($partita_iva) != 11 || preg_match('/^[0-9]+$/D', $partita_iva) != 1) {
2018-11-30 18:31:16 +01:00
return false;
}
$s = 0;
for ($i = 0; $i <= 9; $i += 2) {
$s += ord($partita_iva[$i]) - ord('0');
}
for ($i = 1; $i <= 9; $i += 2) {
2018-12-23 14:01:59 +01:00
$c = 2 * (ord($partita_iva[$i]) - ord('0'));
2018-11-30 18:31:16 +01:00
if ($c > 9) {
$c = $c - 9;
}
$s += $c;
}
2018-12-23 14:01:59 +01:00
if ((10 - $s % 10) % 10 != ord($partita_iva[10]) - ord('0')) {
2018-11-30 18:31:16 +01:00
return false;
}
return true;
}
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;
}
2018-12-07 11:36:21 +01:00
// Controllo sulla sintassi
2019-02-14 18:04:50 +01:00
if (starts_with($vat_number, 'IT') && !static::vatCheckIT($vat_number)) {
2018-12-07 11:36:21 +01:00
return false;
}
2018-12-12 18:42:41 +01:00
/**
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-12-12 18:42:41 +01:00
} */
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);
2019-01-04 11:39:30 +01:00
/*se la riposta è null verificando 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
}
2018-11-16 19:14:21 +01:00
public static function isValidTaxCode($codice_fiscale)
{
2018-11-16 19:50:02 +01:00
if (empty($codice_fiscale)) {
return true;
}
2018-11-16 19:14:21 +01:00
$validator = new CodiceFiscale\Validator($codice_fiscale);
return $validator->isFormallyValid();
}
}