/**
  * Copying files and folders (action=2)
  *
  * @param	array		$cmds['data'] is the file/folder to copy. $cmds['target'] is the path where to copy to. $cmds['altName'] (boolean): If set, another filename is found in case the target already exists
  * @return	string		Returns the new filename upon success
  */
 function func_copy($cmds, $id)
 {
     if (!$this->isInit) {
         return FALSE;
     }
     // Initialize and check basic conditions:
     $theFile = $cmds['data'];
     $theDest = $this->is_directory($cmds['target']);
     // Clean up destination directory
     $altName = $cmds['altName'];
     // main log entry
     $this->log['cmd']['move'][$id] = array('errors' => array(), 'orig_filename' => $theFile, 'target_file' => '', 'target_folder' => '', 'target_path' => $theDest);
     if (!$theDest) {
         $this->writelog(2, 2, 100, 'Destination "%s" was not a directory', array($cmds['target']), 'copy', $id);
         return FALSE;
     }
     if (!$this->isPathValid($theFile) || !$this->isPathValid($theDest)) {
         $this->writelog(2, 2, 101, 'Target or destination had invalid path (".." and "//" is not allowed in path). T="%s", D="%s"', array($theFile, $theDest), 'copy', $id);
         return FALSE;
     }
     // Processing of file or directory.
     if (@is_file($theFile)) {
         // If we are copying a file...
         if ($this->actionPerms['copyFile']) {
             if (filesize($theFile) < $this->maxCopyFileSize * 1024) {
                 $fI = t3lib_div::split_fileref($theFile);
                 if ($altName) {
                     // If altName is set, we're allowed to create a new filename if the file already existed
                     $theDestFile = $this->getUniqueName($fI['file'], $theDest);
                     $fI = t3lib_div::split_fileref($theDestFile);
                 } else {
                     $theDestFile = $theDest . '/' . $fI['file'];
                 }
                 if ($theDestFile && !@file_exists($theDestFile)) {
                     if ($this->checkIfAllowed($fI['fileext'], $theDest, $fI['file'])) {
                         if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
                             if ($this->PHPFileFunctions) {
                                 copy($theFile, $theDestFile);
                             } else {
                                 $cmd = 'cp "' . $theFile . '" "' . $theDestFile . '"';
                                 exec($cmd);
                             }
                             t3lib_div::fixPermissions($theDestFile);
                             clearstatcache();
                             if (@is_file($theDestFile)) {
                                 $this->writelog(2, 0, 1, 'File "%s" copied to "%s"', array($theFile, $theDestFile), 'copy', $id);
                                 $this->log['cmd']['move'][$id]['target_file'] = $theDestFile;
                                 // update meta data
                                 if ($this->processMetaUpdate) {
                                     tx_dam::notify_fileCopied($theFile, $theDestFile);
                                 }
                                 return $theDestFile;
                             } else {
                                 $this->writelog(2, 2, 109, 'File "%s" WAS NOT copied to "%s"! Write-permission problem?', array($theFile, $theDestFile), 'copy', $id);
                             }
                         } else {
                             $this->writelog(2, 1, 110, 'Target or destination was not within your mountpoints! T="%s", D="%s"', array($theFile, $theDestFile), 'copy', $id);
                         }
                     } else {
                         $this->writelog(2, 1, 111, 'Fileextension "%s" is not allowed in "%s"!', array($fI['fileext'], $theDest . '/'), 'copy', $id);
                     }
                 } else {
                     $this->writelog(2, 1, 112, 'File "%s" already exists!', array($theDestFile), 'copy', $id);
                 }
             } else {
                 $this->writelog(2, 1, 113, 'File "%s" exceeds the size-limit of %s bytes', array($theFile, $this->maxCopyFileSize * 1024), 'copy', $id);
             }
         } else {
             $this->writelog(2, 1, 114, 'You are not allowed to copy files', '', 'copy', $id);
         }
         // FINISHED copying file
     } elseif (@is_dir($theFile) && !$this->dont_use_exec_commands) {
         // if we're copying a folder
         if ($this->actionPerms['copyFolder']) {
             $theFile = $this->is_directory($theFile);
             if ($theFile) {
                 $fI = t3lib_div::split_fileref($theFile);
                 if ($altName) {
                     // If altName is set, we're allowed to create a new filename if the file already existed
                     $theDestFile = $this->getUniqueName($fI['file'], $theDest);
                     $fI = t3lib_div::split_fileref($theDestFile);
                 } else {
                     $theDestFile = $theDest . '/' . $fI['file'];
                 }
                 if ($theDestFile && !@file_exists($theDestFile)) {
                     if (!t3lib_div::isFirstPartOfStr($theDestFile . '/', $theFile . '/')) {
                         // Check if the one folder is inside the other or on the same level... to target/dest is the same?
                         if ($this->checkIfFullAccess($theDest) || $this->is_webPath($theDestFile) == $this->is_webPath($theFile)) {
                             // no copy of folders between spaces
                             if ($this->checkPathAgainstMounts($theDestFile) && $this->checkPathAgainstMounts($theFile)) {
                                 // No way to do this under windows!
                                 $cmd = 'cp -R "' . $theFile . '" "' . $theDestFile . '"';
                                 exec($cmd);
                                 clearstatcache();
                                 if (@is_dir($theDestFile)) {
                                     $this->writelog(2, 0, 2, 'Directory "%s" copied to "%s"', array($theFile, $theDestFile), 'copy', $id);
                                     $this->log['cmd']['move'][$id]['target_folder'] = $theDestFile;
                                     // update meta data
                                     if ($this->processMetaUpdate) {
                                         tx_dam::notify_fileCopied($theFile, $theDestFile);
                                     }
                                     return $theDestFile;
                                 } else {
                                     $this->writelog(2, 2, 119, 'Directory "%s" WAS NOT copied to "%s"! Write-permission problem?', array($theFile, $theDestFile), 'copy', $id);
                                 }
                             } else {
                                 $this->writelog(2, 1, 120, 'Target or destination was not within your mountpoints! T="%s", D="%s"', array($theFile, $theDestFile), 'copy', $id);
                             }
                         } else {
                             $this->writelog(2, 1, 121, 'You don\'t have full access to the destination directory "%s"!', array($theDest . '/'), 'copy', $id);
                         }
                     } else {
                         $this->writelog(2, 1, 122, 'Destination cannot be inside the target! D="%s", T="%s"', array($theDestFile . '/', $theFile . '/'), 'copy', $id);
                     }
                 } else {
                     $this->writelog(2, 1, 123, 'Target "%s" already exists!', array($theDestFile), 'copy', $id);
                 }
             } else {
                 $this->writelog(2, 2, 124, 'Target seemed not to be a directory! (Shouldn\'t happen here!)', '', 'copy', $id);
             }
         } else {
             $this->writelog(2, 1, 125, 'You are not allowed to copy directories', '', 'copy', $id);
         }
         // FINISHED copying directory
     } else {
         $this->writelog(2, 2, 130, 'The item "%s" was not a file or directory!', array($theFile), 'copy', $id);
     }
 }