Skip to content

Commit

Permalink
Support exporting attached files in iCalendar.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Jan 11, 2016
1 parent 9fadb90 commit 95997b2
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions kronolith/lib/Event.php
Expand Up @@ -644,10 +644,12 @@ public function fromDriver($event)
*
* @param Horde_Icalendar $calendar A Horde_Icalendar object that acts as
* a container.
* @param boolean $includeFiles Include attached files in the iCalendar
* file? @since 4.3.0
*
* @return array An array of Horde_Icalendar_Vevent objects for this event.
*/
public function toiCalendar($calendar)
public function toiCalendar($calendar, $includeFiles = true)
{
$vEvent = Horde_Icalendar::newComponent('vevent', $calendar);
$v1 = $calendar->getAttribute('VERSION') == '1.0';
Expand Down Expand Up @@ -920,6 +922,25 @@ public function toiCalendar($calendar)
}
}

// Attached files
if ($includeFiles && count($this->listFiles())) {
$vfs = $this->vfsInit();
foreach ($this->listFiles() as $file) {
try {
$data = $vfs->read(Kronolith::VFS_PATH . '/' . $this->getVfsUid(), $file['name']);
} catch (Horde_Vfs_Exception $e) {
Horde::log($e->getMessage, 'ERR');
}
if ($data) {
try {
$vEvent->setAttribute('ATTACH', $data, array('FMTTYPE' => $file['type'], 'VALUE' => 'BINARY', 'ENCODING' => 'BASE64'));
} catch (Horde_Icalendar_Exception $e) {
Horde::log($e->getMessage(), 'ERR');
}
}
}
}

// Recurrence.
if ($this->recurs()) {
if ($v1) {
Expand Down Expand Up @@ -1274,6 +1295,41 @@ public function fromiCalendar($vEvent, $parseAttendees = false)
}
}

// Attached files, we require a UID so we can attach the file in VFS.
if ($this->uid) {
$attach = false;
$attach_params = array();
try {
$attach = $vEvent->getAttribute('ATTACH');
$attach_params = $vEvent->getAttribute('ATTACH', true);
} catch (Horde_Icalendar_Exception $e) {}
if (!is_array($attach)) {
$attach = array($attach);
}
foreach ($attach as $key => $attribute) {
if (isset($attach_params[$key]['VALUE']) &&
Horde_String::lower($attach_params[$key]['VALUE']) == 'uri') {
// @todo
} elseif (Horde_String::upper($attach_params[$key]['ENCODING']) == 'BASE64') {
$mime_type = !empty($attach_params[$key]['FMTTYPE'])
? $attach_params[$key]['FMTTYPE']
: '';
// @todo We really should add stream support to VFS
$file_data = base64_decode($attribute);
$vfs = $this->vfsInit();
$dir = Kronolith::VFS_PATH . '/' . $this->getVfsUid();
// @todo - Is there a way to get a filename from a non-uri
// ATTACH attribute??
$filename = sprintf(_("File %d.%s"), $key, Horde_Mime_Magic::mimeToExt($mime_type));
try {
$vfs->writeData($dir, $filename, $file_data, true);
} catch (Horde_Vfs_Exception $e) {
Horde::log($e->getMessage(), 'ERR');
}
}
}
}

// Attendance.
// Importing attendance may result in confusion: editing an imported
// copy of an event can cause invitation updates to be sent from
Expand Down Expand Up @@ -3759,7 +3815,9 @@ public function vfsInit()
{
if (!isset($this->_vfs)) {
try {
$this->_vfs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('documents');
$this->_vfs = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('documents');
} catch (Horde_Exception $e) {
throw new Kronolith_Exception($e);
}
Expand Down

0 comments on commit 95997b2

Please sign in to comment.