Example #1
0
 /**
  * Queries the CardDAV backend
  *
  * @param string        $searchquery        string to be searched for
  * @param string        $searchrange        specified searchrange
  *
  * @access public
  * @return array        search results
  */
 public function GetGALSearchResults($searchquery, $searchrange)
 {
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->GetGALSearchResults(%s, %s)", $searchquery, $searchrange));
     if ($this->gal_url !== false && $this->server !== false) {
         // Don't search if the length is < 5, we are typing yet
         if (strlen($searchquery) < CARDDAV_GAL_MIN_LENGTH) {
             return false;
         }
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->GetGALSearchResults searching: %s", $this->url));
         try {
             ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->GetGALSearchResults server is null? %d", $this->server == null));
             $this->server->set_url($this->gal_url);
             $vcards = $this->server->search_vcards(str_replace("<", "", str_replace(">", "", $searchquery)), 15, true, false, defined('CARDDAV_SUPPORTS_FN_SEARCH') ? CARDDAV_SUPPORTS_FN_SEARCH : false);
         } catch (Exception $e) {
             $vcards = false;
             ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendCardDAV->GetGALSearchResults : Error in search %s", $e->getMessage()));
         }
         if ($vcards === false) {
             ZLog::Write(LOGLEVEL_ERROR, "BackendCardDAV->GetGALSearchResults : Error in search query. Search aborted");
             return false;
         }
         $xml_vcards = new SimpleXMLElement($vcards);
         unset($vcards);
         // 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();
         // TODO the limiting of the searchresults could be refactored into Utils as it's probably used more than once
         $querycnt = $xml_vcards->count();
         //do not return more results as requested in range
         $querylimit = $rangeend + 1 < $querycnt ? $rangeend + 1 : $querycnt == 0 ? 1 : $querycnt;
         $items['range'] = $rangestart . '-' . ($querylimit - 1);
         $items['searchtotal'] = $querycnt;
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->GetGALSearchResults : %s entries found, returning %s to %s", $querycnt, $rangestart, $querylimit));
         $i = 0;
         $rc = 0;
         foreach ($xml_vcards->element as $xml_vcard) {
             if ($i >= $rangestart && $i < $querylimit) {
                 $contact = $this->ParseFromVCard($xml_vcard->vcard->__toString());
                 if ($contact === false) {
                     ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendCardDAV->GetGALSearchResults : error converting vCard to AS contact\n%s\n", $xml_vcard->vcard->__toString()));
                 } else {
                     $items[$rc][SYNC_GAL_EMAILADDRESS] = $contact->email1address;
                     if (isset($contact->fileas)) {
                         $items[$rc][SYNC_GAL_DISPLAYNAME] = $contact->fileas;
                     } else {
                         if (isset($contact->firstname) || isset($contact->middlename) || isset($contact->lastname)) {
                             $items[$rc][SYNC_GAL_DISPLAYNAME] = $contact->firstname . (isset($contact->middlename) ? " " . $contact->middlename : "") . (isset($contact->lastname) ? " " . $contact->lastname : "");
                         } else {
                             $items[$rc][SYNC_GAL_DISPLAYNAME] = $contact->email1address;
                         }
                     }
                     if (isset($contact->firstname)) {
                         $items[$rc][SYNC_GAL_FIRSTNAME] = $contact->firstname;
                     } else {
                         $items[$rc][SYNC_GAL_FIRSTNAME] = "";
                     }
                     if (isset($contact->lastname)) {
                         $items[$rc][SYNC_GAL_LASTNAME] = $contact->lastname;
                     } else {
                         $items[$rc][SYNC_GAL_LASTNAME] = "";
                     }
                     if (isset($contact->business2phonenumber)) {
                         $items[$rc][SYNC_GAL_PHONE] = $contact->business2phonenumber;
                     }
                     if (isset($contact->home2phonenumber)) {
                         $items[$rc][SYNC_GAL_HOMEPHONE] = $contact->home2phonenumber;
                     }
                     if (isset($contact->mobilephonenumber)) {
                         $items[$rc][SYNC_GAL_MOBILEPHONE] = $contact->mobilephonenumber;
                     }
                     if (isset($contact->title)) {
                         $items[$rc][SYNC_GAL_TITLE] = $contact->title;
                     }
                     if (isset($contact->companyname)) {
                         $items[$rc][SYNC_GAL_COMPANY] = $contact->companyname;
                     }
                     if (isset($contact->department)) {
                         $items[$rc][SYNC_GAL_OFFICE] = $contact->department;
                     }
                     if (isset($contact->nickname)) {
                         $items[$rc][SYNC_GAL_ALIAS] = $contact->nickname;
                     }
                     unset($contact);
                     $rc++;
                 }
             }
             $i++;
         }
         unset($xml_vcards);
         return $items;
     } else {
         unset($xml_vcards);
         return false;
     }
 }
echo "{$gal_url}\n";
$server = new carddav_backend($url);
$server->set_auth($username, $password);
//$server->enable_debug();
$raw = $server->get(false, false, true);
echo "{$raw}\n";
//var_dump($server->get_debug());
if ($raw !== false) {
    $xml = new SimpleXMLElement($raw);
    foreach ($xml->addressbook_element as $response) {
        if ($gal_url !== false) {
            if (strcmp(urldecode($response->url), $gal_url) == 0) {
                echo sprintf("BackendCardDAV::discoverAddressbooks() Ignoring GAL addressbook '%s'\n", $this->gal_url);
                continue;
            }
        }
        echo sprintf("BackendCardDAV::discoverAddressbooks() Found addressbook '%s'\n", urldecode($response->url));
    }
    unset($xml);
}
//$server->enable_debug();
$server->set_url($default_url);
$vcards = $server->do_sync(true, false, false);
//var_dump($server->get_debug());
echo "{$vcards}\n";
echo "-----------\n";
//$server->enable_debug();
// TODO: set to an existing vcard ID (you will get a list with the do_sync operation
$xml = $server->get_xml_vcard('131-52C19B00-7-7A512880');
//var_dump($server->get_debug());
echo "{$xml}\n";