importmanager.php
4.58 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
<?php
/**
* ownCloud - Import manager
*
* @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;
use Sabre\VObject\Component;
use OCA\Contacts\Connector\ImportCsvConnector;
use OCA\Contacts\Connector\ImportVCardConnector;
use OCA\Contacts\Connector\ImportLdifConnector;
use OCA\Contacts\Addressbook;
/**
* Manages the import with basic functionalities
*/
class ImportManager {
private function loadXmlFile($path) {
if (file_exists($path)) {
$format = simplexml_load_file ( $path );
if ($format) {
if (isset($format->import_core)
&& isset($format->import_core->name)
&& isset($format->import_core->display_name)
&& isset($format->import_core->type)
&& isset($format->import_core->active)
&& $format->import_core->active == '1') {
return $format;
}
}
}
return false;
}
/**
* @brief return the different import formats available by scanning the contacts/formats folder
* @return array(string, string)
*/
public function getTypes() {
$prefix = "import_";
$suffix = "_connector.xml";
$path = __DIR__ . "/../formats/";
$files = scandir($path);
$formats = array();
foreach ($files as $file) {
if (!strncmp($file, $prefix, strlen($prefix)) && substr($file, - strlen($suffix)) === $suffix) {
$format = $this->loadXmlFile(realpath($path.$file));
if ($format) {
$formats[(string)$format->import_core->name] = (string)$format->import_core->display_name;
}
}
}
return $formats;
}
/**
* @brief get all the preferences for the addressbook
* @param string $id
* @return SimpleXml
*/
public function getType($typeName) {
$path = __DIR__ . "/../formats/import_" . $typeName . "_connector.xml";
return $this->loadXmlFile($path);
}
/**
* @brief imports the file with the selected type, and converts into VCards
* @param $file the path to the file
* @param $typeName the type name to use as stored into the app settings
* @param $limit the number of elements to import
* @return an array containing VCard elements|false if empty of error
*/
public function importFile($file, $typeName, $limit=-1) {
\OCP\Util::writeLog('contacts import manager', __METHOD__.' importing as '.$typeName, \OCP\Util::INFO);
$connector = $this->getConnector($typeName);
if ($connector) {
$elements = $connector->getElementsFromInput($file, $limit);
if (count($elements) > 0) {
return $elements;
} else {
return false;
}
} else {
return false;
}
}
public function getConnector($type) {
$importType = $this->getType($type);
$elements = array();
if (!$importType) {
return false;
}
if ((string)$importType->import_core->type == 'csv') {
// use class ImportCsvConnector
return new ImportCsvConnector($importType);
} else if ((string)$importType->import_core->type == 'vcard') {
// use class importVcardConnector
return new ImportVCardConnector($importType);
} else if ((string)$importType->import_core->type == 'ldif') {
// use class importVcardConnector
return new ImportLdifConnector($importType);
}
return false;
}
/**
* @brief import the first element of the file with all the types
* detects wich imported type has the least elements "X-Unknown-Element"
* then returns the corresponding type
* @param $file the path to the file
* @return array containing the probability for each format
*/
public function detectFileType($file) {
$types = $this->getTypes();
$probability = array();
foreach ($types as $type => $description) {
$connector = $this->getConnector($type);
if ($connector) {
$probability[$type] = $connector->getFormatMatch($file);
}
}
return $probability;
}
/**
* @brief get the raw entries from the input file
* @param $file the path to the file
* @param $limit the maximum number of entries to return (-1: no limit)
* @return array|false
*/
public function getEntries($file, $limit=-1) {
return $connector->getElementsFromInput($file, $limit);
}
}
?>