0

I have setup a CardDav server using https://sabre.io/baikal/. It is working as expected but I now want to create a custom CRM with Laravel which can post contacts to the address books.

Does anyone know if Baikal has its own REST API which I can use to send POST/PUT requests to create new contacts? I came across this in the Github issues https://github.com/sabre-io/Baikal/issues/4 this was posted in 2014 and after looking into Baikal2 it has now been archived.

If Baikal doesnt have its own REST API do you have any suggestions on how I can setup a CardDav Server with its own REST API so I create and list all the address book contacts on an external CRM?

Lukerayner
  • 412
  • 6
  • 23

1 Answers1

0

I use baikal. I like it. It is relatively simple to use actually, you just got to get the hang of it. So you need to make time to mess around with it, get your trial and error on...

I think most of the project has been archived because the main dev @evert has moved on. But he created a great CALDAV/CARDDAV. And @ByteHamster still looks over it and I believe has contributed, or at least provides help on git to answer Q's as much as he can. There is no traditional REST with JSON payload, but once you understand the XML you can break it down and build a JSON response. I did it, and seems to work well. I just can't find it right now..

I didn't get a chance to venture into CARDDAV, but once baikal is installed as a sub domain, or however you want to do it, sub domain is recommended, you can do calls to the endpoint you need via curl in PHP to receive an XML response.

Take note, this is a mess and is out of context. I've also never had time to rewrite for efficiency or make it pretty, sorry. But hopefully it gives you a lead. Practice your calls from the terminal with curl

use Sabre\VObject;  //this is to use the vobjects
use Sabre\DAV\Client;

require_once( str_replace( 'classes', '',  __DIR__ . '/baikal/vendor/autoload.php') );

class ical{

    public function getVTODOS($model, $cnx){
        /*
        *   getVEVENTS and getVTODOS are practically the same, deprecate this to make only one call dumbass
        */
//array(3) { ["datestamp"]=> string(10) "2021-07-27" ["start"]=> string(8) "20210401" ["end"]=> string(9) "20220731 " }

        $ical = new ical;
        $accounts = $ical->authenticate($cnx);

if(!empty($accounts['accounts'])){
        if(isset($model['start']) && isset($model['end'])){
            $start = date('Ymd', strtotime($model['start'])) . 'T000000Z';
            $end = date('Ymd', strtotime($model['end'])) . 'T000000Z';
        }else{
            //  use $date and strtotime() to get last year and next year, date format ex. 20171214T000000Z
            //  date('Ymd', strtotime($datestamp . '- 1 year'));  //ex. present year 2017
            $start = date('Ymd', strtotime($datestamp . '- 1 year')) . 'T000000Z';//ex. 20161213T000000Z
            $end = date('Ymd', strtotime($datestamp . '+ 1 year')) . 'T000000Z';//ex. 20181213T000000Z
        }

        $request = '<?xml version="1.0" encoding="UTF-8" ?>
                <L:calendar-query xmlns:L="urn:ietf:params:xml:ns:caldav">
                    <D:prop xmlns:D="DAV:">
                        <D:getcontenttype/>
                        <D:resourcetype/>
                        <D:getetag/>
                        <L:calendar-data/>
                    </D:prop>
                    <L:filter>
                        <L:comp-filter name="VCALENDAR"><L:comp-filter name="VTODO">
                            <L:time-range start="'.$start.'" end="'.$end.'"/>
                            </L:comp-filter>
                            </L:comp-filter>
                    </L:filter>
                </L:calendar-query>';

        $headers = array(
            'Content-Type: text/xml; charset=utf-8',
            'Depth:1',
        );

        //$url = 'https://cal.domain.ca/cal.php/calendars/' . $user['caldav-username'] . '/default/';
        $url = CALDAV . '/cal.php/calendars/' . $user['caldav-username'] . '/default/';
        $userpwd = $user['caldav-username'] . ':' . $user['caldav-password'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'REPORT');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

        return curl_exec($ch);
        curl_close($ch);
    }//public function getVTODOS($model, $cnx, $datestamp)

    }else{
        $response = null;
    }//if(!empty($accounts['accounts']))

}//ical
gstlouis
  • 113
  • 2
  • 10