-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbackend.class.php
More file actions
109 lines (91 loc) · 3.12 KB
/
backend.class.php
File metadata and controls
109 lines (91 loc) · 3.12 KB
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
<?php
/**
* -------------------------------------------------------------------------
* DataInjection plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of DataInjection.
*
* DataInjection 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 2 of the License, or
* (at your option) any later version.
*
* DataInjection 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 DataInjection. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2007-2023 by DataInjection plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/datainjection
* -------------------------------------------------------------------------
*/
/*
* Common backend to read files to import
*/
abstract class PluginDatainjectionBackend
{
protected $file = "";
protected $delimiter = "";
protected $encoding;
protected $errmsg;
protected $numberOfLines = 0;
const ENCODING_ISO8859_1 = 0;
const ENCODING_UFT8 = 1;
const ENCODING_AUTO = 2;
/**
* Get header of the file
*
* @param $injectionData
* @param $header_present
*
* @return array with the data from the header
**/
static function getHeader(PluginDatainjectionData $injectionData, $header_present) {
if ($header_present) {
return $injectionData->getDataAtLine(0);
}
$nb = count($injectionData->getDataAtLine(0));
for ($i=0; $i<$nb; $i++) {
$header[] = $i;
}
return $header;
}
/**
* Get the backend implementation by type
*
* @param $type
* @return PluginDatainjectionBackendInterface
**/
static function getInstance($type) {
$class = 'PluginDatainjectionBackend'.$type;
return new $class();
}
static function is_utf8($string) {
// From http://w3.org/International/questions/qa-forms-utf-8.html
return preg_match(
'%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string
);
}
static function toUTF8($string) {
if (!self::is_utf8($string)) {
return utf8_encode($string);
}
return $string;
}
}