openstamanager/src/Util/XML.php

188 lines
4.6 KiB
PHP
Raw Normal View History

2018-12-07 10:56:49 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-12-07 10:56:49 +01:00
namespace Util;
2019-02-01 16:02:05 +01:00
use Exception;
2019-01-10 18:41:25 +01:00
2018-12-07 10:56:49 +01:00
/**
* Classe dedicata all'interpretazione dei file XML.
*
* @since 2.4.3
*/
class XML
{
/**
2019-01-04 11:39:30 +01:00
* Interpreta i contenuti di una stringa in formato XML.
2018-12-07 10:56:49 +01:00
*
* @param string $string
*
* @return array
*/
public static function read($string)
{
2019-03-23 17:43:42 +01:00
$content = $string;
2019-01-10 18:41:25 +01:00
libxml_use_internal_errors(true);
2018-12-07 10:56:49 +01:00
$xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
2019-01-10 18:41:25 +01:00
if ($xml === false) {
$message = libxml_get_last_error()->message;
2019-02-01 16:02:05 +01:00
throw new Exception($message);
2019-01-10 18:41:25 +01:00
}
$xpath = '//*[not(normalize-space())]';
foreach (array_reverse($xml->xpath($xpath)) as $remove) {
unset($remove[0]);
}
2018-12-07 10:56:49 +01:00
$result = json_decode(json_encode($xml), true);
return $result;
}
/**
* Interpreta i contenuti di un file XML.
*
2019-02-13 11:26:56 +01:00
* @param string $file
2018-12-07 10:56:49 +01:00
*
* @return array
*/
2019-02-13 11:26:56 +01:00
public static function readFile($file)
2018-12-07 10:56:49 +01:00
{
2019-02-13 11:26:56 +01:00
return static::read(file_get_contents($file));
}
/**
* Interpreta i contenuti di un file XML.
*
* @param string $file
*
* @return string|bool
*/
public static function decodeP7M($file)
{
2019-03-23 17:43:42 +01:00
$directory = pathinfo($file, PATHINFO_DIRNAME);
2019-02-13 11:26:56 +01:00
$content = file_get_contents($file);
2019-03-23 17:43:42 +01:00
$base64 = base64_decode(base64_encode($content), true);
2019-02-13 11:26:56 +01:00
if ($base64 !== false) {
$content = $base64;
}
file_put_contents($file, self::removeBOM($content));
2019-03-23 17:43:42 +01:00
$output_file = $directory.'/'.basename($file, '.p7m');
2019-02-13 11:26:56 +01:00
2019-03-23 17:43:42 +01:00
exec('openssl smime -verify -noverify -in "'.$file.'" -inform DER -out "'.$output_file.'"', $output, $cmd);
if (!file_exists($output_file)) {
$signer = $directory.'/signer';
2019-02-13 11:26:56 +01:00
2019-03-23 17:43:42 +01:00
self::decode($file, $output_file, $signer);
self::der2smime($file);
self::decode($file, $output_file, $signer);
2019-02-13 11:26:56 +01:00
2019-03-23 17:43:42 +01:00
if (!file_exists($output_file)) {
2019-02-13 11:26:56 +01:00
return false;
}
}
2019-03-23 17:43:42 +01:00
return $output_file;
}
/**
* Decodifica il file utilizzando le funzioni native PHP.
*
* @param $file
* @param $output_file
* @param $signer
*
* @return mixed
*/
protected static function decode($file, $output_file, $signer)
{
openssl_pkcs7_verify($file, PKCS7_NOVERIFY | PKCS7_NOSIGS, $signer);
$result = openssl_pkcs7_verify($file, PKCS7_NOVERIFY | PKCS7_NOSIGS, $signer, [], $signer, $output_file);
return $result;
2019-02-13 11:26:56 +01:00
}
/**
* Remove UTF8 BOM.
*
* @param $text
*
* @return string
*
* @source https://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences
*/
protected static function removeBOM($text)
{
$bom = pack('H*', 'EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
/**
* @param $file
*
* @return bool|int
*
* @source http://php.net/manual/en/function.openssl-pkcs7-verify.php#123118
*/
protected static function der2smime($file)
{
$to = <<<TXT
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"
Content-Transfer-Encoding: base64
\n
TXT;
$from = file_get_contents($file);
$to .= chunk_split(base64_encode($from));
return file_put_contents($file, $to);
2018-12-07 10:56:49 +01:00
}
2022-04-06 10:49:05 +02:00
/**
* Interpreta i contenuti di un file XML.
*
* @param string $file
*
* @return array
*/
2022-04-06 11:07:18 +02:00
public static function forceArray($value)
2022-04-06 10:49:05 +02:00
{
2022-04-06 11:07:18 +02:00
if (is_array($value)) {
if (!array_key_exists(0, $value)) {
$result = $value;
$value = [];
$value[0] = $result;
}
2022-04-06 10:49:05 +02:00
}
2022-04-06 11:07:18 +02:00
return $value;
2022-04-06 10:49:05 +02:00
}
2018-12-07 10:56:49 +01:00
}