/**
  * Renaming files or foldes (action=5)
  *
  * @param	array		$cmds['data'] is the new name. $cmds['target'] is the target (file or dir).
  * @param	string		$id: ID of the item
  * @return	string		Returns the new filename upon success
  */
 function func_rename($cmds, $id)
 {
     if (!$this->isInit) {
         return FALSE;
     }
     $theNewName = tx_dam::file_makeCleanName($cmds['data'], true);
     #		$theNewName = $this->cleanFileName($cmds['data']);
     if (empty($theNewName)) {
         return;
     }
     // main log entry
     $this->log['cmd']['rename'][$id] = array('errors' => array(), 'orig_filename' => $cmds['target'], 'target_file' => $theNewName);
     if (!$this->checkFileNameLen($theNewName)) {
         $this->writelog(5, 1, 124, 'New name "%s" was too long (max %s characters)', array($theNewName, $this->maxInputNameLen), 'rename', $id);
         return;
     }
     $theTarget = $cmds['target'];
     $type = filetype($theTarget);
     // $type MUST BE file or dir
     if (!($type == 'file' || $type == 'dir')) {
         $this->writelog(5, 2, 123, 'Target "%s" was neither a directory nor a file!', array($theTarget), 'rename', $id);
         return;
     }
     // Fetches info about path, name, extention of $theTarget
     $fileInfo = t3lib_div::split_fileref($theTarget);
     // The name should be different from the current. And the filetype must be allowed
     if ($fileInfo['file'] == $theNewName) {
         $this->writelog(5, 1, 122, 'Old and new name is the same (%s)', array($theNewName), 'rename', $id);
         return;
     }
     $theRenameName = $fileInfo['path'] . $theNewName;
     // check mountpoints
     if (!$this->checkPathAgainstMounts($fileInfo['path'])) {
         $this->writelog(5, 1, 121, 'Destination path "%s" was not within your mountpoints!', array($fileInfo['path']), 'rename', $id);
         return;
     }
     // check if dest exists
     if (@file_exists($theRenameName)) {
         $this->writelog(5, 1, 120, 'Destination "%s" existed already!', array($theRenameName), 'rename', $id);
         return;
     }
     if ($type == 'file') {
         // user have permissions for action
         if (!$this->actionPerms['renameFile']) {
             $this->writelog(5, 1, 102, 'You are not allowed to rename files!', '', 'rename', $id);
             return;
         }
         $fI = t3lib_div::split_fileref($theRenameName);
         if (!$this->checkIfAllowed($fI['fileext'], $fileInfo['path'], $fI['file'])) {
             $this->writelog(5, 1, 101, 'Fileextension "%s" was not allowed!', array($fI['fileext']), 'rename', $id);
             return;
         }
         if (!@rename($theTarget, $theRenameName)) {
             $this->writelog(5, 1, 100, 'File "%s" was not renamed! Write-permission problem in "%s"?', array($theTarget, $fileInfo['path']), 'rename', $id);
             return;
         }
         $this->writelog(5, 0, 1, 'File renamed from "%s" to "%s"', array($fileInfo['file'], $theNewName), 'rename', $id);
         // update meta data
         if ($this->processMetaUpdate) {
             tx_dam::notify_fileMoved($theTarget, $theRenameName);
         }
     } elseif ($type == 'dir') {
         // user have permissions for action
         if (!$this->actionPerms['renameFolder']) {
             $this->writelog(5, 1, 111, 'You are not allowed to rename directories!', '', 'rename', $id);
             return;
         }
         if (!@rename($theTarget, $theRenameName)) {
             $this->writelog(5, 1, 110, 'Directory "%s" was not renamed! Write-permission problem in "%s"?', array($theTarget, $fileInfo['path']), 'rename', $id);
             return;
         }
         $this->writelog(5, 0, 2, 'Directory renamed from "%s" to "%s"', array($fileInfo['file'], $theNewName), 'rename', $id);
         // update meta data
         if ($this->processMetaUpdate) {
             tx_dam::notify_fileMoved($theTarget, $theRenameName);
         }
     } else {
         return;
     }
     // add file to log entry
     $this->log['cmd']['rename'][$id]['target_' . $type] = $theRenameName;
     return $theRenameName;
 }
 /**
  * Main function, rendering the content of the rename form
  *
  * @return	void
  */
 function main()
 {
     global $LANG;
     $content = '';
     // Cleaning and checking target
     $this->folder = tx_dam::path_compileInfo($this->pObj->folder[0]);
     if ($this->folder['dir_accessable']) {
         if (is_array($this->pObj->data) and $this->pObj->data['new_name']) {
             $newFolderName = tx_dam::file_makeCleanName($this->pObj->data['new_name'], true);
             $error = tx_dam::process_renameFolder($this->folder['dir_path_absolute'], $newFolderName);
             if ($error) {
                 $content .= $GLOBALS['SOBE']->getMessageBox($LANG->getLL('error'), htmlspecialchars($error), $this->pObj->buttonBack(0), 2);
             } else {
                 $this->pObj->redirect(true);
             }
         } else {
             $content .= $this->renderForm();
         }
     } else {
         // this should have happen in index.php already
         $content .= $this->pObj->accessDeniedMessageBox(tx_dam_guiFunc::getFolderInfoBar($this->folder));
     }
     return $content;
 }
Example #3
0
 /**
  * Rename a folder and process DB update
  *
  * @param	string		$oldPath Folder path
  * @param	string		$newName New folder name
  * @param	boolean		$getFullErrorLogEntry If set the full error log entry will be returned as array
  * @return	mixed		error message or error array
  * @see tx_dam_tce_file::getLastError()
  */
 function process_renameFolder($oldPath, $newName, $getFullErrorLogEntry = FALSE)
 {
     global $TYPO3_CONF_VARS;
     $error = false;
     $oldPath = tx_dam::path_makeAbsolute($oldPath);
     $newName = tx_dam::file_makeCleanName($newName, true);
     $newPath = dirname($oldPath) . $newName . '/';
     // Init TCE-file-functions object:
     require_once PATH_txdam . 'lib/class.tx_dam_tce_file.php';
     $TCEfile = t3lib_div::makeInstance('tx_dam_tce_file');
     $TCEfile->init();
     // Processing rename folder
     $cmd = array();
     $cmd['rename']['NONE']['target'] = preg_replace('#/$#', '', $oldPath);
     $cmd['rename']['NONE']['data'] = $newName;
     $TCEfile->setCmdmap($cmd);
     $TCEfile->process();
     if ($TCEfile->errors()) {
         $error = $TCEfile->getLastError($getFullErrorLogEntry);
     }
     //} else {
     // already done in tx_dam_tce_file:
     // tx_dam::notify_fileMoved($oldPath, $newPath);
     //}
     if (!$error) {
         $info = array('target_path' => $oldPath, 'new_name' => $newName, 'new_path' => $newPath);
         tx_dam::_callProcessPostTrigger('renameFolder', $info);
     }
     return $error;
 }
 /**
  * tx_dam::file_makeCleanName()
  */
 public function test_file_makeCleanName()
 {
     $GLOBALS['T3_VAR']['ext']['dam']['pathInfoCache'] = array();
     $tempSave = $GLOBALS['TYPO3_CONF_VARS']['SYS']['maxFileNameLength'];
     $GLOBALS['TYPO3_CONF_VARS']['SYS']['maxFileNameLength'] = 25;
     $filename = 'abcdefghijklmnopqrstuvwxyz.abc';
     $filename = tx_dam::file_makeCleanName($filename, true);
     self::assertEquals($filename, 'abcdefghijklmnopqrstu.abc', 'File name differs: ' . $filename . ' (abcdefghijklmnopqrstu.abc)');
     $filename = 'abcdefghijKLMNOPQRSTUVWXYZ.abc';
     $filename = tx_dam::file_makeCleanName($filename, true);
     self::assertEquals($filename, 'abcdefghijKLMNOPQRSTU.abc', 'File name differs: ' . $filename . ' (abcdefghijKLMNOPQRSTU.abc)');
     $filename = 'a0-_.,;:#+*=()/!§$%&XYZ.abc';
     $filename = tx_dam::file_makeCleanName($filename, true);
     self::assertEquals($filename, 'a0-_.,;_#+_=()_!§$%&X.abc', 'File name differs: ' . $filename . ' (a0-_.,;_#+_=()_!$%&X.abc)');
     $GLOBALS['TYPO3_CONF_VARS']['SYS']['maxFileNameLength'] = $tempSave;
 }