Beispiel #1
0
 /**
  * Returns message stats, analogous to the folder stats from StatFolder().
  *
  * @param string        $folderid       id of the folder
  * @param string        $id             id of the message
  *
  * @access public
  * @return array
  */
 public function StatMessage($folderid, $id)
 {
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->StatMessage('%s', '%s')", $folderid, $id));
     $message = array();
     if (!isset($this->contactsetag[$id])) {
         $addressbookId = $this->getAddressbookIdFromVcard($id);
         $vcardId = $this->getVcardId($id);
         $addressbookUrl = $this->getAddressbookFromId($addressbookId);
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV->StatMessage - No contactsetag found, getting vcard '%s' in '%s'", $vcardId, $addressbookId));
         if ($addressbookUrl !== false) {
             $xml_vcard = false;
             try {
                 $this->server->set_url($addressbookUrl);
                 $xml_vcard = $this->server->get_xml_vcard($vcardId);
             } catch (Exception $ex) {
                 ZLog::Write(LOGLEVEL_ERROR, sprintf("BackendCardDAV->StatMessage - Error getting vcard '%s' in '%s': %s", $vcardId, $addressbookId, $ex->getMessage()));
             }
             if ($xml_vcard !== false) {
                 $vcard = new SimpleXMLElement($xml_vcard);
                 $this->contactsetag[$id] = $vcard->element[0]->etag->__toString();
                 unset($vcard);
             }
             unset($xml_vcard);
         }
     }
     $message["mod"] = $this->contactsetag[$id];
     $message["id"] = $id;
     $message["flags"] = 1;
     return $message;
 }
 /**
  * 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;
 }
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";