/** * Resolves recipient from the GAL and gets his certificates. * * @param string $to * @return SyncResolveRecipient|boolean */ private function resolveRecipientGAL($to) { $addrbook = $this->getAddressbook(); $ab_entryid = mapi_ab_getdefaultdir($addrbook); if ($ab_entryid) { $ab_dir = mapi_ab_openentry($addrbook, $ab_entryid); } if ($ab_dir) { $table = mapi_folder_getcontentstable($ab_dir); } // if (!$table) // throw new StatusException(sprintf("ZarafaBackend->resolveRecipient(): could not open addressbook: 0x%X", mapi_last_hresult()), SYNC_RESOLVERECIPSSTATUS_RESPONSE_UNRESOLVEDRECIP); if (!$table) { ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to open addressbook:0x%X", mapi_last_hresult())); return false; } $restriction = MAPIUtils::GetSearchRestriction(u2w($to)); mapi_table_restrict($table, $restriction); $querycnt = mapi_table_getrowcount($table); if ($querycnt > 0) { $abentries = mapi_table_queryrows($table, array(PR_DISPLAY_NAME, PR_EMS_AB_TAGGED_X509_CERT), 0, 1); $certificates = isset($abentries[0][PR_EMS_AB_TAGGED_X509_CERT]) && is_array($abentries[0][PR_EMS_AB_TAGGED_X509_CERT]) && count($abentries[0][PR_EMS_AB_TAGGED_X509_CERT]) ? $this->getCertificates($abentries[0][PR_EMS_AB_TAGGED_X509_CERT], $querycnt) : false; if ($certificates === false) { // the recipient does not have a valid certificate, set the appropriate status ZLog::Write(LOGLEVEL_INFO, sprintf("No certificates found for '%s'", $to)); $certificates = $this->getCertificates(false); } $recipient = $this->createResolveRecipient(SYNC_RESOLVERECIPIENTS_TYPE_GAL, w2u($abentries[0][PR_DISPLAY_NAME]), $to, $certificates); return $recipient; } else { ZLog::Write(LOGLEVEL_WARN, sprintf("No recipient found for: '%s'", $to)); return SYNC_RESOLVERECIPSSTATUS_RESPONSE_UNRESOLVEDRECIP; } return false; }
/** * Searches the GAB of Zarafa * Can be overwitten globally by configuring a SearchBackend * * @param string $searchquery * @param string $searchrange * * @access public * @return array * @throws StatusException */ public function GetGALSearchResults($searchquery, $searchrange) { // only return users from who the displayName or the username starts with $name //TODO: use PR_ANR for this restriction instead of PR_DISPLAY_NAME and PR_ACCOUNT $addrbook = mapi_openaddressbook($this->session); if ($addrbook) { $ab_entryid = mapi_ab_getdefaultdir($addrbook); } if ($ab_entryid) { $ab_dir = mapi_ab_openentry($addrbook, $ab_entryid); } if ($ab_dir) { $table = mapi_folder_getcontentstable($ab_dir); } if (!$table) { throw new StatusException(sprintf("ZarafaBackend->GetGALSearchResults(): could not open addressbook: 0x%X", mapi_last_hresult()), SYNC_SEARCHSTATUS_STORE_CONNECTIONFAILED); } $restriction = MAPIUtils::GetSearchRestriction(u2w($searchquery)); mapi_table_restrict($table, $restriction); mapi_table_sort($table, array(PR_DISPLAY_NAME => TABLE_SORT_ASCEND)); if (mapi_last_hresult()) { throw new StatusException(sprintf("ZarafaBackend->GetGALSearchResults(): could not apply restriction: 0x%X", mapi_last_hresult()), SYNC_SEARCHSTATUS_STORE_TOOCOMPLEX); } //range for the search results, default symbian range end is 50, wm 99, //so we'll use that of nokia $rangestart = 0; $rangeend = 50; if ($searchrange != '0') { $pos = strpos($searchrange, '-'); $rangestart = substr($searchrange, 0, $pos); $rangeend = substr($searchrange, $pos + 1); } $items = array(); $querycnt = mapi_table_getrowcount($table); //do not return more results as requested in range $querylimit = $rangeend + 1 < $querycnt ? $rangeend + 1 : $querycnt; $items['range'] = $querylimit > 0 ? $rangestart . '-' . ($querylimit - 1) : '0-0'; $items['searchtotal'] = $querycnt; if ($querycnt > 0) { $abentries = mapi_table_queryrows($table, array(PR_ACCOUNT, PR_DISPLAY_NAME, PR_SMTP_ADDRESS, PR_BUSINESS_TELEPHONE_NUMBER, PR_GIVEN_NAME, PR_SURNAME, PR_MOBILE_TELEPHONE_NUMBER, PR_HOME_TELEPHONE_NUMBER, PR_TITLE, PR_COMPANY_NAME, PR_OFFICE_LOCATION), $rangestart, $querylimit); } for ($i = 0; $i < $querylimit; $i++) { $items[$i][SYNC_GAL_DISPLAYNAME] = w2u($abentries[$i][PR_DISPLAY_NAME]); if (strlen(trim($items[$i][SYNC_GAL_DISPLAYNAME])) == 0) { $items[$i][SYNC_GAL_DISPLAYNAME] = w2u($abentries[$i][PR_ACCOUNT]); } $items[$i][SYNC_GAL_ALIAS] = w2u($abentries[$i][PR_ACCOUNT]); //it's not possible not get first and last name of an user //from the gab and user functions, so we just set lastname //to displayname and leave firstname unset //this was changed in Zarafa 6.40, so we try to get first and //last name and fall back to the old behaviour if these values are not set if (isset($abentries[$i][PR_GIVEN_NAME])) { $items[$i][SYNC_GAL_FIRSTNAME] = w2u($abentries[$i][PR_GIVEN_NAME]); } if (isset($abentries[$i][PR_SURNAME])) { $items[$i][SYNC_GAL_LASTNAME] = w2u($abentries[$i][PR_SURNAME]); } if (!isset($items[$i][SYNC_GAL_LASTNAME])) { $items[$i][SYNC_GAL_LASTNAME] = $items[$i][SYNC_GAL_DISPLAYNAME]; } $items[$i][SYNC_GAL_EMAILADDRESS] = w2u($abentries[$i][PR_SMTP_ADDRESS]); //check if an user has an office number or it might produce warnings in the log if (isset($abentries[$i][PR_BUSINESS_TELEPHONE_NUMBER])) { $items[$i][SYNC_GAL_PHONE] = w2u($abentries[$i][PR_BUSINESS_TELEPHONE_NUMBER]); } //check if an user has a mobile number or it might produce warnings in the log if (isset($abentries[$i][PR_MOBILE_TELEPHONE_NUMBER])) { $items[$i][SYNC_GAL_MOBILEPHONE] = w2u($abentries[$i][PR_MOBILE_TELEPHONE_NUMBER]); } //check if an user has a home number or it might produce warnings in the log if (isset($abentries[$i][PR_HOME_TELEPHONE_NUMBER])) { $items[$i][SYNC_GAL_HOMEPHONE] = w2u($abentries[$i][PR_HOME_TELEPHONE_NUMBER]); } if (isset($abentries[$i][PR_COMPANY_NAME])) { $items[$i][SYNC_GAL_COMPANY] = w2u($abentries[$i][PR_COMPANY_NAME]); } if (isset($abentries[$i][PR_TITLE])) { $items[$i][SYNC_GAL_TITLE] = w2u($abentries[$i][PR_TITLE]); } if (isset($abentries[$i][PR_OFFICE_LOCATION])) { $items[$i][SYNC_GAL_OFFICE] = w2u($abentries[$i][PR_OFFICE_LOCATION]); } } return $items; }
/** * Resolves recipient from the GAL and gets his certificates. * * @param string $to * @param int $maxAmbiguousRecipients * @return array|boolean */ private function resolveRecipientGAL($to, $maxAmbiguousRecipients) { ZLog::Write(LOGLEVEL_WBXML, sprintf("Resolving recipient '%s' in GAL", $to)); $addrbook = $this->getAddressbook(); // FIXME: create a function to get the adressbook contentstable $ab_entryid = mapi_ab_getdefaultdir($addrbook); if ($ab_entryid) { $ab_dir = mapi_ab_openentry($addrbook, $ab_entryid); } if ($ab_dir) { $table = mapi_folder_getcontentstable($ab_dir); } if (!$table) { ZLog::Write(LOGLEVEL_WARN, sprintf("Unable to open addressbook:0x%X", mapi_last_hresult())); return false; } $restriction = MAPIUtils::GetSearchRestriction(u2w($to)); mapi_table_restrict($table, $restriction); $querycnt = mapi_table_getrowcount($table); if ($querycnt > 0) { $recipientGal = array(); $rowsToQuery = $maxAmbiguousRecipients; // some devices request 0 ambiguous recipients if ($querycnt == 1 && $maxAmbiguousRecipients == 0) { $rowsToQuery = 1; } elseif ($querycnt > 1 && $maxAmbiguousRecipients == 0) { ZLog::Write(LOGLEVEL_INFO, sprintf("GAL search found %d recipients but the device hasn't requested ambiguous recipients", $querycnt)); return $recipientGal; } // get the certificate every time because caching the certificate is less expensive than opening addressbook entry again $abentries = mapi_table_queryrows($table, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_EMS_AB_TAGGED_X509_CERT, PR_OBJECT_TYPE), 0, $rowsToQuery); for ($i = 0, $nrEntries = count($abentries); $i < $nrEntries; $i++) { if ($abentries[$i][PR_OBJECT_TYPE] == MAPI_DISTLIST) { // dist lists must be expanded into their members ZLog::Write(LOGLEVEL_DEBUG, sprintf("'%s' is a dist list. Expand it to members.", $to)); $distList = mapi_ab_openentry($addrbook, $abentries[$i][PR_ENTRYID]); $distListContent = mapi_folder_getcontentstable($distList); $distListMembers = mapi_table_queryallrows($distListContent, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_EMS_AB_TAGGED_X509_CERT)); for ($j = 0, $nrDistListMembers = mapi_table_getrowcount($distListContent); $j < $nrDistListMembers; $j++) { ZLog::Write(LOGLEVEL_WBXML, sprintf("distlist's '%s' member", $to, $distListMembers[$j][PR_DISPLAY_NAME])); $recipientGal[] = $this->createResolveRecipient(SYNC_RESOLVERECIPIENTS_TYPE_GAL, $to, $distListMembers[$j], $nrDistListMembers); } } elseif ($abentries[$i][PR_OBJECT_TYPE] == MAPI_MAILUSER) { $recipientGal[] = $this->createResolveRecipient(SYNC_RESOLVERECIPIENTS_TYPE_GAL, $to, $abentries[$i]); } } ZLog::Write(LOGLEVEL_WBXML, "Found a recipient in GAL"); return $recipientGal; } else { ZLog::Write(LOGLEVEL_WARN, sprintf("No recipient found for: '%s' in GAL", $to)); return SYNC_RESOLVERECIPSSTATUS_RESPONSE_UNRESOLVEDRECIP; } return false; }