forked from Befox/cdav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
216 lines (175 loc) · 6.48 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/******************************************************************
* cdav is a Dolibarr module
* It allows caldav and carddav clients to sync with Dolibarr
* calendars and contacts.
*
* cdav is distributed under GNU/GPLv3 license
* (see COPYING file)
*
* cdav uses Sabre/dav library http://sabre.io/dav/
* Sabre/dav is distributed under use the three-clause BSD-license
*
* Author : Befox SARL http://www.befox.fr/
*
******************************************************************/
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 0);
ini_set("log_errors", 1);
function exception_error_handler($errno, $errstr, $errfile, $errline) {
if(function_exists("debug_log"))
{
debug_log("Error $errno : $errstr - $errfile @ $errline");
foreach(debug_backtrace(false) as $trace)
debug_log(" - ".$trace['file'].'@'.$trace['line'].' '.$trace['function'].'(...)');
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
// debug
//$debug_file = fopen('/tmp/cdav_'.date('Ymd').'.log','a');
$debug_file = false;
function debug_log($txt)
{
global $debug_file;
if ($debug_file)
{
fputs($debug_file, '========' . date('H:i:s').': '.$txt."\n");
fflush($debug_file);
}
}
// HTTP auth workaround for php in fastcgi mode HTTP_AUTHORIZATION set by rewrite engine in .htaccess
if( isset($_SERVER['HTTP_AUTHORIZATION']) &&
(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) )
{
$rAuth = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
if(count($rAuth)>1)
{
$_SERVER['PHP_AUTH_USER'] = $rAuth[0];
$_SERVER['PHP_AUTH_PW'] = $rAuth[1];
}
}
// HTTP auth workaround for php in fastcgi mode REDIRECT_HTTP_AUTHORIZATION set by rewrite engine in .htaccess
if( isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) &&
(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) )
{
$rAuth = explode(':', base64_decode(substr($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 6)));
if(count($rAuth)>1)
{
$_SERVER['PHP_AUTH_USER'] = $rAuth[0];
$_SERVER['PHP_AUTH_PW'] = $rAuth[1];
}
}
define('NOTOKENRENEWAL',1); // Disables token renewal
if (! defined('NOLOGIN')) define('NOLOGIN','1');
if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); // We accept to go on this page from external web site.
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
function llxHeader() { }
function llxFooter() { }
if(is_file('../main.inc.php'))
$dir = '../';
elseif(is_file('../../../main.inc.php'))
$dir = '../../../';
else
$dir = '../../';
require $dir.'main.inc.php'; // Load $user and permissions
require $dir.'core/lib/security2.lib.php'; // auth method
require_once $dir.'/contact/class/contact.class.php';
if(!$conf->cdav->enabled)
die('module CDav not enabled !');
set_error_handler("exception_error_handler", E_ERROR | E_USER_ERROR |
E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR );
require_once './lib/cdav.lib.php';
// define CDAV_CONTACT_TAG if not
if(!defined('CDAV_CONTACT_TAG'))
{
if(isset($conf->global->CDAV_CONTACT_TAG))
define('CDAV_CONTACT_TAG', $conf->global->CDAV_CONTACT_TAG);
else
define('CDAV_CONTACT_TAG', '');
}
// define CDAV_URI_KEY if not
if(!defined('CDAV_URI_KEY'))
{
if(isset($conf->global->CDAV_URI_KEY))
define('CDAV_URI_KEY', $conf->global->CDAV_URI_KEY);
else
define('CDAV_URI_KEY', substr(md5($_SERVER['HTTP_HOST']),0,8));
}
// Sabre/dav configuration
use Sabre\DAV;
use Sabre\DAVACL;
// The autoloader
require './lib/SabreDAV/vendor/autoload.php';
require './class/PrincipalsDolibarr.php';
require './class/CardDAVDolibarr.php';
require './class/CalDAVDolibarr.php';
$user = new User($db);
if(isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER']!='')
{
$user->fetch('',$_SERVER['PHP_AUTH_USER']);
$user->getrights();
}
$cdavLib = new CdavLib($user, $db, $langs);
// Authentication
$authBackend = new DAV\Auth\Backend\BasicCallBack(function ($username, $password)
{
global $user;
global $conf;
global $dolibarr_main_authentication;
if ( ! isset($user->login) || $user->login=='')
return false;
if ($user->societe_id!=0)
return false;
if ($user->login!=$username)
return false;
/*if ($user->pass_indatabase_crypted == '' || dol_hash($password) != $user->pass_indatabase_crypted)
return false;*/
// Authentication mode
if (empty($dolibarr_main_authentication))
$dolibarr_main_authentication='http,dolibarr';
$authmode = explode(',',$dolibarr_main_authentication);
$entity = (GETPOST('entity','int') ? GETPOST('entity','int') : (!empty($conf->entity) ? $conf->entity : 1));
if(checkLoginPassEntity($username,$password,$entity,$authmode)!=$username)
return false;
return true;
});
$authBackend->setRealm('Dolibarr');
// The lock manager is reponsible for making sure users don't overwrite
// each others changes.
$lockBackend = new DAV\Locks\Backend\File($dolibarr_main_data_root.'/cdav/.locks');
// Principals Backend
$principalBackend = new DAVACL\PrincipalBackend\Dolibarr($user,$db);
// CardDav & CalDav Backend
$carddavBackend = new Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
$caldavBackend = new Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
// Setting up the directory tree //
$nodes = array(
// /principals
new DAVACL\PrincipalCollection($principalBackend),
// /addressbook
new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend),
// /calendars
new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend),
// / Public docs
new DAV\FS\Directory($dolibarr_main_data_root. '/cdav/public')
);
// admin can access all dolibarr documents
if($user->admin)
$nodes[] = new DAV\FS\Directory($dolibarr_main_data_root);
// The server object is responsible for making sense out of the WebDAV protocol
$server = new DAV\Server($nodes);
// If your server is not on your webroot, make sure the following line has the
// correct information
$server->setBaseUri(dol_buildpath('cdav/server.php', 1).'/');
$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
$server->addPlugin(new \Sabre\DAV\Locks\Plugin($lockBackend));
$server->addPlugin(new \Sabre\DAV\Browser\Plugin());
$server->addPlugin(new \Sabre\CardDAV\Plugin());
$server->addPlugin(new \Sabre\CalDAV\Plugin());
$server->addPlugin(new \Sabre\DAVACL\Plugin());
// $server->addPlugin(new \Sabre\DAV\Sync\Plugin());
// All we need to do now, is to fire up the server
$server->exec();
if (is_object($db)) $db->close();