1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* LibreDTE: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) LibreDTE <https://www.libredte.cl> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de |
20
|
|
|
* GNU junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace libredte\lib\Core\Xml; |
26
|
|
|
|
27
|
|
|
use DOMDocument; |
28
|
|
|
use DOMNodeList; |
29
|
|
|
use DOMXPath; |
30
|
|
|
use InvalidArgumentException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Utilidades (métodos auxiliares) para trabajar con XML. |
34
|
|
|
*/ |
35
|
|
|
class XmlUtils |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Ejecuta una consulta XPath en un documento XML. |
39
|
|
|
* |
40
|
|
|
* @param DOMDocument $document Documento XML donde se ejecutará la consulta. |
41
|
|
|
* @param string $expression Expresión XPath a ejecutar. |
42
|
|
|
* @return DOMNodeList Nodos resultantes de la consulta XPath. |
43
|
|
|
*/ |
44
|
68 |
|
public static function xpath(DOMDocument $document, string $expression): DOMNodeList |
45
|
|
|
{ |
46
|
68 |
|
$xpath = new DOMXPath($document); |
47
|
68 |
|
$result = @$xpath->query($expression); |
48
|
|
|
|
49
|
68 |
|
if ($result === false) { |
50
|
1 |
|
throw new InvalidArgumentException(sprintf( |
51
|
1 |
|
'Expresión XPath inválida: %s', |
52
|
1 |
|
$expression |
53
|
1 |
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
67 |
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Codifica el string como ISO-8859-1 si es que fue pasado como UTF-8. |
61
|
|
|
* |
62
|
|
|
* @param string $string String en UTF-8 o ISO-8859-1. |
63
|
|
|
* @return string String en ISO-8859-1 si se logró convertir. |
64
|
|
|
*/ |
65
|
81 |
|
public static function utf2iso(string $string): string |
66
|
|
|
{ |
67
|
81 |
|
if (!mb_detect_encoding($string, 'UTF-8', true)) { |
68
|
1 |
|
return $string; |
69
|
|
|
} |
70
|
|
|
|
71
|
80 |
|
return (string) mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Codifica el string como UTF-8 si es que fue pasado como ISO-8859-1. |
76
|
|
|
* |
77
|
|
|
* @param string $string String en UTF-8 o ISO-8859-1. |
78
|
|
|
* @return string String en UTF-8 si se logró convertir. |
79
|
|
|
*/ |
80
|
2 |
|
public static function iso2utf(string $string): string |
81
|
|
|
{ |
82
|
2 |
|
if (!mb_detect_encoding($string, 'ISO-8859-1', true)) { |
83
|
|
|
return $string; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return (string) mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sanitiza los valores que son asignados a los tags del XML. |
91
|
|
|
* |
92
|
|
|
* @param string $string Texto que se asignará como valor al nodo XML. |
93
|
|
|
* @return string Texto sanitizado. |
94
|
|
|
*/ |
95
|
126 |
|
public static function sanitize(string $string): string |
96
|
|
|
{ |
97
|
|
|
// Si no se paso un texto o bien es un número no se hace nada. |
98
|
126 |
|
if (!$string || is_numeric($string)) { |
99
|
98 |
|
return $string; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Convertir "predefined entities" de XML. |
103
|
121 |
|
$string = str_replace( |
104
|
121 |
|
['&', '&', '<', '<', '>', '>', '"', '"', ''', '''], |
105
|
121 |
|
['&', '&', '<', '<', '>', '>', '"', '"', '\'', '\''], |
106
|
121 |
|
$string |
107
|
121 |
|
); |
108
|
|
|
|
109
|
121 |
|
$string = str_replace('&', '&', $string); |
110
|
|
|
|
111
|
|
|
/*$string = str_replace( |
112
|
|
|
['"', '\''], |
113
|
|
|
['"', '''], |
114
|
|
|
$string |
115
|
|
|
);*/ |
116
|
|
|
|
117
|
|
|
// Entregar texto sanitizado. |
118
|
121 |
|
return $string; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Corrige las entities ''' y '"' en el XML. |
123
|
|
|
* |
124
|
|
|
* La corrección se realiza solo dentro del contenido de tags del XML, pero |
125
|
|
|
* no en los atributos de los tags. |
126
|
|
|
* |
127
|
|
|
* @param string $string XML a corregir. |
128
|
|
|
* @return string XML corregido. |
129
|
|
|
*/ |
130
|
141 |
|
public static function fixEntities(string $string): string |
131
|
|
|
{ |
132
|
141 |
|
$newString = ''; |
133
|
141 |
|
$n_letras = strlen($string); |
134
|
141 |
|
$convertir = false; |
135
|
141 |
|
for ($i = 0; $i < $n_letras; ++$i) { |
136
|
138 |
|
if ($string[$i] === '>') { |
137
|
138 |
|
$convertir = true; |
138
|
|
|
} |
139
|
138 |
|
if ($string[$i] === '<') { |
140
|
138 |
|
$convertir = false; |
141
|
|
|
} |
142
|
138 |
|
$newString .= $convertir |
143
|
138 |
|
? str_replace( |
144
|
138 |
|
['\'', '"'], |
145
|
138 |
|
[''', '"'], |
146
|
138 |
|
$string[$i] |
147
|
138 |
|
) |
148
|
138 |
|
: $string[$i] |
149
|
138 |
|
; |
150
|
|
|
} |
151
|
141 |
|
return $newString; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|