Calendar.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * This object represents a CalDAV calendar.
  4. *
  5. * A calendar can contain multiple TODO and or Events. These are represented
  6. * as Sabre_CalDAV_CalendarObject objects.
  7. *
  8. * @package Sabre
  9. * @subpackage CalDAV
  10. * @copyright Copyright (C) 2007-2011 Rooftop Solutions. All rights reserved.
  11. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class Sabre_CalDAV_Calendar implements Sabre_CalDAV_ICalendar, Sabre_DAV_IProperties, Sabre_DAVACL_IACL {
  15. /**
  16. * This is an array with calendar information
  17. *
  18. * @var array
  19. */
  20. protected $calendarInfo;
  21. /**
  22. * CalDAV backend
  23. *
  24. * @var Sabre_CalDAV_Backend_Abstract
  25. */
  26. protected $caldavBackend;
  27. /**
  28. * Principal backend
  29. *
  30. * @var Sabre_DAVACL_IPrincipalBackend
  31. */
  32. protected $principalBackend;
  33. /**
  34. * Constructor
  35. *
  36. * @param Sabre_CalDAV_Backend_Abstract $caldavBackend
  37. * @param array $calendarInfo
  38. * @return void
  39. */
  40. public function __construct(Sabre_DAVACL_IPrincipalBackend $principalBackend, Sabre_CalDAV_Backend_Abstract $caldavBackend, $calendarInfo) {
  41. $this->caldavBackend = $caldavBackend;
  42. $this->principalBackend = $principalBackend;
  43. $this->calendarInfo = $calendarInfo;
  44. }
  45. /**
  46. * Returns the name of the calendar
  47. *
  48. * @return string
  49. */
  50. public function getName() {
  51. return $this->calendarInfo['uri'];
  52. }
  53. /**
  54. * Updates properties such as the display name and description
  55. *
  56. * @param array $mutations
  57. * @return array
  58. */
  59. public function updateProperties($mutations) {
  60. return $this->caldavBackend->updateCalendar($this->calendarInfo['id'],$mutations);
  61. }
  62. /**
  63. * Returns the list of properties
  64. *
  65. * @param array $properties
  66. * @return array
  67. */
  68. public function getProperties($requestedProperties) {
  69. $response = array();
  70. foreach($requestedProperties as $prop) switch($prop) {
  71. case '{urn:ietf:params:xml:ns:caldav}supported-calendar-data' :
  72. $response[$prop] = new Sabre_CalDAV_Property_SupportedCalendarData();
  73. break;
  74. case '{urn:ietf:params:xml:ns:caldav}supported-collation-set' :
  75. $response[$prop] = new Sabre_CalDAV_Property_SupportedCollationSet();
  76. break;
  77. case '{DAV:}owner' :
  78. $response[$prop] = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::HREF,$this->calendarInfo['principaluri']);
  79. break;
  80. default :
  81. if (isset($this->calendarInfo[$prop])) $response[$prop] = $this->calendarInfo[$prop];
  82. break;
  83. }
  84. return $response;
  85. }
  86. /**
  87. * Returns a calendar object
  88. *
  89. * The contained calendar objects are for example Events or Todo's.
  90. *
  91. * @param string $name
  92. * @return Sabre_DAV_ICalendarObject
  93. */
  94. public function getChild($name) {
  95. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
  96. if (!$obj) throw new Sabre_DAV_Exception_FileNotFound('Calendar object not found');
  97. return new Sabre_CalDAV_CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
  98. }
  99. /**
  100. * Returns the full list of calendar objects
  101. *
  102. * @return array
  103. */
  104. public function getChildren() {
  105. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  106. $children = array();
  107. foreach($objs as $obj) {
  108. $children[] = new Sabre_CalDAV_CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
  109. }
  110. return $children;
  111. }
  112. /**
  113. * Checks if a child-node exists.
  114. *
  115. * @param string $name
  116. * @return bool
  117. */
  118. public function childExists($name) {
  119. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
  120. if (!$obj)
  121. return false;
  122. else
  123. return true;
  124. }
  125. /**
  126. * Creates a new directory
  127. *
  128. * We actually block this, as subdirectories are not allowed in calendars.
  129. *
  130. * @param string $name
  131. * @return void
  132. */
  133. public function createDirectory($name) {
  134. throw new Sabre_DAV_Exception_MethodNotAllowed('Creating collections in calendar objects is not allowed');
  135. }
  136. /**
  137. * Creates a new file
  138. *
  139. * The contents of the new file must be a valid ICalendar string.
  140. *
  141. * @param string $name
  142. * @param resource $calendarData
  143. * @return void
  144. */
  145. public function createFile($name,$calendarData = null) {
  146. $calendarData = stream_get_contents($calendarData);
  147. // Converting to UTF-8, if needed
  148. $calendarData = Sabre_DAV_StringUtil::ensureUTF8($calendarData);
  149. $supportedComponents = $this->calendarInfo['{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}supported-calendar-component-set'];
  150. if ($supportedComponents) {
  151. $supportedComponents = $supportedComponents->getValue();
  152. } else {
  153. $supportedComponents = null;
  154. }
  155. Sabre_CalDAV_ICalendarUtil::validateICalendarObject($calendarData, $supportedComponents);
  156. $this->caldavBackend->createCalendarObject($this->calendarInfo['id'],$name,$calendarData);
  157. }
  158. /**
  159. * Deletes the calendar.
  160. *
  161. * @return void
  162. */
  163. public function delete() {
  164. $this->caldavBackend->deleteCalendar($this->calendarInfo['id']);
  165. }
  166. /**
  167. * Renames the calendar. Note that most calendars use the
  168. * {DAV:}displayname to display a name to display a name.
  169. *
  170. * @param string $newName
  171. * @return void
  172. */
  173. public function setName($newName) {
  174. throw new Sabre_DAV_Exception_MethodNotAllowed('Renaming calendars is not yet supported');
  175. }
  176. /**
  177. * Returns the last modification date as a unix timestamp.
  178. *
  179. * @return void
  180. */
  181. public function getLastModified() {
  182. return null;
  183. }
  184. /**
  185. * Returns the owner principal
  186. *
  187. * This must be a url to a principal, or null if there's no owner
  188. *
  189. * @return string|null
  190. */
  191. public function getOwner() {
  192. return $this->calendarInfo['principaluri'];
  193. }
  194. /**
  195. * Returns a group principal
  196. *
  197. * This must be a url to a principal, or null if there's no owner
  198. *
  199. * @return string|null
  200. */
  201. public function getGroup() {
  202. return null;
  203. }
  204. /**
  205. * Returns a list of ACE's for this node.
  206. *
  207. * Each ACE has the following properties:
  208. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  209. * currently the only supported privileges
  210. * * 'principal', a url to the principal who owns the node
  211. * * 'protected' (optional), indicating that this ACE is not allowed to
  212. * be updated.
  213. *
  214. * @return array
  215. */
  216. public function getACL() {
  217. return array(
  218. array(
  219. 'privilege' => '{DAV:}read',
  220. 'principal' => $this->calendarInfo['principaluri'],
  221. 'protected' => true,
  222. ),
  223. array(
  224. 'privilege' => '{DAV:}write',
  225. 'principal' => $this->calendarInfo['principaluri'],
  226. 'protected' => true,
  227. ),
  228. array(
  229. 'privilege' => '{DAV:}read',
  230. 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
  231. 'protected' => true,
  232. ),
  233. array(
  234. 'privilege' => '{DAV:}write',
  235. 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
  236. 'protected' => true,
  237. ),
  238. array(
  239. 'privilege' => '{DAV:}read',
  240. 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
  241. 'protected' => true,
  242. ),
  243. );
  244. }
  245. /**
  246. * Updates the ACL
  247. *
  248. * This method will receive a list of new ACE's.
  249. *
  250. * @param array $acl
  251. * @return void
  252. */
  253. public function setACL(array $acl) {
  254. throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
  255. }
  256. }