Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix vcard export/import support #2084

Merged
merged 19 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/Http/Controllers/CardDAV/CardDAVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Sabre\CardDAV\Plugin as CardDAVPlugin;
use App\Models\CardDAV\MonicaAddressBookRoot;
use Sabre\DAV\Browser\Plugin as BrowserPlugin;
use App\Models\CardDAV\Backends\MonicaSabreBackend;
use App\Models\CardDAV\Backends\MonicaAuthBackend;
use App\Models\CardDAV\Backends\MonicaCardDAVBackend;
use App\Models\CardDAV\Backends\MonicaPrincipleBackend;
use App\Models\CardDAV\Backends\MonicaPrincipalBackend;

class CardDAVController extends Controller
{
Expand All @@ -42,7 +42,7 @@ public function init(Request $request)
private function getNodes() : array
{
// Initiate custom backends for link between Sabre and Monica
$principalBackend = new MonicaPrincipleBackend(); // User rights
$principalBackend = new MonicaPrincipalBackend(); // User rights
$carddavBackend = new MonicaCardDAVBackend(); // Contacts

return [
Expand Down Expand Up @@ -97,7 +97,7 @@ public function fullUrl(Request $request)
private function addPlugins(SabreServer $server)
{
// Authentication backend
$authBackend = new MonicaSabreBackend();
$authBackend = new MonicaAuthBackend();
$server->addPlugin(new AuthPlugin($authBackend, 'SabreDAV'));

// CardDAV plugin
Expand All @@ -112,7 +112,7 @@ private function addPlugins(SabreServer $server)
// VCFExport
$server->addPlugin(new VCFExportPlugin());

// In debug mode add browser plugin
// In local environment add browser plugin
if (App::environment('local')) {
$server->addPlugin(new BrowserPlugin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Sabre\DAV\Auth\Backend\BackendInterface;

class MonicaSabreBackend implements BackendInterface
class MonicaAuthBackend implements BackendInterface
{
/**
* Authentication Realm.
Expand All @@ -20,13 +19,6 @@ class MonicaSabreBackend implements BackendInterface
*/
protected $realm = 'sabre/dav';

/**
* This is the prefix that will be used to generate principal urls.
*
* @var string
*/
protected $principalPrefix = 'principals/';

/**
* Sets the authentication realm for this backend.
*
Expand All @@ -51,9 +43,7 @@ public function check(RequestInterface $request, ResponseInterface $response)
return [false, 'User is not authenticated'];
}

Log::debug(__CLASS__.' validateUserPass', [Auth::user()->name]);

return [true, $this->principalPrefix.Auth::user()->email];
return [true, MonicaPrincipalBackend::getPrincipalUser()];
}

/**
Expand Down
90 changes: 22 additions & 68 deletions app/Models/CardDAV/Backends/MonicaCardDAVBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use App\Services\VCard\ImportVCard;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Sabre\CardDAV\Backend\BackendInterface as SabreBackendInterface;
use Sabre\CardDAV\Backend\AbstractBackend;

class MonicaCardDAVBackend implements SabreBackendInterface
class MonicaCardDAVBackend extends AbstractBackend
{
/**
* Returns the list of addressbooks for a specific user.
Expand All @@ -32,13 +32,12 @@ class MonicaCardDAVBackend implements SabreBackendInterface
*/
public function getAddressBooksForUser($principalUri)
{
Log::debug(__CLASS__.' getAddressBooksForUser', func_get_args());

return [
[
'id' => '0',
'uri' => 'contacts',
'principaluri' => 'principals/'.Auth::user()->email,
'id' => '0',
'uri' => 'contacts',
'principaluri' => MonicaPrincipalBackend::getPrincipalUser(),
'{DAV:}displayname' => Auth::user()->name,
],
];
}
Expand All @@ -61,8 +60,6 @@ public function getAddressBooksForUser($principalUri)
*/
public function updateAddressBook($addressBookId, DAV\PropPatch $propPatch)
{
Log::debug(__CLASS__.' updateAddressBook', func_get_args());

return false;
}

Expand All @@ -79,8 +76,6 @@ public function updateAddressBook($addressBookId, DAV\PropPatch $propPatch)
*/
public function createAddressBook($principalUri, $url, array $properties)
{
Log::debug(__CLASS__.' createAddressBook', func_get_args());

return false;
}

Expand All @@ -92,8 +87,6 @@ public function createAddressBook($principalUri, $url, array $properties)
*/
public function deleteAddressBook($addressBookId)
{
Log::debug(__CLASS__.' deleteAddressBook', func_get_args());

return false;
}

Expand All @@ -113,47 +106,39 @@ private function prepareCard($contact)
Log::debug(__CLASS__.' prepareCard: '.(string) $e);
}

$carddata = $vcard->serialize();

return [
'id' => $contact->hashid(),
'etag' => md5($vcard->serialize()),
'id' => $contact->hashID(),
'uri' => $this->encodeUri($contact),
'lastmodified' => $contact->updated_at->timestamp,
'carddata' => $vcard->serialize(),
'carddata' => $carddata,
'etag' => '"'.md5($carddata).'"',
'lastmodified' => $contact->updated_at,
];
}

private function encodeUri($contact)
{
return urlencode($contact->hashid().'.vcf');
return urlencode($contact->uuid.'.vcf');
}

private function decodeUri($uri)
{
return str_replace('.vcf', '', urldecode($uri));
return pathinfo(urldecode($uri), PATHINFO_FILENAME);
}

private function getContact($uri)
{
try {
return (new Contact)->resolveRouteBinding($this->decodeUri($uri));
return Contact::where([
'account_id' => Auth::user()->account_id,
'uuid' => $this->decodeUri($uri),
])->first();
} catch (\Exception $e) {
return;
}
}

private function prepareCards($contacts)
{
$results = [];

foreach ($contacts as $contact) {
$results[] = $this->prepareCard($contact);
}

Log::debug(__CLASS__.' prepareCards', ['count' => count($results)]);

return $results;
}

/**
* Returns all cards for a specific addressbook id.
*
Expand All @@ -175,19 +160,19 @@ private function prepareCards($contacts)
*/
public function getCards($addressbookId)
{
Log::debug(__CLASS__.' getCards', func_get_args());

$contacts = Auth::user()->account
->contacts()
->real()
->active()
->get();

return $this->prepareCards($contacts);
return $contacts->map(function ($contact) {
return $this->prepareCard($contact);
});
}

/**
* Returns a specfic card.
* Returns a specific card.
*
* The same set of prope
* @param mixed $addressBookId
Expand All @@ -196,36 +181,11 @@ public function getCards($addressbookId)
*/
public function getCard($addressBookId, $cardUri)
{
Log::debug(__CLASS__.' getCard', func_get_args());

$contact = $this->getContact($cardUri);

return $this->prepareCard($contact);
}

/**
* Returns a list of cards.
*
* This method should work identical to getCard, but instead return all the
* cards in the list as an array.
*
* If the backend supports this, it may allow for some speed-ups.
*
* @param mixed $addressBookId
* @param array $uris
* @return array
*/
public function getMultipleCards($addressBookId, array $uris)
{
Log::debug(__CLASS__.' getMultipleCards', func_get_args());

$contacts = array_map(function ($uri) {
return $this->getContact($uri);
}, $uris);

return $this->prepareCards($contacts);
}

/**
* Creates a new card.
*
Expand Down Expand Up @@ -253,8 +213,6 @@ public function getMultipleCards($addressBookId, array $uris)
*/
public function createCard($addressBookId, $cardUri, $cardData)
{
Log::debug(__CLASS__.' createCard', func_get_args());

return $this->importCard(null, $cardData);
}

Expand Down Expand Up @@ -285,8 +243,6 @@ public function createCard($addressBookId, $cardUri, $cardData)
*/
public function updateCard($addressBookId, $cardUri, $cardData)
{
Log::debug(__CLASS__.' updateCard', func_get_args());

return $this->importCard($cardUri, $cardData);
}

Expand Down Expand Up @@ -330,8 +286,6 @@ private function importCard($cardUri, $cardData)
*/
public function deleteCard($addressBookId, $cardUri)
{
Log::debug(__CLASS__.' deleteCard', func_get_args());

return false;
}
}
Loading