1: <?php
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: class Horde_Kolab_Storage_Data_Format_Mime
29: implements Horde_Kolab_Storage_Data_Format
30: {
31: 32: 33: 34: 35:
36: private $_factory;
37:
38: 39: 40: 41: 42:
43: private $_structure;
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: public function __construct(Horde_Kolab_Storage_Factory $factory,
54: Horde_Kolab_Storage_Data_Parser_Structure $structure)
55: {
56: $this->_factory = $factory;
57: $this->_structure = $structure;
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
70: public function parse($folder, $obid, $data, array $options)
71: {
72: if (!$data instanceOf Horde_Mime_Part) {
73: throw new Horde_Kolab_Storage_Exception(
74: sprintf(
75: 'The provided data is not of type Horde_Mime_Part but %s instead!',
76: get_class($data)
77: )
78: );
79: }
80: $mime_id = $this->matchMimeId($options['type'], $data->contentTypeMap());
81: if (empty($mime_id)) {
82: throw new Horde_Kolab_Storage_Exception(
83: sprintf(
84: 'Unable to identify Kolab mime part in message %s in folder %s!',
85: $obid,
86: $folder
87: )
88: );
89: }
90:
91: $mime_part = $data->getPart($mime_id);
92: if (empty($mime_part)) {
93: throw new Horde_Kolab_Storage_Exception(
94: sprintf(
95: 'Unable to identify Kolab mime part in message %s in folder %s!',
96: $obid,
97: $folder
98: )
99: );
100: }
101: $mime_part->setContents(
102: $this->_structure->fetchId($folder, $obid, $mime_id)
103: );
104: $content = $mime_part->getContents(array('stream' => true));
105: if (empty($options['raw'])) {
106: try {
107: return $this->_factory->createFormat('Xml', $options['type'], $options['version'])
108: ->load($content);
109: } catch (Horde_Kolab_Format_Exception $e) {
110: throw new Horde_Kolab_Storage_Exception(
111: sprintf(
112: 'Failed parsing Kolab object %s in folder %s: %s',
113: $obid,
114: $folder,
115: $e->getMessage()
116: ),
117: 0,
118: $e
119: );
120: }
121: } else {
122: return array('content' => $content);
123: }
124: }
125:
126: public function matchMimeId($type, $types)
127: {
128: switch ($type) {
129: case 'contact':
130: return array_search('application/x-vnd.kolab.contact', $types);
131: break;
132: case 'event':
133: return array_search('application/x-vnd.kolab.event', $types);
134: break;
135: case 'note':
136: return array_search('application/x-vnd.kolab.note', $types);
137: break;
138: case 'task':
139: return array_search('application/x-vnd.kolab.task', $types);
140: break;
141: case 'h-prefs':
142: return array_search('application/x-vnd.kolab.h-prefs', $types);
143: break;
144: case 'h-ledger':
145: return array_search('application/x-vnd.kolab.h-ledger', $types);
146: break;
147: default:
148: throw new Horde_Kolab_Storage_Exception(
149: sprintf('Unsupported object type %s!', $type)
150: );
151: }
152: }
153:
154: public function getMimeType($type)
155: {
156: switch ($type) {
157: case 'contact':
158: return 'application/x-vnd.kolab.contact';
159: case 'event':
160: return 'application/x-vnd.kolab.event';
161: case 'note':
162: return 'application/x-vnd.kolab.note';
163: case 'task':
164: return 'application/x-vnd.kolab.task';
165: case 'h-prefs':
166: return 'application/x-vnd.kolab.h-prefs';
167: case 'h-ledger':
168: return 'application/x-vnd.kolab.h-ledger';
169: default:
170: throw new Horde_Kolab_Storage_Exception(
171: sprintf('Unsupported object type %s!', $type)
172: );
173: }
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public function ($uid, $user, $type)
184: {
185: $headers = new Horde_Mime_Headers();
186: $headers->setEOL("\r\n");
187: $headers->addHeader('From', $user);
188: $headers->addHeader('To', $user);
189: $headers->addHeader('Date', date('r'));
190: $headers->addHeader('X-Kolab-Type', $this->getMimeType($type));
191: $headers->addHeader('Subject', $uid);
192: $headers->addHeader('User-Agent', 'Horde::Kolab::Storage v' . Horde_Kolab_Storage::VERSION);
193: $headers->addHeader('MIME-Version', '1.0');
194: return $headers;
195: }
196:
197: 198: 199: 200: 201:
202: public function createEnvelope()
203: {
204: $envelope = new Horde_Mime_Part();
205: $envelope->setName('Kolab Groupware Data');
206: $envelope->setType('multipart/mixed');
207: $description = new Horde_Mime_Part();
208: $description->setName('Kolab Groupware Information');
209: $description->setType('text/plain');
210: $description->setDisposition('inline');
211: $description->setCharset('utf-8');
212: $description->setContents(
213: Horde_String::wrap(
214: sprintf(
215: Horde_Kolab_Storage_Translation::t(
216: "This is a Kolab Groupware object. To view this object you will need an email client that understands the Kolab Groupware format. For a list of such email clients please visit %s"
217: ),
218: 'http://www.kolab.org/kolab2-clients.html'
219: ),
220: 76,
221: "\r\n"
222: ),
223: array('encoding' => 'quoted-printable')
224: );
225: $envelope->addPart($description);
226: $envelope->buildMimeIds();
227: return $envelope;
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: public function createKolabPart($object, array $options)
239: {
240: $kolab = new Horde_Mime_Part();
241: $kolab->setType($this->getMimeType($options['type']));
242: if (empty($options['raw'])) {
243: try {
244: $kolab->setContents(
245: $this->_factory->createFormat(
246: 'Xml', $options['type'], $options['version']
247: )->save($object),
248: array('encoding' => 'quoted-printable')
249: );
250: } catch (Horde_Kolab_Format_Exception $e) {
251: throw new Horde_Kolab_Storage_Exception(
252: 'Failed saving Kolab object!', 0, $e
253: );
254: }
255: } else {
256: $kolab->setContents(
257: $object['content'],
258: array('encoding' => 'quoted-printable')
259: );
260: }
261: $kolab->setCharset('utf-8');
262: $kolab->setDisposition('inline');
263: $kolab->setDispositionParameter('x-kolab-type', 'xml');
264: $kolab->setName('kolab.xml');
265: return $kolab;
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275: 276: 277:
278: public function modify(Horde_Kolab_Storage_Data_Modifiable $modifiable,
279: $object,
280: array $options)
281: {
282: $mime_id = $this->matchMimeId(
283: $options['type'], $modifiable->getStructure()->contentTypeMap()
284: );
285: $modifiable->setPart(
286: $mime_id, $this->createKolabPart($object, $options)
287: );
288: return $modifiable->store();
289: }
290: }
291: