ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectDAV.php
Go to the documentation of this file.
1 <?php
2 // BEGIN WebDAV
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 
37 {
41  var $refId;
42 
46  var $obj;
47 
52  var $isDebug = false;
53 
59  function ilObjectDAV($refId, $obj = null)
60  {
61  if (is_object($obj))
62  {
63  $this->writelog('<constructor>('.$refId.','.get_class($obj).')');
64  }
65  $this->refId = $refId;
66  $this->obj =& $obj;
67  }
68 
69 
74  function getRefId()
75  {
76  return $this->refId;
77  }
82  function getObjectId()
83  {
84  return ($this->obj == null) ? null : $this->obj->getId();
85  }
86 
93  function getNodeId()
94  {
95  return 0;
96  }
97 
104  function initFromNull()
105  {
106  $this->obj->setPermissions($this->getRefId());
107  }
108 
109 
110 
111 
116  function read()
117  {
118  global $ilias;
119 
120  if (is_null($this->obj))
121  {
122  $this->obj =& $ilias->obj_factory->getInstanceByRefId($this->getRefId());
123  $this->obj->read();
124  }
125  }
130  function write()
131  {
132  $this->writelog('write() refid='.$this->refId);
133  $this->obj->update();
134  }
135 
136 
142  function getResourceName()
143  {
144  return $this->obj->getUntranslatedTitle();
145  }
151  function setResourceName($name)
152  {
153  $this->writelog('setResourceName('.$name.')');
154  return $this->obj->setTitle($name);
155  }
161  function getDisplayName()
162  {
163  return $this->obj->getTitle();
164  }
165 
172  {
173  return strtotime($this->obj->getCreateDate());
174  }
175 
182  {
183  return strtotime($this->obj->getLastUpdateDate());
184  }
185 
191  function getResourceType()
192  {
193  return "";
194  }
195 
201  function isCollection()
202  {
203  return $this->getResourceType() == 'collection';
204  }
210  function isFile()
211  {
212  return $this->getResourceType() == '';
213  }
218  function isNullResource()
219  {
220  return $this->getResourceType() == 'null';
221  }
222 
227  function getContentType()
228  {
229  return 'application/x-non-readable';//'application/octet-stream';
230  }
235  function setContentType($type)
236  {
237  // subclass responsibility
238  }
243  function setContentLength($length)
244  {
245  // subclass responsibility
246  }
251  function getContentLength()
252  {
253  return 0;
254  }
259  function getContentStream()
260  {
261  return null;
262  }
268  {
269  return null;
270  }
281  {
282  // subclass responsibility
283  }
289  function getContentData()
290  {
291  return null;
292  }
293 
297  function isOnline()
298  {
299  return true;
300  }
301 
312  function isPermitted($operations, $type = '')
313  {
314  // The 'visible' operation is only permitted if the object is online,
315  // or if the user is also permitted the perform the 'write' operation.
316  $ops = explode(',',$operations);
317  if (in_array('visible',$ops) && ! in_array('write',$ops))
318  {
319  if (! $this->isOnline()) {
320  $operations .= ',write';
321  }
322  }
323 
324  global $rbacsystem;
325  return $rbacsystem->checkAccess($operations, $this->getRefId(), $type);
326  }
327 
331  function getILIASType()
332  {
333  return $this->obj->getType();
334  }
339  {
340  return 'fold';
341  }
345  function getILIASFileType()
346  {
347  return 'file';
348  }
349 
354  function createNewVersion() {
355  }
356 
357 
364  function createCollection($name)
365  {
366  global $tree;
367 
368  // create and insert Folder in tree
369  require_once 'Modules/Folder/classes/class.ilObjFolder.php';
370  $newObj = new ilObjFolder(0);
371  $newObj->setType($this->getILIASCollectionType());
372  $newObj->setTitle($name);
373  //$newObj->setDescription('');
374  $newObj->create();
375  $newObj->createReference();
376  $newObj->setPermissions($this->getRefId());
377  $newObj->putInTree($this->getRefId());
378 
379  require_once 'class.ilObjFolderDAV.php';
380  return new ilObjFolderDAV($newObj->getRefId(), $newObj);
381  }
388  function createFile($name)
389  {
390  global $tree;
391 
392  // create and insert Folder in tree
393  require_once 'Modules/File/classes/class.ilObjFile.php';
394  $newObj =& new ilObjFile(0);
395  $newObj->setType($this->getILIASFileType());
396  $newObj->setTitle($name);
397  $newObj->setFileName($name);
398  include_once("./Services/Utilities/classes/class.ilMimeTypeUtil.php");
399  $mime = ilMimeTypeUtil::getMimeType("", $name, 'application/octet-stream');
400  //$newObj->setFileType('application/octet-stream');
401  $newObj->setFileType($mime);
402  //$newObj->setDescription('');
403  $newObj->create();
404  $newObj->createReference();
405  $newObj->setPermissions($this->getRefId());
406  $newObj->putInTree($this->getRefId());
407  //$newObj->createDirectory();
408 
409  require_once 'class.ilObjFileDAV.php';
410  $objDAV =& new ilObjFileDAV($newObj->getRefId(), $newObj);
411  /*
412  $fs = $objDAV->getContentOutputStream();
413  fwrite($fs,' ');
414  fclose($fs);
415  */
416  return $objDAV;
417  }
424  function createFileFromNull($name, &$nullDAV)
425  {
426  global $tree;
427 
428  // create and insert Folder in tree
429  require_once 'Modules/File/classes/class.ilObjFile.php';
430  $objDAV =& $nullDAV->convertToILIASType($this->getRefId(), $this->getILIASFileType());
431  $objDAV->initFromNull();
432  return $objDAV;
433  }
441  function createNull($name)
442  {
443  global $tree;
444 
445  // create and insert Folder in tree
446  require_once 'classes/class.ilObject.php';
447  $newObj =& new ilObject(0);
448  $newObj->setType('null');
449  $newObj->setTitle($name);
450  $newObj->create();
451  $newObj->createReference();
452  $newObj->setPermissions($this->getRefId());
453  $newObj->putInTree($this->getRefId());
454 
455  require_once 'class.ilObjNullDAV.php';
456  $objDAV =& new ilObjNullDAV($newObj->getRefId(), $newObj);
457 
458  return $objDAV;
459  }
460 
461 
462 
468  function remove($objDAV)
469  {
470  global $tree, $rbacadmin;
471 
472  $subnodes = $tree->getSubTree($tree->getNodeData($objDAV->getRefId()));
473  foreach ($subnodes as $node)
474  {
475  $rbacadmin->revokePermission($node["child"]);
476  $affectedUsers = ilUtil::removeItemFromDesktops($node["child"]);
477  }
478  $tree->saveSubTree($objDAV->getRefId());
479  $tree->deleteTree($tree->getNodeData($objDAV->getRefId()));
480  }
481 
489  function addCopy(&$objDAV, $newName = null)
490  {
491  $this->writelog("addCopy($objDAV,$newName) ....");
492  global $rbacadmin, $tree;
493  $revIdMapping = array();
494  $newRef = $this->cloneNodes($objDAV->getRefId(),$this->getRefId(),$revIdMapping, $newName);
495  //$rbacadmin->adjustMovedObjectPermissions($newRef, $tree->getParentId($objDAV->getRefId()));
496  return $this->createObject($newRef, $objDAV->getILIASType());
497  $this->writelog('... addCopy done.');
498  }
499 
510  function cloneNodes($srcRef,$dstRef,&$mapping, $newName=null)
511  {
512  $this->writelog("cloneNodes($srcRef,$dstRef,$mapping,$newName)");
513  global $tree;
514  global $ilias;
515 
516  // clone the source node
517  $srcObj =& $ilias->obj_factory->getInstanceByRefId($srcRef);
518  $this->writelog('cloneNodes cloning srcRef='.$srcRef.' dstRef='.$dstRef.'...');
519  $newObj = $srcObj->cloneObject($dstRef);
520  $newRef = $newObj->getRefId();
521 
522  // We must immediately apply a new name to the object, to
523  // prevent confusion of WebDAV clients about having two objects with identical
524  // name in the repository.
525  $this->writelog("cloneNodes newname not null? ".(! is_null($newName)));
526  if (! is_null($newName))
527  {
528  $newObjDAV = $this->createObject($newRef, $srcObj->getType());
529  $newObjDAV->setResourceName($newName);
530  $newObjDAV->write();
531  }
532  unset($srcObj);
533  $mapping[$newRef] = $srcRef;
534 
535  // clone all children of the source node
536  $children = $tree->getChilds($srcRef);
537  foreach ($tree->getChilds($srcRef) as $child)
538  {
539  // Don't clone role folders, because it does not make sense to clone local roles
540  // FIXME - Maybe it does make sense (?)
541  if ($child["type"] != 'rolf')
542  {
543  $this->cloneNodes($child["ref_id"],$newRef,$mapping,null);
544  }
545  else
546  {
547  if (count($rolf = $tree->getChildsByType($newRef,"rolf")))
548  {
549  $mapping[$rolf[0]["ref_id"]] = $child["ref_id"];
550  }
551  }
552  }
553  $this->writelog('cloneNodes ...cloned srcRef='.$srcRef.' dstRef='.$dstRef.' newRef='.$newRef);
554  return $newRef;
555  }
556 
564  function addMove(&$objDAV, $newName = null)
565  {
566  global $tree;
567  global $rbacadmin;
568  global $ilias;
569  global $log;
570 
571  $this->writelog('addMove('.$objDAV->getRefId().' to '.$this->getRefId().', newName='.$newName.')');
572 
573  // Step 0:Assign new name to moved object
574  if (! is_null($newName))
575  {
576  $objDAV->setResourceName($newName);
577  $objDAV->write();
578  }
579 
580  // Step 1: Store old parent
581  $old_parent = $tree->getParentId($objDAV->getRefId());
582 
583  // Step 2: Move the tree
584  $tree->moveTree($objDAV->getRefId(),$this->getRefId());
585 
586  // Step 3: Repair permissions
587  $rbacadmin->adjustMovedObjectPermissions($objDAV->getRefId(), $old_parent);
588 
589  /*
590  // STEP 1: Move subtree to trash
591  $this->writelog('addMove('.$objDAV->getRefId().' to '.$this->getRefId().') step 1: move subtree to trash');
592  $subnodes = $tree->getSubTree($tree->getNodeData($objDAV->getRefId()));
593  foreach ($subnodes as $node)
594  {
595  $rbacadmin->revokePermission($node["child"]);
596  $affectedUsers = ilUtil::removeItemFromDesktops($node["child"]);
597  }
598  $tree->saveSubTree($objDAV->getRefId());
599  $tree->deleteTree($tree->getNodeData($objDAV->getRefId()));
600 
601  // STEP 2: Move subtree to new location
602  // TODO: this whole put in place again stuff needs revision. Permission settings get lost.
603  $this->writelog('addMove() step 2: move subtree to new location');
604  // put top node to dest
605  $rbacadmin->revokePermission($subnodes[0]['child']);
606  $obj_data =& $ilias->obj_factory->getInstanceByRefId($subnodes[0]['child']);
607  $obj_data->putInTree($this->getRefId());
608  $obj_data->setPermissions($this->getRefId());
609  array_shift($subnodes);
610 
611  // put all sub nodes to their parent (of which we have moved top already to dest).
612  foreach ($subnodes as $node)
613  {
614  $rbacadmin->revokePermission($node['child']);
615  $obj_data =& $ilias->obj_factory->getInstanceByRefId($node['child']);
616  $obj_data->putInTree($node['parent']);
617  $obj_data->setPermissions($node['parent']);
618  }
619 
620  // STEP 3: Remove trashed objects from system
621  $this->writelog('addMove('.$objDAV->getRefID().') step 3: remove trashed objects from system');
622  require_once 'Services/Tree/classes/class.ilTree.php';
623  $trashTree = new ilTree(- (int) $objDAV->getRefId());
624  $node = $trashTree->getNodeData($objDAV->getRefId());
625  $subnodes = $trashTree->getSubTree($node);
626 
627  // remember already checked deleted node_ids
628  $checked[] = -(int) $objDAV->getRefId();
629 
630  // dive in recursive manner in each already deleted subtrees and remove these objects too
631  $this->removeDeletedNodes($objDAV->getRefId(), $checked, false);
632 
633  // delete trash tree
634  $tree->deleteTree($node);
635  $this->writelog('addMove('.$objDAV->getRefID().') all 3 steps done');
636  */
637  }
638 
647  function removeDeletedNodes($a_node_id, $a_checked, $a_delete_objects = true)
648  {
649  global $log, $ilias, $tree;
650 
651  $q = "SELECT tree FROM tree WHERE parent='".$a_node_id."' AND tree < 0";
652 
653  $r = $ilias->db->query($q);
654 
655  while($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
656  {
657  // only continue recursion if fetched node wasn't touched already!
658  if (!in_array($row->tree,$a_checked))
659  {
660  $deleted_tree = new ilTree($row->tree);
661  $a_checked[] = $row->tree;
662 
663  $row->tree = $row->tree * (-1);
664  $del_node_data = $deleted_tree->getNodeData($row->tree);
665  $del_subtree_nodes = $deleted_tree->getSubTree($del_node_data);
666 
667  $this->removeDeletedNodes($row->tree,$a_checked);
668 
669  if ($a_delete_objects)
670  {
671  foreach ($del_subtree_nodes as $node)
672  {
673  $node_obj =& $ilias->obj_factory->getInstanceByRefId($node["ref_id"]);
674 
675  // write log entry
676  /*$this->writelog("removeDeletedNodes(), delete obj_id: ".$node_obj->getId().
677  ", ref_id: ".$node_obj->getRefId().", type: ".$node_obj->getType().", ".
678  "title: ".$node_obj->getTitle());
679  */
680  $node_obj->delete();
681  }
682  }
683 
684  $tree->deleteTree($del_node_data);
685 
686  // write log entry
687  //$this->writelog("removeDeletedNodes(), deleted tree, tree_id: ".$del_node_data["tree"].", child: ".$del_node_data["child"]);
688  }
689  }
690 
691  return true;
692  }
699  function children()
700  {
701  // FIXME: Remove duplicate entries from this list, because of RFC2518, chapter 5.2
702  // If a duplicate is found, the older object must win. We use the object
703  // id to determine this. This is based on the assumption, that new objects
704  // have higher object id's then older objects.
705 
706  global $tree;
707 
708  $childrenDAV = array();
709  // Performance optimization. We sort the children using PHP instead of using the database.
710  //$childrenData =& $tree->getChilds($this->getRefId(),'title');
711  $childrenData =& $tree->getChilds($this->getRefId(),'');
712  foreach ($childrenData as $data)
713  {
714  $childDAV =& $this->createObject($data['ref_id'],$data['type']);
715  if (! is_null($childDAV))
716  {
717  // Note: We must not assign with =& here, because this will cause trouble
718  // when other functions attempt to work with the $childrenDAV array.
719  $childrenDAV[] = $childDAV;
720  }
721 
722  }
723  return $childrenDAV;
724  }
734  function childrenWithPermission($operations, $type ='')
735  {
736  //$this->writelog('@'.$this->getRefId().'.childrenWithPermission('.$operations.','.$type.')');
737  $childrenDAV = $this->children();
738  $permittedChildrenDAV = array();
739  foreach ($childrenDAV as $childDAV)
740  {
741  if ($childDAV->isPermitted($operations, $type))
742  {
743  $permittedChildrenDAV[] = $childDAV;
744  }
745 
746  }
747  //$this->writelog('@'.$this->getRefId().'.childrenWithPermission():'.count($permittedChildrenDAV).' children');
748  return $permittedChildrenDAV;
749  }
750 
759  function createObject($refId, $type)
760  {
761  $newObj = null;
762  switch ($type)
763  {
764  case 'mountPoint' :
765  require_once 'class.ilObjMountPointDAV.php';
766  $newObj =& new ilObjMountPointDAV($refId,null);
767  break;
768  case 'root' :
769  require_once 'class.ilObjRootDAV.php';
770  $newObj =& new ilObjRootDAV($refId,null);
771  break;
772  case 'cat' :
773  require_once 'class.ilObjCategoryDAV.php';
774  $newObj =& new ilObjCategoryDAV($refId,null);
775  break;
776  case 'fold' :
777  require_once 'class.ilObjFolderDAV.php';
778  $newObj =& new ilObjFolderDAV($refId,null);
779  break;
780  case 'crs' :
781  require_once 'class.ilObjCourseDAV.php';
782  $newObj =& new ilObjCourseDAV($refId,null);
783  break;
784  case 'grp' :
785  require_once 'class.ilObjGroupDAV.php';
786  $newObj =& new ilObjGroupDAV($refId,null);
787  break;
788  case 'file' :
789  require_once 'class.ilObjFileDAV.php';
790  $newObj =& new ilObjFileDAV($refId,null);
791  break;
792  case 'null' :
793  require_once 'class.ilObjNullDAV.php';
794  $newObj =& new ilObjNullDAV($refId,null);
795  break;
796  default :
797  break;
798  }
799  if (! is_null($newObj))
800  {
801  $newObj->read();
802  }
803  return $newObj;
804  }
811  function writelog($message)
812  {
813  if ($this->isDebug)
814  {
815  global $log, $ilias;
816  $log->write(
817  $ilias->account->getLogin()
818  .' DAV .'.get_class($this).' '.str_replace("\n",";",$message)
819  );
820  /*
821  $fh = fopen('/opt/ilias/log/ilias.log', 'a');
822  fwrite($fh, date('Y-m-d h:i:s '));
823  fwrite($fh, str_replace("\n",";",$message));
824  fwrite($fh, "\n\n");
825  fclose($fh);
826  */
827  }
828  }
829 
834  function __toString() {
835  return get_class($this).'#'.$this->getObjectId();
836  }
837 }
838 // END WebDAV
839 ?>