1 | <?php |
---|
2 | /** |
---|
3 | * CalDAV Server - main program |
---|
4 | * |
---|
5 | * @package davical |
---|
6 | * @subpackage caldav |
---|
7 | * @author Andrew McMillan <andrew@mcmillan.net.nz> |
---|
8 | * @copyright Catalyst .Net Ltd, Morphoss Ltd <http://www.morphoss.com/> |
---|
9 | * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later |
---|
10 | */ |
---|
11 | if ( isset($_SERVER['PATH_INFO']) && preg_match( '{^(/favicon.ico|davical.css|(images|js|css)/.+)$}', $_SERVER['PATH_INFO'], $matches ) ) { |
---|
12 | $filename = $_SERVER['DOCUMENT_ROOT'] . preg_replace('{(\.\.|\\\\)}', '', $matches[1]); |
---|
13 | $fh = @fopen($matches[1],'r'); |
---|
14 | if ( ! $fh ) { |
---|
15 | @header( sprintf("HTTP/1.1 %d %s", 404, 'Not found') ); |
---|
16 | } |
---|
17 | else { |
---|
18 | fpassthru($fh); |
---|
19 | } |
---|
20 | exit(0); |
---|
21 | } |
---|
22 | elseif ( isset($_SERVER['PATH_INFO']) && preg_match( '{^/\.well-known/(.+)$}', $_SERVER['PATH_INFO'], $matches ) ) { |
---|
23 | require ('well-known.php'); |
---|
24 | exit(0); |
---|
25 | } |
---|
26 | require_once('./always.php'); |
---|
27 | // dbg_error_log( 'caldav', ' User agent: %s', ((isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unfortunately Mulberry does not send a "User-agent" header with its requests :-(')) ); |
---|
28 | // dbg_log_array( 'headers', '_SERVER', $_SERVER, true ); |
---|
29 | require_once('HTTPAuthSession.php'); |
---|
30 | $session = new HTTPAuthSession(); |
---|
31 | |
---|
32 | function send_dav_header() { |
---|
33 | global $c; |
---|
34 | |
---|
35 | /** |
---|
36 | * access-control is rfc3744, we do most of it, but no way to say that. |
---|
37 | * calendar-schedule is another one we do most of, but the spec is not final yet either. |
---|
38 | */ |
---|
39 | if ( isset($c->override_dav_header) ) { |
---|
40 | $dav = $c->override_dav_header; |
---|
41 | } |
---|
42 | else { |
---|
43 | /** hack to get around bugzilla #463392 - remove sometime after 2011-02-28 */ |
---|
44 | if ( isset($_SERVER['HTTP_USER_AGENT']) && preg_match( '{ Gecko/(20[01]\d[01]\d[0123]\d)(\d+)? }', $_SERVER['HTTP_USER_AGENT'], $matches ) && $matches[1] < 20100520 ) { |
---|
45 | $dav = '1, 2, 3, access-control, calendar-access, calendar-schedule, extended-mkcol, calendar-proxy, bind, addressbook'; |
---|
46 | } |
---|
47 | else { |
---|
48 | // We don't actually do calendar-auto-schedule yet - when we do we should add it on here. |
---|
49 | $dav = '1, 2, 3, access-control, calendar-access, calendar-schedule, extended-mkcol, calendar-proxy, bind, addressbook'; |
---|
50 | } |
---|
51 | } |
---|
52 | $dav = explode( "\n", wordwrap( $dav ) ); |
---|
53 | foreach( $dav AS $v ) { |
---|
54 | header( 'DAV: '.trim($v, ', '), false); |
---|
55 | } |
---|
56 | } |
---|
57 | send_dav_header(); // Avoid polluting global namespace |
---|
58 | |
---|
59 | require_once('CalDAVRequest.php'); |
---|
60 | $request = new CalDAVRequest(); |
---|
61 | |
---|
62 | $allowed = implode( ', ', array_keys($request->supported_methods) ); |
---|
63 | // header( 'Allow: '.$allowed); |
---|
64 | |
---|
65 | if ( ! ($request->IsPrincipal() || isset($request->collection) || $request->method == 'PUT' || $request->method == 'MKCALENDAR' || $request->method == 'MKCOL' ) ) { |
---|
66 | if ( preg_match( '#^/principals/users(/.*/)$#', $request->path, $matches ) ) { |
---|
67 | // Although this doesn't work with the iPhone, perhaps it will with iCal... |
---|
68 | /** @TODO: integrate handling this URL into CalDAVRequest.php */ |
---|
69 | $redirect_url = ConstructURL('/caldav.php'.$matches[1]); |
---|
70 | dbg_error_log( 'LOG WARNING', 'Redirecting %s for "%s" to "%s"', $request->method, $request->path, $redirect_url ); |
---|
71 | header('Location: '.$redirect_url ); |
---|
72 | exit(0); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | dbg_error_log( 'LOG WARNING', 'Redirecting %s for "%s" to "%s"', $request->method, $request->path, $request->IsAddressBook() ); |
---|
77 | |
---|
78 | switch ( $request->method ) { |
---|
79 | case 'OPTIONS': include_once('caldav-OPTIONS.php'); break; |
---|
80 | case 'REPORT': include_once('caldav-REPORT.php'); break; |
---|
81 | case 'PROPFIND': include('caldav-PROPFIND.php'); break; |
---|
82 | case 'GET': |
---|
83 | if ( $request->IsAddressBook()) |
---|
84 | { |
---|
85 | include('caldav-GET-addressbook.php'); |
---|
86 | break; |
---|
87 | } |
---|
88 | else { |
---|
89 | include('caldav-GET.php'); |
---|
90 | break; |
---|
91 | } |
---|
92 | case 'POST': include('caldav-POST.php'); break; |
---|
93 | case 'HEAD': include('caldav-GET.php'); break; |
---|
94 | case 'PROPPATCH': include('caldav-PROPPATCH.php'); break; |
---|
95 | case 'PUT': |
---|
96 | $request->CoerceContentType(); |
---|
97 | switch( $request->content_type ) { |
---|
98 | case 'text/calendar': |
---|
99 | /** use original DAViCal 'PUT' code which will be rewritten */ |
---|
100 | include('caldav-PUT.php'); |
---|
101 | break; |
---|
102 | case 'text/vcard': |
---|
103 | case 'text/x-vcard': |
---|
104 | dbg_error_log( 'LOG WARNING', 'FLAAAAAAAAAAAAAAAAAA X-VCARD'); |
---|
105 | include('caldav-PUT-vcard.php'); |
---|
106 | break; |
---|
107 | default: |
---|
108 | include('caldav-PUT-default.php'); |
---|
109 | break; |
---|
110 | } |
---|
111 | break; |
---|
112 | case 'MKCALENDAR': include('caldav-MKCOL.php'); break; |
---|
113 | case 'MKCOL': include('caldav-MKCOL.php'); break; |
---|
114 | case 'DELETE': include('caldav-DELETE.php'); break; |
---|
115 | case 'MOVE': include('caldav-MOVE.php'); break; |
---|
116 | case 'ACL': include('caldav-ACL.php'); break; |
---|
117 | case 'LOCK': include('caldav-LOCK.php'); break; |
---|
118 | case 'UNLOCK': include('caldav-LOCK.php'); break; |
---|
119 | case 'MKTICKET': include('caldav-MKTICKET.php'); break; |
---|
120 | case 'DELTICKET': include('caldav-DELTICKET.php'); break; |
---|
121 | case 'BIND': include('caldav-BIND.php'); break; |
---|
122 | |
---|
123 | case 'TESTRRULE': include('test-RRULE-v2.php'); break; |
---|
124 | |
---|
125 | default: |
---|
126 | dbg_error_log( 'caldav', 'Unhandled request method >>%s<<', $request->method ); |
---|
127 | dbg_log_array( 'caldav', '_SERVER', $_SERVER, true ); |
---|
128 | dbg_error_log( 'caldav', 'RAW: %s', str_replace("\n", '',str_replace("\r", '', $request->raw_post)) ); |
---|
129 | } |
---|
130 | |
---|
131 | $request->DoResponse( 500, translate('The application program does not understand that request.') ); |
---|
132 | |
---|