public static function validateXsdData($xsdData, &$errorMessage) { // validates the xsd libxml_use_internal_errors(true); libxml_clear_errors(); $xml = new KDOMDocument(); if (!$xml->loadXML($xsdData)) { $errorMessage = kXml::getLibXmlErrorDescription($xsdData); return false; } libxml_clear_errors(); libxml_use_internal_errors(false); return true; }
public function doTest($xmlPath, $type) { echo "\tTesting File [{$xmlPath}]\n"; $serviceUrl = $this->client->getConfig()->serviceUrl; $xsdPath = "{$serviceUrl}/api_v3/index.php/service/schema/action/serve/type/{$type}"; // libxml_use_internal_errors(true); // libxml_clear_errors(); $doc = new DOMDocument(); $doc->Load($xmlPath); //Validate the XML file against the schema if (!$doc->schemaValidate($xsdPath)) { $description = kXml::getLibXmlErrorDescription(file_get_contents($xmlPath)); $this->fail("Type [{$type}] File [{$xmlPath}]: {$description}"); } }
/** * Allows you to add a metadata profile object and metadata profile file associated with Kaltura object type * * @action addFromFile * @param KalturaMetadataProfile $metadataProfile * @param file $xsdFile XSD metadata definition * @param file $viewsFile UI views definition * @return KalturaMetadataProfile * @throws MetadataErrors::METADATA_FILE_NOT_FOUND */ function addFromFileAction(KalturaMetadataProfile $metadataProfile, $xsdFile, $viewsFile = null) { $filePath = $xsdFile['tmp_name']; if (!file_exists($filePath)) { throw new KalturaAPIException(MetadataErrors::METADATA_FILE_NOT_FOUND, $xsdFile['name']); } // validates the xsd libxml_use_internal_errors(true); libxml_clear_errors(); $xml = new KDOMDocument(); if (!$xml->load($filePath)) { $errorMessage = kXml::getLibXmlErrorDescription(file_get_contents($xsdFile)); throw new KalturaAPIException(MetadataErrors::INVALID_METADATA_PROFILE_SCHEMA, $errorMessage); } libxml_clear_errors(); libxml_use_internal_errors(false); // must be validatebefore checking available searchable fields count $metadataProfile->validatePropertyNotNull('metadataObjectType'); kMetadataManager::validateMetadataProfileField($this->getPartnerId(), $xsdFile, false, $metadataProfile->metadataObjectType); $dbMetadataProfile = $metadataProfile->toInsertableObject(); $dbMetadataProfile->setStatus(KalturaMetadataProfileStatus::ACTIVE); $dbMetadataProfile->setPartnerId($this->getPartnerId()); $dbMetadataProfile->save(); $key = $dbMetadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); kFileSyncUtils::moveFromFile($filePath, $key); if ($viewsFile && $viewsFile['size']) { $filePath = $viewsFile['tmp_name']; if (!file_exists($filePath)) { throw new KalturaAPIException(MetadataErrors::METADATA_FILE_NOT_FOUND, $viewsFile['name']); } $key = $dbMetadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_VIEWS); kFileSyncUtils::moveFromFile($filePath, $key); } kMetadataManager::parseProfileSearchFields($this->getPartnerId(), $dbMetadataProfile); $metadataProfile = new KalturaMetadataProfile(); $metadataProfile->fromObject($dbMetadataProfile); return $metadataProfile; }
/** * Transform Xml file with conversion profile xsl * If xsl is not found, original Xml returned * @param string $filePath the original xml that was taken from partner file path * @return string - transformed Xml */ protected function xslTransform($filePath) { $xdoc = file_get_contents($filePath); if (is_null($xdoc) || is_null($this->conversionProfileXsl)) { return $xdoc; } libxml_clear_errors(); $xml = new KDOMDocument(); if (!$xml->loadXML($xdoc)) { KalturaLog::debug("Could not load xml"); $errorMessage = kXml::getLibXmlErrorDescription($xdoc); throw new KalturaBatchException("Could not load xml [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } libxml_clear_errors(); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(kXml::getXslEnabledPhpFunctions()); $xsl = new KDOMDocument(); if (!$xsl->loadXML($this->conversionProfileXsl)) { KalturaLog::debug("Could not load xsl" . $this->conversionProfileXsl); $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl); throw new KalturaBatchException("Could not load xsl [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } $proc->importStyleSheet($xsl); libxml_clear_errors(); $transformedXml = $proc->transformToXML($xml); if (!$transformedXml) { KalturaLog::debug("Could not transform xml" . $this->conversionProfileXsl); $errorMessage = kXml::getLibXmlErrorDescription($this->conversionProfileXsl); throw new KalturaBatchException("Could not transform xml [{$this->job->id}], {$errorMessage}", KalturaBatchJobAppErrors::BULK_VALIDATION_FAILED); } return $transformedXml; }
/** * @param array<CuePoint> $cuePoints * @return string xml */ public static function generateXml(array $cuePoints) { $schemaType = CuePointPlugin::getApiValue(CuePointSchemaType::SERVE_API); $xsdUrl = "http://" . kConf::get('cdn_host') . "/api_v3/service/schema/action/serve/type/{$schemaType}"; $scenes = new SimpleXMLElement('<scenes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="' . $xsdUrl . '" />'); $pluginInstances = KalturaPluginManager::getPluginInstances('IKalturaCuePointXmlParser'); foreach ($cuePoints as $cuePoint) { $scene = null; foreach ($pluginInstances as $pluginInstance) { $scene = $pluginInstance->generateXml($cuePoint, $scenes, $scene); } } $xmlContent = $scenes->asXML(); $xml = new KDOMDocument(); libxml_use_internal_errors(true); libxml_clear_errors(); if (!$xml->loadXML($xmlContent)) { $errorMessage = kXml::getLibXmlErrorDescription($xmlContent); throw new kCuePointException("XML is invalid:\n{$errorMessage}", kCuePointException::XML_INVALID); } $xsdPath = SchemaService::getSchemaPath($schemaType); libxml_clear_errors(); if (!$xml->schemaValidate($xsdPath)) { $errorMessage = kXml::getLibXmlErrorDescription($xmlContent); throw new kCuePointException("XML is invalid:\n{$errorMessage}", kCuePointException::XML_INVALID); } return $xmlContent; }
/** * Validate the XML against the profile XSD and set the metadata status * * @param int $metadataProfileId * @param string $metadata * @param string $errorMessage * @param int $compareAgainstPreviousVersion leave it false to use the latest metadata profile version * * returns bool */ public static function validateMetadata($metadataProfileId, $metadata, &$errorMessage, $compareAgainstPreviousVersion = false) { KalturaLog::debug("Validating metadata [{$metadata}]"); $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); if (!$metadataProfile) { $errorMessage = "Metadata profile [{$metadataProfileId}] not found"; KalturaLog::err($errorMessage); return false; } $metadataProfileFSVersion = null; if ($compareAgainstPreviousVersion) { $metadataProfileFSVersion = $metadataProfile->getPreviousFileSyncVersion(); } $metadataProfileKey = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION, $metadataProfileFSVersion); $xsdData = kFileSyncUtils::file_get_contents($metadataProfileKey, true, false); if (!$xsdData) { $errorMessage = "Metadata profile xsd not found"; KalturaLog::err($errorMessage); return false; } libxml_use_internal_errors(true); libxml_clear_errors(); $xml = new KDOMDocument(); $xml->loadXML($metadata); if ($xml->schemaValidateSource($xsdData)) { if (!self::validateMetadataObjects($metadataProfileId, $xml, $errorMessage)) { KalturaLog::err($errorMessage); return false; } KalturaLog::debug("Metadata is valid"); return true; } $errorMessage = kXml::getLibXmlErrorDescription($metadata); KalturaLog::err("Metadata is invalid:\n{$errorMessage}"); return false; }
protected function getOriginalOrTransformIfNeeded(DropFolder $folder, $xmlPath) { if (!file_exists($xmlPath) || !filesize($xmlPath)) { throw new Exception('Empty file supplied as input'); } if (!$folder->getConversionProfileId()) { KalturaLog::debug('No conversion profile found on drop folder [' . $folder->getId() . '] assuming no xsl transformation is needed'); return file_get_contents($xmlPath); } $conversionProfile = conversionProfile2Peer::retrieveByPK($folder->getConversionProfileId()); if (!$conversionProfile || strlen($conversionProfile->getXsl()) == 0) { KalturaLog::debug('No conversion profile found Or no xsl transform found'); return file_get_contents($xmlPath); } $originalXmlDoc = file_get_contents($xmlPath); $origianlXml = new KDOMDocument(); if (!$origianlXml->loadXML($originalXmlDoc)) { KalturaLog::debug('Could not load original xml'); $errorMessage = kXml::getLibXmlErrorDescription($originalXmlDoc); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } libxml_clear_errors(); $proc = new XSLTProcessor(); $xsl = new KDOMDocument(); if (!$xsl->loadXML($conversionProfile->getXsl())) { KalturaLog::debug('Could not load xsl ' . $conversionProfile->getXsl()); $errorMessage = kXml::getLibXmlErrorDescription($conversionProfile->getXsl()); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } $proc->importStyleSheet($xsl); libxml_clear_errors(); $transformedXml = $proc->transformToXML($origianlXml); if (!$transformedXml) { KalturaLog::debug('Could not transform xml ' . $conversionProfile->getXsl()); $errorMessage = kXml::getLibXmlErrorDescription($conversionProfile->getXsl()); throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } $xmlDoc = new KDOMDocument(); $res = $xmlDoc->loadXML($transformedXml); if (!$res) { throw new Exception(DropFolderXmlBulkUploadPlugin::MALFORMED_XML_FILE_MESSAGE, DropFolderXmlBulkUploadPlugin::getErrorCodeCoreValue(DropFolderXmlBulkUploadErrorCode::MALFORMED_XML_FILE)); } return $transformedXml; }
/** * Validate the XML against the profile XSD and set the metadata status * * @param Metadata $metadata * * returns metadata status */ public static function validateMetadata(Metadata $metadata, &$errorMessage) { KalturaLog::debug('Validating metadata [' . $metadata->getId() . ']'); $metadataProfile = $metadata->getMetadataProfile(); if (!$metadataProfile) { $errorMessage = 'Metadata profile [' . $metadata->getMetadataProfileId() . '] not found'; KalturaLog::err($errorMessage); return self::setMetadataStatus($metadata, Metadata::STATUS_INVALID); } $metadataKey = $metadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); $xmlPath = kFileSyncUtils::getLocalFilePathForKey($metadataKey); if (!file_exists($xmlPath)) { $errorMessage = "Metadata xml [{$xmlPath}] not found"; KalturaLog::err($errorMessage); return self::setMetadataStatus($metadata, Metadata::STATUS_INVALID); } $metadataProfileKey = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); $xsdPath = kFileSyncUtils::getLocalFilePathForKey($metadataProfileKey); if (!file_exists($xsdPath)) { $errorMessage = "Metadata profile xsd [{$xsdPath}] not found"; KalturaLog::err($errorMessage); return self::setMetadataStatus($metadata, Metadata::STATUS_INVALID); } libxml_use_internal_errors(true); libxml_clear_errors(); $xml = new DOMDocument(); $xml->load($xmlPath); if ($xml->schemaValidate($xsdPath)) { KalturaLog::debug("Metadata is valid"); return self::setMetadataStatus($metadata, Metadata::STATUS_VALID, $metadataProfile->getVersion()); } $errorMessage = kXml::getLibXmlErrorDescription(file_get_contents($xmlPath)); KalturaLog::debug("Metadata is invalid:\n{$errorMessage}"); return self::setMetadataStatus($metadata, Metadata::STATUS_INVALID); }