|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud - CSV Import connector |
|
4
|
|
|
* |
|
5
|
|
|
* @author Nicolas Mora |
|
6
|
|
|
* @copyright 2013-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\StringUtil; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Abstract class used to implement import classes |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class ImportConnector { |
|
31
|
|
|
|
|
32
|
|
|
// XML Configuration, class SimpleXml format |
|
33
|
|
|
protected $configContent; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \SimpleXMLElement $xml_config |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($xml_config = null) { |
|
|
|
|
|
|
39
|
|
|
if ($xml_config != null) { |
|
40
|
|
|
$this->setConfig($xml_config); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// returns a table containing converted elements from the input file |
|
45
|
|
|
abstract function getElementsFromInput($input, $limit=-1); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
// returns a single converted element |
|
48
|
|
|
abstract function convertElementToVCard($element); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// returns the probability that the file matchs the current format |
|
51
|
|
|
abstract function getFormatMatch($file); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function setConfig($xml_config) { |
|
|
|
|
|
|
54
|
|
|
$this->configContent = $xml_config; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @brief updates a property given in parameter with the value and using the importEntry to set the different parameters |
|
59
|
|
|
* @param $property the property to update |
|
60
|
|
|
* @param $importEntry the entry configuration to update in SimpleXml format |
|
61
|
|
|
* @value the value to update |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function updateProperty(&$property, $importEntry, $value) { |
|
64
|
|
|
if (isset($property) && isset($importEntry) && isset($value)) { |
|
65
|
|
|
if (isset($importEntry->vcard_entry)) { |
|
66
|
|
|
if (isset($importEntry->vcard_entry['type'])) { |
|
67
|
|
|
$property->add('TYPE', StringUtil::convertToUTF8($importEntry->vcard_entry['type'])); |
|
68
|
|
|
} |
|
69
|
|
|
if (isset($importEntry->vcard_entry->additional_property)) { |
|
70
|
|
|
foreach ($importEntry->vcard_entry->additional_property as $additionalProperty) { |
|
71
|
|
|
$property->add($additionalProperty['name'], $additionalProperty['value']); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
if (isset($importEntry->vcard_entry['prefix'])) { |
|
75
|
|
|
$value = $importEntry->vcard_entry['prefix'].$value; |
|
76
|
|
|
} |
|
77
|
|
|
if (isset($importEntry->vcard_entry['group'])) { |
|
78
|
|
|
$property->group = $importEntry->vcard_entry['group']; |
|
79
|
|
|
} |
|
80
|
|
|
if (isset($importEntry->vcard_entry['position'])) { |
|
81
|
|
|
$separator=";"; |
|
|
|
|
|
|
82
|
|
|
if (isset($importEntry->vcard_entry['separator'])) { |
|
83
|
|
|
$separator=$importEntry->vcard_entry['separator']; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
$position = $importEntry->vcard_entry['position']; |
|
86
|
|
|
$vArray = $property->getParts(); |
|
87
|
|
|
$vArray[intval($position)] = StringUtil::convertToUTF8($value); |
|
88
|
|
|
$property->setParts($vArray); |
|
89
|
|
|
} else { |
|
90
|
|
|
if (isset($importEntry->vcard_entry['value'])) { |
|
91
|
|
|
$property->add('TYPE', StringUtil::convertToUTF8($value)); |
|
92
|
|
|
} else { |
|
93
|
|
|
$curVal = $property->getParts(); |
|
94
|
|
|
$curVal[] = StringUtil::convertToUTF8($value); |
|
95
|
|
|
$property->setValue($curVal); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
if (isset($importEntry->vcard_parameter)) { |
|
100
|
|
|
$property->add($importEntry->vcard_parameter['parameter'], StringUtil::convertToUTF8($value)); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @brief modifies a vcard property array with the image |
|
107
|
|
|
*/ |
|
108
|
|
|
public function updateImageProperty(&$property, $entry, $version=null) { |
|
109
|
|
|
$image = new \OC_Image(); |
|
110
|
|
|
$image->loadFromData($entry); |
|
111
|
|
View Code Duplication |
if (strcmp($version, '4.0') == 0) { |
|
|
|
|
|
|
112
|
|
|
$type = $image->mimeType(); |
|
113
|
|
|
} else { |
|
114
|
|
|
$arrayType = explode('/', $image->mimeType()); |
|
115
|
|
|
$type = strtoupper(array_pop($arrayType)); |
|
116
|
|
|
} |
|
117
|
|
|
$property->add('ENCODING', 'b'); |
|
118
|
|
|
$property->add('TYPE', $type); |
|
119
|
|
|
$property->setValue($image->__toString()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @brief returns the vcard property corresponding to the parameter |
|
124
|
|
|
* creates the property if it doesn't exists yet |
|
125
|
|
|
* @param $vcard the vcard to get or create the properties with |
|
126
|
|
|
* @param $importEntry the parameter to find |
|
127
|
|
|
* @return the property|false |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getOrCreateVCardProperty(&$vcard, $importEntry) { |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
if (isset($vcard) && isset($importEntry)) { |
|
132
|
|
|
// looking for a property with the same name |
|
133
|
|
|
$properties = $vcard->select($importEntry['property']); |
|
134
|
|
|
foreach ($properties as $property) { |
|
135
|
|
|
if ($importEntry['type'] == null && !isset($importEntry->additional_property)) { |
|
136
|
|
|
return $property; |
|
137
|
|
|
} |
|
138
|
|
|
foreach ($property->parameters as $parameter) { |
|
139
|
|
|
// Filtering types |
|
140
|
|
|
if ($parameter->name == 'TYPE' && !strcmp($parameter->getValue(), $importEntry['type'])) { |
|
141
|
|
|
$found=0; |
|
142
|
|
|
if (isset($importEntry->additional_property)) { |
|
143
|
|
|
// Filtering additional properties if necessary (I know, there are a lot of inner loops, sorry) |
|
144
|
|
|
foreach($importEntry->additional_property as $additional_property) { |
|
145
|
|
|
if ((string)$parameter->name == $additional_property['name']) { |
|
146
|
|
|
$found++; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
if ($found == count($importEntry->additional_property)) { |
|
150
|
|
|
return $property; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
return $property; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (isset($importEntry['group']) && $property->group == $importEntry['group']) { |
|
158
|
|
|
return $property; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Property not found, creating one |
|
163
|
|
|
$property = $vcard->createProperty($importEntry['property']); |
|
164
|
|
|
$vcard->add($property); |
|
165
|
|
|
if ($importEntry['type']!=null) { |
|
166
|
|
|
$property->add('TYPE', StringUtil::convertToUTF8($importEntry['type'])); |
|
167
|
|
|
} |
|
168
|
|
|
switch ($importEntry['property']) { |
|
169
|
|
|
case "ADR": |
|
170
|
|
|
$property->setValue(array('', '', '', '', '', '', '')); |
|
171
|
|
|
break; |
|
172
|
|
|
case "N": |
|
173
|
|
|
$property->setValue(array('', '', '', '', '')); |
|
174
|
|
|
break; |
|
175
|
|
|
} |
|
176
|
|
|
if ($importEntry['group']!=null) { |
|
177
|
|
|
$property->group = $importEntry['group']; |
|
178
|
|
|
} |
|
179
|
|
|
return $property; |
|
180
|
|
|
} else { |
|
181
|
|
|
return false; |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
?> |
|
|
|
|
|
|
187
|
|
|
|