dolibarr  13.0.2
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')) define('NOTOKENRENEWAL', '1');
29 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1'); // If there is no menu to show
30 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
31 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
32 if (!defined('NOLOGIN')) define("NOLOGIN", 1); // This means this output page does not require to be logged.
33 if (!defined('NOCSRFCHECK')) define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
34 
35 require "../main.inc.php";
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/dav/dav.class.php';
39 require_once DOL_DOCUMENT_ROOT.'/dav/dav.lib.php';
40 require_once DOL_DOCUMENT_ROOT.'/includes/sabre/autoload.php';
41 
42 
43 $user = new User($db);
44 if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] != '')
45 {
46  $user->fetch('', $_SERVER['PHP_AUTH_USER']);
47  $user->getrights();
48 }
49 
50 // Load translation files required by the page
51 $langs->loadLangs(array("main", "other"));
52 
53 
54 if (empty($conf->dav->enabled))
56 
57 
58 // Restrict API to some IPs
59 if (!empty($conf->global->DAV_RESTRICT_ON_IP))
60 {
61  $allowedip = explode(' ', $conf->global->DAV_RESTRICT_ON_IP);
62  $ipremote = getUserRemoteIP();
63  if (!in_array($ipremote, $allowedip))
64  {
65  dol_syslog('Remote ip is '.$ipremote.', not into list '.$conf->global->DAV_RESTRICT_ON_IP);
66  print 'DAV not allowed from the IP '.$ipremote;
67  header('HTTP/1.1 503 DAV not allowed from your IP '.$ipremote);
68  //print $conf->global->DAV_RESTRICT_ON_IP;
69  exit(0);
70  }
71 }
72 
73 
74 $entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : (!empty($conf->entity) ? $conf->entity : 1));
75 
76 // settings
77 $publicDir = $conf->dav->multidir_output[$entity].'/public';
78 $privateDir = $conf->dav->multidir_output[$entity].'/private';
79 $ecmDir = $conf->ecm->multidir_output[$entity];
80 $tmpDir = $conf->dav->multidir_output[$entity]; // We need root dir, not a dir that can be deleted
81 //var_dump($tmpDir);mkdir($tmpDir);exit;
82 
83 
84 // Authentication callback function
85 $authBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(function ($username, $password) {
86  global $user;
87  global $conf;
88  global $dolibarr_main_authentication, $dolibarr_auto_user;
89 
90  if (empty($user->login))
91  {
92  dol_syslog("Failed to authenticate to DAV, login is not provided", LOG_WARNING);
93  return false;
94  }
95  if ($user->socid > 0)
96  {
97  dol_syslog("Failed to authenticate to DAV, use is an external user", LOG_WARNING);
98  return false;
99  }
100  if ($user->login != $username)
101  {
102  dol_syslog("Failed to authenticate to DAV, login does not match the login of loaded user", LOG_WARNING);
103  return false;
104  }
105 
106  // Authentication mode
107  if (empty($dolibarr_main_authentication)) $dolibarr_main_authentication = 'dolibarr';
108 
109  // Authentication mode: forceuser
110  if ($dolibarr_main_authentication == 'forceuser')
111  {
112  if (empty($dolibarr_auto_user)) $dolibarr_auto_user = 'auto';
113  if ($dolibarr_auto_user != $username)
114  {
115  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.");
116  return false;
117  }
118  }
119 
120  $authmode = explode(',', $dolibarr_main_authentication);
121  $entity = (GETPOST('entity', 'int') ? GETPOST('entity', 'int') : (!empty($conf->entity) ? $conf->entity : 1));
122 
123  if (checkLoginPassEntity($username, $password, $entity, $authmode, 'dav') != $username)
124  return false;
125 
126  return true;
127 });
128 
129 $authBackend->setRealm(constant('DOL_APPLICATION_TITLE'));
130 
131 
132 
133 
134 
135 /*
136  * Actions and View
137  */
138 
139 // Create the root node
140 // Setting up the directory tree //
141 $nodes = array();
142 
143 // Enable directories and features according to DAV setup
144 // Public dir
145 if (!empty($conf->global->DAV_ALLOW_PUBLIC_DIR))
146 {
147  $nodes[] = new \Sabre\DAV\FS\Directory($publicDir);
148 }
149 // Private dir
150 $nodes[] = new \Sabre\DAV\FS\Directory($privateDir);
151 // ECM dir
152 if (!empty($conf->ecm->enabled) && !empty($conf->global->DAV_ALLOW_ECM_DIR))
153 {
154  $nodes[] = new \Sabre\DAV\FS\Directory($ecmDir);
155 }
156 
157 
158 
159 // Principals Backend
160 //$principalBackend = new \Sabre\DAVACL\PrincipalBackend\Dolibarr($user,$db);
161 // /principals
162 //$nodes[] = new \Sabre\DAVACL\PrincipalCollection($principalBackend);
163 // CardDav & CalDav Backend
164 //$carddavBackend = new \Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
165 //$caldavBackend = new \Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
166 // /addressbook
167 //$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
168 // /calendars
169 //$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend);
170 
171 
172 // The rootnode needs in turn to be passed to the server class
173 $server = new \Sabre\DAV\Server($nodes);
174 
175 // If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
176 // You can override the baseUri here.
177 $baseUri = DOL_URL_ROOT.'/dav/fileserver.php/';
178 if (isset($baseUri)) $server->setBaseUri($baseUri);
179 
180 // Add authentication function
181 if ((empty($conf->global->DAV_ALLOW_PUBLIC_DIR)
182  || !preg_match('/'.preg_quote(DOL_URL_ROOT.'/dav/fileserver.php/public', '/').'/', $_SERVER["PHP_SELF"]))
183  && !preg_match('/^sabreAction=asset&assetName=[a-zA-Z0-9%\-\/]+\.(png|css|woff|ico|ttf)$/', $_SERVER["QUERY_STRING"]) // URL for Sabre browser resources
184  )
185 {
186  //var_dump($_SERVER["QUERY_STRING"]);exit;
187  $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
188 }
189 // Support for LOCK and UNLOCK
190 $lockBackend = new \Sabre\DAV\Locks\Backend\File($tmpDir.'/.locksdb');
191 $lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
192 $server->addPlugin($lockPlugin);
193 
194 // Support for html frontend
195 if (empty($conf->global->DAV_DISABLE_BROWSER))
196 {
197  $browser = new \Sabre\DAV\Browser\Plugin();
198  $server->addPlugin($browser);
199 }
200 
201 // Automatically guess (some) contenttypes, based on extension
202 //$server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
203 
204 //$server->addPlugin(new \Sabre\CardDAV\Plugin());
205 //$server->addPlugin(new \Sabre\CalDAV\Plugin());
206 //$server->addPlugin(new \Sabre\DAVACL\Plugin());
207 
208 // Temporary file filter
209 /*$tempFF = new \Sabre\DAV\TemporaryFileFilterPlugin($tmpDir);
210 $server->addPlugin($tempFF);
211 */
212 
213 // And off we go!
214 $server->exec();
215 
216 if (is_object($db)) $db->close();
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.
Class ot manage authentication for pos module (cashdesk)
Definition: Auth.class.php:23
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context= '')
Return a login if login/pass was successfull.