importconnector.php
6.09 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
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
<?php
/**
* ownCloud - CSV Import connector
*
* @author Nicolas Mora
* @copyright 2013-2014 Nicolas Mora mail@babelouest.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation
* version 3 of the License
*
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts\Connector;
use Sabre\VObject\Component,
Sabre\VObject\StringUtil;
/**
* Abstract class used to implement import classes
*/
abstract class ImportConnector {
// XML Configuration, class SimpleXml format
protected $configContent;
public function __construct($xml_config = null) {
if ($xml_config != null) {
$this->setConfig($xml_config);
}
}
// returns a table containing converted elements from the input file
abstract function getElementsFromInput($input, $limit=-1);
// returns a single converted element
abstract function convertElementToVCard($element);
// returns the probability that the file matchs the current format
abstract function getFormatMatch($file);
public function setConfig($xml_config) {
$this->configContent = $xml_config;
}
/**
* @brief updates a property given in parameter with the value and using the importEntry to set the different parameters
* @param $property the property to update
* @param $importEntry the entry configuration to update in SimpleXml format
* @value the value to update
*/
protected function updateProperty(&$property, $importEntry, $value) {
if (isset($property) && isset($importEntry) && isset($value)) {
if (isset($importEntry->vcard_entry)) {
if (isset($importEntry->vcard_entry['type'])) {
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($importEntry->vcard_entry['type']));
}
if (isset($importEntry->vcard_entry->additional_property)) {
foreach ($importEntry->vcard_entry->additional_property as $additionalProperty) {
$property->parameters[] = new \Sabre\VObject\Parameter(''.$additionalProperty['name'], ''.$additionalProperty['value']);
}
}
if (isset($importEntry->vcard_entry['prefix'])) {
$value = $importEntry->vcard_entry['prefix'].$value;
}
if (isset($importEntry->vcard_entry['group'])) {
$property->group = $importEntry->vcard_entry['group'];
}
if (isset($importEntry->vcard_entry['position'])) {
$separator=";";
if (isset($importEntry->vcard_entry['separator'])) {
$separator=$importEntry->vcard_entry['separator'];
}
$position = $importEntry->vcard_entry['position'];
$vArray = explode($separator, $property);
$vArray[intval($position)] = StringUtil::convertToUTF8($value);
$property->setValue(implode($separator, $vArray));
} else {
if (isset($importEntry->vcard_entry['value'])) {
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($value));
} else {
$property->setValue(StringUtil::convertToUTF8($value));
}
}
}
if (isset($importEntry->vcard_parameter)) {
$property->parameters[] = new \Sabre\VObject\Parameter($importEntry->vcard_parameter['parameter'], ''.StringUtil::convertToUTF8($value));
}
}
}
/**
* @brief modifies a vcard property array with the image
*/
public function updateImageProperty(&$property, $entry, $version=null) {
$image = new \OC_Image();
$image->loadFromData($entry);
if (strcmp($version, '4.0') == 0) {
$type = $image->mimeType();
} else {
$arrayType = explode('/', $image->mimeType());
$type = strtoupper(array_pop($arrayType));
}
$property->add('ENCODING', 'b');
$property->add('TYPE', $type);
$property->setValue($image->__toString());
}
/**
* @brief returns the vcard property corresponding to the parameter
* creates the property if it doesn't exists yet
* @param $vcard the vcard to get or create the properties with
* @param $importEntry the parameter to find
* @return the property|false
*/
protected function getOrCreateVCardProperty(&$vcard, $importEntry) {
if (isset($vcard) && isset($importEntry)) {
// looking for a property with the same name
$properties = $vcard->select($importEntry['property']);
foreach ($properties as $property) {
if ($importEntry['type'] == null && !isset($importEntry->additional_property)) {
return $property;
}
foreach ($property->parameters as $parameter) {
// Filtering types
if ($parameter->name == 'TYPE' && !strcmp($parameter->value, $importEntry['type'])) {
$found=0;
if (isset($importEntry->additional_property)) {
// Filtering additional properties if necessary (I know, there are a lot of inner loops, sorry)
foreach($importEntry->additional_property as $additional_property) {
if ((string)$parameter->name == $additional_property['name']) {
$found++;
}
}
if ($found == count($importEntry->additional_property)) {
return $property;
}
}
return $property;
}
}
if (isset($importEntry['group']) && $property->group == $importEntry['group']) {
return $property;
}
}
// Property not found, creating one
$property = \Sabre\VObject\Property::create($importEntry['property']);
$vcard->add($property);
if ($importEntry['type']!=null) {
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($importEntry['type']));
switch ($importEntry['property']) {
case "ADR":
$property->setValue(";;;;;;");
break;
case "FN":
$property->setValue(";;;;");
break;
}
}
if ($importEntry['group']!=null) {
$property->group = $importEntry['group'];
}
return $property;
} else {
return false;
}
}
}
?>