Coder Social home page Coder Social logo

laravel-sabre's Introduction

Sabre adapter for Laravel

Laravel-Sabre is an adapter to use Sabre.io DAV Server on Laravel.

Latest Version Downloads Workflow Status Quality Gate Coverage Status

Installation

You may use Composer to install this package into your Laravel project:

composer require monicahq/laravel-sabre

You don't need to add this package to your service providers.

Support

This package supports Laravel 5.6 and newer, and has been tested with php 7.1 and newer versions.

Configuration

If you want, you can publish the package config file to config/laravelsabre.php:

php artisan vendor:publish --provider="LaravelSabre\LaravelSabreServiceProvider"

If desired, you may disable LaravelSabre entirely using the enabled configuration option:

'enabled' => env('LARAVELSABRE_ENABLED', true),

Change the path configuration to set the url path where the Sabre server will answer to.

Usage

Use LaravelSabre\LaravelSabre class to add node collection and plugins to the Sabre server.

In the example above, DAVServiceProvider is a service provider that has been added to the list of providers in config/app.php file.

Nodes

LaravelSabre::nodes() is used to add nodes collection to the Sabre server.

It may be an array, or a callback function, like in this example here:

Example:

use LaravelSabre\LaravelSabre;
use Sabre\DAVACL\PrincipalCollection;
use Sabre\DAVACL\PrincipalBackend\PDO as PrincipalBackend;

class DAVServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        LaravelSabre::nodes(function () {
            return $this->nodes();
        });
    }

    /**
     * List of nodes for DAV Collection.
     */
    private function nodes() : array
    {
        $principalBackend = new PrincipalBackend();

        return [
            new PrincipalCollection($principalBackend),
        ];
    }
}

Plugins

You can use either:

  • LaravelSbre::plugins() to define a new array of plugins to add to the Sabre server. It may be a callback function.
  • or LaravelSbre::plugin() to add 1 plugin to the list of plugins.

Example:

use LaravelSabre\LaravelSabre;
use LaravelSabre\Http\Auth\AuthBackend;
use Sabre\DAV\Auth\Plugin as AuthPlugin;
use Sabre\CardDAV\Plugin as CardDAVPlugin;

class DAVServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        LaravelSabre::plugins(function () {
            return $this->plugins();
        });
    }

    /**
     * List of Sabre plugins.
     */
    private function plugins()
    {
        // Authentication backend
        $authBackend = new AuthBackend();
        yield new AuthPlugin($authBackend);

        // CardDAV plugin
        yield new CardDAVPlugin();
    }
}

Auth

Use the LaravelSabre::auth() method with the Authorize::class middleware gate, to allow access to some people, based on some criteria.

Example:

LaravelSabre::auth(function () {
    return auth()->user()->email == '[email protected]';
})

License

Author: Alexis Saettler

This project is part of MonicaHQ.

Copyright ยฉ 2019โ€“2022.

Licensed under the MIT License. View license.

laravel-sabre's People

Contributors

asbiin avatar dependabot-preview[bot] avatar dependabot[bot] avatar monicabot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-sabre's Issues

Download large files

When I try to download large files (50MB or greater) I get following error:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 765055744 bytes) {"userId":14,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 1): Allowed memory size of 134217728 bytes exhausted (tried to allocate 765055744 bytes) at /my-project/vendor/monicahq/laravel-sabre/src/Sabre/Server.php:87)
[stacktrace]
#0 /my-project/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(148): Symfony\\Component\\Debug\\Exception\\FatalErrorException->__construct()
#1 /my-project/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(134): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->fatalExceptionFromError()
#2 /my-project/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(0): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleShutdown()
#3 /my-project/vendor/monicahq/laravel-sabre/src/Sabre/Server.php(87): stream_get_contents()
#4 /my-project/vendor/symfony/http-foundation/StreamedResponse.php(114): LaravelSabre\\Sabre\\Server->LaravelSabre\\Sabre\\{closure:/my-project/vendor/monicahq/laravel-sabre/src/Sabre/Server.php:85-91}()
#5 /my-project/vendor/symfony/http-foundation/Response.php(376): Symfony\\Component\\HttpFoundation\\StreamedResponse->sendContent()
#6 /my-project/public/index.php(58): Symfony\\Component\\HttpFoundation\\StreamedResponse->send()
#7 /my-project/server.php(21): ()
#8 /my-project/server.php(0): {main}()
#9 {main}
"}

If I replace the lines vendor/monicahq/laravel-sabre/src/Sabre/Server.php:85-91 to:

if (ob_get_level()) ob_end_clean();
return response()->stream(function () use ($body) {
    fpassthru($body);
}, $status, $headers);

it works perfectly.

So my qustions are:

  • Is it possible to change the response as I did or does this break functionality?
  • Isn't it possible to fully delegate Sabre/DAV the responses?

Note: I want to support files up to 10GB, so just to increase the memory_limit, I think, isn't a good idea.

SabreDAV can't authenticate user

Hi and thank you very much for this library.

I have multiple users and want to authenticate them against the laravel authentication.

This is my app/Providers/DAVServiceProvider.php:

<?php

namespace App\Providers;

use App\Models\User;
use Sabre\HTTP\Sapi;
use Sabre\HTTP\Response;
use LaravelSabre\LaravelSabre;
use Sabre\DAVACL\PrincipalCollection;
use Illuminate\Support\ServiceProvider;
use LaravelSabre\Http\Auth\AuthBackend;
use Sabre\HTTP\Auth\Basic as BasicAuth;
use Sabre\DAV\Auth\Plugin as AuthPlugin;
use Sabre\DAV\FS\Directory as FsDirectory;
use Sabre\DAV\Locks\Plugin as LocksPlugin;
use Sabre\DAV\Browser\Plugin as BrowserPlugin;
use Sabre\DAVACL\PrincipalBackend\PDO as PrincipalBackend;

class DAVServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        LaravelSabre::auth(function () {
            $request  = Sapi::getRequest();
            $response = new Response();
            $auth     = new BasicAuth('MyDrive WebDAV', $request, $response);
            list($username, $password) = $auth->getCredentials();

            \Log::debug($username . $password);
            if ($username && $password) {
                $user = User::whereUsername($username)
                            ->wherePassword(md5($password))
                            ->first();
    
                if ($user && auth()->login($user)) {
                    return true;
                }
            }

            $auth->requireLogin();
            Sapi::sendResponse($response);
            exit;
        });

        LaravelSabre::plugins(function () {
            return $this->plugins();
        });

        LaravelSabre::nodes(function () {
            return $this->nodes();
        });
    }

    /**
     * List of nodes for DAV Collection.
     */
    private function nodes() : array
    {
        $rootDirectory = new FsDirectory(env('DATA_DIR'));

        return [
            $rootDirectory
        ];
    }

    /**
     * List of Sabre plugins.
     */
    private function plugins()
    {
        // Authentication backend
        $authBackend = new AuthBackend();
        yield new AuthPlugin($authBackend);

        yield new BrowserPlugin(); // TODO: remove
        /* yield new LocksPlugin(); */
    }
}

As you can see, I search the user by username and password and get the correct eloquent model. Unfortunately at auth()->login($user) I receive NULL.

Have you any idea?

how to setup DAV in subdomain?

I want to access dav server at subdomain, but it is not clear how to setup it

in config I have this

'domain' => 'dav',
'path' => '',

In DNS I setup A record dav.mydomain.com pointing to my IP. But I cannot understand how to setup nginx config.

For example I have working config for baikal server. Here root is /var/www/baikal/html. Which root should I use for laravel-sabre? Public folder of laravel doesn't work, because at dav.mydomain.com, I just see laravel homepage, not sabre homepage

server {
    server_name dav.lan baikal.lan dav.mydomain2.com baikal.mydomain2.com;
    root /var/www/baikal/html;
    index index.php;

    rewrite ^/.well-known/caldav /dav.php redirect;
    rewrite ^/.well-known/carddav /dav.php redirect;

    charset utf-8;

    location ~ /(\.ht|Core|Specific|config) {
        deny all;
        return 404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.