summaryrefslogtreecommitdiffstats
path: root/apps/calendar/lib/export.php
blob: 8f26891f366cde9e4060be52496e84110eb6ebe5 (plain)
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
<?php
/**
 * Copyright (c) 2012 Georg Ehrke <ownclouddev@georgswebsite.de>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
/**
 * This class does export and converts all times to UTC
 */
class OC_Calendar_Export{
	/**
	 * @brief Use one of these constants as second parameter if you call OC_Calendar_Export::export()
	 */
	const CALENDAR = 'calendar';
	const EVENT = 'event';

	/**
	 * @brief export a calendar or an event
	 * @param integer $id id of calendar / event
	 * @param string $type use OC_Calendar_Export constants
	 * @return string
	 */
	public static function export($id, $type){
		if($type == self::EVENT){
			$return = self::event($id);
		}else{
			$return = self::calendar($id);
		}
		return self::fixLineBreaks($return);
	}

	/**
	 * @brief exports a calendar and convert all times to UTC
	 * @param integer $id id of the calendar
	 * @return string
	 */
	private static function calendar($id){
		$events = OC_Calendar_Object::all($id);
		$calendar = OC_Calendar_Calendar::find($id);
		$return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME:" . $calendar['displayname'] . "\n";
		foreach($events as $event){
			$return .= self::generateEvent($event);
		}
		$return .= "END:VCALENDAR";
		return $return;
	}
	
	/**
	 * @brief exports an event and convert all times to UTC
	 * @param integer $id id of the event
	 * @return string
	 */
	private static function event($id){
		$event = OC_Calendar_Object::find($id);
		$return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME:" . $event['summary'] . "\n";
		$return .= self::generateEvent($event);
		$return .= "END:VCALENDAR";
		return $return;
	 }
	 
	 /**
	  * @brief generates the VEVENT with UTC dates
	  * @param array $event
	  * @return string
	  */
	 private static function generateEvent($event){
		$object = OC_VObject::parse($event['calendardata']);
		$dtstart = $object->VEVENT->DTSTART;
		$start_dt = $dtstart->getDateTime();
		$dtend = OC_Calendar_Object::getDTEndFromVEvent($object->VEVENT);
		$end_dt = $dtend->getDateTime();
		if($dtstart->getDateType() !== Sabre_VObject_Element_DateTime::DATE){
			$start_dt->setTimezone(new DateTimeZone('UTC'));
			$end_dt->setTimezone(new DateTimeZone('UTC'));
			$object->VEVENT->setDateTime('DTSTART', $start_dt, Sabre_VObject_Property_DateTime::UTC);
			$object->VEVENT->setDateTime('DTEND', $end_dt, Sabre_VObject_Property_DateTime::UTC);
		}
		return $object->VEVENT->serialize();
	}
	
	/**
	 * @brief fixes new line breaks
	 * (fixes problems with Apple iCal)
	 * @param string $string to fix
	 * @return string 
	 */
	private static function fixLineBreaks($string){
		$string = str_replace("\r\n", "\n", $string);
		$string = str_replace("\r", "\n", $string);
		$string = str_replace("\n", "\r\n", $string);
		return $string;
	}
}