Blame view

sources/apps/calendar/lib/export.php 3.46 KB
03e52840d   Kload   Init
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
  <?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
  VERSION:2.0
  PRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "
  X-WR-CALNAME:" . $calendar['displayname'] . "
  ";
  		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
  VERSION:2.0
  PRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "
  X-WR-CALNAME:" . $event['summary'] . "
  ";
  		$return .= self::generateEvent($event);
  		$return .= "END:VCALENDAR";
  		return $return;
  	 }
  
  	 /**
  	  * @brief generates the VEVENT/VTODO/VJOURNAL with UTC dates
  	  * @param array $event
  	  * @return string
  	  */
  	 private static function generateEvent($event) {
  	 	$object = OC_VObject::parse($event['calendardata']);
  		if(!$object){
  			return false;
  		}
  
  		$sharedAccessClassPermissions = OC_Calendar_Object::getAccessClassPermissions($object);
  		if(OC_Calendar_Object::getowner($event['id']) !== OCP\User::getUser()){
  			if (!($sharedAccessClassPermissions & OCP\PERMISSION_READ)) {
  				return '';
  			}
  		}
  		$object = OC_Calendar_Object::cleanByAccessClass($event['id'], $object);
  
  		if($object->VEVENT){
  			$dtstart = $object->VEVENT->DTSTART;
  			$start_dt = $dtstart->getDateTime();
  			$dtend = OC_Calendar_Object::getDTEndFromVEvent($object->VEVENT);
  			$end_dt = $dtend->getDateTime();
  			if($dtstart->getDateType() !== Sabre\VObject\Property\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();
  		}
  		if($object->VTODO){
  			return $object->VTODO->serialize();
  		}
  		if($object->VJOURNAL){
  			return $object->VJOURNAL->serialize();
  		}
  		return '';
  	}
  
  	/**
  	 * @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
  ", "
  ", $string);
  		$string = str_replace("\r", "
  ", $string);
  		$string = str_replace("
  ", "\r
  ", $string);
  		return $string;
  	}
  }