Example #1
0
 /**
  * Autorename file if previous name is already taken
  *
  * @param string $filePath
  * @param string $fileName
  * @param string $sFileNameOrginal
  */
 public static function autoRename($filePath, $fileName)
 {
     $sFileNameOrginal = $fileName;
     $iCounter = 0;
     while (true) {
         $sFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($filePath, $fileName);
         if (file_exists($sFilePath)) {
             $iCounter++;
             $fileName = CKFinder_Connector_Utils_FileSystem::getFileNameWithoutextension($sFileNameOrginal, false) . "(" . $iCounter . ")" . "." . CKFinder_Connector_Utils_FileSystem::getextension($sFileNameOrginal, false);
         } else {
             break;
         }
     }
     return $fileName;
 }
Example #2
0
 /**
  * handle request and build XML
  * @access protected
  *
  */
 function buildXml()
 {
     if (empty($_POST['CKFinderCommand']) || $_POST['CKFinderCommand'] != 'true') {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
     }
     $this->checkConnector();
     $this->checkRequest();
     //resizing to 1x1 is almost equal to deleting a file, that's why FILE_DELETE permissions are required
     if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_DELETE) || !$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_UPLOAD)) {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
     }
     $_config =& CKFinder_Connector_core_Factory::getInstance("core_config");
     $resourceTypeInfo = $this->_currentFolder->getResourceTypeconfig();
     if (!isset($_POST["fileName"])) {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
     }
     $fileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_POST["fileName"]);
     if (!CKFinder_Connector_Utils_FileSystem::checkFileName($fileName) || $resourceTypeInfo->checkIsHiddenFile($fileName)) {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
     }
     if (!$resourceTypeInfo->checkextension($fileName, false)) {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
     }
     $filePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $fileName);
     if (!file_exists($filePath) || !is_file($filePath)) {
         $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND);
     }
     $newWidth = trim($_POST['width']);
     $newHeight = trim($_POST['height']);
     $quality = 80;
     $resizeOriginal = !empty($_POST['width']) && !empty($_POST['height']);
     if ($resizeOriginal) {
         if (!preg_match("/^\\d+\$/", $newWidth) || !preg_match("/^\\d+\$/", $newHeight) || !preg_match("/^\\d+\$/", $newWidth)) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
         }
         if (!isset($_POST["newFileName"])) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
         }
         $newFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_POST["newFileName"]);
         if (!$resourceTypeInfo->checkextension($newFileName)) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
         }
         if (!CKFinder_Connector_Utils_FileSystem::checkFileName($newFileName) || $resourceTypeInfo->checkIsHiddenFile($newFileName)) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
         }
         $newFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $newFileName);
         if (!is_writable(dirname($newFilePath))) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
         }
         if ($_POST['overwrite'] != "1" && file_exists($newFilePath)) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ALREADY_EXIST);
         }
         $_imagesconfig = $_config->getImagesconfig();
         $maxWidth = $_imagesconfig->getMaxWidth();
         $maxHeight = $_imagesconfig->getMaxHeight();
         // Shouldn't happen as the JavaScript validation should not allow this.
         if ($maxWidth > 0 && $newWidth > $maxWidth || $maxHeight > 0 && $newHeight > $maxHeight) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
         }
     }
     require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/Thumbnail.php";
     if ($resizeOriginal) {
         $result = CKFinder_Connector_CommandHandler_Thumbnail::createThumb($filePath, $newFilePath, $newWidth, $newHeight, $quality, false);
         if (!$result) {
             $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
         }
     }
     $config = $this->getconfig();
     $nameWithoutExt = preg_replace("/^(.+)\\_\\d+x\\d+\$/", "\$1", CKFinder_Connector_Utils_FileSystem::getFileNameWithoutextension($fileName));
     $extension = CKFinder_Connector_Utils_FileSystem::getextension($fileName);
     foreach (array('small', 'medium', 'large') as $size) {
         if (!empty($_POST[$size]) && $_POST[$size] == '1') {
             $thumbName = $nameWithoutExt . "_" . $size . "." . $extension;
             $newFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $thumbName);
             if (!empty($config[$size . 'Thumb'])) {
                 if (preg_match("/^(\\d+)x(\\d+)\$/", $config[$size . 'Thumb'], $matches)) {
                     CKFinder_Connector_CommandHandler_Thumbnail::createThumb($filePath, $newFilePath, $matches[1], $matches[2], $quality, true);
                 }
             }
         }
     }
 }