-
Notifications
You must be signed in to change notification settings - Fork 127
/
Structure.php
255 lines (243 loc) · 7.98 KB
/
Structure.php
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Parses an object by relying on the MIME capabilities of the backend.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
/**
* Parses an object by relying on the MIME capabilities of the backend.
er.
*
* Copyright 2011 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.fsf.org/copyleft/lgpl.html LGPL
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Data_Parser_Structure
implements Horde_Kolab_Storage_Data_Parser
{
/**
* The backend driver.
*
* @param Horde_Kolab_Storage_Driver
*/
private $_driver;
/**
* The bridge between the backend object and the format parser.
*
* @param Horde_Kolab_Storage_Data_Format
*/
private $_format;
/**
* Constructor
*
* @param Horde_Kolab_Storage_Driver $driver The backend driver.
*/
public function __construct(
Horde_Kolab_Storage_Driver $driver
) {
$this->_driver = $driver;
}
/**
* Set the format handler.
*
* @param Horde_Kolab_Storage_Data_Format $format The data object <-> format
* bridge.
*
* @return NULL
*/
public function setFormat(Horde_Kolab_Storage_Data_Format $format)
{
$this->_format = $format;
}
/**
* Return the format handler.
*
* @return Horde_Kolab_Storage_Data_Format The data object <-> format
* bridge.
*/
public function getFormat()
{
if ($this->_format === null) {
throw new Horde_Kolab_Storage_Exception(
'The format handler has been left undefined!'
);
}
return $this->_format;
}
/**
* Fetches the objects for the specified backend IDs.
*
* @param string $folder The folder to access.
* @param array $obids The object backend IDs to fetch.
* @param array $options Additional options for fetching.
* <pre>
* 'type' - Required argument specifying the object type that should be
* parsed.
* 'version' - Optional argument specifying the version of the object
* format.
* </pre>
*
* @return array The objects.
*/
public function fetch($folder, $obids, $options = array())
{
$objects = array();
$this->_completeOptions($options);
$structures = $this->_driver->fetchStructure($folder, $obids);
foreach ($structures as $obid => $structure) {
if (!isset($structure['structure'])) {
throw new Horde_Kolab_Storage_Exception(
'Backend returned a structure without the expected "structure" element.'
);
}
//@todo: deal with exceptions
$objects[$obid] = $this->getFormat()->parse($folder, $obid, $structure['structure'], $options);
if ($this->_driver->hasCatenateSupport()) {
$objects[$obid]['__structure'] = $structure['structure'];
}
$this->_fetchAttachments($objects[$obid], $folder, $obid, $options);
}
return $objects;
}
/**
* Completes the given object with any required attachments.
*
* @param array $object The object to fetch attachments for.
* @param string $folder The folder to access.
* @param array $obid The object backend ID.
* @param array $options Additional options for fetching.
*
* @return NULL
*/
private function _fetchAttachments(array &$object, $folder, $obid, $options = array())
{
//@todo: implement
}
/**
* Fetch the specified mime part.
*
* @param string $folder The folder to access.
* @param string $obid The backend ID to parse from.
* @param string $mime_id The ID of the part that should be fetched.
*
* @return resource A stream for the specified body part.
*/
public function fetchId($folder, $obid, $mime_id)
{
return $this->_driver->fetchBodypart($folder, $obid, $mime_id);
}
/**
* Complete the options.
*
* @param array $options Options.
* <pre>
* 'type' - Required argument specifying the object type.
* 'version' - Optional argument specifying the version of the object
* format.
* </pre>
*
* @return NULL
*/
private function _completeOptions(&$options)
{
if (!isset($options['type'])) {
throw new Horde_Kolab_Storage_Exception(
'The object type must be specified!'
);
}
if (!isset($options['version'])) {
$options['version'] = 1;
}
}
/**
* Create a new object in the specified folder.
*
* @param string $folder The folder to use.
* @param array $object The object.
* @param array $options Additional options for storing.
* <pre>
* 'type' - Required argument specifying the object type that should be
* stored.
* 'version' - Optional argument specifying the version of the object
* format.
* </pre>
*
* @return string The ID of the new object or true in case the backend does
* not support this return value.
*/
public function create($folder, $object, $options = array())
{
return $this->_driver->appendMessage(
$folder,
$this->createObject($object, $options)
);
}
/**
* Modify an existing object in the specified folder.
*
* @param string $folder The folder to use.
* @param array $object The object.
* @param string $obid The object ID in the backend.
* @param array $options Additional options for storing.
* <pre>
* 'type' - Required argument specifying the object type that should be
* stored.
* 'version' - Optional argument specifying the version of the object
* format.
* </pre>
*
* @return string The ID of the modified object or true in case the backend
* does not support this return value.
*/
public function modify($folder, $object, $obid, $options = array())
{
$modifiable = $this->_driver->getModifiable($folder, $obid, $object);
$new_uid = $this->_format->modify($modifiable, $object, $options);
$this->_driver->deleteMessages($folder, array($obid));
return $new_uid;
}
/**
* Create a new MIME representation for the object.
*
* @param array $object The object.
* @param array $options Additional options for storing.
* <pre>
* 'type' - Required argument specifying the object type that should be
* stored.
* 'version' - Optional argument specifying the version of the object
* format.
* </pre>
*
* @return resource The MIME message representing the object.
*/
public function createObject($object, $options = array())
{
$this->_completeOptions($options);
$envelope = $this->_format->createEnvelope();
$envelope->addPart($this->_format->createKolabPart($object, $options));
return $envelope->toString(
array(
'canonical' => true,
'stream' => true,
'headers' => $this->_format->createEnvelopeHeaders(
$object['uid'],
$this->_driver->getAuth(),
$options['type']
)
)
);
}
}