Beispiel #1
0
 /**
  * Discover all the addressbooks collections for a user under a root.
  *
  */
 private function discoverAddressbooks()
 {
     unset($this->addressbooks);
     $this->addressbooks = array();
     $raw = $this->server->get(false, false, true);
     if ($raw !== false) {
         $xml = new SimpleXMLElement($raw);
         foreach ($xml->addressbook_element as $response) {
             if ($this->gal_url !== false) {
                 if (strcmp(urldecode($response->url), $this->gal_url) == 0) {
                     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::discoverAddressbooks() Ignoring GAL addressbook '%s'", $this->gal_url));
                     continue;
                 }
             }
             ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::discoverAddressbooks() Found addressbook '%s'", urldecode($response->url)));
             $this->addressbooks[] = urldecode($response->url);
         }
         unset($xml);
     }
 }
 /**
  * Synchronize CardDAV-Addressbook
  *
  * @param  array   $server             CardDAV server array
  * @param  integer $carddav_contact_id CardDAV contact id
  * @param  string  $vcard_id           vCard id
  * @return boolean                     if no error occurred "true" else "false"
  */
 function carddav_addressbook_sync($server, $carddav_contact_id = null, $vcard_id = null, $start = 0, $timeout = 29)
 {
     $any_data_synced = false;
     $this->write_log('Starting CardDAV-Addressbook synchronization');
     $carddav_backend = new carddav_backend($server['url']);
     if ($this->rc->decrypt($server['password']) == '%p') {
         $server['password'] = $this->rcpassword;
     }
     $carddav_backend->set_auth($server['username'], $this->rc->decrypt($server['password']), $server['authtype']);
     if ($carddav_backend->check_connection()) {
         $this->write_log('Connected to the CardDAV-Server ' . $server['url']);
         if ($vcard_id !== null) {
             // server does not support REPORT request
             $noreport = $this->rc->config->get('carddav_noreport', array());
             if ($noreport[$server['url']]) {
                 $elements = $carddav_backend->get_xml_vcard($vcard_id);
             } else {
                 $elements = false;
             }
             try {
                 $xml = new SimpleXMLElement($elements);
                 if (count($xml->element) < 1) {
                     $elements = false;
                 }
             } catch (Exception $e) {
                 $elements = false;
             }
             if ($elements === false) {
                 $elements = $carddav_backend->get(false);
                 $this->filter = null;
                 $carddav_addressbook_contacts = $this->get_carddav_addressbook_contacts();
                 if (!isset($noreport[$server['url']])) {
                     $a_prefs['carddav_noreport'][$server['url']] = 1;
                     $this->rc->user->save_prefs($a_prefs);
                 }
             } else {
                 if ($carddav_contact_id) {
                     $carddav_addressbook_contact = $this->get_carddav_addressbook_contact($carddav_contact_id);
                     $carddav_addressbook_contacts = array($carddav_addressbook_contact['vcard_id'] => $carddav_addressbook_contact);
                 }
             }
         } else {
             $elements = $carddav_backend->get(false);
             $carddav_addressbook_contacts = $this->get_carddav_addressbook_contacts();
         }
         try {
             $xml = new SimpleXMLElement($elements);
             if (!empty($xml->element)) {
                 foreach ($xml->element as $element) {
                     if ($start != 0 && time() - $start >= $timeout) {
                         return null;
                     }
                     $element_id = (string) $element->id;
                     $element_etag = (string) $element->etag;
                     $element_last_modified = (string) $element->last_modified;
                     if (isset($carddav_addressbook_contacts[$element_id])) {
                         if ($carddav_addressbook_contacts[$element_id]['etag'] != $element_etag || $carddav_addressbook_contacts[$element_id]['last_modified'] != $element_last_modified) {
                             $carddav_content = array('vcard' => $carddav_backend->get_vcard($element_id), 'vcard_id' => $element_id, 'etag' => $element_etag, 'last_modified' => $element_last_modified);
                             if ($this->carddav_addressbook_update($carddav_content)) {
                                 $any_data_synced = true;
                             }
                         }
                     } else {
                         $carddav_content = array('vcard' => $carddav_backend->get_vcard($element_id), 'vcard_id' => $element_id, 'etag' => $element_etag, 'last_modified' => $element_last_modified);
                         if (!empty($carddav_content['vcard'])) {
                             if ($this->carddav_addressbook_add($carddav_content)) {
                                 $any_data_synced = true;
                             }
                         }
                     }
                     unset($carddav_addressbook_contacts[$element_id]);
                 }
             } else {
                 $logging_message = 'No CardDAV XML-Element found!';
                 if ($carddav_contact_id !== null && $vcard_id !== null) {
                     $this->write_log($logging_message . ' The CardDAV-Server does not have a contact with the vCard id ' . $vcard_id);
                 } else {
                     $this->write_log($logging_message . ' The CardDAV-Server seems to have no contacts');
                 }
             }
             if (!empty($carddav_addressbook_contacts)) {
                 foreach ($carddav_addressbook_contacts as $vcard_id => $etag) {
                     if ($this->carddav_addressbook_delete($vcard_id)) {
                         $any_data_synced = true;
                     }
                 }
             }
             if ($any_data_synced === false) {
                 $this->write_log('all CardDAV-Data are synchronous, nothing todo!');
             }
             $this->write_log('Syncronization complete!');
         } catch (Exception $e) {
             $this->write_log('CardDAV-Server XML-Response is malformed. Synchronization aborted!');
             return false;
         }
     } else {
         $this->write_log('Couldn\'t connect to the CardDAV-Server ' . $server['url']);
         return false;
     }
     return true;
 }
Beispiel #3
0
 
 	We don't want to be hitting the CardDAV server with every single
 	lookup request, so we first fetch a lightweight list of contacts
 	and times modified, then we download any CardDAV Vcards we don't
 	have, or which has been updated.
 
 	Once we have the Vcards, we save them to disk as a file per VCard
 	which we can then search through when a lookup is required.
 
 	We cache the lookup results in memory for the running session
 	to avoid too many file searches.
 */
 // download the list of address book objects
 // (we just grab the IDs and date modified here)
 try {
     $address_list_xml = @$carddav->get(false, false);
 } catch (Exception $e) {
     $log->error("[contacts] Error from CardDAV: {$e->getMessage()}");
     $log->error_fatal("[contacts] A fatal error occured whilst attempting to fetch contacts from CardDAV server");
 }
 $address_list_array = xmlstr_to_array($address_list_xml);
 $address_list_update = array();
 $address_list_all = array();
 // run through the list and see which ones need updating
 foreach ($address_list_array["element"] as $address_list_entry) {
     $timestamp = null;
     if (preg_match("/^([0-9]*)-([0-9]*)-([0-9]*)T([0-9]*):([0-9]*):([0-9]*)/", $address_list_entry["etag"], $matches)) {
         $timestamp = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
     }
     $filename = $config["SMStoXMPP"]["contacts_store"] . "/" . $address_list_entry["id"] . ".vcf";
     if (file_exists($filename)) {
$password = "******";
$domain = "";
$url = CARDDAV_PROTOCOL . '://' . CARDDAV_SERVER . ':' . CARDDAV_PORT . str_replace("%d", $domain, str_replace("%u", $username, CARDDAV_PATH));
$default_url = CARDDAV_PROTOCOL . '://' . CARDDAV_SERVER . ':' . CARDDAV_PORT . str_replace("%d", $domain, str_replace("%u", $username, CARDDAV_DEFAULT_PATH));
if (defined('CARDDAV_GAL_PATH')) {
    $gal_url = CARDDAV_PROTOCOL . '://' . CARDDAV_SERVER . ':' . CARDDAV_PORT . str_replace("%d", $domain, str_replace("%u", $username, CARDDAV_GAL_PATH));
} else {
    $gal_url = false;
}
echo "{$url}\n";
echo "{$default_url}\n";
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);
}