dolibarr  17.0.4
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  *
18  * You can test with the WebDav client cadaver:
19  * cadaver http://myurl/dav/fileserver.php
20  */
21 
28 if (!defined('NOTOKENRENEWAL')) {
29  define('NOTOKENRENEWAL', '1');
30 }
31 if (!defined('NOREQUIREMENU')) {
32  define('NOREQUIREMENU', '1'); // If there is no menu to show
33 }
34 if (!defined('NOREQUIREHTML')) {
35  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
36 }
37 if (!defined('NOREQUIREAJAX')) {
38  define('NOREQUIREAJAX', '1');
39 }
40 if (!defined('NOLOGIN')) {
41  define("NOLOGIN", 1); // This means this output page does not require to be logged.
42 }
43 if (!defined('NOCSRFCHECK')) {
44  define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
45 }
46 
47 require "../main.inc.php";
48 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
49 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
50 require_once DOL_DOCUMENT_ROOT.'/dav/dav.class.php';
51 require_once DOL_DOCUMENT_ROOT.'/dav/dav.lib.php';
52 require_once DOL_DOCUMENT_ROOT.'/includes/sabre/autoload.php';
53 
54 
55 $user = new User($db);
56 if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] != '') {
57  $user->fetch('', $_SERVER['PHP_AUTH_USER']);
58  $user->getrights();
59 }
60 
61 // Load translation files required by the page
62 $langs->loadLangs(array("main", "other"));
63 
64 
65 if (empty($conf->dav->enabled)) {
67 }
68 
69 // Restrict API to some IPs
70 if (!empty($conf->global->DAV_RESTRICT_ON_IP)) {
71  $allowedip = explode(' ', $conf->global->DAV_RESTRICT_ON_IP);
72  $ipremote = getUserRemoteIP();
73  if (!in_array($ipremote, $allowedip)) {
74  dol_syslog('Remote ip is '.$ipremote.', not into list '.$conf->global->DAV_RESTRICT_ON_IP);
75  print 'DAV not allowed from the IP '.$ipremote;
76  header('HTTP/1.1 503 DAV not allowed from your IP '.$ipremote);
77  //print $conf->global->DAV_RESTRICT_ON_IP;
78  exit(0);
79  }
80 }
81 
82 
83 $entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : (!empty($conf->entity) ? $conf->entity : 1));
84 
85 // settings
86 $publicDir = $conf->dav->multidir_output[$entity].'/public';
87 $privateDir = $conf->dav->multidir_output[$entity].'/private';
88 $ecmDir = $conf->ecm->multidir_output[$entity];
89 $tmpDir = $conf->dav->multidir_output[$entity]; // We need root dir, not a dir that can be deleted
90 //var_dump($tmpDir);mkdir($tmpDir);exit;
91 
92 
93 // Authentication callback function
94 $authBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(function ($username, $password) {
95  global $user;
96  global $conf;
97  global $dolibarr_main_authentication, $dolibarr_auto_user;
98 
99  if (empty($user->login)) {
100  dol_syslog("Failed to authenticate to DAV, login is not provided", LOG_WARNING);
101  return false;
102  }
103  if ($user->socid > 0) {
104  dol_syslog("Failed to authenticate to DAV, use is an external user", LOG_WARNING);
105  return false;
106  }
107  if ($user->login != $username) {
108  dol_syslog("Failed to authenticate to DAV, login does not match the login of loaded user", LOG_WARNING);
109  return false;
110  }
111 
112  // Authentication mode
113  if (empty($dolibarr_main_authentication)) {
114  $dolibarr_main_authentication = 'dolibarr';
115  }
116 
117  // Authentication mode: forceuser
118  if ($dolibarr_main_authentication == 'forceuser') {
119  if (empty($dolibarr_auto_user)) {
120  $dolibarr_auto_user = 'auto';
121  }
122  if ($dolibarr_auto_user != $username) {
123  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.");
124  return false;
125  }
126  }
127 
128  $authmode = explode(',', $dolibarr_main_authentication);
129  $entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : (!empty($conf->entity) ? $conf->entity : 1));
130 
131  if (checkLoginPassEntity($username, $password, $entity, $authmode, 'dav') != $username) {
132  return false;
133  }
134 
135  return true;
136 });
137 
138 $authBackend->setRealm(constant('DOL_APPLICATION_TITLE').' - WebDAV');
139 
140 
141 
142 
143 
144 /*
145  * Actions and View
146  */
147 
148 // Create the root node
149 // Setting up the directory tree //
150 $nodes = array();
151 
152 // Enable directories and features according to DAV setup
153 // Public dir
154 if (!empty($conf->global->DAV_ALLOW_PUBLIC_DIR)) {
155  $nodes[] = new \Sabre\DAV\FS\Directory($publicDir);
156 }
157 // Private dir
158 $nodes[] = new \Sabre\DAV\FS\Directory($privateDir);
159 // ECM dir
160 if (isModEnabled('ecm') && !empty($conf->global->DAV_ALLOW_ECM_DIR)) {
161  $nodes[] = new \Sabre\DAV\FS\Directory($ecmDir);
162 }
163 
164 
165 
166 // Principals Backend
167 //$principalBackend = new \Sabre\DAVACL\PrincipalBackend\Dolibarr($user,$db);
168 // /principals
169 //$nodes[] = new \Sabre\DAVACL\PrincipalCollection($principalBackend);
170 // CardDav & CalDav Backend
171 //$carddavBackend = new \Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
172 //$caldavBackend = new \Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
173 // /addressbook
174 //$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
175 // /calendars
176 //$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend);
177 
178 
179 // The rootnode needs in turn to be passed to the server class
180 $server = new \Sabre\DAV\Server($nodes);
181 
182 // If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
183 // You can override the baseUri here.
184 $baseUri = DOL_URL_ROOT.'/dav/fileserver.php/';
185 if (isset($baseUri)) {
186  $server->setBaseUri($baseUri);
187 }
188 
189 // Add authentication function
190 if ((empty($conf->global->DAV_ALLOW_PUBLIC_DIR)
191  || !preg_match('/'.preg_quote(DOL_URL_ROOT.'/dav/fileserver.php/public', '/').'/', $_SERVER["PHP_SELF"]))
192  && !preg_match('/^sabreAction=asset&assetName=[a-zA-Z0-9%\-\/]+\.(png|css|woff|ico|ttf)$/', $_SERVER["QUERY_STRING"]) // URL for Sabre browser resources
193  ) {
194  //var_dump($_SERVER["QUERY_STRING"]);exit;
195  $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
196 }
197 // Support for LOCK and UNLOCK
198 $lockBackend = new \Sabre\DAV\Locks\Backend\File($tmpDir.'/.locksdb');
199 $lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
200 $server->addPlugin($lockPlugin);
201 
202 // Support for the html browser
203 if (empty($conf->global->DAV_DISABLE_BROWSER)) {
204  $browser = new \Sabre\DAV\Browser\Plugin();
205  $server->addPlugin($browser);
206 }
207 
208 // Automatically guess (some) contenttypes, based on extension
209 //$server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
210 
211 //$server->addPlugin(new \Sabre\CardDAV\Plugin());
212 //$server->addPlugin(new \Sabre\CalDAV\Plugin());
213 //$server->addPlugin(new \Sabre\DAVACL\Plugin());
214 
215 // Temporary file filter
216 /*$tempFF = new \Sabre\DAV\TemporaryFileFilterPlugin($tmpDir);
217 $server->addPlugin($tempFF);
218 */
219 
220 // And off we go!
221 $server->exec();
222 
223 if (is_object($db)) {
224  $db->close();
225 }
Class to manage Dolibarr users.
Definition: user.class.php:47
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getUserRemoteIP()
Return the IP of remote user.
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 successfull.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.