19declare(strict_types=1);
35use Psr\Http\Message\ServerRequestInterface;
39 private const SCHEMA_FILE = __DIR__ .
'/../../../xml/personal-settings-template.xsd';
42 private readonly UIFactory $ui_factory,
44 private readonly DataFactory $data_factory,
57 $file_upload_input = $this->ui_factory->input()->field()->file($input_handler, $this->
lng->txt(
'import_file'))
62 return $this->ui_factory->modal()->roundtrip(
63 $this->
lng->txt(
'personal_settings_import'),
65 [
'upload' => $file_upload_input],
67 )->withSubmitLabel($this->
lng->txt(
'import'));
70 public function perform(ServerRequestInterface $request): void
74 if (!isset(
$data[
'upload']) ||
$data[
'upload'] === []) {
75 throw new \InvalidArgumentException(
'import_file_not_valid_here');
78 $upload_dir = array_pop(
$data[
'upload']);
79 $files = $this->
filesystem->listContents($upload_dir);
81 if (count($files) !== 1) {
82 throw new \InvalidArgumentException(
'import_file_not_valid_here');
92 $dom = new \DOMDocument();
93 $dom->resolveExternals =
false;
94 $dom->substituteEntities =
false;
95 $dom->validateOnParse =
false;
96 $dom->loadXML($this->
filesystem->read($file), LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
98 if (!$dom->schemaValidate(self::SCHEMA_FILE)) {
99 throw new \ilImportException(
'XML validation failed against XSD schema');
101 $doc = $dom->documentElement;
103 $imported_ilias_version = $this->data_factory->version($doc->getAttribute(
'ilias-version'));
106 if ($imported_ilias_version->getMajor() > $current_ilias_version->getMajor()) {
107 throw new \ilImportException(
'Unsupported Import between ILIAS major versions');
124 $imported_template->getName(),
125 $imported_template->getDescription(),
126 $imported_template->getAuthor(),
127 $imported_template->getCreatedAt()
130 $this->main_settings_repository->store(
133 $this->score_settings_repository->store(
138 $this->
repository->associateMarkSteps($template->getId(), $mark_ids);
150 foreach ($element->getAttributeNames() as $name) {
151 $property_name = str_replace(
'-',
'_', $name);
152 $attributes[$property_name] = $this->
sanitizeContent($element->getAttribute($name));
163 foreach ($element->getElementsByTagName($element_name) as $item) {
164 if ($item instanceof \DOMElement) {
177 $children = array_filter(
178 iterator_to_array($parent->childNodes),
179 static fn(mixed $child):
bool => $child instanceof \DOMElement,
182 if ($children !== []) {
184 foreach ($children as $child) {
185 if ($name = $child->getAttribute(
'name')) {
194 $type = $parent->getAttribute(
'type') ??
'string';
196 return match($type) {
197 'string' => htmlspecialchars_decode($value),
198 'integer' => (
int) $value,
199 'boolean' => $value ==
'true',
200 'double' => (
float) $value,
202 default =>
throw new \InvalidArgumentException(
"Invalid type: {$type}"),
217 $decoded = html_entity_decode($value, ENT_QUOTES | ENT_HTML5,
'UTF-8');
219 $stripped = strip_tags($decoded);
221 $clean = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u',
'', $stripped);
222 return trim($clean ??
'');
Builds a Color from either hex- or rgb values.
A class defining mark schemas for assessment test objects.
static fromExport(array $data)
Creates an instance of the object from an array.
static fromExport(array $data)
Creates an instance of the object from an array.
static fromExport(array $data)
Creates an instance of the object from an array.
__construct(private readonly UIFactory $ui_factory, private readonly Language $lng, private readonly DataFactory $data_factory, private readonly Filesystem $filesystem, private readonly PersonalSettingsRepository $repository, private readonly MainSettingsRepository $main_settings_repository, private readonly ScoreSettingsRepository $score_settings_repository, private readonly MarksRepository $marks_repository,)
firstChildElement(\DOMElement $element, string $element_name)
Returns the first child element of the given element with the given name.
sanitizeContent(string $value)
Sanitize string values parsed from XML to avoid displaying malicious content.
getAttributes(\DOMElement $element)
Returns the attributes of the given element as an associative array.
parseElementsRecursive(\DOMElement $parent)
Parses the given element recursively into an associative array structure.
perform(ServerRequestInterface $request)
static fromExport(array $data)
Creates an instance of the object from an array.
const array SUPPORTED_IMPORT_MIME_TYPES
const ILIAS_VERSION_NUMERIC
The filesystem interface provides the public interface for the Filesystem service API consumer.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...