fileserver.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* Copyright (C) 2018 Destailleur Laurent <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/dav/fileserver.php
  19. * \ingroup dav
  20. * \brief Server DAV
  21. */
  22. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  23. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show
  24. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  25. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  26. if (! defined('NOLOGIN')) define("NOLOGIN",1); // This means this output page does not require to be logged.
  27. if (! defined('NOCSRFCHECK')) define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  28. require "../main.inc.php";
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/dav/dav.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/dav/dav.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/includes/sabre/autoload.php';
  34. $user = new User($db);
  35. if(isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER']!='')
  36. {
  37. $user->fetch('',$_SERVER['PHP_AUTH_USER']);
  38. $user->getrights();
  39. }
  40. // Load translation files required by the page
  41. $langs->loadLangs(array("main","other"));
  42. if(empty($conf->dav->enabled))
  43. accessforbidden();
  44. // settings
  45. $publicDir = $conf->dav->dir_output.'/public';
  46. $privateDir = $conf->dav->dir_output.'/private';
  47. $tmpDir = $conf->dav->dir_temp;
  48. //var_dump($tmpDir);exit;
  49. // Authentication callback function
  50. $authBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(function ($username, $password)
  51. {
  52. global $user;
  53. global $conf;
  54. global $dolibarr_main_authentication;
  55. if (empty($user->login))
  56. return false;
  57. if ($user->socid > 0)
  58. return false;
  59. if ($user->login != $username)
  60. return false;
  61. // Authentication mode
  62. if (empty($dolibarr_main_authentication))
  63. $dolibarr_main_authentication='http,dolibarr';
  64. $authmode = explode(',',$dolibarr_main_authentication);
  65. $entity = (GETPOST('entity','int') ? GETPOST('entity','int') : (!empty($conf->entity) ? $conf->entity : 1));
  66. if (checkLoginPassEntity($username,$password,$entity,$authmode) != $username)
  67. return false;
  68. return true;
  69. });
  70. $authBackend->setRealm(constant('DOL_APPLICATION_TITLE'));
  71. /*
  72. * Actions and View
  73. */
  74. // Create the root node
  75. // Setting up the directory tree //
  76. $nodes = array();
  77. // Enable directories and features according to DAV setup
  78. // Public dir
  79. if (!empty($conf->global->DAV_ALLOW_PUBLIC_DIR))
  80. {
  81. $nodes[] = new \Sabre\DAV\FS\Directory($dolibarr_main_data_root. '/dav/public');
  82. }
  83. // Private dir
  84. $nodes[] = new \Sabre\DAV\FS\Directory($dolibarr_main_data_root. '/dav/private');
  85. // ECM dir
  86. if (! empty($conf->ecm->enabled) && ! empty($conf->global->DAV_ALLOW_ECM_DIR))
  87. {
  88. $nodes[] = new \Sabre\DAV\FS\Directory($dolibarr_main_data_root. '/ecm');
  89. }
  90. // Principals Backend
  91. //$principalBackend = new \Sabre\DAVACL\PrincipalBackend\Dolibarr($user,$db);
  92. // /principals
  93. //$nodes[] = new \Sabre\DAVACL\PrincipalCollection($principalBackend);
  94. // CardDav & CalDav Backend
  95. //$carddavBackend = new \Sabre\CardDAV\Backend\Dolibarr($user,$db,$langs);
  96. //$caldavBackend = new \Sabre\CalDAV\Backend\Dolibarr($user,$db,$langs, $cdavLib);
  97. // /addressbook
  98. //$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
  99. // /calendars
  100. //$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $caldavBackend);
  101. // The rootnode needs in turn to be passed to the server class
  102. $server = new \Sabre\DAV\Server($nodes);
  103. // If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
  104. // You can override the baseUri here.
  105. $baseUri = DOL_URL_ROOT.'/dav/fileserver.php/';
  106. if (isset($baseUri)) $server->setBaseUri($baseUri);
  107. // Add authentication function
  108. if ((empty($conf->global->DAV_ALLOW_PUBLIC_DIR)
  109. || ! preg_match('/'.preg_quote(DOL_URL_ROOT.'/dav/fileserver.php/public','/').'/', $_SERVER["PHP_SELF"]))
  110. && ! preg_match('/^sabreAction=asset&assetName=[a-zA-Z0-9%\-\/]+\.(png|css|woff|ico|ttf)$/', $_SERVER["QUERY_STRING"]) // URL for Sabre browser resources
  111. )
  112. {
  113. //var_dump($_SERVER["QUERY_STRING"]);exit;
  114. $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
  115. }
  116. // Support for LOCK and UNLOCK
  117. $lockBackend = new \Sabre\DAV\Locks\Backend\File($tmpDir . '/.locksdb');
  118. $lockPlugin = new \Sabre\DAV\Locks\Plugin($lockBackend);
  119. $server->addPlugin($lockPlugin);
  120. // Support for html frontend
  121. $browser = new \Sabre\DAV\Browser\Plugin();
  122. $server->addPlugin($browser);
  123. // Automatically guess (some) contenttypes, based on extension
  124. //$server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
  125. //$server->addPlugin(new \Sabre\CardDAV\Plugin());
  126. //$server->addPlugin(new \Sabre\CalDAV\Plugin());
  127. //$server->addPlugin(new \Sabre\DAVACL\Plugin());
  128. // Temporary file filter
  129. /*$tempFF = new \Sabre\DAV\TemporaryFileFilterPlugin($tmpDir);
  130. $server->addPlugin($tempFF);
  131. */
  132. // And off we go!
  133. $server->exec();
  134. if (is_object($db)) $db->close();