DAViCal
autodiscover-handler.php
1 <?php
9 require_once('HTTPAuthSession.php');
10 $session = new HTTPAuthSession();
11 
12 require_once('CalDAVRequest.php');
13 $request = new CalDAVRequest();
14 
15 if ( !isset($c->enable_autodiscover) || ! $c->enable_autodiscover ) {
16  $request->DoResponse( 404 );
17  exit(0); // unneccessary
18 }
19 
20 $ns_outlook_req_2006 = "http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006";
21 $ns_exchange_resp_2006 = "http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006";
22 $ns_outlook_resp_2006a = "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a";
23 
24 function errorResponse( $code, $message, $debugdata = '' ) {
25  global $request, $ns_exchange_resp_2006;
26 
27  $error_time_id = time();
28  $error_time = gmdate('h:i:s', $error_time_id);
29  $response = <<<ERROR
30 <?xml version="1.0" encoding="utf-8" ?>
31 <Autodiscover xmlns="$ns_exchange_resp_2006">
32  <Response>
33  <Error Time="$error_time" Id="$error_time_id">
34  <ErrorCode>$code</ErrorCode>
35  <Message>$message</Message>
36  <DebugData>$debugdata</DebugData>
37  </Error>
38  </Response>
39 </Autodiscover>
40 ERROR;
41 
42  $request->DoResponse( $code, $response, 'text/xml; charset="utf-8"' );
43  exit(0); // unneccessary
44 }
45 
46 
47 if ( !isset($request->xml_tags) )
48  errorResponse( 406, translate("Body contains no XML data!") );
49 
50 $xmltree = BuildXMLTree( $request->xml_tags );
51 if ( !is_object($xmltree) )
52  errorResponse( 406, translate("REPORT body is not valid XML data!") );
53 
54 $user_email = $xmltree->GetPath(
55 '/'.$ns_outlook_req_2006.':Autodiscover'.
56 '/'.$ns_outlook_req_2006.':Request'.
57 '/'.$ns_outlook_req_2006.':EMailAddress');
58 if ( count($user_email) < 1 ) errorResponse(500,"User not found.");
59 $user_email = $user_email[0]->GetContent();
60 
61 $principal = new Principal();
62 
63 $reply = new XMLDocument( array( $ns_outlook_resp_2006a => "" ) );
64 $response = array(
65  new XMLElement( 'User',
66  array(
67  new XMLElement( 'DisplayName', $principal->$fullname ),
68  new XMLElement('AutoDiscoverSMTPAddress',$user_email),
69  )
70  )
71 );
72 
73 $response[] = new XMLElement('Account',
74  array(
75  new XMLElement( 'AccountType', 'email' ), // The only allowed accounttype
76  new XMLElement('Action','settings'),
77  new XMLElement('Protocol',
78  array(
79  new XMLElement('Type', 'DAV'),
80  new XMLElement('Server', $c->domain_name ),
81  new XMLElement('LoginName', $principal->username())
82  )
83  )
84  )
85 );
86 
87 $autodiscover = new XMLElement( "Autodiscover", $responses, $reply->GetXmlNsArray(), $ns_exchange_resp_2006 );
88 
89 $request->XMLResponse( 207, $autodiscover );