dolibarr 25.0.0-alpha
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-2026 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";
57require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
58require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
59require_once DOL_DOCUMENT_ROOT.'/dav/dav.class.php';
60require_once DOL_DOCUMENT_ROOT.'/dav/dav.lib.php';
61
62require_once DOL_DOCUMENT_ROOT.'/includes/sabre/autoload.php';
63//require_once DOL_DOCUMENT_ROOT.'/includes/autoload.php';
64
65$user = new User($db);
66if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] != '') {
67 $user->fetch(0, $_SERVER['PHP_AUTH_USER']);
68 $user->loadRights();
69}
70
71// Load translation files required by the page
72$langs->loadLangs(array("main", "other"));
73
74
75if (empty($conf->dav->enabled)) {
77}
78
79// Restrict API to some IPs
80if (getDolGlobalString('DAV_RESTRICT_ON_IP')) {
81 $allowedip = explode(' ', getDolGlobalString('DAV_RESTRICT_ON_IP'));
82 $ipremote = getUserRemoteIP();
83 if (!in_array($ipremote, $allowedip)) {
84 dol_syslog('Remote ip is '.$ipremote.', not into list ' . getDolGlobalString('DAV_RESTRICT_ON_IP'));
85 print 'DAV not allowed from the IP '.$ipremote;
86 header('HTTP/1.1 503 DAV not allowed from your IP '.$ipremote);
87 exit(0);
88 }
89}
90
91
92$entity = (GETPOSTINT('entity') ? GETPOSTINT('entity') : (!empty($conf->entity) ? $conf->entity : 1));
93
94// settings
95$publicDir = DOL_DATA_ROOT.'/dav/public';
96$privateDir = DOL_DATA_ROOT.'/dav/private';
97$ecmDir = DOL_DATA_ROOT.'/ecm';
98$tmpDir = DOL_DATA_ROOT.'/ecm/temp';
99if (isModEnabled('dav')) {
100 $publicDir = $conf->dav->multidir_output[$entity].'/public';
101 $privateDir = $conf->dav->multidir_output[$entity].'/private';
102}
103if (isModEnabled('ecm')) {
104 $ecmDir = $conf->ecm->multidir_output[$entity];
105 $tmpDir = $conf->ecm->multidir_output[$entity]; // We need root dir, not a dir that can be deleted, so we use multidir_output
106}
107//var_dump($tmpDir);mkdir($tmpDir);exit;
108
109
110// Authentication callback function
111$authBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(
117 static function ($username, $password) {
118 global $user, $conf;
119 global $dolibarr_main_authentication, $dolibarr_auto_user;
120
121 if (empty($user->login)) {
122 dol_syslog("Failed to authenticate to DAV, login is not provided", LOG_WARNING);
123 return false;
124 }
125 if ($user->socid > 0) {
126 dol_syslog("Failed to authenticate to DAV, user is an external user", LOG_WARNING);
127 return false;
128 }
129 if ($user->login != $username) {
130 dol_syslog("Failed to authenticate to DAV, login does not match the login of loaded user", LOG_WARNING);
131 return false;
132 }
133
134 // Authentication mode
135 if (empty($dolibarr_main_authentication) || $dolibarr_main_authentication == 'openid_connect') {
136 $dolibarr_main_authentication = 'dolibarr';
137 }
138
139 // Authentication mode: forceuser
140 if ($dolibarr_main_authentication == 'forceuser') {
141 if (empty($dolibarr_auto_user)) {
142 $dolibarr_auto_user = 'auto';
143 }
144 if ($dolibarr_auto_user != $username) {
145 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.");
146 return false;
147 }
148 }
149
150 $authmode = explode(',', $dolibarr_main_authentication);
151 $entity = (GETPOSTINT('entity') ? GETPOSTINT('entity') : (!empty($conf->entity) ? $conf->entity : 1));
152
153 if (checkLoginPassEntity($username, $password, $entity, $authmode, 'dav') != $username) {
154 return false;
155 }
156
157 // Check if user status is enabled
158 if ($user->status != $user::STATUS_ENABLED) {
159 // Status is disabled
160 dol_syslog("The user has been disabled.");
161 return false;
162 }
163
164 // Check if session was unvalidated by a password change
165 if (($user->flagdelsessionsbefore && !empty($_SESSION["dol_logindate"]) && $user->flagdelsessionsbefore > $_SESSION["dol_logindate"])) {
166 // Session is no more valid
167 dol_syslog("The user has a date for session invalidation = ".$user->flagdelsessionsbefore." and a session date = ".$_SESSION["dol_logindate"].". We must invalidate its sessions.");
168 return false;
169 }
170
171 // Check date validity
172 if ($user->isNotIntoValidityDateRange()) {
173 // User validity dates are no more valid
174 dol_syslog("The user login has a validity between [".$user->datestartvalidity." and ".$user->dateendvalidity."], current date is ".dol_now());
175 return false;
176 }
177
178 return true;
179 }
180);
181
182$authBackend->setRealm(constant('DOL_APPLICATION_TITLE').' - WebDAV');
183
184
185
186
187
188/*
189 * Actions and View
190 */
191
192// Create the root node
193// Setting up the directory tree //
194$nodes = array();
195
196// Enable directories and features according to DAV setup
197// Public dir
198if (getDolGlobalString('DAV_ALLOW_PUBLIC_DIR')) {
199 $nodes[] = new \Sabre\DAV\FS\Directory($publicDir);
200}
201// Private dir
202$nodes[] = new \Sabre\DAV\FS\Directory($privateDir);
203// ECM dir
204if (isModEnabled('ecm') && getDolGlobalString('DAV_ALLOW_ECM_DIR')) {
205 $nodes[] = new \Sabre\DAV\FS\Directory($ecmDir);
206}
207
208
209
210// Principals Backend
211//$principalBackend = new \Sabre\DAVACL\PrincipalBackend\Dolibarr($user,$db);
212// /principals
213//$nodes[] = new \Sabre\DAVACL\PrincipalCollection($principalBackend);
214// CardDav & CalDav Backend
215//$carddavBackend = new \Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
216//$caldavBackend = new \Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
217// /addressbook
218//$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
219// /calendars
220//$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend);
221
222
223// The rootnode needs in turn to be passed to the server class
224$server = new \Sabre\DAV\Server($nodes);
225
226// If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
227// You can override the baseUri here.
228$baseUri = DOL_URL_ROOT.'/dav/fileserver.php/';
229if (isset($baseUri)) {
230 $server->setBaseUri($baseUri);
231}
232
233// Add authentication function
234if ((!getDolGlobalString('DAV_ALLOW_PUBLIC_DIR')
235 || !preg_match('/'.preg_quote(DOL_URL_ROOT.'/dav/fileserver.php/public', '/').'/', Sabre\Uri\normalize($_SERVER["PHP_SELF"])))
236 && !preg_match('/^sabreAction=asset&assetName=[a-zA-Z0-9%\-\/]+\.(png|css|woff|ico|ttf)$/', $_SERVER["QUERY_STRING"]) // URL for Sabre browser resources
237) {
238 //var_dump($_SERVER["QUERY_STRING"]);exit;
239 $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
240}
241// Support for LOCK and UNLOCK
242$lockBackend = new \Sabre\DAV\Locks\Backend\File($tmpDir.'/.locksdb');
243$lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
244$server->addPlugin($lockPlugin);
245
246// Support for the html browser
247if (!getDolGlobalString('DAV_DISABLE_BROWSER')) {
248 $browser = new \Sabre\DAV\Browser\Plugin();
249 $server->addPlugin($browser);
250}
251
252// Automatically guess (some) contenttypes, based on extension
253//$server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
254
255//$server->addPlugin(new \Sabre\CardDAV\Plugin());
256//$server->addPlugin(new \Sabre\CalDAV\Plugin());
257//$server->addPlugin(new \Sabre\DAVACL\Plugin());
258
259// Temporary file filter
260/*$tempFF = new \Sabre\DAV\TemporaryFileFilterPlugin($tmpDir);
261$server->addPlugin($tempFF);
262*/
263
264// And off we go!
265$server->start();
266
267if (is_object($db)) {
268 $db->close();
269}
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
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.