Autocreated docs for new classes.
[davical.git] / htdocs / freebusy.php
blob8be903e287eff21ffee9074d5a6b10d088f3cdb2
1 <?php
2 require_once("./always.php");
3 dbg_error_log( "freebusy", " User agent: %s", ((isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "Unfortunately Mulberry and Chandler don't send a 'User-agent' header with their requests :-(")) );
4 dbg_log_array( "headers", '_SERVER', $_SERVER, true );
5 if ( isset($c->public_freebusy_url) && $c->public_freebusy_url ) {
6 require_once("PublicSession.php");
7 $session = new PublicSession();
9 else {
10 require_once("HTTPAuthSession.php");
11 $session = new HTTPAuthSession();
15 /**
16 * Submission parameters recommended by calconnect, plus some generous alternatives
18 param_to_global('fb_start', '#^[a-z0-9/:.,+-]+$#i', 'start', 'from');
19 param_to_global('fb_end', '#^[a-z0-9/:.,+-]+$#i', 'end', 'until', 'finish', 'to');
20 param_to_global('fb_period', '#^[+-]?P?(\d+[WD]?)(T(\d+H)?(\d+M)?(\d+S)?)?+$#', 'period');
21 param_to_global('fb_format', '#^\S+/\S+$#', 'format');
22 param_to_global('fb_user', '#^.*$#', 'user', 'userid', 'user_no', 'email');
23 param_to_global('fb_token', '#^[a-z0-9+/-]+$#i', 'token');
25 if ( isset($fb_period) ) $fb_period = strtoupper($fb_period);
27 if ( !isset($fb_start) || $fb_start == '' ) $fb_start = date('Y-m-d\TH:i:s', time() - 86400 ); // no recommended default. -1 day
28 if ( (!isset($fb_period) && !isset($fb_end)) || ($fb_period == '' && $fb_end == '') )
29 $fb_period = 'P44D'; // 44 days - 2 days more than recommended default
32 /**
33 * If fb_user (user, userid, user_no or email parameter) then we adjust
34 * the path of the request to suit.
36 if ( isset($fb_user) ) $_SERVER['PATH_INFO'] = '/'.$fb_user.'/';
38 /**
39 * We also allow URLs like .../freebusy.php/user@example.com to work, so long as
40 * the e-mail matches a single user whose calendar we have rights to.
41 * @NOTE: It is OK for there to *be* duplicate e-mail addresses, just so long as we
42 * only have read permission (or more) for only one of them.
44 require_once("CalDAVRequest.php");
45 $request = new CalDAVRequest(array("allow_by_email" => 1));
46 $path_match = '^'.$request->path;
47 if ( preg_match( '{^/(\S+@[a-z0-9][a-z0-9-]*[.][a-z0-9.-]+)/?$}i', $request->path, $matches ) ) {
48 $principal = new Principal('email',$matches[1]);
49 $path_match = '^'.$principal->dav_name();
52 if ( isset($fb_format) && $fb_format != 'text/calendar' ) {
53 $request->DoResponse( 406, translate('This server only supports the text/calendar format for freebusy URLs') );
56 if ( ! $request->HavePrivilegeTo('read-free-busy') ) $request->DoResponse( 404 );
58 require_once("freebusy-functions.php");
60 switch ( $_SERVER['REQUEST_METHOD'] ) {
61 case 'GET':
62 $range_start = new RepeatRuleDateTime($fb_start);
63 if ( !isset($fb_end) ) {
64 $range_end = clone($range_start);
65 $range_end->modify($fb_period);
67 else {
68 $range_end = new RepeatRuleDateTime($fb_end);
70 $freebusy = get_freebusy( $path_match, $range_start, $range_end );
72 $result = new iCalComponent();
73 $result->VCalendar();
74 $result->AddComponent($freebusy);
76 $request->DoResponse( 200, $result->Render(), 'text/calendar' );
77 break;
79 default:
80 dbg_error_log( "freebusy", "Unhandled request method >>%s<<", $_SERVER['REQUEST_METHOD'] );
81 dbg_log_array( "freebusy", 'HEADERS', $raw_headers );
82 dbg_log_array( "freebusy", '_SERVER', $_SERVER, true );
83 @dbg_error_log( "freebusy", "RAW: %s", str_replace("\n", "",str_replace("\r", "", $request->raw_post)) );