Code

Updated in_array checks in GOsa.
[gosa.git] / gosa-core / include / class_CopyPasteHandler.inc
index 1c0ff80460452d389ae298b341860677b4b53ea0..1149370076b558c8f19c07a94fd80e5169a97006 100644 (file)
@@ -1,6 +1,26 @@
 <?php
-
-define("LDAP_DUMP_PATH","/tmp/gosa");
+/*
+ * This code is part of GOsa (http://www.gosa-project.org)
+ * Copyright (C) 2003-2008 GONICUS GmbH
+ *
+ * ID: $$Id$$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+define("LDAP_DUMP_PATH","/var/cache/gosa/tmp");
 
 class CopyPasteHandler {
 
@@ -15,6 +35,12 @@ class CopyPasteHandler {
 
   /* The dn of the last edited object */
   var $lastdn      = "";
+  
+  var $disallowed_objects = array();
+  var $objects_to_fix     = array();
+  var $clean_objects      = array();
+  var $require_update     = FALSE;
+
 
 
   /* Create CP handler  */
@@ -35,20 +61,20 @@ class CopyPasteHandler {
    *  array['tab_class']  - Tab object that should be used to initialize the new object
    *  array['tab_object'] - Tab object name used to initialize correct object Type like USERTABS
    */
-  function add_to_queue($dn,$action,$tab_class,$tab_object,$tab_acl_category)
+  function add_to_queue($dn,$action,$tab_class,$tab_object,$tab_acl_category,&$parent = NULL)
   {
-    if(!class_exists($tab_class)){
-      trigger_error(sprintf("Specified class object '%s' does not exists.",$tab_class));
+    if(!class_available($tab_class)){
+      trigger_error(sprintf("Specified class object %s does not exists.", bold($tab_class)));
       return(FALSE);
     }
 
     if(!isset($this->config->data['TABS'][$tab_object])){
-      trigger_error(sprintf("Specified tab object '%s' does not exists.",$tab_object));
+      trigger_error(sprintf("Specified tab object %s does not exists.", bold($tab_object)));
       return(FALSE);
     }
 
-    if(!in_array($action,array("cut","copy"))){
-      trigger_error(sprintf("Specified action '%s' does not exists for copy & paste.",$action));
+    if(!in_array_strict($action,array("cut","copy"))){
+      trigger_error(sprintf("Specified action %s does not exists for copy & paste.", bold($action)));
       return(FALSE);
     } 
 
@@ -60,7 +86,9 @@ class CopyPasteHandler {
       $tmp['tab_class'] = $tab_class;
       $tmp['tab_object']= $tab_object;
       $tmp['tab_acl_category']= $tab_acl_category;
+      $tmp['parent']    = $parent;
       $this->queue[]    = $tmp; 
+      $this->require_update = TRUE;
     }
   }
 
@@ -72,6 +100,7 @@ class CopyPasteHandler {
   function cleanup_queue()
   {
     $this->current = FALSE;
+    $this->require_update = TRUE;
     $this->setvar_array = array();
 
     /* Remove all entries from queue */  
@@ -83,12 +112,19 @@ class CopyPasteHandler {
     /* Create patch if it doesn't exists */
     if(!is_dir(LDAP_DUMP_PATH)){
       @mkdir(LDAP_DUMP_PATH);
-      @chmod(LDAP_DUMP_PATH,0700);
+
+      /* Update folder permissions */
+      if(!@chmod(LDAP_DUMP_PATH,0700)){
+        $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": "._("Cannot set permission for %s"), bold(LDAP_DUMP_PATH));
+        msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
+        new log("copy","all/all","copy & paste, event queue.",array(), $msg);
+        return(FALSE);
+      }
     }    
     
     /* check if we are able to create a new file the given directory */
     if(!is_writeable(LDAP_DUMP_PATH)){
-      $msg= sprintf(_("Cannot cleanup copy & paste queue: no write permission inside '%s'!"),LDAP_DUMP_PATH);
+      $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile(LDAP_DUMP_PATH)."</i>";
       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
       return(FALSE);
@@ -120,7 +156,7 @@ class CopyPasteHandler {
 
     /* Check if given dn is valid and ldap search was succesfull */ 
     if(!$res){
-      $msg= sprintf(_("Copy and paste failed: object '%s' is not a valid ldap object!"), LDAP::fix($dn));
+      $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": "._("'%s' is no valid LDAP object"), bold(LDAP::fix($dn)));
       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
       new log("copy","all/all",$dn,array(), $msg);
       return(FALSE);
@@ -133,12 +169,11 @@ class CopyPasteHandler {
     /* Create patch if it doesn't exists */
     if(!is_dir($path)){
       @mkdir($path);
-      @chmod($path,0700);
     }    
 
     /* check if we are able to create a new file the given directory */
     if(!is_writeable($path)){
-      $msg= sprintf(_("Cannot save LDAP dump: no write permission inside '%s'!"),LDAP_DUMP_PATH);
+      $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": "._("No write permission in '%s'"), bold(LDAP_DUMP_PATH));
       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
       new log("copy","all/all",$dn,array(), $msg);
       return(FALSE);
@@ -147,12 +182,20 @@ class CopyPasteHandler {
     /* Create file handle */
     $fp = @fopen($path."/".$filename,"w+");
     if(!$fp){
-      $msg= sprintf(_("Cannot save LDAP dump: no write permission to '%s/%s'!"),$path,$filename);
+      $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile("$path/$filename")."</i>";
       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
       new log("copy","all/all",$dn,array(), $msg);
       return(FALSE);
     }    
 
+    /* Update folder permissions */
+    if(!@chmod($path."/".$filename,0700)){
+      $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": "._("Cannot set permission for '%s'"), bold(LDAP_DUMP_PATH));
+      msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
+      new log("copy","all/all","copy & paste, event queue.",array(), $msg);
+      return(FALSE);
+    }
+
     $data = serialize($ldap->fetch());
     fwrite($fp,$data,strlen($data));
     fclose($fp);
@@ -171,51 +214,46 @@ class CopyPasteHandler {
 
 
   /* Paste one entry from queue */
-  function load_entry_from_queue()
+  function load_entry_from_queue($entry)
   {
+    if(!isset($entry['tab_class'])){
+      return(array());
+    }
 
-    /* Save posted variables, handle dialog posts like 'cancel' */
-    $this->save_object();
 
-    /* If there is currently no object pasted 
-     *  create new object and prepare object to be pasted
-     */
-    if(!$this->current && $this->entries_queued()){
-      $key    = key($this->queue);
-      $entry  = $this->queue[$key];
-      $tab_c = $entry['tab_class'];
-      $tab_o = $entry['tab_object'];
-      $tab_a = $entry['tab_acl_category'];
-
-      if($entry['method'] == "copy"){
-        $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
-      }else{
-        $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
-      }
+    $tab_c = $entry['tab_class'];
+    $tab_o = $entry['tab_object'];
+    $tab_a = $entry['tab_acl_category'];
+    $parent = $entry['parent'];
+
+    if($entry['method'] == "copy"){
+      $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
+    }else{
+      $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
+    }
 
-      $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
+    if($parent ){
+      $entry['object']->parent = $parent;
+    }
+    $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
 
-      if($entry['method'] == "copy"){
+    if($entry['method'] == "copy"){
 
-        /* Prepare each plugin of this tab object to be posted */
-        foreach($entry['object']->by_object as $name => $obj){
+      /* Prepare each plugin of this tab object to be posted */
+      foreach($entry['object']->by_object as $name => $obj){
 
-          /* Prepare every single class, to be copied  */
-          $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
+        /* Prepare every single class, to be copied  */
+        $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
 
-          /* handle some special vars */
-          foreach(array("is_account") as $attr){
-            if(isset($entry['source_data'][$attr])){
-              $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
-            }
+        /* handle some special vars */
+        foreach(array("is_account") as $attr){
+          if(isset($entry['source_data'][$attr])){
+            $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
           }
         }
       }
-
-      /* Assign created object as current */
-      $this->current = $entry;
-      unset($this->queue[$key]);
     }
+    return($entry);
   }
 
 
@@ -232,7 +270,7 @@ class CopyPasteHandler {
       }
       return(unserialize($data));
     }else{
-      $msg= sprintf(_("Cannot load dumped file '%s'!"),$filename);
+      $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotReadFile($filename)."</i>");
       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
       new log("copy","all/all",$dn,array(), $msg);
       return(FALSE);
@@ -246,83 +284,145 @@ class CopyPasteHandler {
   {
     $ui = get_userinfo();
     $type = $this->current['method'];
-    if($type == "cut"){
-      if(isset($_POST['PerformCopyPaste'])){
-        while($this->entries_queued()){
-          $this->load_entry_from_queue();      
-          $this->_update_vars();
-
-          $msgs = $this->check();
-          $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
-          /* Check permissions */ 
-          if(!preg_match("/((c|w)|(w|c))/",$acl)){
-            msg_dialog::display(_("Error"), sprintf(_("You have no permission to save object '%s'."), $this->current['dn']), ERROR_DIALOG);
-          }elseif(count ($msgs) ){
-            foreach( $msgs as $msg){
-              msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
-            }
-          }else{
-
-            /* Load next queue entry */
-            $this->lastdn = $this->current['object']->dn;
-            $this->current['object']->save();
-          }
 
-          $this->current =FALSE;
+    /* Check which entries can be pasted directly.
+     * Create a list of all entries that can be pasted directly.
+     */
+    if($this->require_update){
+      $this->clean_objects      = array();
+      $this->objects_to_fix     = array();
+      $this->disallowed_objects = array();
+
+      /* Put each queued object in one of the above arrays 
+       */
+      foreach($this->queue as $key => $entry){
+
+        /* Update entries on demand 
+         */
+        if(!isset($entry['object'])){
+          $entry = $this->load_entry_from_queue($entry);
+          $this->queue[$key] = $entry;
+        }
+        $entry= $this->_update_vars($entry);
+        $msgs = $entry['object']->check();
+
+        /* To copy an object we require full read access to the object category 
+         */
+        $copy_acl = preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
+
+        /* In order to copy an object we require read an delete acls 
+         */
+        $cut_acl  = preg_match("/d/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
+        $cut_acl &= preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
+
+        /* Check permissions */
+        if($entry['method'] == "copy" && !$copy_acl){
+          $this->disallowed_objects[$key] = $entry;
+        }elseif($entry['method'] == "cut" && !$cut_acl){
+          $this->disallowed_objects[$key] = $entry;
+        }elseif(!count($msgs)){
+          $this->clean_objects[$key]  = $entry;
+        }else{
+          $this->objects_to_fix[$key] = $entry;
         }
       }
-      if($this->current){
-
-        $dns = $this->current['dn']."\n";
-        foreach($this->queue as $entry){
-          $dns .= $entry['dn']."\n";
+      if(count($this->disallowed_objects)){
+        $dns = array();
+        foreach($this->disallowed_objects as $entry){
+          $dns[] = $entry['dn'];
         }
+#        msg_dialog::display(_("Permission"),msgPool::permCreate($dns),INFO_DIALOG);
+      }
+      $this->require_update = FALSE;
+    }
 
-        $smarty = get_smarty();
-        $smarty->assign("type","cut");
-        $smarty->assign("Complete",false);
-        $smarty->assign("AttributesToFix","&nbsp;");
-        $smarty->assign("SubDialog",$this->current['object']->SubDialog);
-        $smarty->assign("objectDN"     ,$dns);
-        $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), "<pre>".$dns."</pre>"));
-        return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
+    /* Save objects that can be pasted directly 
+     */
+    if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
+      $this->save_object();
+      $this->current = FALSE;
+      foreach($this->clean_objects as $key => $entry){
+
+        /* Remove from queue -> avoid saving twice */
+        unset($this->queue[$key]);
+        unset($this->clean_objects[$key]);
+
+        /* Load next queue entry */
+        $this->current = $entry;
+        $this->lastdn = $this->current['object']->dn;
+        $this->current= $this->_update_vars($this->current);
+        $this->current['object']->save();
+        $this->handleReferences();
+        $this->current = FALSE;
+      } 
+    }
+
+    /* Save edited entry and force loading new one 
+     */
+    if(isset($this->current['object']) && method_exists($this->current['object'],"saveCopyDialog")) {
+      $this->current['object']->saveCopyDialog();
+    }
+
+    if(isset($_POST['PerformCopyPaste']) && $this->current){
+      $msgs = $this->check();
+
+      /* Load next queue entry */
+      if(!count($msgs)){
+        $this->current['object']->save();
+        $this->handleReferences();
+        $this->lastdn = $this->current['object']->dn;
+        $this->current = FALSE;
+      }else{
+        foreach( $msgs as $msg){
+          msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
+        }
       }
     }
-    if($type == "copy"){
-      if(isset($_POST['PerformCopyPaste'])){
-        $this->_update_vars();
-        $msgs = $this->check();
-
-        $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
-        /* Check permissions */ 
-        if(!preg_match("/((c|w)|(w|c))/",$acl)){
-          msg_dialog::display(_("Error"), sprintf(_("You have no permission to copy and paste object '%s'!"),$this->current['dn']), ERROR_DIALOG);
-        }elseif(count ($msgs) ){
-          foreach( $msgs as $msg){
-            msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
-          }
-        }else{
-          $this->current['object']->save();
-          $this->lastdn = $this->current['object']->dn;
-          $this->current =FALSE;
+      
+    /* Display a list of all pastable entries 
+     */
+    if(count($this->clean_objects)){
+      
+      $dns = array();
+      foreach($this->clean_objects as $object){
+        $dns[] = $object['dn'];
+      }
 
-          /* Load next queue entry */
-          $this->load_entry_from_queue();
+      $smarty = get_smarty();
+      $smarty->assign("type","directly");
+      $smarty->assign("Complete",false);
+      $smarty->assign("AttributesToFix","&nbsp;");
+      $smarty->assign("SubDialog","");
+      $smarty->assign("message"  , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($dns)));
+      return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
+    }
+
+    /* Display a list of all pastable entries 
+     */
+    if($this->current || count($this->objects_to_fix)){
+      $this->save_object(); 
+      if(!$this->current){
+        $key = key($this->objects_to_fix);
+        if(isset($this->objects_to_fix[$key])){
+          $this->current = $this->objects_to_fix[$key];
+          $this->current= $this->_update_vars($this->current);
+          unset($this->objects_to_fix[$key]);
+          unset($this->queue[$key]); 
         }
       }
       if($this->current){
         $smarty = get_smarty();
-        $smarty->assign("type","copy");
+        $smarty->assign("type","modified");
         $smarty->assign("Complete",false);
         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
-        $smarty->assign("objectDN"     ,$this->current['source_data']['dn']);
-        $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
+        $smarty->assign("objectDN",$this->current['source_data']['dn']);
+        $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".
+                    bold(@LDAP::fix($this->current['source_data']['dn']))));
         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
       }
     }
+    return("");
   }
 
 
@@ -344,12 +444,6 @@ class CopyPasteHandler {
       $this->cleanup_queue();
       $this->current = FALSE;
     }
-
-    /* Assign posted var to all tabs
-     */
-    if($this->current){
-      $this->current['object']->saveCopyDialog();
-    }
   }
 
 
@@ -373,28 +467,30 @@ class CopyPasteHandler {
 
 
   /* Update current object attributes, collected via SetVar */
-  function _update_vars()
+  function _update_vars($entry)
   {
-    if($this->current){
+    /* Update all attributes specified with SetVar */
+    foreach($this->setvar_array as $name => $value){
+      if(isset($entry['object']->$name)){
+        $entry['object']->$name = $value;
+      }
+    }
+
+    /* Walk through tabs */
+    foreach($entry['object']->by_object as $key => $obj){
 
       /* Update all attributes specified with SetVar */
       foreach($this->setvar_array as $name => $value){
-        if(isset($this->current['object']->$name)){
-          $this->current['object']->$name = $value;
-        }
-      }
 
-      /* Walk through tabs */
-      foreach($this->current['object']->by_object as $key => $obj){
+        /* Do not update parent for plugins, this may break things */
+        if($name == "parent") continue;
 
-        /* Update all attributes specified with SetVar */
-        foreach($this->setvar_array as $name => $value){
-          if(isset($this->current['object']->by_object[$key]->$name)){
-            $this->current['object']->by_object[$key]->$name = $value;
-          }
+        if(isset($entry['object']->by_object[$key]->$name)){
+          $entry['object']->by_object[$key]->$name = $value;
         }
       }
     }
+    return($entry);
   }
 
 
@@ -411,16 +507,70 @@ class CopyPasteHandler {
   }
 
 
+  function handleReferences()
+  {
+    $dst_dn = $this->current['object']->dn;
+    $src_dn = $this->current['dn'];
+
+    // Only copy references if required 
+    if($this->current['method'] != 'copy') return;
+
+    // Migrate objectgroups
+    $ogroups = get_sub_list("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter(LDAP::fix($src_dn))."))",
+            "ogroups", array(get_ou("group", "ogroupRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
+
+    // Walk through all objectGroups
+    foreach($ogroups as $ogroup){
+        $o_ogroup= new ogroup($this->config,$ogroup['dn']);
+        $o_ogroup->member[$dst_dn]= $dst_dn;
+        $o_ogroup->save();
+    }
+
+    // Update roles 
+    $roles = get_sub_list("(&(objectClass=organizationalRole)(roleOccupant=".LDAP::prepare4filter(LDAP::fix($src_dn))."))",
+            "roles", array(get_ou("roleGeneric", "roleRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
+
+    // Walk through all roles
+    foreach($roles as $role){
+        $role = new roleGeneric($this->config,$role['dn']);
+        $role->roleOccupant[] = $dst_dn;
+        $role->save();
+    }
+
+    // Update groups
+    if(isset($this->current['object']->uid) && !empty($this->current['object']->uid)){
+
+        $ldap = $this->config->get_ldap_link();
+        $ldap->cd($this->config->current['BASE']);
+        $ldap->cat($src_dn);
+        $attrs = $ldap->fetch();
+        if(isset($attrs['uid'][0])){
+            $suid = $attrs['uid'][0];
+
+            $uid = $this->current['object']->uid;
+            $groups = get_sub_list("(&(objectClass=posixGroup)(memberUid={$suid}))",
+                    "groups",array(get_ou("core", "groupRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
+
+            // Walk through all POSIX groups
+            foreach($groups as $group){
+                $o_group= new group($this->config,$group['dn']);
+                $o_group->addUser($uid);
+                $o_group->save();
+            }
+        }
+    }
+  }
+
   /* returns the paste icon for headpages */ 
   function generatePasteIcon()
   {
-    $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
+    $Copy_Paste= "&nbsp;<img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
     if($this->entries_queued()){
-      $img= "images/copypaste.png";
+      $img= "images/lists/paste.png";
       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
         src='".$img."' alt='"._("Paste")."'>&nbsp;";
     }else{
-      $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\">&nbsp;";
+      $Copy_Paste.= "<img class='center' src='images/lists/paste-grey.png' alt=\""._("Cannot paste")."\">&nbsp;";
     }
     return ($Copy_Paste);
   }