Reworked ics handling by using sabre vobject

This results in more clean ics files, which for example respect spaces
which occur directly after a internal line break.
This commit is contained in:
Tim Schumacher 2016-06-02 16:01:44 +02:00
parent 7c30f8956e
commit b3a4836a10
10 changed files with 204 additions and 780 deletions

View file

@ -15,19 +15,8 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Hackspace\Bundle\CalciferBundle\Entity\Event;
use Hackspace\Bundle\CalciferBundle\Form\EventType;
use Symfony\Component\HttpFoundation\Response;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use
Sabre\VObject,
Sabre\CalDAV,
Sabre\DAV,
Sabre\DAVACL,
Sabre\DAV\Exception\Forbidden,
Hackspace\Bundle\CalciferBundle\libs\CalciferCaldavBackend,
Hackspace\Bundle\CalciferBundle\libs\CalciferPrincipalBackend;
use Sabre\VObject;
/**
* Event controller.
*
@ -35,45 +24,6 @@ use
*/
class EventController extends Controller
{
/**
* Finds and displays a Event entity.
*
* @Route("/{url}", name="events_caldav", requirements={"url" : "caldav(.+)"})
*/
public function caldavEntry()
{
// Backends
$calendarBackend = new CalciferCaldavBackend($this);
$principalBackend = new CalciferPrincipalBackend();
// Directory structure
$tree = [
new CalDAV\CalendarRootNode($principalBackend, $calendarBackend),
];
$server = new DAV\Server($tree);
$server->setBaseUri('/caldav');
/*$aclPlugin = new DAVACL\Plugin();
$aclPlugin->allowAccessToNodesWithoutACL = false;
$server->addPlugin($aclPlugin);*/
/* CalDAV support */
$caldavPlugin = new CalDAV\Plugin();
$server->addPlugin($caldavPlugin);
/* WebDAV-Sync plugin */
$server->addPlugin(new DAV\Sync\Plugin());
// Support for html frontend
$browser = new DAV\Browser\Plugin();
$server->addPlugin($browser);
// And off we go!
$server->exec();
return new Response();
}
/**
* Lists all Event entities as ICS.
*
@ -96,22 +46,14 @@ class EventController extends Controller
->setParameter('startdate', $now);
$entities = $qb->getQuery()->execute();
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
$vcalendar = new VObject\Component\VCalendar();
foreach ($entities as $entity) {
/** @var Event $entity */
$event = $entity->ConvertToCalendarEvent();
$calendar->addEvent($event);
/** @var Event $entity */
$vcalendar->add('VEVENT',$entity->ConvertToCalendarEvent());
}
$calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendarExport->addCalendar($calendar);
//output .ics formatted text
$result = $calendarExport->getStream();
$response = new Response($result);
$response = new Response($vcalendar->serialize());
$response->headers->set('Content-Type', 'text/calendar');
return $response;

View file

@ -16,12 +16,9 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Hackspace\Bundle\CalciferBundle\Entity\Event;
use Hackspace\Bundle\CalciferBundle\Form\EventType;
use Symfony\Component\HttpFoundation\Response;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use Symfony\Component\HttpFoundation\AcceptHeader;
use Sabre\VObject;
/**
* Location controller.
*
@ -68,24 +65,15 @@ class LocationController extends Controller
$entities = $qb->getQuery()->execute();
if ($format == 'ics') {
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
$vcalendar = new VObject\Component\VCalendar();
foreach ($entities as $entity) {
/** @var Event $entity */
$event = $entity->ConvertToCalendarEvent();
$calendar->addEvent($event);
/** @var Event $entity */
$vcalendar->add('VEVENT',$entity->ConvertToCalendarEvent());
}
$calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendarExport->addCalendar($calendar);
//output .ics formatted text
$result = $calendarExport->getStream();
$response = new Response($result);
$response = new Response($vcalendar->serialize());
$response->headers->set('Content-Type', 'text/calendar');
return $response;
} else {
return array(

View file

@ -7,7 +7,6 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag;
use Jsvrcek\ICS\Model\Description\Geo;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@ -16,18 +15,12 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Hackspace\Bundle\CalciferBundle\Entity\Event;
use Hackspace\Bundle\CalciferBundle\Form\EventType;
use Symfony\Component\HttpFoundation\Response;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Model\Relationship\Attendee;
use Jsvrcek\ICS\Model\Relationship\Organizer;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use Symfony\Component\Validator\Constraints\DateTime;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Symfony\Component\HttpFoundation\AcceptHeader;
use Sabre\VObject;
/**
* Tag controller.
*
@ -143,23 +136,14 @@ EOF;
}
if ($format == 'ics') {
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
$calendar->setTimeZone(new \DateTimeZone('Europe/Berlin'));
$vcalendar = new VObject\Component\VCalendar();
foreach ($entities as $entity) {
/** @var Event $entity */
$event = $entity->ConvertToCalendarEvent();
$calendar->addEvent($event);
/** @var Event $entity */
$vcalendar->add('VEVENT',$entity->ConvertToCalendarEvent());
}
$calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendarExport->addCalendar($calendar);
//output .ics formatted text
$result = $calendarExport->getStream();
$response = new Response($result);
$response = new Response($vcalendar->serialize());
$response->headers->set('Content-Type', 'text/calendar');
return $response;

View file

@ -5,17 +5,7 @@ namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Jsvrcek\ICS\Model\Description\Location As EventLocation;
use Symfony\Component\Validator\Constraints\DateTime;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Model\Relationship\Attendee;
use Jsvrcek\ICS\Model\Relationship\Organizer;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\Model\Description\Geo;
/**
* Event
@ -144,32 +134,30 @@ class Event extends BaseEntity
public function ConvertToCalendarEvent() {
$event = new CalendarEvent();
$event->setStart($this->startdate);
if ($this->enddate instanceof \DateTime)
$event->setEnd($this->enddate);
$event->setSummary($this->summary);
$event->setUrl($this->url);
$uid = sprintf("https://%s/termine/%s",$_SERVER['HTTP_HOST'],$this->slug);
$event->setUid($uid);
if (count($this->tags) > 0) {
$categories = [];
foreach($this->tags as $tag) {
$event->addCategory($tag->name);
}
$categories = [];
foreach($this->tags as $tag) {
$categories[] = $tag->name;
}
$event = [
"VEVENT" => [
'SUMMARY' => $this->summary,
'DTSTART' => $this->startdate,
'DESCRIPTION' => $this->description,
'URL' => $this->url,
'CATEGORIES' => $categories,
]
];
if (!is_null($this->enddate))
$event["VEVENT"]["DTEND"] = $this->enddate;
if ($this->location instanceof Location) {
$location = new EventLocation();
$location->setName($this->location->name);
$event->setLocations([$location]);
$event["VEVENT"]["LOCATION"] = $this->location->name;
if (\is_float($this->location->lon) && \is_float($this->location->lat)) {
$geo = new Geo();
$geo->setLatitude($this->location->lat);
$geo->setLongitude($this->location->lon);
$event->setGeo($geo);
$event["VEVENT"]["GEO"] = [$this->location->lat, $this->location->lon];
}
}
$event->setDescription($this->description);
return $event;
}
}

View file

@ -1,312 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: tim
* Date: 15.11.14
* Time: 17:13
*/
namespace Hackspace\Bundle\CalciferBundle\libs;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Utility\Formatter;
use Sabre\CalDAV\Backend\AbstractBackend;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Event;
class CalciferCaldavBackend extends AbstractBackend
{
/** @var Controller */
private $controller = null;
function __construct(Controller $controller)
{
$this->controller = $controller;
}
/**
* Returns a list of calendars for a principal.
*
* Every project is an array with the following keys:
* * id, a unique id that will be used by other functions to modify the
* calendar. This can be the same as the uri or a database key.
* * uri, which the basename of the uri with which the calendar is
* accessed.
* * principaluri. The owner of the calendar. Almost always the same as
* principalUri passed to this method.
*
* Furthermore it can contain webdav properties in clark notation. A very
* common one is '{DAV:}displayname'.
*
* Many clients also require:
* {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
* For this property, you can just return an instance of
* Sabre\CalDAV\Property\SupportedCalendarComponentSet.
*
* @param string $principalUri
* @return array
*/
public function getCalendarsForUser($principalUri)
{
return [[
'id' => 1,
'uri' => 'calendar',
'principaluri' => '/caldav/calcifer',
]];
}
/**
* Creates a new calendar for a principal.
*
* If the creation was a success, an id must be returned that can be used to reference
* this calendar in other methods, such as updateCalendar.
*
* @param string $principalUri
* @param string $calendarUri
* @param array $properties
* @return void
*/
public function createCalendar($principalUri, $calendarUri, array $properties)
{
throw new \Exception('Not implemented');
}
/**
* Delete a calendar and all it's objects
*
* @param mixed $calendarId
* @return void
*/
public function deleteCalendar($calendarId)
{
throw new \Exception('Not implemented');
}
private function FormatCalendarEvent(CalendarEvent $event)
{
$stream = new CalendarStream();
$formatter = new Formatter();
$stream->addItem('BEGIN:VEVENT')
->addItem('UID:' . $event->getUid())
->addItem('DTSTART:' . $formatter->getFormattedUTCDateTime($event->getStart()))
->addItem('DTEND:' . $formatter->getFormattedUTCDateTime($event->getEnd()))
->addItem('SUMMARY:' . $event->getSummary())
->addItem('DESCRIPTION:' . $event->getDescription());
if ($event->getClass())
$stream->addItem('CLASS:' . $event->getClass());
/* @var $location Location */
foreach ($event->getLocations() as $location) {
$stream
->addItem('LOCATION' . $location->getUri() . $location->getLanguage() . ':' . $location->getName());
}
if ($event->getGeo())
$stream->addItem('GEO:' . $event->getGeo()->getLatitude() . ';' . $event->getGeo()->getLongitude());
if ($event->getUrl())
$stream->addItem('URL:' . $event->getUrl());
if ($event->getCreated())
$stream->addItem('CREATED:' . $formatter->getFormattedUTCDateTime($event->getCreated()));
if ($event->getLastModified())
$stream->addItem('LAST-MODIFIED:' . $formatter->getFormattedUTCDateTime($event->getLastModified()));
foreach ($event->getAttendees() as $attendee) {
$stream->addItem($attendee->__toString());
}
if ($event->getOrganizer())
$stream->addItem($event->getOrganizer()->__toString());
$stream->addItem('END:VEVENT');
return $stream->getStream();
}
private function formatEvent(Event $event)
{
/** @var CalendarEvent $calendar_event */
$calendar_event = $event->ConvertToCalendarEvent();
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
$calendar->addEvent($calendar_event);
$calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendarExport->addCalendar($calendar);
//output .ics formatted text
$calendar_data = $calendarExport->getStream();
$event_data = [
'id' => $event->id,
'uri' => $event->slug . '.ics',
'lastmodified' => $event->startdate,
'etag' => '"' . sha1($calendar_data) . '"',
'calendarid' => 1,
'calendardata' => $calendar_data,
'size' => strlen($calendar_data),
'component' => 'VEVENT',
];
return $event_data;
}
/**
* Returns all calendar objects within a calendar.
*
* Every item contains an array with the following keys:
* * id - unique identifier which will be used for subsequent updates
* * calendardata - The iCalendar-compatible calendar data
* * uri - a unique key which will be used to construct the uri. This can
* be any arbitrary string, but making sure it ends with '.ics' is a
* good idea. This is only the basename, or filename, not the full
* path.
* * lastmodified - a timestamp of the last modification time
* * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
* '"abcdef"')
* * calendarid - The calendarid as it was passed to this function.
* * size - The size of the calendar objects, in bytes.
* * component - optional, a string containing the type of object, such
* as 'vevent' or 'vtodo'. If specified, this will be used to populate
* the Content-Type header.
*
* Note that the etag is optional, but it's highly encouraged to return for
* speed reasons.
*
* The calendardata is also optional. If it's not returned
* 'getCalendarObject' will be called later, which *is* expected to return
* calendardata.
*
* If neither etag or size are specified, the calendardata will be
* used/fetched to determine these numbers. If both are specified the
* amount of times this is needed is reduced by a great degree.
*
* @param mixed $calendarId
* @return array
*/
public function getCalendarObjects($calendarId)
{
/** @var EntityManager $em */
$em = $this->controller->getDoctrine()->getManager();
$now = new \DateTime();
$now->setTime(0, 0, 0);
/** @var QueryBuilder $qb */
$qb = $em->createQueryBuilder();
$qb->select(array('e'))
->from('CalciferBundle:Event', 'e')
->orderBy('e.startdate');
$entities = $qb->getQuery()->execute();
if (count($entities) > 0) {
$events = [];
foreach ($entities as $event) {
/** @var Event $event */
$events[] = $this->formatEvent($event);
}
return $events;
}
}
/**
* Returns information from a single calendar object, based on it's object
* uri.
*
* The object uri is only the basename, or filename and not a full path.
*
* The returned array must have the same keys as getCalendarObjects. The
* 'calendardata' object is required here though, while it's not required
* for getCalendarObjects.
*
* This method must return null if the object did not exist.
*
* @param mixed $calendarId
* @param string $objectUri
* @return array|null
*/
public function getCalendarObject($calendarId, $objectUri)
{
/** @var EntityManager $em */
$em = $this->controller->getDoctrine()->getManager();
/** @var EntityRepository $repo */
$repo = $em->getRepository('CalciferBundle:Event');
/** @var Event $entity */
$event = $repo->findOneBy(['slug' => substr($objectUri,0,strlen($objectUri) - 4)]);
if (!($event instanceof Event)) {
throw $this->controller->createNotFoundException('Unable to find Event entity.');
}
return $this->formatEvent($event);
}
/**
* Creates a new calendar object.
*
* The object uri is only the basename, or filename and not a full path.
*
* It is possible return an etag from this function, which will be used in
* the response to this PUT request. Note that the ETag must be surrounded
* by double-quotes.
*
* However, you should only really return this ETag if you don't mangle the
* calendar-data. If the result of a subsequent GET to this object is not
* the exact same as this request body, you should omit the ETag.
*
* @param mixed $calendarId
* @param string $objectUri
* @param string $calendarData
* @return string|null
*/
public function createCalendarObject($calendarId, $objectUri, $calendarData)
{
// TODO: Implement createCalendarObject() method.
}
/**
* Updates an existing calendarobject, based on it's uri.
*
* The object uri is only the basename, or filename and not a full path.
*
* It is possible return an etag from this function, which will be used in
* the response to this PUT request. Note that the ETag must be surrounded
* by double-quotes.
*
* However, you should only really return this ETag if you don't mangle the
* calendar-data. If the result of a subsequent GET to this object is not
* the exact same as this request body, you should omit the ETag.
*
* @param mixed $calendarId
* @param string $objectUri
* @param string $calendarData
* @return string|null
*/
public function updateCalendarObject($calendarId, $objectUri, $calendarData)
{
throw new \Exception('Not implemented');
}
/**
* Deletes an existing calendar object.
*
* The object uri is only the basename, or filename and not a full path.
*
* @param mixed $calendarId
* @param string $objectUri
* @return void
*/
public function deleteCalendarObject($calendarId, $objectUri)
{
throw new \Exception('Not implemented');
}
}

View file

@ -1,158 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: tim
* Date: 15.11.14
* Time: 19:45
*/
namespace Hackspace\Bundle\CalciferBundle\libs;
use
Sabre\DAV,
Sabre\DAVACL,
Sabre\HTTP\URLUtil;
class CalciferPrincipalBackend extends DAVACL\PrincipalBackend\AbstractBackend
{
/**
* Returns a list of principals based on a prefix.
*
* This prefix will often contain something like 'principals'. You are only
* expected to return principals that are in this base path.
*
* You are expected to return at least a 'uri' for every user, you can
* return any additional properties if you wish so. Common properties are:
* {DAV:}displayname
* {http://sabredav.org/ns}email-address - This is a custom SabreDAV
* field that's actually injected in a number of other properties. If
* you have an email address, use this property.
*
* @param string $prefixPath
* @return array
*/
function getPrincipalsByPrefix($prefixPath)
{
return [
[
'{DAV:}displayname' => 'calcifer',
'{http://sabredav.org/ns}email-address' => 'calcifer@example.com',
'uri' => '/caldav/calcifer',
]
];
}
/**
* Returns a specific principal, specified by it's path.
* The returned structure should be the exact same as from
* getPrincipalsByPrefix.
*
* @param string $path
* @return array
*/
function getPrincipalByPath($path)
{
return [
'{DAV:}displayname' => 'calcifer',
'{http://sabredav.org/ns}email-address' => 'calcifer@example.com',
'uri' => '/caldav/calcifer',
];
}
/**
* Updates one ore more webdav properties on a principal.
*
* The list of mutations is stored in a Sabre\DAV\PropPatch object.
* To do the actual updates, you must tell this object which properties
* you're going to process with the handle() method.
*
* Calling the handle method is like telling the PropPatch object "I
* promise I can handle updating this property".
*
* Read the PropPatch documenation for more info and examples.
*
* @param string $path
* @param \Sabre\DAV\PropPatch $propPatch
* @return void
*/
function updatePrincipal($path, \Sabre\DAV\PropPatch $propPatch)
{
throw new \Exception('Not implemented');
}
/**
* This method is used to search for principals matching a set of
* properties.
*
* This search is specifically used by RFC3744's principal-property-search
* REPORT. You should at least allow searching on
* http://sabredav.org/ns}email-address.
*
* The actual search should be a unicode-non-case-sensitive search. The
* keys in searchProperties are the WebDAV property names, while the values
* are the property values to search on.
*
* If multiple properties are being searched on, the search should be
* AND'ed.
*
* This method should simply return an array with full principal uri's.
*
* If somebody attempted to search on a property the backend does not
* support, you should simply return 0 results.
*
* You can also just return 0 results if you choose to not support
* searching at all, but keep in mind that this may stop certain features
* from working.
*
* @param string $prefixPath
* @param array $searchProperties
* @return array
*/
function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof')
{
return [
[
'{DAV:}displayname' => 'calcifer',
'{http://sabredav.org/ns}email-address' => 'calcifer@example.com',
'uri' => '/caldav/calcifer',
]
];
}
/**
* Returns the list of members for a group-principal
*
* @param string $principal
* @return array
*/
function getGroupMemberSet($principal)
{
throw new \Exception('Not implemented');
}
/**
* Returns the list of groups a principal is a member of
*
* @param string $principal
* @return array
*/
function getGroupMembership($principal)
{
throw new \Exception('Not implemented');
}
/**
* Updates the list of group members for a group principal.
*
* The principals should be passed as a list of uri's.
*
* @param string $principal
* @param array $members
* @return void
*/
function setGroupMemberSet($principal, array $members)
{
throw new \Exception('Not implemented');
}
}