dolibarr 21.0.0-beta
fileserver.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Destailleur Laurent <eldy@users.sourceforge.net>
3 * Copyright (C) 2019 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 *
20 * You can test with the WebDav client cadaver:
21 * cadaver http://myurl/dav/fileserver.php
22 */
23
30if (!defined('NOTOKENRENEWAL')) {
31 define('NOTOKENRENEWAL', '1');
32}
33if (!defined('NOREQUIREMENU')) {
34 define('NOREQUIREMENU', '1'); // If there is no menu to show
35}
36if (!defined('NOREQUIREHTML')) {
37 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
38}
39if (!defined('NOREQUIREAJAX')) {
40 define('NOREQUIREAJAX', '1');
41}
42if (!defined('NOLOGIN')) {
43 define("NOLOGIN", 1); // This means this output page does not require to be logged.
44}
45if (!defined('NOCSRFCHECK')) {
46 define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
47}
48
49require "../main.inc.php";
50require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
51require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
52require_once DOL_DOCUMENT_ROOT.'/dav/dav.class.php';
53require_once DOL_DOCUMENT_ROOT.'/dav/dav.lib.php';
54
55require_once DOL_DOCUMENT_ROOT.'/includes/sabre/autoload.php';
56//require_once DOL_DOCUMENT_ROOT.'/includes/autoload.php';
57
66$user = new User($db);
67if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] != '') {
68 $user->fetch(0, $_SERVER['PHP_AUTH_USER']);
69 $user->loadRights();
70}
71
72// Load translation files required by the page
73$langs->loadLangs(array("main", "other"));
74
75
76if (empty($conf->dav->enabled)) {
78}
79
80// Restrict API to some IPs
81if (getDolGlobalString('DAV_RESTRICT_ON_IP')) {
82 $allowedip = explode(' ', getDolGlobalString('DAV_RESTRICT_ON_IP'));
83 $ipremote = getUserRemoteIP();
84 if (!in_array($ipremote, $allowedip)) {
85 dol_syslog('Remote ip is '.$ipremote.', not into list ' . getDolGlobalString('DAV_RESTRICT_ON_IP'));
86 print 'DAV not allowed from the IP '.$ipremote;
87 header('HTTP/1.1 503 DAV not allowed from your IP '.$ipremote);
88 exit(0);
89 }
90}
91
92
93$entity = (GETPOSTINT('entity') ? GETPOSTINT('entity') : (!empty($conf->entity) ? $conf->entity : 1));
94
95// settings
96$publicDir = DOL_DATA_ROOT.'/dav/public';
97$privateDir = DOL_DATA_ROOT.'/dav/private';
98$ecmDir = DOL_DATA_ROOT.'/ecm';
99$tmpDir = DOL_DATA_ROOT.'/ecm/temp';
100if (isModEnabled('dav')) {
101 $publicDir = $conf->dav->multidir_output[$entity].'/public';
102 $privateDir = $conf->dav->multidir_output[$entity].'/private';
103}
104if (isModEnabled('ecm')) {
105 $ecmDir = $conf->ecm->multidir_output[$entity];
106 $tmpDir = $conf->ecm->multidir_output[$entity]; // We need root dir, not a dir that can be deleted, so we use multidir_output
107}
108//var_dump($tmpDir);mkdir($tmpDir);exit;
109
110
111// Authentication callback function
112$authBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(
118 static function ($username, $password) {
119 global $user, $conf;
120 global $dolibarr_main_authentication, $dolibarr_auto_user;
121
122 if (empty($user->login)) {
123 dol_syslog("Failed to authenticate to DAV, login is not provided", LOG_WARNING);
124 return false;
125 }
126 if ($user->socid > 0) {
127 dol_syslog("Failed to authenticate to DAV, user is an external user", LOG_WARNING);
128 return false;
129 }
130 if ($user->login != $username) {
131 dol_syslog("Failed to authenticate to DAV, login does not match the login of loaded user", LOG_WARNING);
132 return false;
133 }
134
135 // Authentication mode
136 if (empty($dolibarr_main_authentication) || $dolibarr_main_authentication == 'openid_connect') {
137 $dolibarr_main_authentication = 'dolibarr';
138 }
139
140 // Authentication mode: forceuser
141 if ($dolibarr_main_authentication == 'forceuser') {
142 if (empty($dolibarr_auto_user)) {
143 $dolibarr_auto_user = 'auto';
144 }
145 if ($dolibarr_auto_user != $username) {
146 dol_syslog("Warning: your instance is set to use the automatic forced login '".$dolibarr_auto_user."' that is not the requested login. DAV usage is forbidden in this mode.");
147 return false;
148 }
149 }
150
151 $authmode = explode(',', $dolibarr_main_authentication);
152 $entity = (GETPOSTINT('entity') ? GETPOSTINT('entity') : (!empty($conf->entity) ? $conf->entity : 1));
153
154 if (checkLoginPassEntity($username, $password, $entity, $authmode, 'dav') != $username) {
155 return false;
156 }
157
158 // Check if user status is enabled
159 if ($user->statut != $user::STATUS_ENABLED) {
160 // Status is disabled
161 dol_syslog("The user has been disabled.");
162 return false;
163 }
164
165 // Check if session was unvalidated by a password change
166 if (($user->flagdelsessionsbefore && !empty($_SESSION["dol_logindate"]) && $user->flagdelsessionsbefore > $_SESSION["dol_logindate"])) {
167 // Session is no more valid
168 dol_syslog("The user has a date for session invalidation = ".$user->flagdelsessionsbefore." and a session date = ".$_SESSION["dol_logindate"].". We must invalidate its sessions.");
169 return false;
170 }
171
172 // Check date validity
173 if ($user->isNotIntoValidityDateRange()) {
174 // User validity dates are no more valid
175 dol_syslog("The user login has a validity between [".$user->datestartvalidity." and ".$user->dateendvalidity."], current date is ".dol_now());
176 return false;
177 }
178
179 return true;
180 }
181);
182
183$authBackend->setRealm(constant('DOL_APPLICATION_TITLE').' - WebDAV');
184
185
186
187
188
189/*
190 * Actions and View
191 */
192
193// Create the root node
194// Setting up the directory tree //
195$nodes = array();
196
197// Enable directories and features according to DAV setup
198// Public dir
199if (getDolGlobalString('DAV_ALLOW_PUBLIC_DIR')) {
200 $nodes[] = new \Sabre\DAV\FS\Directory($publicDir);
201}
202// Private dir
203$nodes[] = new \Sabre\DAV\FS\Directory($privateDir);
204// ECM dir
205if (isModEnabled('ecm') && getDolGlobalString('DAV_ALLOW_ECM_DIR')) {
206 $nodes[] = new \Sabre\DAV\FS\Directory($ecmDir);
207}
208
209
210
211// Principals Backend
212//$principalBackend = new \Sabre\DAVACL\PrincipalBackend\Dolibarr($user,$db);
213// /principals
214//$nodes[] = new \Sabre\DAVACL\PrincipalCollection($principalBackend);
215// CardDav & CalDav Backend
216//$carddavBackend = new \Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
217//$caldavBackend = new \Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
218// /addressbook
219//$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
220// /calendars
221//$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend);
222
223
224// The rootnode needs in turn to be passed to the server class
225$server = new \Sabre\DAV\Server($nodes);
226
227// If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
228// You can override the baseUri here.
229$baseUri = DOL_URL_ROOT.'/dav/fileserver.php/';
230if (isset($baseUri)) {
231 $server->setBaseUri($baseUri);
232}
233
234// Add authentication function
235if ((!getDolGlobalString('DAV_ALLOW_PUBLIC_DIR')
236 || !preg_match('/'.preg_quote(DOL_URL_ROOT.'/dav/fileserver.php/public', '/').'/', $_SERVER["PHP_SELF"]))
237 && !preg_match('/^sabreAction=asset&assetName=[a-zA-Z0-9%\-\/]+\.(png|css|woff|ico|ttf)$/', $_SERVER["QUERY_STRING"]) // URL for Sabre browser resources
238) {
239 //var_dump($_SERVER["QUERY_STRING"]);exit;
240 $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
241}
242// Support for LOCK and UNLOCK
243$lockBackend = new \Sabre\DAV\Locks\Backend\File($tmpDir.'/.locksdb');
244$lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
245$server->addPlugin($lockPlugin);
246
247// Support for the html browser
248if (!getDolGlobalString('DAV_DISABLE_BROWSER')) {
249 $browser = new \Sabre\DAV\Browser\Plugin();
250 $server->addPlugin($browser);
251}
252
253// Automatically guess (some) contenttypes, based on extension
254//$server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
255
256//$server->addPlugin(new \Sabre\CardDAV\Plugin());
257//$server->addPlugin(new \Sabre\CalDAV\Plugin());
258//$server->addPlugin(new \Sabre\DAVACL\Plugin());
259
260// Temporary file filter
261/*$tempFF = new \Sabre\DAV\TemporaryFileFilterPlugin($tmpDir);
262$server->addPlugin($tempFF);
263*/
264
265// And off we go!
266$server->start();
267
268if (is_object($db)) {
269 $db->close();
270}
Class to manage Dolibarr users.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context='')
Return a login if login/pass was successful.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.