Added DAV path error to log for #822

This commit is contained in:
the-djmaze 2023-01-03 15:11:17 +01:00
parent 0e5ae38c58
commit 19c0c5c98e

View file

@ -42,16 +42,12 @@ trait CardDAV
/** /**
* find every <response><href>*.vcf</href> with empty <resourcetype/> * find every <response><href>*.vcf</href> with empty <resourcetype/>
*/ */
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{
$mResult = array(); $mResult = array();
foreach ($aResponse as $sKey => $aItem) foreach ($aResponse as $sKey => $aItem) {
{
$sKey = \rtrim(\trim($sKey), '\\/'); $sKey = \rtrim(\trim($sKey), '\\/');
if (!empty($sKey) && is_array($aItem)) if (!empty($sKey) && is_array($aItem)) {
{ if (isset($aItem['{DAV:}getetag'])) {
if (isset($aItem['{DAV:}getetag']))
{
$aMatch = array(); $aMatch = array();
if (\preg_match('/\/([^\/?]+)$/', $sKey, $aMatch) && !empty($aMatch[1]) if (\preg_match('/\/([^\/?]+)$/', $sKey, $aMatch) && !empty($aMatch[1])
&& !static::hasDAVCollection($aItem)) && !static::hasDAVCollection($aItem))
@ -68,14 +64,11 @@ trait CardDAV
'changed' => 0 'changed' => 0
); );
if (isset($aItem['{DAV:}getlastmodified'])) if (isset($aItem['{DAV:}getlastmodified'])) {
{
$mResult[$sKeyID]['lastmodified'] = $aItem['{DAV:}getlastmodified']; $mResult[$sKeyID]['lastmodified'] = $aItem['{DAV:}getlastmodified'];
$mResult[$sKeyID]['changed'] = \MailSo\Base\DateTimeHelper::ParseRFC2822DateString( $mResult[$sKeyID]['changed'] = \MailSo\Base\DateTimeHelper::ParseRFC2822DateString(
$aItem['{DAV:}getlastmodified']); $aItem['{DAV:}getlastmodified']);
} } else {
else
{
$mResult[$sKeyID]['changed'] = \MailSo\Base\DateTimeHelper::TryToParseSpecEtagFormat($mResult[$sKeyID]['etag']); $mResult[$sKeyID]['changed'] = \MailSo\Base\DateTimeHelper::TryToParseSpecEtagFormat($mResult[$sKeyID]['etag']);
$mResult[$sKeyID]['lastmodified'] = 0 < $mResult[$sKeyID]['changed'] ? $mResult[$sKeyID]['lastmodified'] = 0 < $mResult[$sKeyID]['changed'] ?
\gmdate('c', $mResult[$sKeyID]['changed']) : ''; \gmdate('c', $mResult[$sKeyID]['changed']) : '';
@ -98,26 +91,15 @@ trait CardDAV
$this->oLogger->Write($sCmd.' '.$sUrl.(('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData ? ' ('.\strlen($mData).')' : ''), $this->oLogger->Write($sCmd.' '.$sUrl.(('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData ? ' ('.\strlen($mData).')' : ''),
\LOG_INFO, 'DAV'); \LOG_INFO, 'DAV');
// if ('PUT' === $sCmd || 'POST' === $sCmd)
// {
// $this->oLogger->Write($mData, \LOG_INFO, 'DAV');
// }
try try
{ {
if (('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData) if (('PUT' === $sCmd || 'POST' === $sCmd) && null !== $mData) {
{
return $oClient->request($sCmd, $sUrl, $mData, array( return $oClient->request($sCmd, $sUrl, $mData, array(
'Content-Type' => 'text/vcard; charset=utf-8' 'Content-Type' => 'text/vcard; charset=utf-8'
)); ));
} }
else return $oClient->request($sCmd, $sUrl);
{ // if ('GET' === $sCmd) {
return $oClient->request($sCmd, $sUrl);
}
// if ('GET' === $sCmd)
// {
// $this->oLogger->WriteDump($aResponse, \LOG_INFO, 'DAV'); // $this->oLogger->WriteDump($aResponse, \LOG_INFO, 'DAV');
// } // }
} }
@ -131,11 +113,9 @@ trait CardDAV
private function detectionPropFind(DAVClient $oClient, string $sPath) : ?array private function detectionPropFind(DAVClient $oClient, string $sPath) : ?array
{ {
$aResponse = null;
try try
{ {
$aResponse = $oClient->propFind($sPath, array( return $oClient->propFind($sPath, array(
'{DAV:}current-user-principal', '{DAV:}current-user-principal',
'{DAV:}resourcetype', '{DAV:}resourcetype',
'{DAV:}displayname', '{DAV:}displayname',
@ -147,7 +127,7 @@ trait CardDAV
$this->oLogger->WriteException($oException); $this->oLogger->WriteException($oException);
} }
return $aResponse; return null;
} }
private function getContactsPaths(DAVClient $oClient, string $sPath, string $sUser, string $sPassword, string $sProxy = '') : array private function getContactsPaths(DAVClient $oClient, string $sPath, string $sUser, string $sPassword, string $sProxy = '') : array
@ -162,10 +142,8 @@ trait CardDAV
$sNextPath = ''; $sNextPath = '';
$sFirstNextPath = ''; $sFirstNextPath = '';
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{ foreach ($aResponse as $sPropPath => $aItem) {
foreach ($aResponse as $sPropPath => $aItem)
{
if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href']) if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'])
&& false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy')) && false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy'))
{ {
@ -173,23 +151,18 @@ trait CardDAV
continue; continue;
} }
if (empty($sCurrentUserPrincipal) && !empty($aItem['{DAV:}current-user-principal']['{DAV:}href'])) if (empty($sCurrentUserPrincipal) && !empty($aItem['{DAV:}current-user-principal']['{DAV:}href'])) {
{
$sCurrentUserPrincipal = $aItem['{DAV:}current-user-principal']['{DAV:}href']; $sCurrentUserPrincipal = $aItem['{DAV:}current-user-principal']['{DAV:}href'];
continue; continue;
} }
if (!empty($sPropPath)) if (!empty($sPropPath)) {
{ if (empty($sFirstNextPath)) {
if (empty($sFirstNextPath))
{
$sFirstNextPath = $sPropPath; $sFirstNextPath = $sPropPath;
} }
if (empty($sNextPath)) if (empty($sNextPath)) {
{ if (static::hasDAVCollection($aItem)) {
if (static::hasDAVCollection($aItem))
{
$sNextPath = $sPropPath; $sNextPath = $sPropPath;
continue; continue;
} }
@ -197,23 +170,18 @@ trait CardDAV
} }
} }
if (empty($sNextPath) && empty($sCurrentUserPrincipal) && empty($sAddressbookHomeSet) && !empty($sFirstNextPath)) if (empty($sNextPath) && empty($sCurrentUserPrincipal) && empty($sAddressbookHomeSet) && !empty($sFirstNextPath)) {
{
$sNextPath = $sFirstNextPath; $sNextPath = $sFirstNextPath;
} }
} }
if (empty($sCurrentUserPrincipal) && empty($sAddressbookHomeSet)) if (empty($sCurrentUserPrincipal) && empty($sAddressbookHomeSet)) {
{ if (empty($sNextPath)) {
if (empty($sNextPath))
{
return $aContactsPaths; return $aContactsPaths;
} }
if (\preg_match('/^http[s]?:\/\//i', $sNextPath)) if (\preg_match('/^http[s]?:\/\//i', $sNextPath)) {
{
$oClient = $this->getDavClientFromUrl($sNextPath, $sUser, $sPassword, $sProxy); $oClient = $this->getDavClientFromUrl($sNextPath, $sUser, $sPassword, $sProxy);
if (!$oClient) if (!$oClient) {
{
return $aContactsPaths; return $aContactsPaths;
} }
$sNextPath = $oClient->__UrlPath__; $sNextPath = $oClient->__UrlPath__;
@ -221,10 +189,8 @@ trait CardDAV
if ($sPath != $sNextPath) { if ($sPath != $sNextPath) {
$aResponse = $this->detectionPropFind($oClient, $sNextPath); $aResponse = $this->detectionPropFind($oClient, $sNextPath);
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{ foreach ($aResponse as $aItem) {
foreach ($aResponse as $aItem)
{
if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href']) && if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href']) &&
false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy')) false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy'))
{ {
@ -232,8 +198,7 @@ trait CardDAV
continue; continue;
} }
if (empty($sCurrentUserPrincipal) && !empty($aItem['{DAV:}current-user-principal']['{DAV:}href'])) if (empty($sCurrentUserPrincipal) && !empty($aItem['{DAV:}current-user-principal']['{DAV:}href'])) {
{
$sCurrentUserPrincipal = $aItem['{DAV:}current-user-principal']['{DAV:}href']; $sCurrentUserPrincipal = $aItem['{DAV:}current-user-principal']['{DAV:}href'];
continue; continue;
} }
@ -242,30 +207,21 @@ trait CardDAV
} }
} }
if (empty($sAddressbookHomeSet)) if (empty($sAddressbookHomeSet)) {
{ if (empty($sCurrentUserPrincipal)) {
if (empty($sCurrentUserPrincipal))
{
return $aContactsPaths; return $aContactsPaths;
} }
if (\preg_match('/^http[s]?:\/\//i', $sCurrentUserPrincipal)) if (\preg_match('/^http[s]?:\/\//i', $sCurrentUserPrincipal)) {
{
$oClient = $this->getDavClientFromUrl($sCurrentUserPrincipal, $sUser, $sPassword, $sProxy); $oClient = $this->getDavClientFromUrl($sCurrentUserPrincipal, $sUser, $sPassword, $sProxy);
if ($oClient) if (!$oClient) {
{
$sCurrentUserPrincipal = $oClient->__UrlPath__;
}
else
{
return $aContactsPaths; return $aContactsPaths;
} }
$sCurrentUserPrincipal = $oClient->__UrlPath__;
} }
$aResponse = $this->detectionPropFind($oClient, $sCurrentUserPrincipal); $aResponse = $this->detectionPropFind($oClient, $sCurrentUserPrincipal);
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{ foreach ($aResponse as $aItem) {
foreach ($aResponse as $aItem)
{
if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href']) && if (empty($sAddressbookHomeSet) && !empty($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href']) &&
false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy')) false === \strpos($aItem['{urn:ietf:params:xml:ns:carddav}addressbook-home-set']['{DAV:}href'], '/calendar-proxy'))
{ {
@ -276,28 +232,20 @@ trait CardDAV
} }
} }
if (empty($sAddressbookHomeSet)) if (empty($sAddressbookHomeSet)) {
{
return $aContactsPaths; return $aContactsPaths;
} }
if (\preg_match('/^http[s]?:\/\//i', $sAddressbookHomeSet)) if (\preg_match('/^http[s]?:\/\//i', $sAddressbookHomeSet)) {
{
$oClient = $this->getDavClientFromUrl($sAddressbookHomeSet, $sUser, $sPassword, $sProxy); $oClient = $this->getDavClientFromUrl($sAddressbookHomeSet, $sUser, $sPassword, $sProxy);
if ($oClient) if (!$oClient) {
{
$sAddressbookHomeSet = $oClient->__UrlPath__;
}
else
{
return $aContactsPaths; return $aContactsPaths;
} }
$sAddressbookHomeSet = $oClient->__UrlPath__;
} }
$aResponse = $this->detectionPropFind($oClient, $sAddressbookHomeSet); $aResponse = $this->detectionPropFind($oClient, $sAddressbookHomeSet);
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{ foreach ($aResponse as $sPropPath => $aItem) {
foreach ($aResponse as $sPropPath => $aItem)
{
if (!empty($sPropPath) && static::hasDAVCollection($aItem) if (!empty($sPropPath) && static::hasDAVCollection($aItem)
&& \in_array('{urn:ietf:params:xml:ns:carddav}addressbook', $aItem['{DAV:}resourcetype'])) && \in_array('{urn:ietf:params:xml:ns:carddav}addressbook', $aItem['{DAV:}resourcetype']))
{ {
@ -314,11 +262,6 @@ trait CardDAV
*/ */
private function checkContactsPath(DAVClient $oClient, string $sPath) : bool private function checkContactsPath(DAVClient $oClient, string $sPath) : bool
{ {
if (!$oClient)
{
return false;
}
$aResponse = null; $aResponse = null;
try try
{ {
@ -332,21 +275,17 @@ trait CardDAV
} }
$bGood = false; $bGood = false;
if (\is_array($aResponse)) if (\is_array($aResponse)) {
{ foreach ($aResponse as $sKey => $aItem) {
foreach ($aResponse as $sKey => $aItem)
{
if (!empty($sKey) && static::hasDAVCollection($aItem) if (!empty($sKey) && static::hasDAVCollection($aItem)
&& \in_array('{urn:ietf:params:xml:ns:carddav}addressbook', $aItem['{DAV:}resourcetype'])) && \in_array('{urn:ietf:params:xml:ns:carddav}addressbook', $aItem['{DAV:}resourcetype']))
{ {
$bGood = true; $bGood = true;
} }
} }
} if ($bGood) {
$oClient->__UrlPath__ = $sPath;
if ($bGood) }
{
$oClient->__UrlPath__ = $sPath;
} }
return $bGood; return $bGood;
@ -354,8 +293,7 @@ trait CardDAV
private function getDavClientFromUrl(string $sUrl, string $sUser, string $sPassword, string $sProxy = '') : DAVClient private function getDavClientFromUrl(string $sUrl, string $sUser, string $sPassword, string $sProxy = '') : DAVClient
{ {
if (!\preg_match('/^http[s]?:\/\//i', $sUrl)) if (!\preg_match('/^http[s]?:\/\//i', $sUrl)) {
{
$sUrl = \preg_replace('/^fruux\.com/i', 'dav.fruux.com', $sUrl); $sUrl = \preg_replace('/^fruux\.com/i', 'dav.fruux.com', $sUrl);
$sUrl = \preg_replace('/^icloud\.com/i', 'contacts.icloud.com', $sUrl); $sUrl = \preg_replace('/^icloud\.com/i', 'contacts.icloud.com', $sUrl);
$sUrl = \preg_replace('/^gmail\.com/i', 'google.com', $sUrl); $sUrl = \preg_replace('/^gmail\.com/i', 'google.com', $sUrl);
@ -363,8 +301,7 @@ trait CardDAV
} }
$aUrl = \parse_url($sUrl); $aUrl = \parse_url($sUrl);
if (!\is_array($aUrl)) if (!\is_array($aUrl)) {
{
$aUrl = array(); $aUrl = array();
} }
@ -381,8 +318,7 @@ trait CardDAV
$this->oLogger->AddSecret($sPassword); $this->oLogger->AddSecret($sPassword);
if (!empty($sProxy)) if (!empty($sProxy)) {
{
$aSettings['proxy'] = $sProxy; $aSettings['proxy'] = $sProxy;
} }
@ -409,8 +345,7 @@ trait CardDAV
$aMatch = array(); $aMatch = array();
$sUserAddressBookNameName = ''; $sUserAddressBookNameName = '';
if (\preg_match('/\|(.+)$/', $sUrl, $aMatch) && !empty($aMatch[1])) if (\preg_match('/\|(.+)$/', $sUrl, $aMatch) && !empty($aMatch[1])) {
{
$sUserAddressBookNameName = \trim($aMatch[1]); $sUserAddressBookNameName = \trim($aMatch[1]);
$sUserAddressBookNameName = \mb_strtolower($sUserAddressBookNameName); $sUserAddressBookNameName = \mb_strtolower($sUserAddressBookNameName);
@ -422,8 +357,7 @@ trait CardDAV
$sPath = $oClient->__UrlPath__; $sPath = $oClient->__UrlPath__;
$bGood = true; $bGood = true;
if ('' === $sPath || '/' === $sPath || !$this->checkContactsPath($oClient, $sPath)) if ('' === $sPath || '/' === $sPath || !$this->checkContactsPath($oClient, $sPath)) {
{
/** /**
* Path is not an addressbook, try to find it * Path is not an addressbook, try to find it
*/ */
@ -431,30 +365,22 @@ trait CardDAV
$this->oLogger->WriteDump($aPaths); $this->oLogger->WriteDump($aPaths);
$sNewPath = ''; $sNewPath = '';
if (\is_array($aPaths)) if (\is_array($aPaths)) {
{ if (1 < \count($aPaths)) {
if (1 < \count($aPaths)) if ('' !== $sUserAddressBookNameName) {
{ foreach ($aPaths as $sKey => $sValue) {
if ('' !== $sUserAddressBookNameName)
{
foreach ($aPaths as $sKey => $sValue)
{
$sValue = \mb_strtolower(\trim($sValue)); $sValue = \mb_strtolower(\trim($sValue));
if ($sValue === $sUserAddressBookNameName) if ($sValue === $sUserAddressBookNameName) {
{
$sNewPath = $sKey; $sNewPath = $sKey;
break; break;
} }
} }
} }
if (empty($sNewPath)) if (empty($sNewPath)) {
{ foreach ($aPaths as $sKey => $sValue) {
foreach ($aPaths as $sKey => $sValue)
{
$sValue = \mb_strtolower($sValue); $sValue = \mb_strtolower($sValue);
if (\in_array($sValue, array('contacts', 'default', 'addressbook', 'address book'))) if (\in_array($sValue, array('contacts', 'default', 'addressbook', 'address book'))) {
{
$sNewPath = $sKey; $sNewPath = $sKey;
break; break;
} }
@ -462,19 +388,18 @@ trait CardDAV
} }
} }
if (empty($sNewPath)) if (empty($sNewPath)) {
{ foreach ($aPaths as $sKey => $sValue) {
foreach ($aPaths as $sKey => $sValue)
{
$sNewPath = $sKey; $sNewPath = $sKey;
break; break;
} }
} }
} }
$sPath = $sNewPath; $bGood = $sNewPath && $this->checkContactsPath($oClient, $sNewPath);
if (!$bGood) {
$bGood = $sPath && $this->checkContactsPath($oClient, $sPath); $this->oLogger->Write('Contacts path not found at: '.$sPath, \LOG_INFO, 'DAV');
}
} }
return $bGood ? $oClient : null; return $bGood ? $oClient : null;