ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilWebDAVUtil.php
Go to the documentation of this file.
1<?php
2
13{
14 protected static $clientBrowser = "firefox";
15 protected static $clientOS = "windows";
16 protected static $clientFlavor = "nichtxp";
17
23 public static function _isActionsVisible()
24 {
25 global $DIC;
26 return $DIC->clientIni()->readVariable('file_access', 'webdav_actions_visible') == '1';
27 }
28
36 public function showMountInstructions(&$objDAV, &$options)
37 {
38 global $DIC;
39 $lng = $DIC->language();
40 $ilUser = $DIC->user();
41
42 $path = $this->davDeslashify($options['path']);
43
44 // The $path variable may contain a full or a shortened DAV path.
45 // We convert it into an object path, which we can then use to
46 // construct a new full DAV path.
47 $objectPath = $this->toObjectPath($path);
48
49 // Construct a (possibly) full DAV path from the object path.
50 $fullPath = '';
51 foreach ($objectPath as $object) {
52 if ($object->getRefId() == 1 && $this->isFileHidden($object)) {
53 // If the repository root object is hidden, we can not
54 // create a full path, because nothing would appear in the
55 // webfolder. We resort to a shortened path instead.
56 $fullPath .= '/ref_1';
57 } else {
58 $fullPath .= '/' . $this->davUrlEncode($object->getResourceName());
59 }
60 }
61
62 // Construct a shortened DAV path from the object path.
63 $shortenedPath = '/ref_' .
64 $objectPath[count($objectPath) - 1]->getRefId();
65
66 if ($objDAV->isCollection()) {
67 $shortenedPath .= '/';
68 $fullPath .= '/';
69 }
70
71 // Prepend client id to path
72 $shortenedPath = '/' . CLIENT_ID . $shortenedPath;
73 $fullPath = '/' . CLIENT_ID . $fullPath;
74
75 // Construct webfolder URI's. The URI's are used for mounting the
76 // webfolder. Since mounting using URI's is not standardized, we have
77 // to create different URI's for different browsers.
78 $webfolderURI = $this->base_uri . $shortenedPath;
79 $webfolderURI_Konqueror = ($this->isWebDAVoverHTTPS() ? "webdavs" : "webdav") .
80 substr($this->base_uri, strrpos($this->base_uri, ':')) .
81 $shortenedPath;
82 ;
83 $webfolderURI_Nautilus = ($this->isWebDAVoverHTTPS() ? "davs" : "dav") .
84 substr($this->base_uri, strrpos($this->base_uri, ':')) .
85 $shortenedPath
86 ;
87 $webfolderURI_IE = $this->base_uri . $shortenedPath;
88
89 $webfolderTitle = $objectPath[count($objectPath) - 1]->getResourceName();
90
91 header('Content-Type: text/html; charset=UTF-8');
92 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
93 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN\"\n";
94 echo " \"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd\">\n";
95 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
96 echo " <head>\n";
97 echo " <title>" . sprintf($lng->txt('webfolder_instructions_titletext'), $webfolderTitle) . "</title>\n";
98 echo " </head>\n";
99 echo " <body>\n";
100
101 echo ilDAVServer::_getWebfolderInstructionsFor(
102 $webfolderTitle,
103 $webfolderURI,
104 $webfolderURI_IE,
105 $webfolderURI_Konqueror,
106 $webfolderURI_Nautilus,
107 $this->clientOS,
108 $this->clientOSFlavor
109 );
110
111 echo " </body>\n";
112 echo "</html>\n";
113
114 // Logout anonymous user to force authentication after calling mount uri
115 if ($ilUser->getId() == ANONYMOUS_USER_ID) {
116 $DIC['ilAuthSession']->logout();
117 }
118
119 exit;
120 }
121
135 protected function checkLock($path)
136 {
137 global $DIC;
138
139 $this->writelog('checkLock(' . $path . ')');
140 $result = null;
141
142 // get dav object for path
143 //$objDAV = $this->getObject($path);
144
145 // convert DAV path into ilObjectDAV path
146 $objPath = $this->toObjectPath($path);
147 if (!is_null($objPath)) {
148 $objDAV = $objPath[count($objPath) - 1];
149 $locks = $this->locks->getLocksOnPathDAV($objPath);
150 foreach ($locks as $lock) {
151 $isLastPathComponent = $lock['obj_id'] == $objDAV->getObjectId()
152 && $lock['node_id'] == $objDAV->getNodeId();
153
154 // Check all locks on last object in path,
155 // but only locks with depth infinity on parent objects.
156 if ($isLastPathComponent || $lock['depth'] == 'infinity') {
157 // DAV Clients expects to see their own owner name in
158 // the locks. Since these names are not unique (they may
159 // just be the name of the local user running the DAV client)
160 // we return the ILIAS user name in all other cases.
161 if ($lock['ilias_owner'] == $DIC->user()->getId()) {
162 $owner = $lock['dav_owner'];
163 } else {
164 $owner = $this->getLogin($lock['ilias_owner']);
165 }
166
167 // FIXME - Shouldn't we collect all locks instead of
168 // using an arbitrary one?
169 $result = array(
170 "type" => "write",
171 "obj_id" => $lock['obj_id'],
172 "node_id" => $lock['node_id'],
173 "scope" => $lock['scope'],
174 "depth" => $lock['depth'],
175 "owner" => $owner,
176 "token" => $lock['token'],
177 "expires" => $lock['expires']
178 );
179 if ($lock['scope'] == 'exclusive') {
180 // If there is an exclusive lock in the path, it
181 // takes precedence over all non-exclusive locks in
182 // parent nodes. Therefore we can can finish collecting
183 // locks.
184 break;
185 }
186 }
187 }
188 }
189 $this->writelog('checkLock(' . $path . '):' . var_export($result, true));
190
191 return $result;
192 }
193
199 protected function getLogin($userId)
200 {
202 $this->writelog('getLogin(' . $userId . '):' . var_export($login, true));
203 return $login;
204 }
205
206
214 private function getObject($davPath)
215 {
216 global $DIC;
217 $tree = $DIC->repositoryTree();
218
219
220 // If the second path elements starts with 'file_', the following
221 // characters of the path element directly identify the ref_id of
222 // a file object.
223 $davPathComponents = explode('/', substr($davPath, 1));
224
225 if (count($davPathComponents) > 1 &&
226 substr($davPathComponents[1], 0, 5) == 'file_') {
227 $ref_id = substr($davPathComponents[1], 5);
228 $nodePath = $tree->getNodePath($ref_id, $tree->root_id);
229
230 // Poor IE needs this, in order to successfully display
231 // PDF documents
232 header('Pragma: private');
233 } else {
234 $nodePath = $this->toNodePath($davPath);
235 if ($nodePath == null && count($davPathComponents) == 1) {
236 return ilObjectDAV::createObject(-1, 'mountPoint');
237 }
238 }
239 if (is_null($nodePath)) {
240 return null;
241 } else {
242 $top = $nodePath[count($nodePath) - 1];
243 return ilObjectDAV::createObject($top['child'], $top['type']);
244 }
245 }
253 private function toObjectPath($davPath)
254 {
255 $this->writelog('toObjectPath(' . $davPath);
256
257 $nodePath = $this->toNodePath($davPath);
258
259 if (is_null($nodePath)) {
260 return null;
261 } else {
262 $objectPath = array();
263 foreach ($nodePath as $node) {
264 $pathElement = ilObjectDAV::createObject($node['child'], $node['type']);
265 if (is_null($pathElement)) {
266 break;
267 }
268 $objectPath[] = $pathElement;
269 }
270 return $objectPath;
271 }
272 }
273
286 public function toNodePath($davPath)
287 {
288 global $DIC;
289 $tree = $DIC->repositoryTree();
290
291 $this->writelog('toNodePath(' . $davPath . ')...');
292
293 // Split the davPath into path titles
294 $titlePath = explode('/', substr($davPath, 1));
295
296 // Remove the client id from the beginning of the title path
297 if (count($titlePath) > 0) {
298 array_shift($titlePath);
299 }
300
301 // If the last path title is empty, remove it
302 if (count($titlePath) > 0 && $titlePath[count($titlePath) - 1] == '') {
303 array_pop($titlePath);
304 }
305
306 // If the path is empty, return null
307 if (count($titlePath) == 0) {
308 $this->writelog('toNodePath(' . $davPath . '):null, because path is empty.');
309 return null;
310 }
311
312 // If the path is an absolute path, ref_id is null.
313 $ref_id = null;
314
315 // If the path is a relative folder path, convert it into an absolute path
316 if (count($titlePath) > 0 && substr($titlePath[0], 0, 4) == 'ref_') {
317 $ref_id = substr($titlePath[0], 4);
318 array_shift($titlePath);
319 }
320
321 $nodePath = $tree->getNodePathForTitlePath($titlePath, $ref_id);
322
323 $this->writelog('toNodePath():' . var_export($nodePath, true));
324 return $nodePath;
325 }
326
334 private function davDeslashify($path)
335 {
336 $path = UtfNormal::toNFC($path);
337
338 if ($path[strlen($path) - 1] == '/') {
339 $path = substr($path, 0, strlen($path) - 1);
340 }
341 return $path;
342 }
343
351 private function davBasename($path)
352 {
353 $components = explode('/', $path);
354 return count($components) == 0 ? '' : $components[count($components) - 1];
355 }
356
372 public static function getFolderURI($refId, $nodeId = 0, $ressourceName = null, $parentRefId = null)
373 {
374 if (self::$clientOS == 'windows') {
375 $baseUri = "https:";
376 $query = null;
377 } elseif (self::$clientBrowser == 'konqueror') {
378 $baseUri = "webdavs:";
379 $query = null;
380 } elseif (self::$clientBrowser == 'nautilus') {
381 $baseUri = "davs:";
382 $query = null;
383 } else {
384 $baseUri = "https:";
385 $query = null;
386 }
387 $baseUri .= "//$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]";
388 $baseUri = substr($baseUri, 0, strrpos($baseUri, '/')) . '/webdav.php/' . CLIENT_ID;
389
390 $uri = $baseUri . '/ref_' . $refId . '/';
391 if ($query != null) {
392 $uri .= '?' . $query;
393 }
394
395 return $uri;
396 }
397
398
399
413 public static function getMountURI($refId, $nodeId = 0, $ressourceName = null, $parentRefId = null, $genericURI = false)
414 {
415 if ($genericURI) {
416 $baseUri = "https:";
417 $query = null;
418 } elseif (self::$clientOS == 'windows') {
419 $baseUri = "http:";
420 $query = 'mount-instructions';
421 } elseif (self::$clientBrowser == 'konqueror') {
422 $baseUri = "webdavs:";
423 $query = null;
424 } elseif (self::$clientBrowser == 'nautilus') {
425 $baseUri = "davs:";
426 $query = null;
427 } else {
428 $baseUri = "https:";
429 $query = 'mount-instructions';
430 }
431 $baseUri .= "//$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]";
432 $baseUri = substr($baseUri, 0, strrpos($baseUri, '/')) . '/webdav.php/' . CLIENT_ID;
433
434 $uri = $baseUri . '/ref_' . $refId . '/';
435 if ($query != null) {
436 $uri .= '?' . $query;
437 }
438
439 return $uri;
440 }
441
454 public function getObjectURI($refId, $ressourceName = null, $parentRefId = null)
455 {
456 global $DIC;
457 $nodeId = 0;
458 $baseUri = ($this->isWebDAVoverHTTPS() ? "https:" : "http:") .
459 "//$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]";
460 $baseUri = substr($baseUri, 0, strrpos($baseUri, '/')) . '/webdav.php/' . CLIENT_ID;
461
462 if (!is_null($ressourceName) && !is_null($parentRefId)) {
463 // Quickly create URI from the known data without needing SQL queries
464 $uri = $baseUri . '/ref_' . $parentRefId . '/' . $this->davUrlEncode($ressourceName);
465 } else {
466 // Create URI and use some SQL queries to get the missing data
467 $nodePath = $DIC->repositoryTree()->getNodePath($refId);
468
469 if (is_null($nodePath) || count($nodePath) < 2) {
470 // No object path? Return null - file is not in repository.
471 $uri = null;
472 } else {
473 $uri = $baseUri . '/ref_' . $nodePath[count($nodePath) - 2]['child'] . '/' .
474 $this->davUrlEncode($nodePath[count($nodePath) - 1]['title']);
475 }
476 }
477 return $uri;
478 }
479
498 public function getFileURI($refId, $ressourceName = null, $parentRefId = null)
499 {
500 global $DIC;
501 $nodeId = 0;
502 $baseUri = ($this->isWebDAVoverHTTPS() ? "https:" : "http:") .
503 "//$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]";
504 $baseUri = substr($baseUri, 0, strrpos($baseUri, '/')) . '/webdav.php/' . CLIENT_ID;
505
506 if (!is_null($ressourceName) && !is_null($parentRefId)) {
507 // Quickly create URI from the known data without needing SQL queries
508 $uri = $baseUri . '/file_' . $refId . '/' . $this->davUrlEncode($ressourceName);
509 } else {
510 // Create URI and use some SQL queries to get the missing data
511 $nodePath = $DIC->repositoryTree()->getNodePath($refId);
512
513 if (is_null($nodePath) || count($nodePath) < 2) {
514 // No object path? Return null - file is not in repository.
515 $uri = null;
516 } else {
517 $uri = $baseUri . '/file_' . $nodePath[count($nodePath) - 1]['child'] . '/' .
518 $this->davUrlEncode($nodePath[count($nodePath) - 1]['title']);
519 }
520 }
521 return $uri;
522 }
523
530 public function isWebDAVoverHTTPS()
531 {
532 if ($this->isHTTPS == null) {
533 global $DIC;
534 $ilSetting = $DIC->settings();
535 require_once './Services/Http/classes/class.ilHTTPS.php';
536 $https = new ilHTTPS();
537 $this->isHTTPS = $https->isDetected() || $ilSetting->get('https');
538 }
539 return $this->isHTTPS;
540 }
541
551 public static function _isActive()
552 {
553 global $DIC;
554 return $DIC->clientIni()->readVariable('file_access', 'webdav_enabled') == '1';
555 }
556
563 private function getUploadMaxFilesize()
564 {
565 $val = ini_get('upload_max_filesize');
566
567 $val = trim($val);
568 $last = strtolower($val[strlen($val) - 1]);
569 switch ($last) {
570 // The 'G' modifier is available since PHP 5.1.0
571 case 'g':
572 $val *= 1024;
573 // no break
574 case 'm':
575 $val *= 1024;
576 // no break
577 case 'k':
578 $val *= 1024;
579 }
580
581 return $val;
582 }
583
584 private static $instance = null;
585
586 private $pwd_instruction = null;
587
592 private function __construct()
593 {
594 }
595
600 public static function getInstance()
601 {
602 if (self::$instance) {
603 return self::$instance;
604 }
605 return self::$instance = new ilWebDAVUtil();
606 }
607
613 {
614 global $DIC;
615 $ilUser = $DIC->user();
616
617 if ($this->pwd_instruction !== null) {
619 }
620 include_once './Services/Authentication/classes/class.ilAuthUtils.php';
621 $status = ilAuthUtils::supportsLocalPasswordValidation($ilUser->getAuthMode(true));
622 if ($status != ilAuthUtils::LOCAL_PWV_USER) {
623 return $this->pwd_instruction = false;
624 }
625 // Check if user has local password
626 return $this->pwd_instruction = (bool) !strlen($ilUser->getPasswd());
627 }
628}
$result
An exception for terminatinating execution or to throw for unit testing.
static toNFC($string)
Convert a UTF-8 string to normal form C, canonical composition.
Definition: UtfNormal.php:159
static supportsLocalPasswordValidation($a_authmode)
Check if local password validation is supported.
HTTPS.
static _lookupLogin($a_user_id)
lookup login
This class contains some functions from the old ilDAVServer.
static _isActive()
TODO: Check if needed and refactor Static getter.
isWebDAVoverHTTPS()
TODO: Check if needed and refactor Returns true, if the WebDAV server transfers data over HTTPS.
checkLock($path)
TODO: Check if needed and refactor checkLock() helper.
getUploadMaxFilesize()
TODO: Check if needed and refactor Gets the maximum permitted upload filesize from php....
static getInstance()
Get singleton instance.
static _isActionsVisible()
Static getter.
static getMountURI($refId, $nodeId=0, $ressourceName=null, $parentRefId=null, $genericURI=false)
TODO: Check if needed and refactor Returns an URI for mounting the repository object as a webfolder.
davDeslashify($path)
TODO: Check if needed and refactor davDeslashify - make sure path does not end in a slash.
getObjectURI($refId, $ressourceName=null, $parentRefId=null)
TODO: Check if needed and refactor Returns an URI for getting a object using WebDAV by its name.
toObjectPath($davPath)
TODO: Check if needed and refactor Converts a DAV path into an array of DAV objects.
davBasename($path)
TODO: Check if needed and refactor Private implementation of PHP basename() function.
showMountInstructions(&$objDAV, &$options)
TODO: Check if needed and refactor Mount instructions method handler for directories.
getObject($davPath)
TODO: Check if needed and refactor Gets a DAV object for the specified path.
static getFolderURI($refId, $nodeId=0, $ressourceName=null, $parentRefId=null)
TODO: Check if needed and refactor Returns an URI for mounting the repository object as a webfolder u...
getLogin($userId)
TODO: Check if needed and refactor Returns the login for the specified user id, or null if the user d...
toNodePath($davPath)
TODO: Check if needed and refactor Converts a DAV path into a node path.
getFileURI($refId, $ressourceName=null, $parentRefId=null)
TODO: Check if needed and refactor Returns an URI for getting a file object using WebDAV.
__construct()
Singleton constructor.
$login
Definition: cron.php:13
exit
Definition: login.php:29
global $ilSetting
Definition: privfeed.php:17
$query
$lng
$https
Definition: imgupload.php:19
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46
$refId
Definition: xapitoken.php:42