[3733] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * CalDAV Server - handle GET method |
---|
| 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 | dbg_error_log("get-addressbook", "GET-ADDRESSBOOK method handler"); |
---|
| 12 | |
---|
| 13 | require_once("XMLDocument.php"); |
---|
| 14 | require_once('DAVResource.php'); |
---|
| 15 | include_once("drivers_ldap.php"); |
---|
| 16 | require_once('RRule-v2.php'); |
---|
| 17 | |
---|
| 18 | $dav_resource = new DAVResource($request->path); |
---|
| 19 | $dav_resource->NeedPrivilege( array('urn:ietf:params:xml:ns:caldav:read-free-busy','DAV::read') ); |
---|
| 20 | |
---|
| 21 | if ( ! $dav_resource->Exists() ) { |
---|
| 22 | $request->DoResponse( 404, translate("Resource Not Found.") ); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | function obfuscated_event( $icalendar ) { |
---|
| 26 | // The user is not admin / owner of this calendar looking at his calendar and can not admin the other cal, |
---|
| 27 | // or maybe they don't have *read* access but they got here, so they must at least have free/busy access |
---|
| 28 | // so we will present an obfuscated version of the event that just says "Busy" (translated :-) |
---|
| 29 | $confidential = new iCalComponent(); |
---|
| 30 | $confidential->SetType($icalendar->GetType()); |
---|
| 31 | $confidential->AddProperty( 'SUMMARY', translate('Busy') ); |
---|
| 32 | $confidential->AddProperty( 'CLASS', 'CONFIDENTIAL' ); |
---|
| 33 | $confidential->SetProperties( $icalendar->GetProperties('DTSTART'), 'DTSTART' ); |
---|
| 34 | $confidential->SetProperties( $icalendar->GetProperties('RRULE'), 'RRULE' ); |
---|
| 35 | $confidential->SetProperties( $icalendar->GetProperties('DURATION'), 'DURATION' ); |
---|
| 36 | $confidential->SetProperties( $icalendar->GetProperties('DTEND'), 'DTEND' ); |
---|
| 37 | $confidential->SetProperties( $icalendar->GetProperties('UID'), 'UID' ); |
---|
| 38 | $confidential->SetProperties( $icalendar->GetProperties('CREATED'), 'CREATED' ); |
---|
| 39 | |
---|
| 40 | return $confidential; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | if ( $dav_resource->IsCollection() ) { |
---|
| 45 | if ( ! $dav_resource->IsAddressbook() && !(isset($c->get_includes_subcollections) && $c->get_includes_subcollections) ) { |
---|
| 46 | /** RFC2616 says we must send an Allow header if we send a 405 */ |
---|
| 47 | header("Allow: PROPFIND,PROPPATCH,OPTIONS,MKCOL,REPORT,DELETE"); |
---|
| 48 | $request->DoResponse( 405, translate("GET requests on collections are only supported for calendars.") ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['report']) && $c->dbg['report'])) ) { |
---|
| 53 | $fh = fopen('/tmp/REPORT.txt','w'); |
---|
| 54 | if ( $fh ) { |
---|
| 55 | fwrite($fh,$request->raw_post); |
---|
| 56 | fclose($fh); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | $position = 0; |
---|
| 61 | $target = new DAVResource($request->path); |
---|
| 62 | require_once("iCalendar.php"); |
---|
| 63 | |
---|
| 64 | $reportnum = -1; |
---|
| 65 | $report = array(); |
---|
| 66 | $denied = array(); |
---|
| 67 | $unsupported = array(); |
---|
| 68 | if ( isset($prop_filter) ) unset($prop_filter); |
---|
| 69 | |
---|
| 70 | $reply = new XMLDocument( array( "DAV:" => "" ) ); |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | function contacts_to_xml( $properties, $item) |
---|
| 74 | { |
---|
| 75 | global $c, $request, $reply , $dav_resource; |
---|
| 76 | $path = preg_replace("/^\//","",$request->path); |
---|
| 77 | //$filtro = "uid=".$request->username; |
---|
| 78 | //$atributos = array("uidNumber"); |
---|
| 79 | //$uidnumber = ldapDrivers::requestAtributo($filtro, $atributos); |
---|
| 80 | //if ($uidnumber == false) { |
---|
| 81 | // dbg_error_log( "REPORT", "Responding with report error: usuario não encontrado no diretorio"); |
---|
| 82 | // $request->DoResponse( 501, 'Database error'); |
---|
| 83 | // } |
---|
| 84 | // $nome = $uidnumber['uidNumber']; |
---|
| 85 | $nome = $dav_resource->GetProperty('user_no'); |
---|
| 86 | $mails = array(); |
---|
| 87 | $phones = array(); |
---|
| 88 | unset($VCARD); |
---|
| 89 | $VCARD = "UID:$item->id_contact@$nome\r\n"; |
---|
| 90 | |
---|
| 91 | /*************************** |
---|
| 92 | ********* DADOS DEFAULT **** |
---|
| 93 | ****************************/ |
---|
| 94 | $VCARD .= "N:$item->family_names;$item->given_names\r\n"; |
---|
| 95 | $VCARD .= "FN:$item->names_ordered\r\n"; |
---|
| 96 | $VCARD .= "NICKNAME:$item->alias\r\n"; |
---|
| 97 | $VCARD .= "PHOTO:ENCODING=b;TYPE=JPEG:$item->photo\r\n"; |
---|
| 98 | $VCARD .= "BDAY:$item->birthdate\r\n"; |
---|
| 99 | $VCARD .= "NOTE:$item->notes\r\n"; |
---|
| 100 | /************************** |
---|
| 101 | ********** EMAIL & TEL **** |
---|
| 102 | ***************************/ |
---|
| 103 | $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)); |
---|
| 104 | if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ){ |
---|
| 105 | while( $email_tel = $qry->Fetch() ) { |
---|
| 106 | if ( $email_tel->id_typeof_contact_connection == 1) |
---|
| 107 | { |
---|
| 108 | if ( $email_tel->connection_name == 'Principal' ) |
---|
| 109 | $type=work; |
---|
| 110 | else |
---|
| 111 | $type=home; |
---|
| 112 | $VCARD .= "EMAIL;TYPE=$type:$email_tel->connection_value\r\n"; |
---|
| 113 | } |
---|
| 114 | else if ( $email_tel->id_typeof_contact_connection == 2) |
---|
| 115 | { |
---|
| 116 | if ( $email_tel->connection_name == 'Trabalho' ) |
---|
| 117 | $type=work; |
---|
| 118 | else if ( $email_tel->connection_name == 'Casa' ) |
---|
| 119 | $type=home; |
---|
| 120 | else if ( $email_tel->connection_name == 'Celular' ) |
---|
| 121 | $type=cell; |
---|
| 122 | else if ( $email_tel->connection_name == 'Fax' ) |
---|
| 123 | $type=fax; |
---|
| 124 | |
---|
| 125 | $VCARD .= "TEL;TYPE=$type:$email_tel->connection_value\r\n"; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | /************************** |
---|
| 130 | ************ ADDR ******** |
---|
| 131 | **************************/ |
---|
| 132 | $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)); |
---|
| 133 | if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ){ |
---|
| 134 | while( $addr = $qry->Fetch() ) { |
---|
| 135 | if ( $addr->id_typeof_contact_address == 1) |
---|
| 136 | { |
---|
| 137 | $type=home; |
---|
| 138 | $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)); |
---|
| 139 | if ($qryy->Exec("REPORT",__LINE__,__FILE__) && $qryy->rows() > 0) |
---|
| 140 | { $locate = $qryy->Fetch(); |
---|
| 141 | $VCARD .= "ADR;TYPE=$type:;;$addr->address1;$locate->city_name;$addr->state_symbol;$addr->postal_code;$addr->id_country\r\n"; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | else if ( $addr->id_typeof_contact_address == 2) |
---|
| 145 | { |
---|
| 146 | $type=work; |
---|
| 147 | $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)); |
---|
| 148 | if ($qryy->Exec("REPORT",__LINE__,__FILE__) && $qryy->rows() > 0) |
---|
| 149 | { $locate = $qryy->Fetch(); |
---|
| 150 | $VCARD .= "ADR;TYPE=$type:;;$addr->address1;$locate->city_name;$addr->state_symbol;$addr->postal_code;$addr->id_country\r\n"; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /********** PUBLIC KEY ******** |
---|
| 158 | *****************************/ |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | dbg_error_log("REPORT","Building XML Response for item '%s' usuario '%s'", $item->id_contact,$nome ); |
---|
| 164 | |
---|
| 165 | $response = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Inverse inc.//SOGo Connector 1.0//EN\r\n"; |
---|
| 166 | $response .= "$VCARD"; |
---|
| 167 | $response .= "END:VCARD\r\n"; |
---|
| 168 | $caldav_data = $response; |
---|
| 169 | |
---|
| 170 | $dav_nam = "{$item->id_contact}@{$item->id_owner}"; |
---|
| 171 | $dav_etag_nam=md5($item->id_contact.$item->id_owner.$item->last_update); |
---|
| 172 | //$dav_name = "{$item->id_contact}@{$item->id_owner}@{$item->last_update}"; |
---|
| 173 | $dav_name = sprintf('/%s/%s%s.vcf',$path,$item->id_contact,$item->id_owner); |
---|
| 174 | $url = ConstructURL($dav_name); |
---|
| 175 | |
---|
| 176 | //$prop = new XMLElement("prop"); |
---|
| 177 | //$status = new XMLElement("status", "HTTP/1.1 200 OK" ); |
---|
| 178 | //$reply->CalDAVElement($prop, 'address-data', $caldav_data ); |
---|
| 179 | //$prop->NewElement('address-data', $caldav_data ); |
---|
| 180 | //$propstat = new XMLElement( "propstat", array( $prop, $status) ); |
---|
| 181 | //$href = new XMLElement("href", $url ); |
---|
| 182 | |
---|
| 183 | //$elements = array($href,$propstat); |
---|
| 184 | //$response = new XMLElement( "response", $elements ); |
---|
| 185 | |
---|
| 186 | //return $caldav_data; |
---|
| 187 | |
---|
| 188 | header( 'Content-Length: '.strlen($response) ); |
---|
| 189 | header( 'Etag: '.$dav_name ); |
---|
| 190 | $request->DoResponse( 200, ($request->method == 'HEAD' ? '' : $response), 'text/vcard; charset="ISO-8859-1"' ); |
---|
| 191 | // return $status; |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | dbg_error_log("REPORT","Building XML REQUESTEEEE PATHHHHHHH '%s'", $request->path ); |
---|
| 195 | $part= explode('/',$request->path); |
---|
| 196 | $id=preg_split('/@/', preg_replace("/.vcf/","",$part[3])); |
---|
| 197 | //$id = $dav_resource->GetProperty('user_no'); |
---|
| 198 | //$mode = 'carddav'; |
---|
| 199 | //$qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget'); |
---|
| 200 | //include("caldav-REPORT-multiget-addressbook.php"); |
---|
| 201 | $sql = "SELECT * FROM phpgw_cc_contact WHERE id_owner= :nome AND id_contact= :id"; |
---|
| 202 | $qry = new AwlQuery( $sql,array(':nome' => $id[1],':id' => $id[0])); |
---|
| 203 | if ( $qry->Exec("REPORT-MULTIGET",__LINE__,__FILE__) && $qry->rows() > 0 ) { |
---|
| 204 | while( $contacts_object = $qry->Fetch() ) { |
---|
| 205 | //if ( $bound_from != $collection->dav_name() ) { |
---|
| 206 | // $contacts_object->dav_name = str_replace( $bound_from, $collection->dav_name(), $contacts_object->dav_name); |
---|
| 207 | // } |
---|
| 208 | //if ( $need_expansion ) { |
---|
| 209 | // $vResource = new vComponent($contacts_object->caldav_data); |
---|
| 210 | // $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end); |
---|
| 211 | // $contacts_object->caldav_data = $expanded->Render(); |
---|
| 212 | // } |
---|
| 213 | |
---|
| 214 | //$responses[] = contacts_to_xml( $properties, $contacts_object); |
---|
| 215 | contacts_to_xml( $properties, $contacts_object); |
---|
| 216 | |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|