示例#1
0
文件: DC_Folder.php 项目: eknoes/core
 /**
  * Save the current value
  *
  * @param mixed $varValue
  *
  * @throws \Exception
  */
 protected function save($varValue)
 {
     if (\Input::post('FORM_SUBMIT') != $this->strTable) {
         return;
     }
     $arrData = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField];
     // File names
     if ($this->strField == 'name') {
         if (!file_exists(TL_ROOT . '/' . $this->strPath . '/' . $this->varValue . $this->strExtension) || !$this->isMounted($this->strPath . '/' . $this->varValue . $this->strExtension) || $this->varValue === $varValue) {
             return;
         }
         $this->import('Files');
         $varValue = utf8_romanize($varValue);
         // Trigger the save_callback
         if (is_array($arrData['save_callback'])) {
             foreach ($arrData['save_callback'] as $callback) {
                 if (is_array($callback)) {
                     $this->import($callback[0]);
                     $varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this);
                 } elseif (is_callable($callback)) {
                     $varValue = $callback($varValue, $this);
                 }
             }
         }
         // The target exists
         if (strcasecmp($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension) !== 0 && file_exists(TL_ROOT . '/' . $this->strPath . '/' . $varValue . $this->strExtension)) {
             throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['fileExists'], $varValue));
         }
         $arrImageTypes = trimsplit(',', strtolower(\Config::get('validImageTypes')));
         // Remove potentially existing thumbnails (see #6641)
         if (in_array(substr($this->strExtension, 1), $arrImageTypes)) {
             foreach (glob(TL_ROOT . '/assets/images/*/' . $this->varValue . '-*' . $this->strExtension) as $strThumbnail) {
                 $this->Files->delete(str_replace(TL_ROOT . '/', '', $strThumbnail));
             }
         }
         // Rename the file
         $this->Files->rename($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension);
         // New folders
         if (stristr($this->intId, '__new__') !== false) {
             // Update the database
             if ($this->blnIsDbAssisted && \Dbafs::shouldBeSynchronized($this->strPath . '/' . $varValue . $this->strExtension)) {
                 $this->objActiveRecord = \Dbafs::addResource($this->strPath . '/' . $varValue . $this->strExtension);
             }
             $this->log('Folder "' . $this->strPath . '/' . $varValue . $this->strExtension . '" has been created', __METHOD__, TL_FILES);
         } else {
             // Update the database
             if ($this->blnIsDbAssisted) {
                 $syncSource = \Dbafs::shouldBeSynchronized($this->strPath . '/' . $this->varValue . $this->strExtension);
                 $syncTarget = \Dbafs::shouldBeSynchronized($this->strPath . '/' . $varValue . $this->strExtension);
                 if ($syncSource && $syncTarget) {
                     \Dbafs::moveResource($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension);
                 } elseif ($syncSource) {
                     \Dbafs::deleteResource($this->strPath . '/' . $this->varValue . $this->strExtension);
                 } elseif ($syncTarget) {
                     \Dbafs::addResource($this->strPath . '/' . $varValue . $this->strExtension);
                 }
             }
             $this->log('File or folder "' . $this->strPath . '/' . $this->varValue . $this->strExtension . '" has been renamed to "' . $this->strPath . '/' . $varValue . $this->strExtension . '"', __METHOD__, TL_FILES);
         }
         // Set the new value so the input field can show it
         if (\Input::get('act') == 'editAll') {
             $session = $this->Session->getData();
             if (($index = array_search($this->strPath . '/' . $this->varValue . $this->strExtension, $session['CURRENT']['IDS'])) !== false) {
                 $session['CURRENT']['IDS'][$index] = $this->strPath . '/' . $varValue . $this->strExtension;
                 $this->Session->setData($session);
             }
         }
         $this->varValue = $varValue;
     } elseif ($this->blnIsDbAssisted && $this->objActiveRecord !== null) {
         // Convert date formats into timestamps
         if ($varValue != '' && in_array($arrData['eval']['rgxp'], array('date', 'time', 'datim'))) {
             $objDate = new \Date($varValue, \Date::getFormatFromRgxp($arrData['eval']['rgxp']));
             $varValue = $objDate->tstamp;
         }
         // Make sure unique fields are unique
         if ($arrData['eval']['unique'] && $varValue != '' && !$this->Database->isUniqueValue($this->strTable, $this->strField, $varValue, $this->objActiveRecord->id)) {
             throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $this->strField));
         }
         // Handle multi-select fields in "override all" mode
         if (\Input::get('act') == 'overrideAll' && ($arrData['inputType'] == 'checkbox' || $arrData['inputType'] == 'checkboxWizard') && $arrData['eval']['multiple']) {
             if ($this->objActiveRecord !== null) {
                 $new = deserialize($varValue, true);
                 $old = deserialize($this->objActiveRecord->{$this->strField}, true);
                 switch (\Input::post($this->strInputName . '_update')) {
                     case 'add':
                         $varValue = array_values(array_unique(array_merge($old, $new)));
                         break;
                     case 'remove':
                         $varValue = array_values(array_diff($old, $new));
                         break;
                     case 'replace':
                         $varValue = $new;
                         break;
                 }
                 if (!is_array($varValue) || empty($varValue)) {
                     $varValue = '';
                 } elseif (isset($arrData['eval']['csv'])) {
                     $varValue = implode($arrData['eval']['csv'], $varValue);
                     // see #2890
                 } else {
                     $varValue = serialize($varValue);
                 }
             }
         }
         // Convert arrays (see #2890)
         if ($arrData['eval']['multiple'] && isset($arrData['eval']['csv'])) {
             $varValue = implode($arrData['eval']['csv'], deserialize($varValue, true));
         }
         // Trigger the save_callback
         if (is_array($arrData['save_callback'])) {
             foreach ($arrData['save_callback'] as $callback) {
                 if (is_array($callback)) {
                     $this->import($callback[0]);
                     $varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this);
                 } elseif (is_callable($callback)) {
                     $varValue = $callback($varValue, $this);
                 }
             }
         }
         // Save the value if there was no error
         if (($varValue != '' || !$arrData['eval']['doNotSaveEmpty']) && ($this->varValue != $varValue || $arrData['eval']['alwaysSave'])) {
             // If the field is a fallback field, empty all other columns
             if ($arrData['eval']['fallback'] && $varValue != '') {
                 $this->Database->execute("UPDATE " . $this->strTable . " SET " . $this->strField . "=''");
             }
             // Set the correct empty value (see #6284, #6373)
             if ($varValue === '') {
                 $varValue = \Widget::getEmptyValueByFieldType($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['sql']);
             }
             $this->objActiveRecord->{$this->strField} = $varValue;
             $this->objActiveRecord->save();
             $this->blnCreateNewVersion = true;
             $this->varValue = deserialize($varValue);
         }
     }
 }
示例#2
0
 /**
  * Rename the folder
  *
  * @param string $strNewName The new path
  *
  * @return boolean True if the operation was successful
  */
 public function renameTo($strNewName)
 {
     $strParent = dirname($strNewName);
     // Create the parent folder if it does not exist
     if (!is_dir(TL_ROOT . '/' . $strParent)) {
         new \Folder($strParent);
     }
     $return = $this->Files->rename($this->strFolder, $strNewName);
     // Update the database AFTER the folder has been renamed
     if ($this->blnSyncDb) {
         $this->objModel = \Dbafs::moveResource($this->strFolder, $strNewName);
     }
     // Reset the object AFTER the database has been updated
     if ($return != false) {
         $this->strFolder = $strNewName;
     }
     return $return;
 }
示例#3
0
 /**
  * Rename the folder
  *
  * @param string $strNewName The new path
  *
  * @return boolean True if the operation was successful
  */
 public function renameTo($strNewName)
 {
     $strParent = dirname($strNewName);
     // Create the parent folder if it does not exist
     if (!is_dir(TL_ROOT . '/' . $strParent)) {
         new \Folder($strParent);
     }
     $return = $this->Files->rename($this->strFolder, $strNewName);
     // Update the database AFTER the folder has been renamed
     $syncSource = \Dbafs::shouldBeSynchronized($this->strFolder);
     $syncTarget = \Dbafs::shouldBeSynchronized($strNewName);
     // Synchronize the database
     if ($syncSource && $syncTarget) {
         $this->objModel = \Dbafs::moveResource($this->strFolder, $strNewName);
     } elseif ($syncSource) {
         $this->objModel = \Dbafs::deleteResource($this->strFolder);
     } elseif ($syncTarget) {
         $this->objModel = \Dbafs::addResource($strNewName);
     }
     // Reset the object AFTER the database has been updated
     if ($return != false) {
         $this->strFolder = $strNewName;
     }
     return $return;
 }
示例#4
0
 /**
  * Move temp files. If DBAFS support is enabled add entries to the dbafs.
  *
  * @CtoCommunication Enable
  *
  * @param  array   $arrFileList List with files for moving.
  *
  * @param  boolean $blnIsDbafs  Flag if we have to change the dbafs system.
  *
  * @return array The list with some more information about the moving of the file.
  */
 public function moveTempFile($arrFileList, $blnIsDbafs)
 {
     foreach ($arrFileList as $key => $value) {
         try {
             $blnMovedFile = false;
             $strTempFile = $this->objSyncCtoHelper->standardizePath($GLOBALS['SYC_PATH']['tmp'], "sync", $value["path"]);
             // Check if the tmp file exists.
             if (!file_exists(TL_ROOT . DIRECTORY_SEPARATOR . $strTempFile)) {
                 $arrFileList[$key]['saved'] = false;
                 $arrFileList[$key]['error'] = sprintf($GLOBALS['TL_LANG']['ERR']['unknown_file'], $strTempFile);
                 $arrFileList[$key]['skipreasons'] = $GLOBALS['TL_LANG']['ERR']['missing_file_information'];
                 continue;
             }
             // Generate the folder if not already there.
             $strFolderPath = dirname($value["path"]);
             if ($strFolderPath != ".") {
                 $objFolder = new Folder($strFolderPath);
                 unset($objFolder);
             }
             // Build folders.
             $strFileSource = $this->objSyncCtoHelper->standardizePath($GLOBALS['SYC_PATH']['tmp'], "sync", $value["path"]);
             $strFileDestination = $this->objSyncCtoHelper->standardizePath($value["path"]);
             // DBAFS support. Check if we have the file already in the locale dbafs system.
             if ($blnIsDbafs) {
                 // Get the information from the dbafs.
                 /**  @var \Model $objLocaleData */
                 $objLocaleData = \FilesModel::findByPath($strFileDestination);
                 // If we have no entry in the dbafs just overwrite the current file and add the entry to the dbafs.
                 if ($objLocaleData == null) {
                     // Move file.
                     $blnMovedFile = $this->objFiles->copy($strFileSource, $strFileDestination);
                     // If success add file to the database.
                     if ($blnMovedFile) {
                         // First add it to the dbafs.
                         $objLocaleData = \Dbafs::addResource($strFileDestination);
                         // PHP 7 compatibility
                         // See #309 (https://github.com/contao/core-bundle/issues/309)
                         if (version_compare(VERSION . '.' . BUILD, '3.5.5', '>=')) {
                             $objLocaleData->uuid = \StringUtil::uuidToBin($value['tl_files']['uuid']);
                         } else {
                             $objLocaleData->uuid = \String::uuidToBin($value['tl_files']['uuid']);
                         }
                         $objLocaleData->meta = $value['tl_files']['meta'];
                         $objLocaleData->save();
                         // Add a status report for debugging and co.
                         $arrFileList[$key]['dbafs']['msg'] = 'Moved file and add to database.';
                         $arrFileList[$key]['dbafs']['state'] = SyncCtoEnum::DBAFS_CREATE;
                     }
                 } else {
                     // PHP 7 compatibility
                     // See #309 (https://github.com/contao/core-bundle/issues/309)
                     if (version_compare(VERSION . '.' . BUILD, '3.5.5', '>=')) {
                         // Get the readable UUID for the work.
                         $strLocaleUUID = \StringUtil::binToUuid($objLocaleData->uuid);
                     } else {
                         // Get the readable UUID for the work.
                         $strLocaleUUID = \String::binToUuid($objLocaleData->uuid);
                     }
                     // Okay it seems we have already a file with this values.
                     if ($strLocaleUUID == $value['tl_files']['uuid']) {
                         // Move file.
                         $blnMovedFile = $this->objFiles->copy($strFileSource, $strFileDestination);
                         // If success add file to the database.
                         if ($blnMovedFile) {
                             $objLocaleData->hash = $value['checksum'];
                             $objLocaleData->meta = $value['tl_files']['meta'];
                             $objLocaleData->save();
                             // Add a status report for debugging and co.
                             $arrFileList[$key]['dbafs']['msg'] = 'UUID same no problem found. Update database with new hash.';
                             $arrFileList[$key]['dbafs']['state'] = SyncCtoEnum::DBAFS_SAME;
                         }
                     } elseif ($strLocaleUUID != $value['tl_files']['uuid']) {
                         // Get information about the current file information.
                         $arrDestinationInformation = pathinfo($strFileDestination);
                         // Try to rename it to _1 or _2 and so on.
                         $strNewDestinationName = null;
                         $intFileNumber = 1;
                         for ($i = 1; $i < 100; $i++) {
                             $strNewDestinationName = sprintf('%s' . DIRECTORY_SEPARATOR . '%s_%s.%s', $arrDestinationInformation['dirname'], $arrDestinationInformation['filename'], $i, $arrDestinationInformation['extension']);
                             if (!file_exists(TL_ROOT . DIRECTORY_SEPARATOR . $strNewDestinationName)) {
                                 $intFileNumber = $i;
                                 break;
                             }
                         }
                         // Move the current file to another name, that we have space for the new one.
                         $this->objFiles->copy($strFileDestination, $strNewDestinationName);
                         $objRenamedLocaleData = \Dbafs::moveResource($strFileDestination, $strNewDestinationName);
                         // Move the tmp file.
                         $blnMovedFile = $this->objFiles->copy($strFileSource, $strFileDestination);
                         // If success add file to the database.
                         if ($blnMovedFile) {
                             // First add it to the dbafs.
                             $objLocaleData = \Dbafs::addResource($strFileDestination);
                             // PHP 7 compatibility
                             // See #309 (https://github.com/contao/core-bundle/issues/309)
                             if (version_compare(VERSION . '.' . BUILD, '3.5.5', '>=')) {
                                 $objLocaleData->uuid = \StringUtil::uuidToBin($value['tl_files']['uuid']);
                             } else {
                                 $objLocaleData->uuid = \String::uuidToBin($value['tl_files']['uuid']);
                             }
                             $objLocaleData->meta = $value['tl_files']['meta'];
                             $objLocaleData->save();
                             // Add a status report for debugging and co.
                             $arrFileList[$key]['dbafs']['msg'] = $GLOBALS['TL_LANG']['ERR']['dbafs_uuid_conflict'];
                             $arrFileList[$key]['dbafs']['error'] = sprintf($GLOBALS['TL_LANG']['ERR']['dbafs_uuid_conflict_rename'], $intFileNumber);
                             $arrFileList[$key]['dbafs']['rename'] = $strNewDestinationName;
                             $arrFileList[$key]['dbafs']['state'] = SyncCtoEnum::DBAFS_CONFLICT;
                         }
                     }
                 }
             } else {
                 $blnMovedFile = $this->objFiles->copy($strFileSource, $strFileDestination);
             }
             // Check the state at moving and add a msg to the return array.
             if ($blnMovedFile) {
                 $arrFileList[$key]['saved'] = true;
                 $arrFileList[$key]['transmission'] = SyncCtoEnum::FILETRANS_MOVED;
             } else {
                 $arrFileList[$key]['saved'] = false;
                 $arrFileList[$key]['error'] = sprintf($GLOBALS['TL_LANG']['ERR']['cant_move_file'], $strFileSource, $strFileDestination);
                 $arrFileList[$key]['transmission'] = SyncCtoEnum::FILETRANS_SKIPPED;
                 $arrFileList[$key]['skipreason'] = $GLOBALS['TL_LANG']['ERR']['cant_move_files'];
             }
         } catch (Exception $e) {
             $arrFileList[$key]['saved'] = false;
             $arrFileList[$key]['error'] = sprintf('Can not move file - %s. Exception message: %s', $value["path"], $e->getMessage());
             $arrFileList[$key]['transmission'] = SyncCtoEnum::FILETRANS_SKIPPED;
             $arrFileList[$key]['skipreason'] = $GLOBALS['TL_LANG']['ERR']['cant_move_files'];
         }
     }
     return $arrFileList;
 }
 /**
  * {@inheritDoc}
  */
 public function rename($pathOrUuid, $newpath)
 {
     $path = $this->convertToPath($pathOrUuid);
     $this->guardNoInvalidUuid($pathOrUuid, $path);
     $this->guardInUploadPath($path);
     $this->guardInUploadPath($newpath);
     if ($this->adapter->rename($path, $newpath)) {
         \Dbafs::moveResource($path, $newpath);
         return true;
     }
     return false;
 }