/** * Returns true if and only if $value meets the validation requirements * * If $value fails validation, then this method returns false, and * getMessages() will return an array of messages that explain why the * validation failed. * * @param string $value * @param string $schema * @param bool $isSecurityCheck * @return bool */ public function isValid($value, $schema = self::LAYOUT_SCHEMA_PAGE_HANDLE, $isSecurityCheck = true) { try { //wrap XML value in the "layout" and "handle" tags to make it validatable $value = '<layout xmlns:xsi="' . self::XML_NAMESPACE_XSI . '">' . $value . '</layout>'; $this->_domConfigFactory->createDom(['xml' => $value, 'schemaFile' => $this->_xsdSchemas[$schema]]); if ($isSecurityCheck) { $value = new \Magento\Framework\Simplexml\Element($value); $value->registerXPathNamespace('xsi', self::XML_NAMESPACE_XSI); foreach ($this->_protectedExpressions as $key => $xpr) { if ($value->xpath($xpr)) { $this->_error($key); } } $errors = $this->getMessages(); if (!empty($errors)) { return false; } } } catch (\Magento\Framework\Config\Dom\ValidationException $e) { $this->_error(self::XML_INVALID, $e->getMessage()); return false; } catch (\Exception $e) { $this->_error(self::XML_INVALID); return false; } return true; }
/** * Save package file to var/connect. * * @return boolean */ public function savePackage() { if ($this->getData('file_name') != '') { $fileName = $this->getData('file_name'); $this->unsetData('file_name'); } else { $fileName = $this->getName(); } if (!preg_match('/^[a-z0-9]+[a-z0-9\\-\\_\\.]*([\\/\\\\]{1}[a-z0-9]+[a-z0-9\\-\\_\\.]*)*$/i', $fileName)) { return false; } if (!$this->getPackageXml()) { $this->generatePackageXml(); } if (!$this->getPackageXml()) { return false; } try { // $path = $this->writeDirectory->getAbsolutePath(); $this->writeDirectory->writeFile(sprintf('connect/%s', 'package.xml'), $this->getPackageXml()); $this->unsPackageXml(); $this->unsTargets(); $xml = $this->_convertArray->assocToXml($this->getData()); $xml = new \Magento\Framework\Simplexml\Element($xml->asXML()); // prepare dir to save $parts = explode('/', $fileName); array_pop($parts); $directoryPath = implode('/', $parts); if (!empty($directoryPath)) { $this->writeDirectory->create(sprintf('connect/%s', $directoryPath)); } $this->writeDirectory->writeFile(sprintf('connect/%s.xml', $fileName), $xml->asNiceXml()); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $this->logger->addStreamLog(\Magento\Framework\Logger::LOGGER_EXCEPTION); $this->logger->log($e->getMessage()); return false; } return true; }