ILIAS  Release_4_0_x_branch Revision 61816
 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  }
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  }
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 if (false) // old implementation deactivated
317 {
318  $ops = explode(',',$operations);
319  if (in_array('visible',$ops) && ! in_array('write',$ops))
320  {
321  if (! $this->isOnline()) {
322  $operations .= ',write';
323  }
324  }
325 
326  global $rbacsystem;
327  return $rbacsystem->checkAccess($operations, $this->getRefId(), $type);
328 }
329 else // this one fixes bug #5367
330 {
331  global $ilAccess;
332  $operations = explode(",",$operations."");
333  foreach ($operations as $operation)
334  {
335  if (!$ilAccess->checkAccess($operation, '', $this->getRefId(), $type))
336  {
337  return false;
338  }
339  }
340  return true;
341 }
342  }
343 
347  function getILIASType()
348  {
349  return $this->obj->getType();
350  }
355  {
356  return 'fold';
357  }
361  function getILIASFileType()
362  {
363  return 'file';
364  }
365 
370  function createNewVersion() {
371  }
372 
373 
381  {
382  global $tree;
383 
384  // create and insert Folder in tree
385  require_once 'Modules/Folder/classes/class.ilObjFolder.php';
386  $newObj = new ilObjFolder(0);
387  $newObj->setType($this->getILIASCollectionType());
388  $newObj->setTitle($name);
389  //$newObj->setDescription('');
390  $newObj->create();
391  $newObj->createReference();
392  $newObj->setPermissions($this->getRefId());
393  $newObj->putInTree($this->getRefId());
394 
395  require_once 'class.ilObjFolderDAV.php';
396  return new ilObjFolderDAV($newObj->getRefId(), $newObj);
397  }
404  function createFile($name)
405  {
406  global $tree;
407 
408  // create and insert Folder in tree
409  require_once 'Modules/File/classes/class.ilObjFile.php';
410  $newObj =& new ilObjFile(0);
411  $newObj->setType($this->getILIASFileType());
412  $newObj->setTitle($name);
413  $newObj->setFileName($name);
414  include_once("./Services/Utilities/classes/class.ilMimeTypeUtil.php");
415  $mime = ilMimeTypeUtil::getMimeType("", $name, 'application/octet-stream');
416  //$newObj->setFileType('application/octet-stream');
417  $newObj->setFileType($mime);
418  //$newObj->setDescription('');
419  $newObj->create();
420  $newObj->createReference();
421  $newObj->setPermissions($this->getRefId());
422  $newObj->putInTree($this->getRefId());
423  //$newObj->createDirectory();
424 
425  require_once 'class.ilObjFileDAV.php';
426  $objDAV =& new ilObjFileDAV($newObj->getRefId(), $newObj);
427  /*
428  $fs = $objDAV->getContentOutputStream();
429  fwrite($fs,' ');
430  fclose($fs);
431  */
432  return $objDAV;
433  }
440  function createFileFromNull($name, &$nullDAV)
441  {
442  global $tree;
443 
444  // create and insert Folder in tree
445  require_once 'Modules/File/classes/class.ilObjFile.php';
446  $objDAV =& $nullDAV->convertToILIASType($this->getRefId(), $this->getILIASFileType());
447  $objDAV->initFromNull();
448  return $objDAV;
449  }
457  function createNull($name)
458  {
459  global $tree;
460 
461  // create and insert Folder in tree
462  require_once 'classes/class.ilObject.php';
463  $newObj =& new ilObject(0);
464  $newObj->setType('null');
465  $newObj->setTitle($name);
466  $newObj->create();
467  $newObj->createReference();
468  $newObj->setPermissions($this->getRefId());
469  $newObj->putInTree($this->getRefId());
470 
471  require_once 'class.ilObjNullDAV.php';
472  $objDAV =& new ilObjNullDAV($newObj->getRefId(), $newObj);
473 
474  return $objDAV;
475  }
476 
477 
478 
484  function remove($objDAV)
485  {
486  global $tree, $rbacadmin;
487 
488  $subnodes = $tree->getSubTree($tree->getNodeData($objDAV->getRefId()));
489  foreach ($subnodes as $node)
490  {
491  $rbacadmin->revokePermission($node["child"]);
492  $affectedUsers = ilUtil::removeItemFromDesktops($node["child"]);
493  }
494  $tree->saveSubTree($objDAV->getRefId());
495  $tree->deleteTree($tree->getNodeData($objDAV->getRefId()));
496  }
497 
505  function addCopy(&$objDAV, $newName = null)
506  {
507  $this->writelog("addCopy($objDAV,$newName) ....");
508  global $rbacadmin, $tree;
509  $revIdMapping = array();
510  $newRef = $this->cloneNodes($objDAV->getRefId(),$this->getRefId(),$revIdMapping, $newName);
511  //$rbacadmin->adjustMovedObjectPermissions($newRef, $tree->getParentId($objDAV->getRefId()));
512  return $this->createObject($newRef, $objDAV->getILIASType());
513  $this->writelog('... addCopy done.');
514  }
515 
526  function cloneNodes($srcRef,$dstRef,&$mapping, $newName=null)
527  {
528  $this->writelog("cloneNodes($srcRef,$dstRef,$mapping,$newName)");
529  global $tree;
530  global $ilias;
531 
532  // clone the source node
533  $srcObj =& $ilias->obj_factory->getInstanceByRefId($srcRef);
534  $this->writelog('cloneNodes cloning srcRef='.$srcRef.' dstRef='.$dstRef.'...');
535  $newObj = $srcObj->cloneObject($dstRef);
536  $newRef = $newObj->getRefId();
537 
538  // We must immediately apply a new name to the object, to
539  // prevent confusion of WebDAV clients about having two objects with identical
540  // name in the repository.
541  $this->writelog("cloneNodes newname not null? ".(! is_null($newName)));
542  if (! is_null($newName))
543  {
544  $newObjDAV = $this->createObject($newRef, $srcObj->getType());
545  $newObjDAV->setResourceName($newName);
546  $newObjDAV->write();
547  }
548  unset($srcObj);
549  $mapping[$newRef] = $srcRef;
550 
551  // clone all children of the source node
552  $children = $tree->getChilds($srcRef);
553  foreach ($tree->getChilds($srcRef) as $child)
554  {
555  // Don't clone role folders, because it does not make sense to clone local roles
556  // FIXME - Maybe it does make sense (?)
557  if ($child["type"] != 'rolf')
558  {
559  $this->cloneNodes($child["ref_id"],$newRef,$mapping,null);
560  }
561  else
562  {
563  if (count($rolf = $tree->getChildsByType($newRef,"rolf")))
564  {
565  $mapping[$rolf[0]["ref_id"]] = $child["ref_id"];
566  }
567  }
568  }
569  $this->writelog('cloneNodes ...cloned srcRef='.$srcRef.' dstRef='.$dstRef.' newRef='.$newRef);
570  return $newRef;
571  }
572 
580  function addMove(&$objDAV, $newName = null)
581  {
582  global $tree;
583  global $rbacadmin;
584  global $ilias;
585  global $log;
586 
587  $this->writelog('addMove('.$objDAV->getRefId().' to '.$this->getRefId().', newName='.$newName.')');
588 
589  // Step 0:Assign new name to moved object
590  if (! is_null($newName))
591  {
592  $objDAV->setResourceName($newName);
593  $objDAV->write();
594  }
595 
596  // Step 1: Store old parent
597  $old_parent = $tree->getParentId($objDAV->getRefId());
598 
599  // Step 2: Move the tree
600  $tree->moveTree($objDAV->getRefId(),$this->getRefId());
601 
602  // Step 3: Repair permissions
603  $rbacadmin->adjustMovedObjectPermissions($objDAV->getRefId(), $old_parent);
604 
605  /*
606  // STEP 1: Move subtree to trash
607  $this->writelog('addMove('.$objDAV->getRefId().' to '.$this->getRefId().') step 1: move subtree to trash');
608  $subnodes = $tree->getSubTree($tree->getNodeData($objDAV->getRefId()));
609  foreach ($subnodes as $node)
610  {
611  $rbacadmin->revokePermission($node["child"]);
612  $affectedUsers = ilUtil::removeItemFromDesktops($node["child"]);
613  }
614  $tree->saveSubTree($objDAV->getRefId());
615  $tree->deleteTree($tree->getNodeData($objDAV->getRefId()));
616 
617  // STEP 2: Move subtree to new location
618  // TODO: this whole put in place again stuff needs revision. Permission settings get lost.
619  $this->writelog('addMove() step 2: move subtree to new location');
620  // put top node to dest
621  $rbacadmin->revokePermission($subnodes[0]['child']);
622  $obj_data =& $ilias->obj_factory->getInstanceByRefId($subnodes[0]['child']);
623  $obj_data->putInTree($this->getRefId());
624  $obj_data->setPermissions($this->getRefId());
625  array_shift($subnodes);
626 
627  // put all sub nodes to their parent (of which we have moved top already to dest).
628  foreach ($subnodes as $node)
629  {
630  $rbacadmin->revokePermission($node['child']);
631  $obj_data =& $ilias->obj_factory->getInstanceByRefId($node['child']);
632  $obj_data->putInTree($node['parent']);
633  $obj_data->setPermissions($node['parent']);
634  }
635 
636  // STEP 3: Remove trashed objects from system
637  $this->writelog('addMove('.$objDAV->getRefID().') step 3: remove trashed objects from system');
638  require_once 'Services/Tree/classes/class.ilTree.php';
639  $trashTree = new ilTree(- (int) $objDAV->getRefId());
640  $node = $trashTree->getNodeData($objDAV->getRefId());
641  $subnodes = $trashTree->getSubTree($node);
642 
643  // remember already checked deleted node_ids
644  $checked[] = -(int) $objDAV->getRefId();
645 
646  // dive in recursive manner in each already deleted subtrees and remove these objects too
647  $this->removeDeletedNodes($objDAV->getRefId(), $checked, false);
648 
649  // delete trash tree
650  $tree->deleteTree($node);
651  $this->writelog('addMove('.$objDAV->getRefID().') all 3 steps done');
652  */
653  }
654 
663  function removeDeletedNodes($a_node_id, $a_checked, $a_delete_objects = true)
664  {
665  global $ilDB, $log, $ilias, $tree;
666 
667  $query = "SELECT tree FROM tree WHERE parent = ? AND tree < 0 ";
668  $sta = $ilDB->prepare($query,array('integer','integer'));
669  $res = $ilDB->execute($sta,array(
670  $a_node_id,
671  0));
672 
673 
674  while($row = $ilDB->fetchObject($res))
675  {
676  // only continue recursion if fetched node wasn't touched already!
677  if (!in_array($row->tree,$a_checked))
678  {
679  $deleted_tree = new ilTree($row->tree);
680  $a_checked[] = $row->tree;
681 
682  $row->tree = $row->tree * (-1);
683  $del_node_data = $deleted_tree->getNodeData($row->tree);
684  $del_subtree_nodes = $deleted_tree->getSubTree($del_node_data);
685 
686  $this->removeDeletedNodes($row->tree,$a_checked);
687 
688  if ($a_delete_objects)
689  {
690  foreach ($del_subtree_nodes as $node)
691  {
692  $node_obj =& $ilias->obj_factory->getInstanceByRefId($node["ref_id"]);
693 
694  // write log entry
695  /*$this->writelog("removeDeletedNodes(), delete obj_id: ".$node_obj->getId().
696  ", ref_id: ".$node_obj->getRefId().", type: ".$node_obj->getType().", ".
697  "title: ".$node_obj->getTitle());
698  */
699  $node_obj->delete();
700  }
701  }
702 
703  $tree->deleteTree($del_node_data);
704 
705  // write log entry
706  //$this->writelog("removeDeletedNodes(), deleted tree, tree_id: ".$del_node_data["tree"].", child: ".$del_node_data["child"]);
707  }
708  }
709 
710  return true;
711  }
718  function children()
719  {
720  // FIXME: Remove duplicate entries from this list, because of RFC2518, chapter 5.2
721  // If a duplicate is found, the older object must win. We use the object
722  // id to determine this. This is based on the assumption, that new objects
723  // have higher object id's then older objects.
724 
725  global $tree;
726 
727  $childrenDAV = array();
728  // Performance optimization. We sort the children using PHP instead of using the database.
729  //$childrenData =& $tree->getChilds($this->getRefId(),'title');
730  $childrenData =& $tree->getChilds($this->getRefId(),'');
731  foreach ($childrenData as $data)
732  {
733  $childDAV =& $this->createObject($data['ref_id'],$data['type']);
734  if (! is_null($childDAV))
735  {
736  // Note: We must not assign with =& here, because this will cause trouble
737  // when other functions attempt to work with the $childrenDAV array.
738  $childrenDAV[] = $childDAV;
739  }
740 
741  }
742  return $childrenDAV;
743  }
753  function childrenWithPermission($operations, $type ='')
754  {
755  //$this->writelog('@'.$this->getRefId().'.childrenWithPermission('.$operations.','.$type.')');
756  $childrenDAV = $this->children();
757  $permittedChildrenDAV = array();
758  foreach ($childrenDAV as $childDAV)
759  {
760  if ($childDAV->isPermitted($operations, $type))
761  {
762  $permittedChildrenDAV[] = $childDAV;
763  }
764 
765  }
766  //$this->writelog('@'.$this->getRefId().'.childrenWithPermission():'.count($permittedChildrenDAV).' children');
767  return $permittedChildrenDAV;
768  }
769 
779  {
780  $newObj = null;
781  switch ($type)
782  {
783  case 'mountPoint' :
784  require_once 'class.ilObjMountPointDAV.php';
785  $newObj =& new ilObjMountPointDAV($refId,null);
786  break;
787  case 'root' :
788  require_once 'class.ilObjRootDAV.php';
789  $newObj =& new ilObjRootDAV($refId,null);
790  break;
791  case 'cat' :
792  require_once 'class.ilObjCategoryDAV.php';
793  $newObj =& new ilObjCategoryDAV($refId,null);
794  break;
795  case 'fold' :
796  require_once 'class.ilObjFolderDAV.php';
797  $newObj =& new ilObjFolderDAV($refId,null);
798  break;
799  case 'crs' :
800  require_once 'class.ilObjCourseDAV.php';
801  $newObj =& new ilObjCourseDAV($refId,null);
802  break;
803  case 'grp' :
804  require_once 'class.ilObjGroupDAV.php';
805  $newObj =& new ilObjGroupDAV($refId,null);
806  break;
807  case 'file' :
808  require_once 'class.ilObjFileDAV.php';
809  $newObj =& new ilObjFileDAV($refId,null);
810  break;
811  case 'null' :
812  require_once 'class.ilObjNullDAV.php';
813  $newObj =& new ilObjNullDAV($refId,null);
814  break;
815  default :
816  break;
817  }
818  if (! is_null($newObj))
819  {
820  $newObj->read();
821  }
822  return $newObj;
823  }
830  function writelog($message)
831  {
832  if ($this->isDebug)
833  {
834  global $log, $ilias;
835  $log->write(
836  $ilias->account->getLogin()
837  .' DAV .'.get_class($this).' '.str_replace("\n",";",$message)
838  );
839  /*
840  $fh = fopen('/opt/ilias/log/ilias.log', 'a');
841  fwrite($fh, date('Y-m-d h:i:s '));
842  fwrite($fh, str_replace("\n",";",$message));
843  fwrite($fh, "\n\n");
844  fclose($fh);
845  */
846  }
847  }
848 
853  function __toString() {
854  return get_class($this).'#'.$this->getObjectId();
855  }
856 }
857 // END WebDAV
858 ?>