1 // CardDAV2.cpp - CardDAV v2 class
3 // (c) 2012-2016 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
23 CardDAV2::CardDAV2(string ServerAddress, int ServerPort, string ServerUser, string ServerPass, bool ServerSSL){
25 this->ServerAddress = ServerAddress;
26 this->ServerPort = ServerPort;
27 this->ServerUser = ServerUser;
28 this->ServerPass = ServerPass;
29 this->ServerSSL = ServerSSL;
32 this->SetupConnectionObject();
36 CardDAV2::CardDAV2(string ServerAddress, int ServerPort, string ServerUser, string ServerPass, bool ServerSSL, string ServerPrefix, string ServerAccount){
38 this->ServerAddress = ServerAddress;
39 this->ServerPort = ServerPort;
40 this->ServerUser = ServerUser;
41 this->ServerPass = ServerPass;
42 this->ServerSSL = ServerSSL;
43 this->ServerPrefix = ServerPrefix;
44 this->ServerAccount = ServerAccount;
48 this->SetupConnectionObject();
52 size_t CardDAV2::WritebackFunc(char *ptr, size_t size, size_t nmemb, void *stream){
54 return static_cast<CardDAV2PassObject*>(stream)->CardDAV2Object->WritebackFuncImplementation(ptr, size, nmemb, stream);
58 size_t CardDAV2::WritebackFuncImplementation(char *ptr, size_t size, size_t nmemb, void *stream){
60 // Writeback function for the CardDAV object.
62 CardDAV2PassObject *data = static_cast<CardDAV2PassObject*>(stream);
63 data->DataSetting->append(ptr);
65 // Get the SSL engine pointer and trust if required on certain operating systems.
67 if (data->ServerUsingSSL == true) {
69 #if defined(__APPLE__)
71 const struct curl_tlssessioninfo *TLSInfo;
73 TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo);
75 SecTrustRef CertificateData;
77 if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) {
78 SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &CertificateData);
79 data->SSLContext = CertificateData;
82 #elif defined(__WIN32__)
84 const struct curl_tlssessioninfo *TLSInfo;
86 TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo);
88 if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) {
90 // Free the previous certificate data.
92 //CertFreeCertificateContext(CertificateData);
94 PCCERT_CONTEXT CertificateData;
96 PCtxtHandle SSLHandle = (PCtxtHandle)TLSInfo->internals;
97 SECURITY_STATUS GetData = QueryContextAttributes(SSLHandle, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &CertificateData);
99 data->SSLContext = CertificateData;
111 void CardDAV2::SetCertificateData() {
115 CardDAV2::~CardDAV2(){
117 curl_easy_cleanup(ConnectionSession);
118 ConnectionSession = nullptr;
120 if (HeaderList != nullptr){
121 curl_slist_free_all(HeaderList);
122 HeaderList = nullptr;
125 #if defined(__WIN32__)
127 if (CertificateData != nullptr) {
129 CertFreeCertificateContext(CertificateData);
137 #if defined(__APPLE__)
139 SecTrustRef CardDAV2::BuildSSLCollection(){
141 return CertificateData;
145 #elif defined(__WIN32__)
147 PCCERT_CONTEXT CardDAV2::BuildSSLCollection(){
149 return CertificateData;
155 SSLCertCollectionString CardDAV2::BuildSSLCollection(){
157 // Build and return the SSL collection.
159 cout << "Build SSL Collection!" << endl;
161 SSLCertCollectionString SSLCertInfo;
163 // Grab the certificate data.
166 struct curl_slist *certdata;
167 struct curl_certinfo *certinfo;
170 certptr.certdata = NULL;
172 CURLcode result = curl_easy_getinfo(ConnectionSession, CURLINFO_CERTINFO, &certptr.certinfo);
174 cout << certptr.certinfo->num_of_certs << endl;
176 std::string CertPropName;
177 std::string CertPropValue;
179 for (int i = 0; i < certptr.certinfo->num_of_certs; i++){
181 struct curl_slist *slist;
182 SSLCertDataString SSLCertDataInc;
184 for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){
186 // Using wxStringTokenizer from wxWidgets.
188 wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");
190 // Get first token as the property name.
192 CertPropName = CertDataInc.GetNextToken().ToStdString();
194 // Get remaining tokens as the property value.
196 while(CertDataInc.HasMoreTokens()){
198 CertPropValue.append(CertDataInc.GetNextToken());
202 SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));
203 CertPropName.clear();
204 CertPropValue.clear();
208 SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));
218 void CardDAV2::BypassSSLVerification(bool EnableBypass) {
219 EnableSSLBypass = EnableBypass;
220 SSLSelfSigned = EnableBypass;
223 void CardDAV2::SetupConnectionObject(){
224 ConnectionSession = curl_easy_init();
227 bool CardDAV2::IsTaskCompleted(){
231 COConnectResult CardDAV2::Connect(bool DoAuthentication){
233 ServerSSL ? SetupDefaultParametersSSL(DoAuthentication) : SetupDefaultParametersNonSSL(DoAuthentication);
236 COConnectResult ConnectResult = COCONNECT_UNITTESTFAIL;
237 string ServerAddressURL = BuildURL("/principals/");
239 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
241 if (TestMode == true){
242 SessionResult = curl_easy_perform(ConnectionSession);
244 SessionResult = curl_easy_perform(ConnectionSession);
247 switch(SessionResult){
249 case CURLE_HTTP_RETURNED_ERROR:
251 SSLVerified = COSSL_VERIFIED;
252 ConnectResult = COCONNECT_OK;
254 case CURLE_SSL_CACERT:
255 case CURLE_SSL_CONNECT_ERROR:
257 ConnectResult = COCONNECT_OK;
258 SSLVerified = COSSL_UNABLETOVERIFY;
261 ConnectResult = COCONNECT_INVALID;
265 // Set the certificate data (if required).
267 #if defined(__APPLE__)
271 CertificateData = PageHeaderObject.SSLContext;
275 #elif defined(__WIN32__)
279 CertificateData = PageHeaderObject.SSLContext;
285 // Check if an error occured before continuing.
287 // Check if authentication was successful.
289 long SessionResponseCode = 0;
291 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
293 if (DoAuthentication == true){
295 // Get the HTTP status code (Should be 200 and not 403).
296 // Return error otherwise.
298 if (SessionResponseCode == 200){
299 ConnectResult = COCONNECT_OK;
301 ValidResponse = true;
302 } else if (SessionResponseCode == 401){
303 ConnectResult = COCONNECT_AUTHFAIL;
305 ValidResponse = true;
306 } else if (SessionResponseCode >= 200) {
307 ConnectResult = COCONNECT_INVALID;
309 ValidResponse = true;
311 ConnectResult = COCONNECT_INVALID;
313 ValidResponse = false;
318 ValidResponse = true;
322 // Check the header to see if CardDAV is supported.
324 vector<string> DAVHeaderValues = GetDAVHeader();
326 for (vector<string>::iterator DAVHeaderValuesIter = DAVHeaderValues.begin();
327 DAVHeaderValuesIter != DAVHeaderValues.end(); DAVHeaderValuesIter++){
329 if ((*DAVHeaderValuesIter) == "addressbook"){
336 return ConnectResult;
340 COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){
342 // Check if authentication was successful, otherwise don't do anything.
344 COServerResponse ServerResponse;
346 if (AuthPassed == false){
347 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
348 ServerResponse.EntityTag = "";
349 ServerResponse.SessionCode = 0;
350 ServerResponse.ResultCode = 0;
351 ServerResponse.ResultMessage = "";
352 return ServerResponse;
355 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
358 // Need to do three requests:
360 // 1. Get the current user principal URI.
361 // 2. Get the address book home URI.
362 // 3. Get the default address book URI.
364 // Setup the first query finding out where the principal URL is.
366 const char* CurrentUserPrincipalXMLQuery = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
367 "<D:propfind xmlns:D=\"DAV:\">\n"
369 " <D:current-user-principal/>\n"
373 // Setup the second query finding out where the address book home URL is.
375 const char* AddressBookHomeXMLQuery = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
376 "<D:propfind xmlns:D=\"DAV:\""
377 " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
379 " <C:addressbook-home-set/>\n"
383 // Setup the third query finding out where the default address book URL is.
385 const char* DefaultAddressBookXMLQuery = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
386 "<D:propfind xmlns:D=\"DAV:\""
387 " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
389 " <C:default-addressbook-URL/>\n"
393 string ServerAddressURL = BuildURL("/principals/");
394 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
395 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PROPFIND");
396 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, CurrentUserPrincipalXMLQuery);
397 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(CurrentUserPrincipalXMLQuery));
399 if (TestMode == true){
400 SessionResult = curl_easy_perform(ConnectionSession);
405 switch(SessionResult){
408 SSLVerified = COSSL_VERIFIED;
410 case CURLE_SSL_CACERT:
411 case CURLE_SSL_CONNECT_ERROR:
413 SSLVerified = COSSL_UNABLETOVERIFY;
419 long SessionResponseCode = 0;
421 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
423 if (SessionResponseCode == 200 || SessionResponseCode == 207){
425 ValidResponse = true;
426 } else if (SessionResponseCode == 403){
428 ValidResponse = true;
429 } else if (SessionResponseCode >= 400) {
431 ValidResponse = true;
434 ValidResponse = false;
437 if (ValidResponse == false && AuthPassed == false){
438 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
439 ServerResponse.EntityTag = "";
440 ServerResponse.SessionCode = SessionResult;
441 ServerResponse.ResultCode = SessionResponseCode;
442 ServerResponse.ResultMessage = "";
443 return ServerResponse;
446 // Process the first response.
448 string UserPrincipalURI = GetUserPrincipalURI();
450 // Cleanup and reset for the second connection.
452 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
455 ServerAddressURL = BuildURL(UserPrincipalURI);
456 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
457 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PROPFIND");
458 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, AddressBookHomeXMLQuery);
459 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(AddressBookHomeXMLQuery));
461 if (TestMode == true){
462 SessionResult = curl_easy_perform(ConnectionSession);
467 switch(SessionResult){
470 SSLVerified = COSSL_VERIFIED;
472 case CURLE_SSL_CACERT:
473 case CURLE_SSL_CONNECT_ERROR:
475 SSLVerified = COSSL_UNABLETOVERIFY;
481 SessionResponseCode = 0;
483 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
485 if (SessionResponseCode == 200 || SessionResponseCode == 207){
487 ValidResponse = true;
488 } else if (SessionResponseCode == 403){
490 ValidResponse = true;
491 } else if (SessionResponseCode >= 400) {
493 ValidResponse = true;
496 ValidResponse = false;
499 if (ValidResponse == false && AuthPassed == false){
500 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
501 ServerResponse.EntityTag = "";
502 ServerResponse.SessionCode = SessionResult;
503 ServerResponse.ResultCode = SessionResponseCode;
504 ServerResponse.ResultMessage = "";
505 return ServerResponse;
508 // Process the second response.
510 string AddressBookHomeURI = GetAddressBookHomeURI();
512 // Cleanup and reset for the second connection.
514 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
517 ServerAddressURL = BuildURL(AddressBookHomeURI);
518 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
519 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PROPFIND");
520 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, DefaultAddressBookXMLQuery);
521 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(DefaultAddressBookXMLQuery));
523 if (TestMode == true){
524 SessionResult = curl_easy_perform(ConnectionSession);
529 switch(SessionResult){
532 SSLVerified = COSSL_VERIFIED;
534 case CURLE_SSL_CACERT:
535 case CURLE_SSL_CONNECT_ERROR:
537 SSLVerified = COSSL_UNABLETOVERIFY;
543 SessionResponseCode = 0;
545 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
547 if (SessionResponseCode == 200 || SessionResponseCode == 207){
549 ValidResponse = true;
550 } else if (SessionResponseCode == 403){
552 ValidResponse = true;
553 } else if (SessionResponseCode >= 200) {
555 ValidResponse = true;
558 ValidResponse = false;
561 if (ValidResponse == false || AuthPassed == false){
562 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
563 ServerResponse.EntityTag = "";
564 ServerResponse.SessionCode = SessionResult;
565 ServerResponse.ResultCode = SessionResponseCode;
566 ServerResponse.ResultMessage = "";
567 return ServerResponse;
570 // Process the second response.
572 (*ServerPrefix) = GetDefaultAddressBookURI();
576 ServerResponse.RequestResult = COREQUEST_OK;
577 ServerResponse.EntityTag = "";
578 ServerResponse.SessionCode = SessionResult;
579 ServerResponse.ResultCode = SessionResponseCode;
580 ServerResponse.ResultMessage = SessionErrorBuffer;
581 return ServerResponse;
585 std::string CardDAV2::GetUserPrincipalURI(){
587 xmlDocPtr xmlCardDAVDoc;
588 xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0);
589 string UserPrincipalURI = "";
591 xmlNodePtr nodeLevel1;
592 xmlNodePtr nodeLevel2;
593 xmlNodePtr nodeLevel3;
594 xmlNodePtr nodeLevel4;
595 xmlNodePtr nodeLevel5;
596 xmlNodePtr nodeLevel6;
597 xmlNodePtr nodeLevel7;
599 for (nodeLevel1 = xmlCardDAVDoc->children;
601 nodeLevel1 = nodeLevel1->next)
604 for (nodeLevel2 = nodeLevel1->children;
606 nodeLevel2 = nodeLevel2->next)
610 for (nodeLevel3 = nodeLevel2->children;
612 nodeLevel3 = nodeLevel3->next)
615 for (nodeLevel4 = nodeLevel3->children;
617 nodeLevel4 = nodeLevel4->next)
620 for (nodeLevel5 = nodeLevel4->children;
622 nodeLevel5 = nodeLevel5->next)
625 for (nodeLevel6 = nodeLevel5->children;
627 nodeLevel6 = nodeLevel6->next)
630 if (!xmlStrcmp(nodeLevel6->name, (const xmlChar *)"href") ||
631 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"d:href") ||
632 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"D:href")
635 // Found the <href> part so extract the principal URL address.
637 for (nodeLevel7 = nodeLevel6->children;
639 nodeLevel7 = nodeLevel7->next)
642 UserPrincipalURI = ((const char*)nodeLevel7->content);
660 xmlFreeDoc(xmlCardDAVDoc);
662 return UserPrincipalURI;
666 std::string CardDAV2::GetAddressBookHomeURI(){
668 xmlDocPtr xmlCardDAVDoc;
669 xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0);
670 string AddressBookHomeURI = "";
672 xmlNodePtr nodeLevel1;
673 xmlNodePtr nodeLevel2;
674 xmlNodePtr nodeLevel3;
675 xmlNodePtr nodeLevel4;
676 xmlNodePtr nodeLevel5;
677 xmlNodePtr nodeLevel6;
678 xmlNodePtr nodeLevel7;
680 for (nodeLevel1 = xmlCardDAVDoc->children;
682 nodeLevel1 = nodeLevel1->next)
685 for (nodeLevel2 = nodeLevel1->children;
687 nodeLevel2 = nodeLevel2->next)
691 for (nodeLevel3 = nodeLevel2->children;
693 nodeLevel3 = nodeLevel3->next)
696 for (nodeLevel4 = nodeLevel3->children;
698 nodeLevel4 = nodeLevel4->next)
701 for (nodeLevel5 = nodeLevel4->children;
703 nodeLevel5 = nodeLevel5->next)
706 for (nodeLevel6 = nodeLevel5->children;
708 nodeLevel6 = nodeLevel6->next)
711 if (!xmlStrcmp(nodeLevel6->name, (const xmlChar *)"href") ||
712 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"d:href") ||
713 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"D:href")
716 // Found the <href> part so extract the principal URL address.
718 for (nodeLevel7 = nodeLevel6->children;
720 nodeLevel7 = nodeLevel7->next)
723 AddressBookHomeURI = ((const char*)nodeLevel7->content);
741 xmlFreeDoc(xmlCardDAVDoc);
743 return AddressBookHomeURI;
747 std::string CardDAV2::GetDefaultAddressBookURI(){
749 xmlDocPtr xmlCardDAVDoc;
750 xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0);
751 string DefaultAddressBookURI = "";
753 xmlNodePtr nodeLevel1;
754 xmlNodePtr nodeLevel2;
755 xmlNodePtr nodeLevel3;
756 xmlNodePtr nodeLevel4;
757 xmlNodePtr nodeLevel5;
758 xmlNodePtr nodeLevel6;
759 xmlNodePtr nodeLevel7;
761 for (nodeLevel1 = xmlCardDAVDoc->children;
763 nodeLevel1 = nodeLevel1->next)
766 for (nodeLevel2 = nodeLevel1->children;
768 nodeLevel2 = nodeLevel2->next)
772 for (nodeLevel3 = nodeLevel2->children;
774 nodeLevel3 = nodeLevel3->next)
777 for (nodeLevel4 = nodeLevel3->children;
779 nodeLevel4 = nodeLevel4->next)
782 for (nodeLevel5 = nodeLevel4->children;
784 nodeLevel5 = nodeLevel5->next)
787 for (nodeLevel6 = nodeLevel5->children;
789 nodeLevel6 = nodeLevel6->next)
792 if (!xmlStrcmp(nodeLevel6->name, (const xmlChar *)"href") ||
793 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"d:href") ||
794 !xmlStrcmp(nodeLevel6->name, (const xmlChar *)"D:href")
797 // Found the <href> part so extract the principal URL address.
799 for (nodeLevel7 = nodeLevel6->children;
801 nodeLevel7 = nodeLevel7->next)
804 DefaultAddressBookURI = ((const char*)nodeLevel7->content);
822 xmlFreeDoc(xmlCardDAVDoc);
824 return DefaultAddressBookURI;
828 COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){
830 // Check if authentication was successful, otherwise don't do anything.
832 COServerResponse ServerResponse;
834 if (AuthPassed == false){
835 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
836 ServerResponse.EntityTag = "";
837 ServerResponse.SessionCode = 0;
838 ServerResponse.ResultCode = 0;
839 ServerResponse.ResultMessage = "";
840 return ServerResponse;
843 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
846 string ServerAddressURL = BuildURL(ServerPrefix + Location);
847 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
848 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PUT");
849 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, Data.c_str());
850 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(Data.c_str()));
852 HeaderList = curl_slist_append(HeaderList, "Content-Type: text/vcard; charset=utf-8");
854 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList);
856 if (TestMode == true){
857 SessionResult = curl_easy_perform(ConnectionSession);
859 SessionResult = curl_easy_perform(ConnectionSession);
862 switch(SessionResult){
865 SSLVerified = COSSL_VERIFIED;
867 case CURLE_SSL_CACERT:
868 case CURLE_SSL_CONNECT_ERROR:
870 SSLVerified = COSSL_UNABLETOVERIFY;
876 long SessionResponseCode = 0;
878 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
880 if (SessionResponseCode == 200 || SessionResponseCode == 201 || SessionResponseCode == 204){
882 ValidResponse = true;
883 } else if (SessionResponseCode == 403){
885 ValidResponse = true;
886 } else if (SessionResponseCode >= 400){
888 ValidResponse = true;
891 ValidResponse = false;
894 if (ValidResponse == false || AuthPassed == false){
895 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
896 ServerResponse.EntityTag = "";
897 ServerResponse.SessionCode = SessionResult;
898 ServerResponse.ResultCode = SessionResponseCode;
899 ServerResponse.ResultMessage = "";
900 return ServerResponse;
905 ServerResponse.RequestResult = COREQUEST_OK;
906 ServerResponse.EntityTag = "";
907 ServerResponse.SessionCode = SessionResult;
908 ServerResponse.ResultCode = SessionResponseCode;
909 ServerResponse.ResultMessage = SessionErrorBuffer;
910 return ServerResponse;
914 COServerResponse CardDAV2::EditContact(std::string Location, std::string Data){
916 // Check if authentication was successful, otherwise don't do anything.
918 COServerResponse ServerResponse;
920 if (AuthPassed == false){
921 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
922 ServerResponse.EntityTag = "";
923 ServerResponse.SessionCode = 0;
924 ServerResponse.ResultCode = 0;
925 ServerResponse.ResultMessage = "";
926 return ServerResponse;
929 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
932 string ServerAddressURL = BuildURL(ServerPrefix + Location);
933 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
934 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PUT");
935 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, Data.c_str());
936 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(Data.c_str()));
938 HeaderList = curl_slist_append(HeaderList, "Content-Type: text/vcard; charset=utf-8");
940 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList);
942 if (TestMode == true){
943 SessionResult = curl_easy_perform(ConnectionSession);
945 SessionResult = curl_easy_perform(ConnectionSession);
948 switch(SessionResult){
951 SSLVerified = COSSL_VERIFIED;
953 case CURLE_SSL_CACERT:
954 case CURLE_SSL_CONNECT_ERROR:
956 SSLVerified = COSSL_UNABLETOVERIFY;
962 long SessionResponseCode = 0;
964 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
966 if (SessionResponseCode == 200 || SessionResponseCode == 201 || SessionResponseCode == 204){
968 ValidResponse = true;
969 } else if (SessionResponseCode == 403){
971 ValidResponse = true;
972 } else if (SessionResponseCode >= 400){
974 ValidResponse = true;
977 ValidResponse = false;
980 if (ValidResponse == false || AuthPassed == false){
981 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
982 ServerResponse.EntityTag = "";
983 ServerResponse.SessionCode = SessionResult;
984 ServerResponse.ResultCode = SessionResponseCode;
985 ServerResponse.ResultMessage = "";
986 return ServerResponse;
991 ServerResponse.RequestResult = COREQUEST_OK;
992 ServerResponse.EntityTag = "";
993 ServerResponse.SessionCode = SessionResult;
994 ServerResponse.ResultCode = SessionResponseCode;
995 ServerResponse.ResultMessage = SessionErrorBuffer;
996 return ServerResponse;
1000 COServerResponse CardDAV2::DeleteContact(std::string Location){
1002 // Check if authentication was successful, otherwise don't do anything.
1004 COServerResponse ServerResponse;
1006 if (AuthPassed == false){
1007 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
1008 ServerResponse.EntityTag = "";
1009 ServerResponse.SessionCode = 0;
1010 ServerResponse.ResultCode = 0;
1011 ServerResponse.ResultMessage = "";
1012 return ServerResponse;
1015 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
1018 string ServerAddressURL = BuildURL(ServerPrefix + Location);
1019 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
1020 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "DELETE");
1022 if (TestMode == true){
1023 SessionResult = curl_easy_perform(ConnectionSession);
1025 SessionResult = curl_easy_perform(ConnectionSession);
1028 switch(SessionResult){
1031 SSLVerified = COSSL_VERIFIED;
1033 case CURLE_SSL_CACERT:
1034 case CURLE_SSL_CONNECT_ERROR:
1036 SSLVerified = COSSL_UNABLETOVERIFY;
1042 long SessionResponseCode = 0;
1044 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
1046 if (SessionResponseCode == 200 || SessionResponseCode == 202 || SessionResponseCode == 204){
1048 ValidResponse = true;
1049 } else if (SessionResponseCode == 403){
1051 ValidResponse = true;
1052 } else if (SessionResponseCode >= 400){
1054 ValidResponse = true;
1057 ValidResponse = false;
1060 if (ValidResponse == false || AuthPassed == false){
1061 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
1062 ServerResponse.EntityTag = "";
1063 ServerResponse.SessionCode = SessionResult;
1064 ServerResponse.ResultCode = SessionResponseCode;
1065 ServerResponse.ResultMessage = "";
1066 return ServerResponse;
1071 ServerResponse.RequestResult = COREQUEST_OK;
1072 ServerResponse.EntityTag = "";
1073 ServerResponse.SessionCode = SessionResult;
1074 ServerResponse.ResultCode = SessionResponseCode;
1075 ServerResponse.ResultMessage = SessionErrorBuffer;
1076 return ServerResponse;
1080 COServerResponse CardDAV2::GetServerEntityTagValue(std::string Location){
1082 // Check if authentication was successful, otherwise don't do anything.
1084 COServerResponse ServerResponse;
1086 if (AuthPassed == false){
1087 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
1088 ServerResponse.EntityTag = "";
1089 ServerResponse.SessionCode = 0;
1090 ServerResponse.ResultCode = 0;
1091 ServerResponse.ResultMessage = "";
1092 return ServerResponse;
1095 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
1098 static const char* GetETagQuery =
1099 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1100 "<C:addressbook-query xmlns:D=\"DAV:\""
1101 " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">"
1102 "<D:prop><D:getetag/>"
1105 "</C:addressbook-query>";
1107 string ServerAddressURL = BuildURL(ServerPrefix + Location);
1108 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
1109 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "REPORT");
1110 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, GetETagQuery);
1111 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(GetETagQuery));
1113 if (TestMode == true){
1114 SessionResult = curl_easy_perform(ConnectionSession);
1116 SessionResult = curl_easy_perform(ConnectionSession);
1119 switch(SessionResult){
1122 SSLVerified = COSSL_VERIFIED;
1124 case CURLE_SSL_CACERT:
1125 case CURLE_SSL_CONNECT_ERROR:
1127 SSLVerified = COSSL_UNABLETOVERIFY;
1133 long SessionResponseCode = 0;
1135 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
1137 if (SessionResponseCode == 207){
1139 ValidResponse = true;
1140 } else if (SessionResponseCode == 403){
1142 ValidResponse = true;
1143 } else if (SessionResponseCode >= 400){
1145 ValidResponse = true;
1148 ValidResponse = false;
1151 if (ValidResponse == false || AuthPassed == false){
1152 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
1153 ServerResponse.EntityTag = "";
1154 ServerResponse.SessionCode = SessionResult;
1155 ServerResponse.ResultCode = SessionResponseCode;
1156 ServerResponse.ResultMessage = "";
1157 return ServerResponse;
1162 ServerResponse.RequestResult = COREQUEST_OK;
1163 ServerResponse.EntityTag = GetETagValue();
1164 ServerResponse.SessionCode = SessionResult;
1165 ServerResponse.ResultCode = SessionResponseCode;
1166 ServerResponse.ResultMessage = SessionErrorBuffer;
1168 return ServerResponse;
1172 COServerResponse CardDAV2::GetContact(std::string Location, std::string *ContactData){
1174 // Check if authentication was successful, otherwise don't do anything.
1176 COServerResponse ServerResponse;
1178 if (AuthPassed == false){
1179 ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
1180 ServerResponse.EntityTag = "";
1181 ServerResponse.SessionCode = 0;
1182 ServerResponse.ResultCode = 0;
1183 ServerResponse.ResultMessage = "";
1184 return ServerResponse;
1187 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
1190 string ServerAddressURL = BuildURL(ServerPrefix + Location);
1191 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
1192 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET");
1194 if (TestMode == true){
1195 SessionResult = curl_easy_perform(ConnectionSession);
1197 SessionResult = curl_easy_perform(ConnectionSession);
1200 switch(SessionResult){
1203 SSLVerified = COSSL_VERIFIED;
1205 case CURLE_SSL_CACERT:
1206 case CURLE_SSL_CONNECT_ERROR:
1208 SSLVerified = COSSL_UNABLETOVERIFY;
1214 long SessionResponseCode = 0;
1216 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
1218 if (SessionResponseCode == 200){
1220 ValidResponse = true;
1221 } else if (SessionResponseCode == 403){
1223 ValidResponse = true;
1224 } else if (SessionResponseCode >= 400){
1226 ValidResponse = true;
1229 ValidResponse = false;
1232 if (ValidResponse == false && AuthPassed == false){
1233 ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
1234 ServerResponse.EntityTag = "";
1235 ServerResponse.SessionCode = SessionResult;
1236 ServerResponse.ResultCode = SessionResponseCode;
1237 ServerResponse.ResultMessage = "";
1238 return ServerResponse;
1243 ServerResponse.RequestResult = COREQUEST_OK;
1244 ServerResponse.EntityTag = "";
1245 ServerResponse.SessionCode = SessionResult;
1246 ServerResponse.ResultCode = SessionResponseCode;
1247 ServerResponse.ResultMessage = SessionErrorBuffer;
1249 (*ContactData) = PageData;
1251 return ServerResponse;
1255 COContactList CardDAV2::GetContactList(std::string SyncToken){
1257 COContactList ServerContactList;
1259 // Check if authentication was successful, otherwise don't do anything.
1261 if (AuthPassed == false){
1262 ServerContactList.ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
1263 ServerContactList.ServerResponse.EntityTag = "";
1264 ServerContactList.ServerResponse.SessionCode = 0;
1265 ServerContactList.ServerResponse.ResultCode = 0;
1266 ServerContactList.ServerResponse.ResultMessage = "";
1267 return ServerContactList;
1270 ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
1273 std::string SyncData;
1275 // TODO: Copy old code from CardDAV class as needed.
1277 if (SyncToken.size() > 0){
1279 SyncData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
1280 "<D:sync-collection xmlns:D=\"DAV:\"\n"
1281 " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
1283 SyncData.append(SyncToken);
1284 SyncData.append("</D:sync-token>\n"
1285 "<D:sync-level>1</D:sync-level>\n"
1289 "</D:sync-collection>");
1293 SyncData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
1294 "<D:sync-collection xmlns:D=\"DAV:\"\n"
1295 " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
1296 "<D:sync-level>1</D:sync-level>\n"
1300 "</D:sync-collection>";
1304 string ServerAddressURL = BuildURL(ServerPrefix);
1306 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
1307 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, SyncData.c_str());
1308 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(SyncData.c_str()));
1309 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "REPORT");
1311 HeaderList = curl_slist_append(HeaderList, "Content-Type: application/xml; charset=utf-8");
1312 HeaderList = curl_slist_append(HeaderList, "Depth: 1");
1314 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList);
1316 if (TestMode == true){
1317 SessionResult = curl_easy_perform(ConnectionSession);
1319 SessionResult = curl_easy_perform(ConnectionSession);
1322 switch(SessionResult){
1325 SSLVerified = COSSL_VERIFIED;
1327 case CURLE_SSL_CACERT:
1328 case CURLE_SSL_CONNECT_ERROR:
1330 SSLVerified = COSSL_UNABLETOVERIFY;
1336 long SessionResponseCode = 0;
1338 curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
1340 if (SessionResponseCode == 207){
1342 ValidResponse = true;
1343 } else if (SessionResponseCode == 403){
1345 ValidResponse = true;
1346 } else if (SessionResponseCode >= 400){
1348 ValidResponse = true;
1351 ValidResponse = false;
1354 if (ValidResponse == false || AuthPassed == false){
1355 ServerContactList.ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
1356 ServerContactList.ServerResponse.EntityTag = "";
1357 ServerContactList.ServerResponse.SessionCode = SessionResult;
1358 ServerContactList.ServerResponse.ResultCode = SessionResponseCode;
1359 ServerContactList.ServerResponse.ResultMessage = "";
1360 return ServerContactList;
1365 ProcessContactData(&ServerContactList);
1367 ServerContactList.ServerResponse.RequestResult = COREQUEST_OK;
1368 ServerContactList.ServerResponse.EntityTag = "";
1369 ServerContactList.ServerResponse.SessionCode = SessionResult;
1370 ServerContactList.ServerResponse.ResultCode = SessionResponseCode;
1371 ServerContactList.ServerResponse.ResultMessage = SessionErrorBuffer;
1373 return ServerContactList;
1377 bool CardDAV2::CanDoProcessing(){
1381 bool CardDAV2::CanDoSSL(){
1385 COSSLVerified CardDAV2::SSLVerify(){
1389 bool CardDAV2::AbleToLogin(){
1393 bool CardDAV2::HasValidResponse(){
1394 return ValidResponse;
1397 bool CardDAV2::IsSelfSigned(){
1398 return SSLSelfSigned;
1401 void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){
1403 std::string ServerAddress = "";
1405 string ServerAddressURL = "http://" + ServerAddress + ":" + to_string(ServerPort) + "/";
1406 string UsernamePassword = ServerUser + ":" + ServerPass;
1408 PageDataObject.CardDAV2Object = this;
1409 PageDataObject.ConnectionSessionObject = ConnectionSession;
1410 PageDataObject.DataSetting = &PageData;
1411 PageDataObject.ServerUsingSSL = false;
1413 PageHeaderObject.CardDAV2Object = this;
1414 PageHeaderObject.ConnectionSessionObject = ConnectionSession;
1415 PageHeaderObject.DataSetting = &PageHeader;
1416 PageHeaderObject.ServerUsingSSL = false;
1418 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddress.c_str());
1419 curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L);
1420 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
1421 curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60);
1422 curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true);
1423 curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT);
1424 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc);
1425 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject);
1426 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject);
1427 curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1L);
1428 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET");
1429 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr);
1430 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr);
1431 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L);
1433 if (DoAuthentication == true){
1434 curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
1436 curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, NULL);
1441 void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){
1443 // Setup the default parameters.
1445 string ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + "/";
1446 string UsernamePassword = ServerUser + ":" + ServerPass;
1448 PageDataObject.CardDAV2Object = this;
1449 PageDataObject.ConnectionSessionObject = ConnectionSession;
1450 PageDataObject.DataSetting = &PageData;
1451 PageDataObject.ServerUsingSSL = true;
1453 PageHeaderObject.CardDAV2Object = this;
1454 PageHeaderObject.ConnectionSessionObject = ConnectionSession;
1455 PageHeaderObject.DataSetting = &PageHeader;
1456 PageHeaderObject.ServerUsingSSL = true;
1458 curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
1459 curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L);
1460 curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1L);
1461 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
1462 curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60);
1463 curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, 0L);
1464 curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT);
1465 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc);
1466 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject);
1467 curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject);
1468 curl_easy_setopt(ConnectionSession, CURLOPT_ERRORBUFFER, SessionErrorBuffer);
1469 curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET");
1470 curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr);
1471 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr);
1472 curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L);
1474 if (DoAuthentication == true){
1475 curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
1477 curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, NULL);
1480 if (EnableSSLBypass == true){
1481 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0L);
1482 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 0L);
1483 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYSTATUS, 0L);
1485 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 2L);
1486 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1L);
1487 curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYSTATUS, 1L);
1490 #if !defined(__APPLE__) || defined(__WIN32__)
1492 if (TestMode == false && ServerAccount.size() > 0){
1494 // Check if the server certificate file exists.
1496 string CertificateFilename = GetAccountDir(ServerAccount, true);
1498 if (wxFile::Exists(CertificateFilename)){
1500 curl_easy_setopt(ConnectionSession, CURLOPT_CAINFO, CertificateFilename.c_str());
1510 string CardDAV2::BuildURL(string URI){
1512 string ServerAddressURL;
1514 if (ServerSSL == true){
1515 ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + URI;
1517 ServerAddressURL = "http://" + ServerAddress + ":" + to_string(ServerPort) + URI;
1520 return ServerAddressURL;
1524 string CardDAV2::GetErrorMessage(){
1526 ErrorMessage = SessionErrorBuffer;
1527 return ErrorMessage;
1531 void CardDAV2::ResetResults(){
1534 COSSLVerified SSLVerified = COSSL_NORESULT;
1535 ValidResponse = false;
1538 SSLSelfSigned = false;
1539 TaskCompleted = false;
1541 SessionErrorBuffer[0] = '\0';
1542 SessionResult = CURLE_OK;
1545 if (HeaderList != nullptr){
1546 curl_slist_free_all(HeaderList);
1547 HeaderList = nullptr;
1552 string CardDAV2::GetETagValue(){
1554 xmlDocPtr xmlCardDAVDoc;
1556 xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0);
1558 xmlNodePtr nodeLevel1;
1559 xmlNodePtr nodeLevel2;
1560 xmlNodePtr nodeLevel3;
1561 xmlNodePtr nodeLevel4;
1562 xmlNodePtr nodeLevel5;
1563 xmlNodePtr nodeLevel6;
1565 //std::map<wxString,wxString> xmlDataMap;
1567 std::string DataFilename;
1568 std::string ETagData;
1570 std::string xmlStringSafe;
1571 std::string ETagValue;
1573 // Tranverse through the catacombs of the response to get our ETag for the file.
1575 for (nodeLevel1 = xmlCardDAVDoc->children;
1577 nodeLevel1 = nodeLevel1->next)
1580 bool HREFFound = FALSE;
1581 bool ETagFound = FALSE;
1583 for (nodeLevel2 = nodeLevel1->children;
1585 nodeLevel2 = nodeLevel2->next)
1588 for (nodeLevel3 = nodeLevel2->children;
1590 nodeLevel3 = nodeLevel3->next)
1593 if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"href") ||
1594 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:href") ||
1595 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:href")
1598 // Get the filename.
1600 for (nodeLevel4 = nodeLevel3->children;
1602 nodeLevel4 = nodeLevel4->next)
1605 if (!xmlStrcmp(nodeLevel4->name, (const xmlChar *)"text") ||
1606 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"d:text") ||
1607 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"D:text")
1610 DataFilename = wxString::FromUTF8((const char*)nodeLevel4->content);
1611 wxStringTokenizer wSTDFilename(DataFilename, wxT("/"));
1613 while (wSTDFilename.HasMoreTokens()){
1615 DataFilename = wSTDFilename.GetNextToken().ToStdString();
1629 for (nodeLevel4 = nodeLevel3->children;
1631 nodeLevel4 = nodeLevel4->next)
1634 for (nodeLevel5 = nodeLevel4->children;
1636 nodeLevel5 = nodeLevel5->next)
1639 if (!xmlStrcmp(nodeLevel5->name, (const xmlChar *)"getetag") ||
1640 !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"d:getetag") ||
1641 !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"D:getetag")
1644 for (nodeLevel6 = nodeLevel5->children;
1646 nodeLevel6 = nodeLevel6->next)
1649 // Strip the quotes from the ETag.
1651 ETagData = (const char*)nodeLevel6->content;
1652 if (ETagData[0] == '"' && ETagData[(ETagData.size() - 1)] == '"'){
1654 ETagData.erase(0, 1);
1655 ETagData.erase((ETagData.size() - 1));
1675 if (HREFFound == TRUE && ETagFound == TRUE){
1677 // Add to the map data.
1679 ETagValue = ETagData;
1690 xmlFreeDoc(xmlCardDAVDoc);
1696 string CardDAV2::GetETagHeader(){
1698 // Go through each of the lines looking for the
1703 bool FastForward = false;
1705 for (int HeaderSeek = 0; HeaderSeek < PageHeader.size(); HeaderSeek++){
1707 if (FastForward == true){
1709 if (PageHeader[HeaderSeek] == '\n'){
1710 FastForward = false;
1718 PageHeader.substr(HeaderSeek, 5) == "ETag:";
1721 catch (const out_of_range &oor){
1725 if (PageHeader.substr(HeaderSeek, 5) == "ETag:"){
1727 int CharacterSeek = 5;
1729 while ((HeaderSeek + CharacterSeek) < PageHeader.size()){
1731 if (PageHeader.substr((HeaderSeek + CharacterSeek), 2) == "\r\n"){
1735 HeaderValue += PageHeader.substr((HeaderSeek + CharacterSeek), 1);
1748 if (PageHeader[HeaderSeek] == '\n'){
1752 //HeaderName += PageHeader.substr(HeaderSeek, 1);
1756 // Check for quotation marks at the start and end and strip
1759 if (HeaderValue.size() >= 2){
1761 if (HeaderValue[0] == '"'){
1762 HeaderValue.erase((HeaderValue.size() - 1));
1763 HeaderValue.erase(0);
1772 vector<string> CardDAV2::GetDAVHeader(){
1774 // Go through each of the lines looking for the
1779 bool FastForward = false;
1780 vector<string> DAVHeaderList;
1782 for (int HeaderSeek = 0; HeaderSeek < PageHeader.size(); HeaderSeek++){
1784 if (FastForward == true){
1786 if (PageHeader[HeaderSeek] == '\n'){
1787 FastForward = false;
1795 PageHeader.substr(HeaderSeek, 4) == "DAV:";
1798 catch (const out_of_range &oor){
1802 if (PageHeader.substr(HeaderSeek, 4) == "DAV:"){
1804 int CharacterSeek = 5;
1806 while ((HeaderSeek + CharacterSeek) < PageHeader.size()){
1808 if (PageHeader.substr((HeaderSeek + CharacterSeek), 2) == "\r\n"){
1812 HeaderValue += PageHeader.substr((HeaderSeek + CharacterSeek), 1);
1825 if (PageHeader[HeaderSeek] == '\n'){
1829 //HeaderName += PageHeader.substr(HeaderSeek, 1);
1833 // Split the header data.
1835 std::string DAVHeaderValue;
1837 for (int HeaderSeek = 0; HeaderSeek < HeaderValue.size(); HeaderSeek++){
1839 if (HeaderValue.substr(HeaderSeek, 1) == ","){
1840 DAVHeaderList.push_back(DAVHeaderValue);
1841 DAVHeaderValue = "";
1846 DAVHeaderValue += HeaderValue[HeaderSeek];
1850 if (DAVHeaderValue.size() > 0){
1852 DAVHeaderList.push_back(DAVHeaderValue);
1856 return DAVHeaderList;
1860 void CardDAV2::ProcessContactData(COContactList *ContactList){
1862 xmlDocPtr xmlCardDAVDoc;
1863 xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0);
1865 xmlNodePtr MultiStatusNode;
1866 xmlNodePtr ResponseNode;
1867 xmlNodePtr ResponseDataNode;
1868 xmlNodePtr PropStatNode;
1869 xmlNodePtr ValueNode;
1870 xmlNodePtr ETagNode;
1871 xmlNodePtr StatusNode;
1873 std::string HREFValue;
1874 std::string ETagValue;
1875 std::string StatusValue;
1876 std::string SyncValue;
1878 // Go through the document!
1880 MultiStatusNode = xmlCardDAVDoc->children;
1882 if (MultiStatusNode == nullptr){
1886 bool SyncTokenFound = false;
1888 // Tranverse through the catacombs of the response to get our ETag for the file and
1889 // the server syncronisation token.
1891 for (ResponseNode = MultiStatusNode->children;
1892 ResponseNode != nullptr;
1893 ResponseNode = ResponseNode->next){
1895 // Check if tag is response or sync-token.
1897 if (!xmlStrcmp(ResponseNode->name, (const xmlChar *)"response") ||
1898 !xmlStrcmp(ResponseNode->name, (const xmlChar *)"d:response") ||
1899 !xmlStrcmp(ResponseNode->name, (const xmlChar *)"D:response")){
1901 COContactStatus ContactStatus = COCS_UNKNOWN;
1903 for (ResponseDataNode = ResponseNode->children;
1904 ResponseDataNode != nullptr;
1905 ResponseDataNode = ResponseDataNode->next){
1907 if (!xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"href") ||
1908 !xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"d:href") ||
1909 !xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"D:href")){
1911 HREFValue = (const char*)ResponseDataNode->children->content;
1913 // Get the filename after the last forward slash.
1917 for (int HREFValueSeek = 0; HREFValueSeek < HREFValue.size(); HREFValueSeek++){
1919 if (HREFValue[HREFValueSeek] == '/'){
1921 LastSlash = HREFValueSeek;
1927 HREFValue = HREFValue.substr((LastSlash + 1));
1929 } else if (!xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"propstat") ||
1930 !xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"d:propstat") ||
1931 !xmlStrcmp(ResponseDataNode->name, (const xmlChar *)"D:propstat")){
1933 for (PropStatNode = ResponseDataNode->children;
1934 PropStatNode != nullptr;
1935 PropStatNode = PropStatNode->next){
1937 if (!xmlStrcmp(PropStatNode->name, (const xmlChar *)"prop") ||
1938 !xmlStrcmp(PropStatNode->name, (const xmlChar *)"d:prop") ||
1939 !xmlStrcmp(PropStatNode->name, (const xmlChar *)"D:prop")){
1941 for (ETagNode = PropStatNode->children;
1942 ETagNode != nullptr;
1943 ETagNode = ETagNode->next){
1945 if (!xmlStrcmp(ETagNode->name, (const xmlChar *)"getetag") ||
1946 !xmlStrcmp(ETagNode->name, (const xmlChar *)"getetag") ||
1947 !xmlStrcmp(ETagNode->name, (const xmlChar *)"getetag")){
1949 ETagValue = (const char*)ETagNode->children->content;
1951 if (ETagValue.size() > 2 && ETagValue.substr(0,1) == "\""){
1952 ETagValue.erase((ETagValue.size() - 1),1);
1953 ETagValue.erase(0,1);
1961 } else if (!xmlStrcmp(PropStatNode->name, (const xmlChar *)"status") ||
1962 !xmlStrcmp(PropStatNode->name, (const xmlChar *)"d:status") ||
1963 !xmlStrcmp(PropStatNode->name, (const xmlChar *)"D:status")){
1965 StatusValue = (const char*)PropStatNode->children->content;
1967 if (StatusValue == "HTTP/1.1 200 OK"){
1969 ContactStatus = COCS_UPDATED;
1971 } else if (StatusValue == "HTTP/1.1 404 Not Found"){
1973 ContactStatus = COCS_DELETED;
1977 ContactStatus = COCS_UNKNOWN;
1989 COContactData ContactInformation;
1991 ContactInformation.Location = HREFValue;
1992 ContactInformation.Data = ETagValue;
1993 ContactInformation.Status = ContactStatus;
1997 StatusValue.clear();
1999 ContactList->ListData.push_back(ContactInformation);
2001 } else if (!xmlStrcmp(ResponseNode->name, (const xmlChar *)"sync-token") ||
2002 !xmlStrcmp(ResponseNode->name, (const xmlChar *)"d:sync-token") ||
2003 !xmlStrcmp(ResponseNode->name, (const xmlChar *)"D:sync-token")){
2005 SyncValue = (const char*)ResponseNode->children->content;
2011 ContactList->SyncToken = SyncValue;
2013 xmlFreeDoc(xmlCardDAVDoc);