forked from scieloorg/Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.XSLTransformer.php
339 lines (295 loc) · 7.94 KB
/
class.XSLTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
include_once (dirname(__FILE__)."/classes/class_xml_check/class_xml_check.php");
include_once (dirname(__FILE__)."/class.XSLTransformerJava.php");
include_once (dirname(__FILE__)."/class.XSLTransformerPHP5.php");
function xml_utf8_decode($xml){
$xml = utf8_decode($xml);
$xml = str_replace('utf-8','iso-8859-1',$xml);
$xml = str_replace('UTF-8','iso-8859-1',$xml);
return $xml;
}
class XSLTransformer {
/* Constructor */
function XSLTransformer(){
$this->defFile = parse_ini_file(dirname(__FILE__)."/scielo.def.php",true);
$this->tPHP = new XSLTransformerPHP5();
$this->tJava = new XSLTransformerJava($_SERVER["SERVER_ADDR"],$this->defFile["SOCKET"]["SOCK_PORT"]);
$this->socket_log_file = $this->defFile['SOCKET']['ACCESS_LOG_FILE'];
$this->enable_socket_log = $this->defFile['SOCKET']['ENABLE_ACCESS_LOG'];
$this->redirectURL = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
function validateXML($xml){
$validXML = true;
if ($this->defFile['XML_ERROR']['ENABLED_XML_ERROR'] == '1'){
$xmlCheck = new XML_check();
$xml1 = $xml;
$validXML = $xmlCheck->check_string($xml1); // verifica se o XML é bem formado
}
return $validXML;
}
function setXslBaseUri($uri){
return $this->tPHP->setXslBaseUri($uri);;
}
/* Destructor */
function destroy() {
$this->tPHP->destroy();
}
/* output methods */
function setOutput ($string) {
$this->output = $string;
}
function getOutput() {
return $this->output;
}
/* set methods */
function setXml($xml) {
if ($this->isXmlContent($xml))
{
$this->xml = $xml;
return true;
}
elseif ($doc = new docReader($xml))
{
$this->xml = $doc->getString();
return true;
}
else
{
$this->setError("Could not open $xml");
return false;
}
}
function setXslFile($xslFile) {
$this->xslFile = $xslFile;
}
function returnXslKey(){
if ($this->xslFile){
$pos_ini = strrpos($this->xslFile,"/")+1;
$pos_ini = strrpos($this->xslFile,"/")+1;
$pos_len = strrpos($this->xslFile,".")-$pos_ini;
$r = strtoupper(substr($this->xslFile,$pos_ini,$pos_len));
} else {
if (strpos($this->xsl,".xsl")==0 && strpos(" ".$this->xsl,"/")==0){
$r = $this->xsl;
}
}
return $r;
}
function returnXslContent(){
if ($this->xslFile){
$r = file_get_contents($this->xslFile);
} else {
if (strpos("x".$this->xsl," ")>0){
$r = $this->xsl;
}
}
return $r;
}
function getXsl($type) {
switch ($type){
case "key":
$r = $this->returnXslKey();
break;
case "filecontent":
$r = $this->returnXslContent();
break;
}
if (!$r){
die('Anteriormente, para a transformação java, era feito $this->xsl = o apelido da XSL. Atualmente, dev usar o metodo $this->setXslFile passando o caminho completo do arquivo XSL. Essa mudança tem o objetivo de padronizar o ato de instanciar a XSL, servindo tanto para transformação java, quanto php. Pois caso o java não esteja operando, a transformação php pode ser usada.');
}
return $r;
}
function setXsl($uri) {
$this->xsl = $uri;
}
function isXmlContent($xml)
{
if (strcmp(substr($xml,0,5), "<?xml") == 0)
{
return true;
}
else
{
return false;
}
}
/* transform method */
function transform()
{
$err = "";
$result = "";
$tryByPHP = false;
$tryRedirect = false;
$this->xml = str_replace("&#","MY_ENT_",$this->xml);
//$this->xml = xmlspecialchars ($this->xml);
if ($this->validateXML($this->xml)){
if ($this->tJava->checkSocketOpen()) {
$result = $this->tJava->transform($this->getXsl("key"), $this->xml);
switch ($result){
case "NO_SOCKET":
$tryPHP = true;
$this->setError($result);
break;
case "ERROR_1":
$tryPHP = true;
$this->setError($result);
break;
case "ERROR_2":
$tryPHP = true;
$this->setError($result);
break;
default:
$this->transformedBy = "JAVA";
break;
}
} else {
$tryPHP = true;
}
if ($tryPHP){
$result = $this->tPHP->transform($this->xml, $this->xslFile , $error);
if ($error){
$this->setError($error);
$result = "ERROR_PHP";
$tryRedirect = true;
} else {
$this->transformedBy = "PHP";
}
}
if ($tryRedirect){
header('Location: http://'.$this->redirectURL);
} else {
$result = str_replace("MY_ENT_","&#",$result);
$this->setOutput ($result."<!--transformed by $this->transformedBy ".date("h:m:s d-m-Y")."-->");
$this->writeLog ();
}
} else {
include_once (dirname(__FILE__)."/mail_msg.php");
$url = "http://".$_SERVER['SERVER_NAME']."/php/xmlError.php?lang=".$_REQUEST['lng'];
}
}
/* Error Handling */
function setError ($string) {
$this->error = $string;
}
function getError() {
return $this->error;
}
function writeLog(){
if ($this->enable_socket_log){
$this->writeFile($_SERVER["SERVER_ADDR"]." ".$this->redirectURL." $this->transformedBy \n",$this->socket_log_file);
}
}
function writeFile($content,$filename){
$content = date("h:m:s d-m-Y")." ".$content;
if (!$handle = fopen($filename, 'x+')) {
if (!$handle = fopen($filename, 'a+')) {
exit;
}
}
if ($handle){
chmod($filename, 0766);
if (fwrite($handle, $content) === FALSE) {
exit;
}
fclose($handle);
}
}
}
/* docReader -- read a file or URL as a string */
class docReader {
var $string; // public string representation of file
var $type; // private URI type: 'file','url'
var $bignum = 3500000;
/* public constructor */
function docReader ($uri) { // returns integer
$this->setUri ($uri);
$this->setType();
$fp = fopen ($this->getUri(),"r");
if ($fp) {
// get length
if ( $this->getType() == 'file' ) {
$length = filesize ($this->getUri());
}
else {
$length = $this->bignum;
}
$acumulado = "";
while (!feof($fp)){
$acumulado .= fread ($fp, $length);
}
$this->setString ($acumulado);
/* $this->setString (fread ($fp, $length)); */
fclose($fp);
return 1;
}
else {
return 0;
}
}
/* determine if a URI is a filename or URL */
function isFile ($uri) { // returns boolean
if (strstr ($uri,'http://') == $uri) {
return false;
}
else {
return true;
}
}
/* set and get methods */
function setUri ($string) {
$this->uri = $string;
}
function getUri() {
return $this->uri;
}
function setString ($string) {
$this->string = $string;
}
function getString() {
return $this->string;
}
function setType() {
if ( $this->isFile ($this->uri) ) {
$this->type = 'file';
}
else {
$this->type = 'url';
}
}
function getType() {
return $this->type;
}
}
function xmlspecialchars($s){
//echo " " . microtime ();
$s = utf8_decode($s);
$s = str_replace("<", "NO_CHANGE_LT", $s);
$s = str_replace(">", "NO_CHANGE_GT", $s);
$s = str_replace('"', "NO_CHANGE_QUOTE", $s);
$s = htmlentities($s);
$s = entityname2number($s);
$s = str_replace("NO_CHANGE_LT", "<", $s);
$s = str_replace("NO_CHANGE_GT", ">", $s);
$s = str_replace("NO_CHANGE_QUOTE", '"', $s);
//echo " " . microtime ();
return $s;
}
function entityname2number($s){
//$t = parse_ini_file("/home/scielo/www/bases/gizmo/entname2number.ini", false);
$f = file_get_contents("../bases/gizmo/entname2number.ini");
$a = explode("\r\n",$f);
foreach ($a as $valor){
$x = explode("=", $valor);
$t1[] = $x[0];
$t2[] = $x[1];
}
/*
var_dump($t1);
var_dump($t2);
var_dump($s);
*/
$s = str_replace($t1, $t2, $s);
// var_dump($s);
return $s;
}
?>