1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - LDIF Import connector |
4
|
|
|
* |
5
|
|
|
* @author Nicolas Mora |
6
|
|
|
* @copyright 2014 Nicolas Mora [email protected] |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
10
|
|
|
* License as published by the Free Software Foundation |
11
|
|
|
* version 3 of the License |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public |
19
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
|
|
|
|
23
|
|
|
namespace OCA\Contacts\Connector; |
24
|
|
|
|
25
|
|
|
use Sabre\VObject\Component; |
26
|
|
|
use Sabre\VObject\StringUtil; |
27
|
|
|
use Sabre\VObject; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @brief Implementation of the LDIF import format |
31
|
|
|
*/ |
32
|
|
|
class ImportLdifConnector extends ImportConnector{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @brief separates elements from the input stream according to the entry_separator value in config |
36
|
|
|
* ignoring the first line if mentionned in the config |
37
|
|
|
* @param $input the input file to import |
38
|
|
|
* @param $limit the number of elements to return (-1 = no limit) |
39
|
|
|
* @return array of strings |
40
|
|
|
*/ |
41
|
|
|
public function getElementsFromInput($file, $limit=-1) { |
42
|
|
|
|
43
|
|
|
$parts = $this->getSourceElementsFromFile($file, $limit); |
44
|
|
|
|
45
|
|
|
$elements = array(); |
46
|
|
|
foreach($parts as $part) |
47
|
|
|
{ |
48
|
|
|
$elements[] = $this->convertElementToVCard($part); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return array_values($elements); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @brief parses the file in vcard format |
56
|
|
|
* @param $file the input file to import |
57
|
|
|
* @param $limit the number of elements to return (-1 = no limit) |
58
|
|
|
* @return array() |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
private function getSourceElementsFromFile($file, $limit=-1) { |
61
|
|
|
$file = StringUtil::convertToUTF8(file_get_contents($file)); |
62
|
|
|
|
63
|
|
|
$nl = "\n"; |
64
|
|
|
$replaceFrom = array("\r","\n "); |
65
|
|
|
$replaceTo = array("\n",""); |
66
|
|
View Code Duplication |
foreach ($this->configContent->import_core->replace as $replace) { |
|
|
|
|
67
|
|
|
if (isset($replace['from']) && isset($replace['to'])) { |
68
|
|
|
$replaceFrom[] = $replace['from']; |
69
|
|
|
$replaceTo[] = $replace['to']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$file = str_replace($replaceFrom, $replaceTo, $file); |
74
|
|
|
|
75
|
|
|
$lines = explode($nl, $file); |
76
|
|
|
$parts = array(); |
77
|
|
|
$card = array(); |
78
|
|
|
$numParts = 0; |
79
|
|
|
foreach($lines as $line) { |
80
|
|
|
if (!preg_match("/^#/", $line)) { // Ignore comment line |
81
|
|
|
if(preg_match("/^\w+:: /",$line)) { |
82
|
|
|
$kv = explode(':: ', $line, 2); |
83
|
|
|
$key = $kv[0]; |
84
|
|
|
$value = base64_decode($kv[1], true); |
85
|
|
|
} else { |
86
|
|
|
$kv = explode(': ', $line, 2); |
87
|
|
|
$key = $kv[0]; |
88
|
|
|
if(count($kv) == 2) { |
89
|
|
|
$value = $kv[1]; |
90
|
|
|
} else { |
91
|
|
|
$value = ""; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
if ($key == "dn") { |
95
|
|
|
if (count($card) > 0) { |
96
|
|
|
$numParts++; |
97
|
|
|
if ($limit > -1 && count($card) == $limit) { |
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
$parts[] = $card; |
101
|
|
|
} |
102
|
|
|
$card = array(array($key, $value)); |
103
|
|
|
} else if ($key != "" && $key != "version" && $value != "") { |
104
|
|
|
$card[] = array($key, $value); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if ($numParts <= $limit && count($card) > 0) { |
109
|
|
|
$parts[] = $card; |
110
|
|
|
} |
111
|
|
|
return $parts; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @brief converts a ldif into a owncloud VCard |
116
|
|
|
* @param $element the VCard element to convert |
117
|
|
|
* @return VCard |
118
|
|
|
*/ |
119
|
|
|
public function convertElementToVCard($element) { |
120
|
|
|
$dest = new \OCA\Contacts\VObject\VCard(); |
|
|
|
|
121
|
|
|
|
122
|
|
|
foreach ($element as $ldifProperty) { |
123
|
|
|
$importEntry = $this->getImportEntry($ldifProperty[0]); |
124
|
|
|
if ($importEntry) { |
125
|
|
|
$value = $ldifProperty[1]; |
126
|
|
|
if (isset($importEntry['remove'])) { |
127
|
|
|
$value = str_replace($importEntry['remove'], '', $ldifProperty[1]); |
128
|
|
|
} |
129
|
|
|
$values = array($value); |
130
|
|
|
if (isset($importEntry['separator'])) { |
131
|
|
|
$values = explode($importEntry['separator'], $value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($values as $oneValue) { |
135
|
|
|
$this->convertElementToProperty($oneValue, $importEntry, $dest); |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$property = $dest->createProperty("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1])); |
139
|
|
|
$property->add('TYPE', StringUtil::convertToUTF8($ldifProperty[0])); |
140
|
|
|
$dest->add($property); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$dest->validate(\Sabre\VObject\Component\VCard::REPAIR); |
145
|
|
|
return $dest; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @brief converts an LDIF element into a VCard property |
150
|
|
|
* and updates the VCard |
151
|
|
|
* @param $value the LDIF value |
152
|
|
|
* @param $importEntry the VCard entry to modify |
153
|
|
|
* @param $dest the VCard to modify (for adding a X-FAVOURITE property) |
154
|
|
|
*/ |
155
|
|
|
private function convertElementToProperty($value, $importEntry, &$dest) { |
156
|
|
|
if (isset($importEntry->vcard_favourites)) { |
157
|
|
|
foreach ($importEntry->vcard_favourites as $vcardFavourite) { |
158
|
|
|
if (strcasecmp((string)$vcardFavourite, trim($value)) == 0) { |
159
|
|
|
$property = $dest->createProperty("X-FAVOURITES", 'yes'); |
160
|
|
|
$dest->add($property); |
161
|
|
View Code Duplication |
} else { |
|
|
|
|
162
|
|
|
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry); |
163
|
|
|
if (isset($importEntry['image']) && $importEntry['image'] == "true") { |
164
|
|
|
$this->updateImageProperty($property, $value); |
165
|
|
|
} else { |
166
|
|
|
$this->updateProperty($property, $importEntry, $value); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
View Code Duplication |
} else { |
|
|
|
|
171
|
|
|
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry); |
172
|
|
|
if (isset($importEntry['image']) && $importEntry['image'] == "true") { |
173
|
|
|
$this->updateImageProperty($property, $value); |
174
|
|
|
} else { |
175
|
|
|
$this->updateProperty($property, $importEntry, $value); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @brief tests if the property has to be translated by looking for its signature in the xml configuration |
182
|
|
|
* @param $property Sabre VObject Property too look |
183
|
|
|
* @param $vcard the parent Sabre VCard object to look for a |
184
|
|
|
*/ |
185
|
|
|
private function getImportEntry($property) { |
186
|
|
|
foreach ($this->configContent->import_entry as $importEntry) { |
187
|
|
|
if ($importEntry['name'] == $property) { |
188
|
|
|
return $importEntry; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @brief returns the probability that the first element is a match for this format |
196
|
|
|
* @param $file the file to examine |
197
|
|
|
* @return 0 if not a valid ldif file |
|
|
|
|
198
|
|
|
* 1 - 0.5*(number of untranslated elements/total number of elements) |
199
|
|
|
* The more the first element has untranslated elements, the more the result is close to 0.5 |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function getFormatMatch($file) { |
|
|
|
|
202
|
|
|
// Examining the first element only |
203
|
|
|
$parts = $this->getSourceElementsFromFile($file, 1); |
204
|
|
|
|
205
|
|
|
if (!$parts || count($parts[0]) == 0) { |
|
|
|
|
206
|
|
|
// Doesn't look like a ldif file |
207
|
|
|
return 0; |
208
|
|
|
} else { |
209
|
|
|
$element = $this->convertElementToVCard($parts[0]); |
210
|
|
|
if ($element) { |
211
|
|
|
$unknownElements = $element->select("X-Unknown-Element"); |
212
|
|
|
return (1 - (0.5 * count($unknownElements)/count($parts[0]))); |
213
|
|
|
} else { |
214
|
|
|
return 0; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
?> |
|
|
|
|
221
|
|
|
|