PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/CalDAV/Calendar.php

https://bitbucket.org/freshflow/sabredav-1.8.5-fork
PHP | 376 lines | 142 code | 71 blank | 163 comment | 7 complexity | 39d837a6e92b0775f54b983ba0c5d239 MD5 | raw file
  1. <?php
  2. namespace Sabre\CalDAV;
  3. use Sabre\DAV;
  4. use Sabre\DAVACL;
  5. /**
  6. * This object represents a CalDAV calendar.
  7. *
  8. * A calendar can contain multiple TODO and or Events. These are represented
  9. * as \Sabre\CalDAV\CalendarObject objects.
  10. *
  11. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  14. */
  15. class Calendar implements ICalendar, DAV\IProperties, DAVACL\IACL {
  16. /**
  17. * This is an array with calendar information
  18. *
  19. * @var array
  20. */
  21. protected $calendarInfo;
  22. /**
  23. * CalDAV backend
  24. *
  25. * @var Backend\BackendInterface
  26. */
  27. protected $caldavBackend;
  28. /**
  29. * Constructor
  30. *
  31. * @param Backend\BackendInterface $caldavBackend
  32. * @param array $calendarInfo
  33. */
  34. public function __construct(Backend\BackendInterface $caldavBackend, $calendarInfo) {
  35. $this->caldavBackend = $caldavBackend;
  36. $this->calendarInfo = $calendarInfo;
  37. }
  38. /**
  39. * Returns the name of the calendar
  40. *
  41. * @return string
  42. */
  43. public function getName() {
  44. return $this->calendarInfo['uri'];
  45. }
  46. /**
  47. * Updates properties such as the display name and description
  48. *
  49. * @param array $mutations
  50. * @return array
  51. */
  52. public function updateProperties($mutations) {
  53. return $this->caldavBackend->updateCalendar($this->calendarInfo['id'],$mutations);
  54. }
  55. /**
  56. * Returns the list of properties
  57. *
  58. * @param array $requestedProperties
  59. * @return array
  60. */
  61. public function getProperties($requestedProperties) {
  62. $response = array();
  63. foreach($requestedProperties as $prop) switch($prop) {
  64. case '{urn:ietf:params:xml:ns:caldav}supported-calendar-data' :
  65. $response[$prop] = new Property\SupportedCalendarData();
  66. break;
  67. case '{urn:ietf:params:xml:ns:caldav}supported-collation-set' :
  68. $response[$prop] = new Property\SupportedCollationSet();
  69. break;
  70. case '{DAV:}owner' :
  71. $response[$prop] = new DAVACL\Property\Principal(DAVACL\Property\Principal::HREF,$this->calendarInfo['principaluri']);
  72. break;
  73. default :
  74. if (isset($this->calendarInfo[$prop])) $response[$prop] = $this->calendarInfo[$prop];
  75. break;
  76. }
  77. return $response;
  78. }
  79. /**
  80. * Returns a calendar object
  81. *
  82. * The contained calendar objects are for example Events or Todo's.
  83. *
  84. * @param string $name
  85. * @return \Sabre\CalDAV\ICalendarObject
  86. */
  87. public function getChild($name) {
  88. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
  89. if (!$obj) throw new DAV\Exception\NotFound('Calendar object not found');
  90. $obj['acl'] = $this->getACL();
  91. // Removing the irrelivant
  92. foreach($obj['acl'] as $key=>$acl) {
  93. if ($acl['privilege'] === '{' . Plugin::NS_CALDAV . '}read-free-busy') {
  94. unset($obj['acl'][$key]);
  95. }
  96. }
  97. return new 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. $obj['acl'] = $this->getACL();
  109. // Removing the irrelivant
  110. foreach($obj['acl'] as $key=>$acl) {
  111. if ($acl['privilege'] === '{' . Plugin::NS_CALDAV . '}read-free-busy') {
  112. unset($obj['acl'][$key]);
  113. }
  114. }
  115. $children[] = new CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
  116. }
  117. return $children;
  118. }
  119. /**
  120. * Checks if a child-node exists.
  121. *
  122. * @param string $name
  123. * @return bool
  124. */
  125. public function childExists($name) {
  126. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
  127. if (!$obj)
  128. return false;
  129. else
  130. return true;
  131. }
  132. /**
  133. * Creates a new directory
  134. *
  135. * We actually block this, as subdirectories are not allowed in calendars.
  136. *
  137. * @param string $name
  138. * @return void
  139. */
  140. public function createDirectory($name) {
  141. throw new DAV\Exception\MethodNotAllowed('Creating collections in calendar objects is not allowed');
  142. }
  143. /**
  144. * Creates a new file
  145. *
  146. * The contents of the new file must be a valid ICalendar string.
  147. *
  148. * @param string $name
  149. * @param resource $calendarData
  150. * @return string|null
  151. */
  152. public function createFile($name,$calendarData = null) {
  153. if (is_resource($calendarData)) {
  154. $calendarData = stream_get_contents($calendarData);
  155. }
  156. return $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 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->getOwner(),
  221. 'protected' => true,
  222. ),
  223. array(
  224. 'privilege' => '{DAV:}write',
  225. 'principal' => $this->getOwner(),
  226. 'protected' => true,
  227. ),
  228. array(
  229. 'privilege' => '{DAV:}read',
  230. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  231. 'protected' => true,
  232. ),
  233. array(
  234. 'privilege' => '{DAV:}write',
  235. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  236. 'protected' => true,
  237. ),
  238. array(
  239. 'privilege' => '{DAV:}read',
  240. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  241. 'protected' => true,
  242. ),
  243. array(
  244. 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
  245. 'principal' => '{DAV:}authenticated',
  246. 'protected' => true,
  247. ),
  248. );
  249. }
  250. /**
  251. * Updates the ACL
  252. *
  253. * This method will receive a list of new ACE's.
  254. *
  255. * @param array $acl
  256. * @return void
  257. */
  258. public function setACL(array $acl) {
  259. throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
  260. }
  261. /**
  262. * Returns the list of supported privileges for this node.
  263. *
  264. * The returned data structure is a list of nested privileges.
  265. * See \Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
  266. * standard structure.
  267. *
  268. * If null is returned from this method, the default privilege set is used,
  269. * which is fine for most common usecases.
  270. *
  271. * @return array|null
  272. */
  273. public function getSupportedPrivilegeSet() {
  274. $default = DAVACL\Plugin::getDefaultSupportedPrivilegeSet();
  275. // We need to inject 'read-free-busy' in the tree, aggregated under
  276. // {DAV:}read.
  277. foreach($default['aggregates'] as &$agg) {
  278. if ($agg['privilege'] !== '{DAV:}read') continue;
  279. $agg['aggregates'][] = array(
  280. 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
  281. );
  282. }
  283. return $default;
  284. }
  285. /**
  286. * Performs a calendar-query on the contents of this calendar.
  287. *
  288. * The calendar-query is defined in RFC4791 : CalDAV. Using the
  289. * calendar-query it is possible for a client to request a specific set of
  290. * object, based on contents of iCalendar properties, date-ranges and
  291. * iCalendar component types (VTODO, VEVENT).
  292. *
  293. * This method should just return a list of (relative) urls that match this
  294. * query.
  295. *
  296. * The list of filters are specified as an array. The exact array is
  297. * documented by Sabre\CalDAV\CalendarQueryParser.
  298. *
  299. * @param array $filters
  300. * @return array
  301. */
  302. public function calendarQuery(array $filters) {
  303. return $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  304. }
  305. }