protected function handleExistingDropFolderFile(KalturaDropFolderFile $dropFolderFile)
 {
     try {
         $fullPath = $this->dropFolder->path . '/' . $dropFolderFile->fileName;
         $lastModificationTime = $this->fileTransferMgr->modificationTime($fullPath);
         $fileSize = $this->fileTransferMgr->fileSize($fullPath);
     } catch (Exception $e) {
         $closedStatuses = array(KalturaDropFolderFileStatus::HANDLED, KalturaDropFolderFileStatus::PURGED, KalturaDropFolderFileStatus::DELETED);
         //In cases drop folder is not configured with auto delete we want to verify that the status file is not in one of the closed statuses so
         //we won't update it to error status
         if (!in_array($dropFolderFile->status, $closedStatuses)) {
             //Currently "modificationTime" does not throw Exception since from php documentation not all servers support the ftp_mdtm feature
             KalturaLog::err('Failed to get modification time or file size for file [' . $fullPath . ']');
             $this->handleFileError($dropFolderFile->id, KalturaDropFolderFileStatus::ERROR_HANDLING, KalturaDropFolderFileErrorCode::ERROR_READING_FILE, DropFolderPlugin::ERROR_READING_FILE_MESSAGE . '[' . $fullPath . ']', $e);
         }
         return false;
     }
     if ($dropFolderFile->status == KalturaDropFolderFileStatus::UPLOADING) {
         $this->handleUploadingDropFolderFile($dropFolderFile, $fileSize, $lastModificationTime);
     } else {
         KalturaLog::info('Last modification time [' . $lastModificationTime . '] known last modification time [' . $dropFolderFile->lastModificationTime . ']');
         $isLastModificationTimeUpdated = $dropFolderFile->lastModificationTime && $dropFolderFile->lastModificationTime != '' && $lastModificationTime > $dropFolderFile->lastModificationTime;
         if ($isLastModificationTimeUpdated) {
             $this->handleFileAdded($dropFolderFile->fileName, $fileSize, $lastModificationTime);
         } else {
             $deleteTime = $dropFolderFile->updatedAt + $this->dropFolder->autoFileDeleteDays * 86400;
             if ($dropFolderFile->status == KalturaDropFolderFileStatus::HANDLED && $this->dropFolder->fileDeletePolicy != KalturaDropFolderFileDeletePolicy::MANUAL_DELETE && time() > $deleteTime || $dropFolderFile->status == KalturaDropFolderFileStatus::DELETED) {
                 $this->purgeFile($dropFolderFile);
             }
         }
     }
 }
 private function validateFileSize(SimpleXMLElement $elementToSearchIn, $filePath)
 {
     if (isset($elementToSearchIn->dropFolderFileContentResource->fileSize)) {
         $fileSize = $this->fileTransferMgr->fileSize($filePath);
         $xmlFileSize = (int) $elementToSearchIn->dropFolderFileContentResource->fileSize;
         if ($xmlFileSize != $fileSize) {
             throw new KalturaBulkUploadXmlException("File size is invalid for file [{$filePath}], Xml size [{$xmlFileSize}], actual size [{$fileSize}]", KalturaBatchJobAppErrors::BULK_ITEM_VALIDATION_FAILED);
         }
     }
 }
 private function verifyLocalResource(DOMElement $localResource)
 {
     $filePath = $localResource->getAttribute(self::DROP_FOLDER_RESOURCE_PATH_ATTRIBUTE);
     $dropFolderPath = $this->dropFolder->path . '/' . $filePath;
     if ($this->dropFolder->type == KalturaDropFolderType::LOCAL) {
         $dropFolderPath = realpath($dropFolderPath);
     }
     if (!$dropFolderPath) {
         $this->dropFolderFile->errorCode = KalturaDropFolderFileErrorCode::ERROR_READING_FILE;
         $this->dropFolderFile->errorDescription = "Cannot find file at [{$dropFolderPath}]";
         return false;
     }
     $fileSize = $localResource->getElementsByTagName(self::DROP_FOLDER_RESOURCE_FILE_SIZE_PARAM);
     $fileSize = $fileSize->length > 0 ? $fileSize->item(0)->nodeValue : null;
     if (!is_null($fileSize)) {
         $realSize = $this->fileTransferMgr->fileSize($dropFolderPath);
         if ($fileSize != $realSize) {
             $this->dropFolderFile->errorCode = KalturaDropFolderFileErrorCode::LOCAL_FILE_WRONG_SIZE;
             $this->dropFolderFile->errorDescription = "Wrong filesize [{$realSize}] for file [{$dropFolderPath}]";
             return false;
         }
         KalturaLog::debug("Filesize [{$fileSize}] verified for local resource [{$filePath}]");
     }
     $fileChecksumTags = $localResource->getElementsByTagName(self::DROP_FOLDER_RESOURCE_FILE_CHECKSUM_PARAM);
     $fileChecksum = $fileChecksumTags->length > 0 ? (string) $fileChecksumTags->item(0)->nodeValue : null;
     if ($this->dropFolder->type != KalturaDropFolderType::LOCAL) {
         //TODO: ok not to support for remote drop folders ?
         KalturaLog::debug('Checksum verification is only supported for local drop folders');
     } else {
         if (!is_null($fileChecksum)) {
             $checksumType = $fileChecksumTags->item(0)->getAttribute('type');
             if ($checksumType == 'sha1') {
                 $localChecksum = sha1_file($dropFolderPath);
             } else {
                 $localChecksum = md5_file($dropFolderPath);
             }
             if ($fileChecksum != $localChecksum) {
                 $this->dropFolderFile->errorCode = KalturaDropFolderFileErrorCode::LOCAL_FILE_WRONG_CHECKSUM;
                 $this->dropFolderFile->errorDescription = "Wrong checksum [{$localChecksum}] for file [{$dropFolderPath}]";
                 return false;
             }
             KalturaLog::debug("Checksum [{$fileChecksum}] verified for local resource [{$filePath}]");
         }
     }
     return true;
 }
 /**
  * Tests kFileTransferMgr->fileSize()
  * 
  * @param string $server Server's hostname or IP address
  * @param string $user User's name
  * @param string $pass Password
  * @param int $port Server's listening port
  * @param bool $ftp_passive_mode Used for FTP only
  * @param string $publicKeyFile
  * @param string $privateKeyFile
  * @param string $passPhrase
  * @param string $remote_path
  * @param string $file
  * @param int $expectedSize
  * @param int $exceptionCode the expected exception code
  * @dataProvider provideData
  */
 public function testFileSize($server, $user, $pass, $port, $ftp_passive_mode, $publicKeyFile, $privateKeyFile, $passPhrase, $remote_path, $file, $expectedSize, $exceptionCode = null)
 {
     try {
         if (strlen(trim($publicKeyFile))) {
             $this->kFileTransferMgr->loginPubKey($server, $user, $publicKeyFile, $privateKeyFile, $passPhrase, $port);
         } else {
             $this->kFileTransferMgr->login($server, $user, $pass, $port, $ftp_passive_mode);
         }
         $actualSize = $this->kFileTransferMgr->fileSize("{$remote_path}/{$file}");
         $this->assertEquals($expectedSize, $actualSize, "Wrong file size [{$remote_path}/{$file}]");
     } catch (kFileTransferMgrException $te) {
         $this->assertEquals($exceptionCode, $te->getCode(), "Wrong transfer exception code [" . $te->getMessage() . "]");
         return;
     } catch (Exception $e) {
         $this->assertEquals($exceptionCode, $e->getCode(), "Wrong exception code [" . $e->getMessage() . "]");
         return;
     }
     if ($exceptionCode) {
         $this->fail("Expected exception code [{$exceptionCode}]");
     }
 }
 /**
  * @param string $filePath
  * @return int file size of the given $filePath
  */
 private function getFileSize($filePath)
 {
     return $this->fileTransferMgr->fileSize($filePath);
 }