/** * Add node as a custom field * * @param SimpleXMLElement $sxeNode * @param string $nodeId * @param string $customFieldNamePrefix * @param array $customFieldSpecs * @param LexiconFieldListConfigObj $levelConfig * @param LexEntryModel|Sense|Example $item */ private function addCustomField($sxeNode, $nodeId, $customFieldNamePrefix, $levelConfig, $item) { $fieldType = FileUtilities::replaceSpecialCharacters($nodeId); $customFieldSpecs = $this->getCustomFieldSpecs($fieldType); $customFieldName = $this->createCustomField($nodeId, $fieldType, $customFieldNamePrefix, $customFieldSpecs, $levelConfig); if ($customFieldSpecs['Type'] == 'ReferenceAtom') { $item->customFields[$customFieldName] = new LexiconField(); $item->customFields[$customFieldName]->value = (string) $sxeNode['value']; } elseif ($customFieldSpecs['Type'] == 'ReferenceCollection') { if (!array_key_exists($customFieldName, $item->customFields)) { $item->customFields[$customFieldName] = new LexiconMultiValueField(); } $item->customFields[$customFieldName]->value((string) $sxeNode['value']); } elseif ($customFieldSpecs['Type'] == 'OwningAtom') { $multiText = $this->readMultiText($sxeNode, $levelConfig->fields[$customFieldName]->inputSystems); $item->customFields[$customFieldName] = self::convertMultiParaMultiText($multiText); } else { $item->customFields[$customFieldName] = $this->readMultiText($sxeNode, $levelConfig->fields[$customFieldName]->inputSystems); } }
/** * Import a LIFT file * * @param string $projectId * @param string $mediaType * @param string $tmpFilePath * @throws \Exception * @return \Api\Model\Shared\Command\UploadResponse */ public static function importLiftFile($projectId, $mediaType, $tmpFilePath) { if ($mediaType != 'import-lift') { throw new \Exception("Unsupported upload type."); } if (!$tmpFilePath) { throw new \Exception("Upload controller did not move the uploaded file."); } $file = $_FILES['file']; $fileName = $file['name']; $mergeRule = $_POST['mergeRule']; $skipSameModTime = $_POST['skipSameModTime']; $deleteMatchingEntry = $_POST['deleteMatchingEntry']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $fileType = finfo_file($finfo, $tmpFilePath); finfo_close($finfo); $fileName = FileUtilities::replaceSpecialCharacters($fileName); $fileExt = false === ($pos = strrpos($fileName, '.')) ? '' : substr($fileName, $pos); $allowedTypes = array("text/xml", "application/xml"); $response = new UploadResponse(); if (in_array(strtolower($fileType), $allowedTypes) && in_array(strtolower($fileExt), self::$allowedLiftExtensions)) { // make the folders if they don't exist $project = new LexiconProjectModel($projectId); $folderPath = $project->getAssetsFolderPath(); FileUtilities::createAllFolders($folderPath); $importer = LiftImport::get()->merge($tmpFilePath, $project, $mergeRule, $skipSameModTime, $deleteMatchingEntry); $project->write(); $moveOk = true; if (!$project->liftFilePath || $mergeRule != LiftMergeRule::IMPORT_LOSES) { // cleanup previous files of any allowed extension $cleanupFiles = glob($folderPath . '/*[' . implode(', ', self::$allowedLiftExtensions) . ']'); foreach ($cleanupFiles as $cleanupFile) { @unlink($cleanupFile); } // move uploaded LIFT file from tmp location to assets $filePath = $folderPath . '/' . $fileName; $project->liftFilePath = $filePath; $project->write(); $moveOk = copy($tmpFilePath, $filePath); @unlink($tmpFilePath); } // construct server response if ($moveOk && $tmpFilePath) { $data = new ImportResult(); $data->path = $project->getAssetsRelativePath(); $data->fileName = $fileName; $data->stats = $importer->stats; $data->importErrors = $importer->getReport()->toFormattedString(); $response->result = true; } else { $data = new ErrorResult(); $data->errorType = 'UserMessage'; $data->errorMessage = "{$fileName} could not be saved to the right location. Contact your Site Administrator."; $response->result = false; } } else { $allowedExtensionsStr = implode(", ", self::$allowedLiftExtensions); $data = new ErrorResult(); $data->errorType = 'UserMessage'; if (count(self::$allowedLiftExtensions) < 1) { $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. No LIFT file formats are currently enabled, contact your Site Administrator."; } elseif (count(self::$allowedLiftExtensions) == 1) { $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. Ensure the file is a {$allowedExtensionsStr}."; } else { $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. Ensure the file is one of the following types: {$allowedExtensionsStr}."; } $response->result = false; } $response->data = $data; return $response; }
/** * Upload a file * * @param string $projectId * @param string $mediaType * @param string $tmpFilePath * @throws \Exception * @return UploadResponse */ public static function uploadFile($projectId, $mediaType, $tmpFilePath) { if ($mediaType != 'audio') { throw new \Exception("Unsupported upload type: {$mediaType}"); } if (!$tmpFilePath) { throw new \Exception("Upload controller did not move the uploaded file."); } $textId = $_POST['textId']; $file = $_FILES['file']; $fileName = $file['name']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $fileType = finfo_file($finfo, $tmpFilePath); finfo_close($finfo); $fileName = FileUtilities::replaceSpecialCharacters($fileName); $fileExt = false === ($pos = strrpos($fileName, '.')) ? '' : substr($fileName, $pos); // allowed types: documented, observed $allowedTypes = array("audio/mpeg", "audio/mp3"); $allowedExtensions = array(".mp3"); $response = new UploadResponse(); if (in_array(strtolower($fileType), $allowedTypes) && in_array(strtolower($fileExt), $allowedExtensions)) { // make the folders if they don't exist $project = new SfchecksProjectModel($projectId); ProjectCommands::checkIfArchivedAndThrow($project); $folderPath = $project->getAssetsFolderPath(); FileUtilities::createAllFolders($folderPath); // cleanup previous files of any allowed extension self::cleanupFiles($folderPath, $textId, $allowedExtensions); // move uploaded file from tmp location to assets $filePath = self::mediaFilePath($folderPath, $textId, $fileName); $moveOk = copy($tmpFilePath, $filePath); @unlink($tmpFilePath); // update database with file location $text = new TextModel($project, $textId); $text->audioFileName = ''; if ($moveOk) { $text->audioFileName = $fileName; } $text->write(); // construct server response if ($moveOk && $tmpFilePath) { $data = new MediaResult(); $data->path = $project->getAssetsRelativePath(); $data->fileName = $fileName; $response->result = true; } else { $data = new ErrorResult(); $data->errorType = 'UserMessage'; $data->errorMessage = "{$fileName} could not be saved to the right location. Contact your Site Administrator."; $response->result = false; } } else { $allowedExtensionsStr = implode(", ", $allowedExtensions); $data = new ErrorResult(); $data->errorType = 'UserMessage'; if (count($allowedExtensions) < 1) { $data->errorMessage = "{$fileName} is not an allowed audio file. No audio file formats are currently enabled, contact your Site Administrator."; } elseif (count($allowedExtensions) == 1) { $data->errorMessage = "{$fileName} is not an allowed audio file. Ensure the file is a {$allowedExtensionsStr}."; } else { $data->errorMessage = "{$fileName} is not an allowed audio file. Ensure the file is one of the following types: {$allowedExtensionsStr}."; } $response->result = false; } $response->data = $data; return $response; }