1<?php 2require_once("./always.php"); 3dbg_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 :-(")) ); 4dbg_log_array( "headers", '_SERVER', $_SERVER, true ); 5if ( isset($c->public_freebusy_url) && $c->public_freebusy_url ) { 6 require_once("PublicSession.php"); 7 $session = new PublicSession(); 8} 9else { 10 require_once("HTTPAuthSession.php"); 11 $session = new HTTPAuthSession(); 12} 13 14 15/** 16* Submission parameters recommended by calconnect, plus some generous alternatives 17*/ 18param_to_global('fb_start', '#^[a-z0-9/:.,+-]+$#i', 'start', 'from'); 19param_to_global('fb_end', '#^[a-z0-9/:.,+-]+$#i', 'end', 'until', 'finish', 'to'); 20param_to_global('fb_period', '#^[+-]?P?(\d+[WD]?)(T(\d+H)?(\d+M)?(\d+S)?)?+$#', 'period'); 21param_to_global('fb_format', '#^\S+/\S+$#', 'format'); 22param_to_global('fb_user', '#^.*$#', 'user', 'userid', 'user_no', 'email'); 23param_to_global('fb_token', '#^[a-z0-9+/-]+$#i', 'token'); 24 25if ( isset($fb_period) ) $fb_period = strtoupper($fb_period); 26 27if ( !isset($fb_start) || $fb_start == '' ) $fb_start = date('Y-m-d\TH:i:s', time() - 86400 ); // no recommended default. -1 day 28if ( (!isset($fb_period) && !isset($fb_end)) || ($fb_period == '' && $fb_end == '') ) 29 $fb_period = 'P44D'; // 44 days - 2 days more than recommended default 30 31 32/** 33* If fb_user (user, userid, user_no or email parameter) then we adjust 34* the path of the request to suit. 35*/ 36if ( isset($fb_user) ) $_SERVER['PATH_INFO'] = '/'.$fb_user.'/'; 37 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. 43*/ 44require_once("CalDAVRequest.php"); 45$request = new CalDAVRequest(array("allow_by_email" => 1)); 46$path_match = '^'.$request->path; 47if ( preg_match( '{^/(\S+@[a-z0-9][a-z0-9-]*[.][a-z0-9.-]+)/?$}i', $request->path, $matches ) ) { 48 $principal = new Principal('email',$matches[1]); 49 50 if ( !$principal->Exists() ) { 51 $request->DoResponse( 404, translate('No user found matching') . ' ' . $matches[1] . "\n" ); 52 } 53 54 $path_match = '^'.$principal->dav_name(); 55} 56 57if ( isset($fb_format) && $fb_format != 'text/calendar' ) { 58 $request->DoResponse( 406, translate('This server only supports the text/calendar format for freebusy URLs') ); 59} 60 61if ( ! $request->HavePrivilegeTo('read-free-busy') ) $request->DoResponse( 404 ); 62 63require_once("freebusy-functions.php"); 64 65switch ( $_SERVER['REQUEST_METHOD'] ) { 66 case 'GET': 67 $range_start = new RepeatRuleDateTime($fb_start); 68 if ( !isset($fb_end) ) { 69 $range_end = clone($range_start); 70 $range_end->modify($fb_period); 71 } 72 else { 73 $range_end = new RepeatRuleDateTime($fb_end); 74 } 75 $freebusy = get_freebusy( $path_match, $range_start, $range_end ); 76 77 $result = new vCalendar(); 78 $result->AddComponent($freebusy); 79 80 $request->DoResponse( 200, $result->Render(), 'text/calendar' ); 81 break; 82 83 default: 84 dbg_error_log( "freebusy", "Unhandled request method >>%s<<", $_SERVER['REQUEST_METHOD'] ); 85 dbg_log_array( "freebusy", 'HEADERS', $raw_headers ); 86 dbg_log_array( "freebusy", '_SERVER', $_SERVER, true ); 87 @dbg_error_log( "freebusy", "RAW: %s", str_replace("\n", "",str_replace("\r", "", $request->raw_post)) ); 88} 89 90