* @copyright Catalyst .Net Ltd, Morphoss Ltd * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later */ dbg_error_log("get-addressbook", "GET-ADDRESSBOOK method handler"); require_once("XMLDocument.php"); require_once('DAVResource.php'); include_once("drivers_ldap.php"); require_once('RRule-v2.php'); $dav_resource = new DAVResource($request->path); $dav_resource->NeedPrivilege( array('urn:ietf:params:xml:ns:caldav:read-free-busy','DAV::read') ); if ( ! $dav_resource->Exists() ) { $request->DoResponse( 404, translate("Resource Not Found.") ); } function obfuscated_event( $icalendar ) { // The user is not admin / owner of this calendar looking at his calendar and can not admin the other cal, // or maybe they don't have *read* access but they got here, so they must at least have free/busy access // so we will present an obfuscated version of the event that just says "Busy" (translated :-) $confidential = new iCalComponent(); $confidential->SetType($icalendar->GetType()); $confidential->AddProperty( 'SUMMARY', translate('Busy') ); $confidential->AddProperty( 'CLASS', 'CONFIDENTIAL' ); $confidential->SetProperties( $icalendar->GetProperties('DTSTART'), 'DTSTART' ); $confidential->SetProperties( $icalendar->GetProperties('RRULE'), 'RRULE' ); $confidential->SetProperties( $icalendar->GetProperties('DURATION'), 'DURATION' ); $confidential->SetProperties( $icalendar->GetProperties('DTEND'), 'DTEND' ); $confidential->SetProperties( $icalendar->GetProperties('UID'), 'UID' ); $confidential->SetProperties( $icalendar->GetProperties('CREATED'), 'CREATED' ); return $confidential; } if ( $dav_resource->IsCollection() ) { if ( ! $dav_resource->IsAddressbook() && !(isset($c->get_includes_subcollections) && $c->get_includes_subcollections) ) { /** RFC2616 says we must send an Allow header if we send a 405 */ header("Allow: PROPFIND,PROPPATCH,OPTIONS,MKCOL,REPORT,DELETE"); $request->DoResponse( 405, translate("GET requests on collections are only supported for calendars.") ); } if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['report']) && $c->dbg['report'])) ) { $fh = fopen('/tmp/REPORT.txt','w'); if ( $fh ) { fwrite($fh,$request->raw_post); fclose($fh); } } $position = 0; $target = new DAVResource($request->path); require_once("iCalendar.php"); $reportnum = -1; $report = array(); $denied = array(); $unsupported = array(); if ( isset($prop_filter) ) unset($prop_filter); $reply = new XMLDocument( array( "DAV:" => "" ) ); function contacts_to_xml( $properties, $item) { global $c, $request, $reply , $dav_resource; $path = preg_replace("/^\//","",$request->path); //$filtro = "uid=".$request->username; //$atributos = array("uidNumber"); //$uidnumber = ldapDrivers::requestAtributo($filtro, $atributos); //if ($uidnumber == false) { // dbg_error_log( "REPORT", "Responding with report error: usuario não encontrado no diretorio"); // $request->DoResponse( 501, 'Database error'); // } // $nome = $uidnumber['uidNumber']; $nome = $dav_resource->GetProperty('user_no'); $mails = array(); $phones = array(); unset($VCARD); $VCARD = "UID:$item->id_contact@$nome\r\n"; /*************************** ********* DADOS DEFAULT **** ****************************/ $VCARD .= "N:$item->family_names;$item->given_names\r\n"; $VCARD .= "FN:$item->names_ordered\r\n"; $VCARD .= "NICKNAME:$item->alias\r\n"; $VCARD .= "PHOTO:ENCODING=b;TYPE=JPEG:$item->photo\r\n"; $VCARD .= "BDAY:$item->birthdate\r\n"; $VCARD .= "NOTE:$item->notes\r\n"; /************************** ********** EMAIL & TEL **** ***************************/ $qry = new AwlQuery("SELECT * FROM phpgw_cc_contact_conns inner join phpgw_cc_connections using(id_connection) WHERE phpgw_cc_contact_conns.id_contact= :id", array(':id' => $item->id_contact)); if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ){ while( $email_tel = $qry->Fetch() ) { if ( $email_tel->id_typeof_contact_connection == 1) { if ( $email_tel->connection_name == 'Principal' ) $type=work; else $type=home; $VCARD .= "EMAIL;TYPE=$type:$email_tel->connection_value\r\n"; } else if ( $email_tel->id_typeof_contact_connection == 2) { if ( $email_tel->connection_name == 'Trabalho' ) $type=work; else if ( $email_tel->connection_name == 'Casa' ) $type=home; else if ( $email_tel->connection_name == 'Celular' ) $type=cell; else if ( $email_tel->connection_name == 'Fax' ) $type=fax; $VCARD .= "TEL;TYPE=$type:$email_tel->connection_value\r\n"; } } } /************************** ************ ADDR ******** **************************/ $qry = new AwlQuery("SELECT * FROM phpgw_cc_contact_addrs inner join phpgw_cc_addresses using(id_address) WHERE phpgw_cc_contact_addrs.id_contact= :id", array(':id' => $item->id_contact)); if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ){ while( $addr = $qry->Fetch() ) { if ( $addr->id_typeof_contact_address == 1) { $type=home; $qryy = new AwlQuery("SELECT city_name,state_symbol FROM phpgw_cc_city inner join phpgw_cc_state using(id_country) WHERE phpgw_cc_city.id_city= :id AND phpgw_cc_state.id_state = :idstate", array(':id' => $addr->id_city,':idstate' => $addr->id_state)); if ($qryy->Exec("REPORT",__LINE__,__FILE__) && $qryy->rows() > 0) { $locate = $qryy->Fetch(); $VCARD .= "ADR;TYPE=$type:;;$addr->address1;$locate->city_name;$addr->state_symbol;$addr->postal_code;$addr->id_country\r\n"; } } else if ( $addr->id_typeof_contact_address == 2) { $type=work; $qryy = new AwlQuery("SELECT city_name,state_symbol FROM phpgw_cc_city inner join phpgw_cc_state using(id_country) WHERE phpgw_cc_city.id_city= :id AND phpgw_cc_state.id_state = :idstate", array(':id' => $addr->id_city,':idstate' => $addr->id_state)); if ($qryy->Exec("REPORT",__LINE__,__FILE__) && $qryy->rows() > 0) { $locate = $qryy->Fetch(); $VCARD .= "ADR;TYPE=$type:;;$addr->address1;$locate->city_name;$addr->state_symbol;$addr->postal_code;$addr->id_country\r\n"; } } } } /********** PUBLIC KEY ******** *****************************/ dbg_error_log("REPORT","Building XML Response for item '%s' usuario '%s'", $item->id_contact,$nome ); $response = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Inverse inc.//SOGo Connector 1.0//EN\r\n"; $response .= "$VCARD"; $response .= "END:VCARD\r\n"; $caldav_data = $response; $dav_nam = "{$item->id_contact}@{$item->id_owner}"; $dav_etag_nam=md5($item->id_contact.$item->id_owner.$item->last_update); //$dav_name = "{$item->id_contact}@{$item->id_owner}@{$item->last_update}"; $dav_name = sprintf('/%s/%s%s.vcf',$path,$item->id_contact,$item->id_owner); $url = ConstructURL($dav_name); //$prop = new XMLElement("prop"); //$status = new XMLElement("status", "HTTP/1.1 200 OK" ); //$reply->CalDAVElement($prop, 'address-data', $caldav_data ); //$prop->NewElement('address-data', $caldav_data ); //$propstat = new XMLElement( "propstat", array( $prop, $status) ); //$href = new XMLElement("href", $url ); //$elements = array($href,$propstat); //$response = new XMLElement( "response", $elements ); //return $caldav_data; header( 'Content-Length: '.strlen($response) ); header( 'Etag: '.$dav_name ); $request->DoResponse( 200, ($request->method == 'HEAD' ? '' : $response), 'text/vcard; charset="ISO-8859-1"' ); // return $status; } dbg_error_log("REPORT","Building XML REQUESTEEEE PATHHHHHHH '%s'", $request->path ); $part= explode('/',$request->path); $id=preg_split('/@/', preg_replace("/.vcf/","",$part[3])); //$id = $dav_resource->GetProperty('user_no'); //$mode = 'carddav'; //$qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget'); //include("caldav-REPORT-multiget-addressbook.php"); $sql = "SELECT * FROM phpgw_cc_contact WHERE id_owner= :nome AND id_contact= :id"; $qry = new AwlQuery( $sql,array(':nome' => $id[1],':id' => $id[0])); if ( $qry->Exec("REPORT-MULTIGET",__LINE__,__FILE__) && $qry->rows() > 0 ) { while( $contacts_object = $qry->Fetch() ) { //if ( $bound_from != $collection->dav_name() ) { // $contacts_object->dav_name = str_replace( $bound_from, $collection->dav_name(), $contacts_object->dav_name); // } //if ( $need_expansion ) { // $vResource = new vComponent($contacts_object->caldav_data); // $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end); // $contacts_object->caldav_data = $expanded->Render(); // } //$responses[] = contacts_to_xml( $properties, $contacts_object); contacts_to_xml( $properties, $contacts_object); } } }