jsonserializer.php
6.57 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* ownCloud - JSONSerializer
*
* @author Thomas Tanghus, Jakob Sack
* @copyright 2011 Jakob Sack mail@jakobsack.de
* @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
*
* 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; either
* version 3 of the License, or any later version.
*
* 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\Utils;
use OCA\Contacts\VObject;
use OCA\Contacts\Contact,
OCA\Contacts\Utils\Properties;
/**
* This class serializes properties, components an
* arrays of components into a format suitable for
* passing to a JSON response.
* TODO: Return jCard (almost) compliant data, but still omitting unneeded data.
* http://tools.ietf.org/html/draft-kewisch-vcard-in-json-01
*/
class JSONSerializer {
/**
* General method serialize method. Use this for arrays
* of contacts.
*
* @param Contact[] $input
* @return array
*/
public static function serialize($input) {
$response = array();
if(is_array($input)) {
foreach($input as $object) {
if($object instanceof Contact) {
\OCP\Util::writeLog('contacts', __METHOD__.' serializing: ' . print_r($object, true), \OCP\Util::DEBUG);
$tmp = self::serializeContact($object);
if($tmp !== null) {
$response[] = $tmp;
}
} else {
throw new \Exception(
'Only arrays of OCA\\Contacts\\VObject\\VCard '
. 'and Sabre\VObject\Property are accepted.'
);
}
}
} else {
if($input instanceof VObject\VCard) {
return self::serializeContact($input);
} elseif($input instanceof Sabre\VObject\Property) {
return self::serializeProperty($input);
} else {
throw new \Exception(
'Only instances of OCA\\Contacts\\VObject\\VCard '
. 'and Sabre\VObject\Property are accepted.'
);
}
}
return $response;
}
/**
* @brief Data structure of vCard
* @param VObject\VCard $contact
* @return associative array|null
*/
public static function serializeContact(Contact $contact) {
if(!$contact->retrieve()) {
\OCP\Util::writeLog('contacts', __METHOD__.' error reading: ' . print_r($contact, true), \OCP\Util::DEBUG);
return null;
}
$details = array();
if(isset($contact->PHOTO) || isset($contact->LOGO)) {
$details['thumbnail'] = Properties::cacheThumbnail(
$contact->getBackend()->name,
$contact->getParent()->getId(),
$contact->getId(),
null,
$contact
);
}
foreach($contact->children as $property) {
$pname = $property->name;
$temp = self::serializeProperty($property);
if(!is_null($temp)) {
// Get Apple X-ABLabels
if(isset($contact->{$property->group . '.X-ABLABEL'})) {
$temp['label'] = $contact->{$property->group . '.X-ABLABEL'}->value;
if($temp['label'] == '_$!<Other>!$_') {
$temp['label'] = Properties::$l10n->t('Other');
}
if($temp['label'] == '_$!<HomePage>!$_') {
$temp['label'] = Properties::$l10n->t('HomePage');
}
}
if(array_key_exists($pname, $details)) {
$details[$pname][] = $temp;
}
else{
$details[$pname] = array($temp);
}
}
}
return array('data' =>$details, 'metadata' => $contact->getMetaData());
}
/**
* @brief Get data structure of property.
* @param \Sabre\VObject\Property $property
* @return associative array
*
* returns an associative array with
* ['name'] name of property
* ['value'] htmlspecialchars escaped value of property
* ['parameters'] associative array name=>value
* ['checksum'] checksum of whole property
* NOTE: $value is not escaped anymore. It shouldn't make any difference
* but we should look out for any problems.
*/
public static function serializeProperty(\Sabre\VObject\Property $property) {
if(!in_array($property->name, Properties::$index_properties)) {
return;
}
$value = $property->value;
if($property->name == 'ADR' || $property->name == 'N' || $property->name == 'ORG' || $property->name == 'CATEGORIES') {
$value = $property->getParts();
$value = array_map('trim', $value);
}
elseif($property->name == 'BDAY') {
if(strpos($value, '-') === false) {
if(strlen($value) >= 8) {
$value = substr($value, 0, 4).'-'.substr($value, 4, 2).'-'.substr($value, 6, 2);
} else {
return null; // Badly malformed :-(
}
}
} elseif($property->name == 'PHOTO') {
$value = true;
}
elseif($property->name == 'IMPP') {
if(strpos($value, ':') !== false) {
$value = explode(':', $value);
$protocol = array_shift($value);
if(!isset($property['X-SERVICE-TYPE'])) {
$property['X-SERVICE-TYPE'] = strtoupper($protocol);
}
$value = implode('', $value);
}
}
if(is_string($value)) {
$value = strtr($value, array('\,' => ',', '\;' => ';'));
}
$temp = array(
//'name' => $property->name,
'value' => $value,
'parameters' => array()
);
// This cuts around a 3rd off of the json response size.
if(in_array($property->name, Properties::$multi_properties)) {
$temp['checksum'] = substr(md5($property->serialize()), 0, 8);
}
foreach($property->parameters as $parameter) {
// Faulty entries by kaddressbook
// Actually TYPE=PREF is correct according to RFC 2426
// but this way is more handy in the UI. Tanghus.
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
$parameter->name = 'PREF';
$parameter->value = '1';
}
// NOTE: Apparently Sabre_VObject_Reader can't always deal with value list parameters
// like TYPE=HOME,CELL,VOICE. Tanghus.
// TODO: Check if parameter is has commas and split + merge if so.
if ($parameter->name == 'TYPE') {
$pvalue = $parameter->value;
if(is_string($pvalue) && strpos($pvalue, ',') !== false) {
$pvalue = array_map('trim', explode(',', $pvalue));
}
$pvalue = is_array($pvalue) ? $pvalue : array($pvalue);
if (isset($temp['parameters'][$parameter->name])) {
$temp['parameters'][$parameter->name][] = \OCP\Util::sanitizeHTML($pvalue);
}
else {
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($pvalue);
}
}
else{
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($parameter->value);
}
}
return $temp;
}
}