-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.inc.php
330 lines (273 loc) · 11.5 KB
/
utility.inc.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
<?php
class FileManagement {
/**
* @abstract Classe per gestire lettura, scrittura file, recupero file da remoto
*/
/**
* @abstract converte in array un file CSV
* @return array data
*
*/
public static function csv_to_array($filename='', $log, $delimiter=',', $local = true)
{
if($local && (!file_exists($filename) || !is_readable($filename))) {
$log->logFatal('Impossibile recuperare il file: '. $filename);
return FALSE;
}
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
/**
* @abstract converte in array un file json
* @return array data
*
*/
public static function json_to_array($filename='', $log)
{
$specificheLog[0] = $filename;
if(!file_exists($filename) || !is_readable($filename)) {
$log->logFatal('Impossibile recuperare il file: '. $filename);
// Logger::fatal("Impossibile recuperare il file:", $specificheLog);
return FALSE;
}
$strJsonFileContents = file_get_contents($filename);
if (!$strJsonFileContents) {
$log->logFatal('Impossibile decodificare: '. $filename);
return FALSE;
}
$data = json_decode($strJsonFileContents, true);
$log->logInfo('recuperato il file: '. $filename);
return $data;
}
public static function getFileFromRemoteBolzano($filename='',$log, $delimiter=';')
{
$specificheLog[] = $filename;
$file_headers = @get_headers($filename);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
$log->logFatal('Impossibile recuperare il file: '. $filename);
// Logger::fatal("Impossibile recuperare il file:", $specificheLog);
return false;
} else {
$csvData = file_get_contents($filename, FILE_USE_INCLUDE_PATH);
if (!$csvData) {
$log->logFatal('Impossibile recuperare il file: '. $filename);
// Logger::fatal("Impossibile recuperare il file:", $specificheLog);
return $csvData;
}
$log->logInfo('recuperato il file: '. $filename);
// Logger::info("recuperato il file:", $specificheLog);
return $csvData;
}
}
public static function convert_utf8($content)
{
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
public static function getFileFromRemote($filename='',$log, $delimiter=';')
{
$specificheLog[] = $filename;
$file_headers = @get_headers($filename);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
$log->logFatal('Impossibile recuperare il file: '. $filename);
// Logger::fatal("Impossibile recuperare il file:", $specificheLog);
return false;
} else {
$csvData = file_get_contents($filename, FILE_USE_INCLUDE_PATH);
if (!$csvData) {
$log->logFatal('Impossibile recuperare il file: '. $filename);
// Logger::fatal("Impossibile recuperare il file:", $specificheLog);
return $csvData;
}
$log->logInfo('recuperato il file: '. $filename);
// Logger::info("recuperato il file:", $specificheLog);
$lines = explode(PHP_EOL, $csvData);
$header = NULL;
$data = array();
$col = array();
foreach ($lines as $line) {
$linePulita = str_replace('"', '', $line);
$row = explode($delimiter,$linePulita);
if(!$header) {
$header = $row;
} else {
if (count($header) == count($row)) {
$data[] = array_combine($header, $row);
}
}
}
return $data;
}
}
Public static function string2array($stringa, $delimiter=';') {
$lines = explode(PHP_EOL, $stringa);
$header = NULL;
$data = array();
$col = array();
foreach ($lines as $line) {
$linePulita = str_replace('"', '', $line);
$row = explode($delimiter,$linePulita);
if(!$header) {
$header = $row;
} else {
for ($i=0; $i < count($row); $i++) {
$row[$i] = str_replace(array(PHP_EOL,'\r'), '', $row[$i]);
}
unset($col);
for ($i=0; $i < count($row); $i++) {
$row[$i] = str_replace(array(PHP_EOL,'\r'), '', $row[$i]);
$col[$header[$i]] = $row[$i];
}
$data[] = $col;
}
}
return $data;
}
public static function upload_to_dl($file2upload, $url=UPLOAD_URL, $cod_prov, $cod_com, $log) {
//The name of the field for the uploaded file.
$uploadFieldName = 'file';
$postName = basename($file2upload);
// curl --location --request POST 'http://10.99.36.78:40525/action/push?path=/dl/prova_upload/test/' --form 'file=@/C: /prova.txt'
//Initiate cURL
$ch = curl_init();
// $escapedAction = curl_escape($ch, UPLOAD_ACTION); //ritorna errore con php 5.3 :/
$url = $url.UPLOAD_ACTION;
// $url = $url.$escapedAction;
$upload_path = DL_PATH.PATH_PROV.'/'.$cod_prov.PATH_COMUNI.$cod_com.'/';
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//Set the HTTP request to POST
curl_setopt($ch, CURLOPT_POST, true);
//Tell cURL to return the output as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//If the function curl_file_create exists
if(function_exists('curl_file_create')){
//Use the recommended way, creating a CURLFile object.
$filePath = curl_file_create($file2upload, '', $postName);
} else{
//Otherwise, do it the old way.
//Get the canonicalized pathname of our file and prepend
//the @ character.
$filePath = '@' . $file2upload.';filename='.$postName;
// $value = "@{$this->filename};filename=" . $this->postname;
//Turn off SAFE UPLOAD so that it accepts files
//starting with an @
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
//Setup our POST fields
$postFields = array(
$uploadFieldName => $filePath,
'path' => $upload_path
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
//Execute the request
$result = curl_exec($ch);
//If an error occured, throw an exception
//with the error message.
if(curl_errno($ch)){
$log->logError('Errore: '. curl_error($ch).' Impossibile caricare il file: '. $file2upload);
} else {
$log->logNotice('File caricato: '. $file2upload .' in '.$upload_path);
}
return $result;
}
//public static function upload_generic_to_dl($file2upload, $log, $upload_path=DL_PATH.PATH_PROV.'/', $url=UPLOAD_URL) {
public static function upload_generic_to_dl($file2upload, $log, $upload_path, $url=UPLOAD_URL) {
$uploadFieldName = 'file';
$postName = basename($file2upload);
$ch = curl_init();
$url = $url.UPLOAD_ACTION;
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//Set the HTTP request to POST
curl_setopt($ch, CURLOPT_POST, true);
//Tell cURL to return the output as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//If the function curl_file_create exists
if(function_exists('curl_file_create')){
//Use the recommended way, creating a CURLFile object.
$filePath = curl_file_create($file2upload, '', $postName);
} else{
//Otherwise, do it the old way.
//Get the canonicalized pathname of our file and prepend
//the @ character.
$filePath = '@' . $file2upload.';filename='.$postName;
// $value = "@{$this->filename};filename=" . $this->postname;
//Turn off SAFE UPLOAD so that it accepts files
//starting with an @
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
//Setup our POST fields
$postFields = array(
$uploadFieldName => $filePath,
'path' => $upload_path
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
//Execute the request
$result = curl_exec($ch);
//If an error occured, throw an exception
//with the error message.
if(curl_errno($ch)){
$log->logError('Errore: '. curl_error($ch).' Impossibile caricare il file: '. $file2upload);
} else {
$log->logNotice('File caricato: '. $file2upload .' in '.$upload_path);
}
return $result;
}
public static function save_object_to_json($jsonObject,$file2write,$log) {
//encode and output jsonObject
$specificheLog[0] = $file2write;
$specificheLog[1] = 'Comune ' . $jsonObject->int->desc_com;
$path_prov = PATH_PROV;
$path_Prov_specifico = '/'.$jsonObject->int->cod_prov;
$path_comune = PATH_COMUNI;
$path_comune_specifico = $jsonObject->int->cod_com;
if (!file_exists(CONV_DIR)) {
mkdir(CONV_DIR, 0777, true);
}
if (!file_exists(CONV_DIR.$path_prov)) {
mkdir(CONV_DIR.$path_prov, 0777, true);
}
if (!file_exists(CONV_DIR.$path_prov.$path_Prov_specifico)) {
mkdir(CONV_DIR.$path_prov.$path_Prov_specifico, 0777, true);
}
if (!file_exists(CONV_DIR.$path_prov.$path_Prov_specifico.$path_comune)) {
mkdir(CONV_DIR.$path_prov.$path_Prov_specifico.$path_comune, 0777, true);
}
if (!file_exists(CONV_DIR.$path_prov.$path_Prov_specifico.$path_comune.$path_comune_specifico)) {
mkdir(CONV_DIR.$path_prov.$path_Prov_specifico.$path_comune.$path_comune_specifico, 0777, true);
}
// header('Content-Type: application/json');
if (file_exists($file2write)) {
if (!copy($file2write, $file2write.'old.json')) {
$log->logError('Impossibile copiare il file: '. $file2write);
//Logger::error("Impossibile copiare il file:", $specificheLog);
}
}
$dataJson = json_encode($jsonObject);
$bytes = file_put_contents($file2write, $dataJson);
if (!$bytes) {
$log->logFatal('Impossibile salvare il file: '. $file2write);
// Logger::fatal("Impossibile salvare il file:", $specificheLog);
return $bytes;
}
$log->logNotice('file salvato correttamente: '. $file2write);
// Logger::notice("file salvato correttamente:", $specificheLog);
}
}