H5PValidatorImport.php in Opigno module 8
File
src/H5PImportClasses/H5PValidatorImport.php
View source
<?php
namespace Drupal\opigno_module\H5PImportClasses;
class H5PValidatorImport extends \H5PValidator {
public function isValidPackage($skipContent = FALSE, $upgradeOnly = FALSE) {
if (!class_exists('ZipArchive')) {
$this->h5pF
->setErrorMessage($this->h5pF
->t('Your PHP version does not support ZipArchive.'), 'zip-archive-unsupported');
return FALSE;
}
$tmpDir = $this->h5pF
->getUploadedH5pFolderPath();
$tmpPath = $this->h5pF
->getUploadedH5pPath();
$zip = new \ZipArchive();
if (strtolower(substr($tmpPath, -3)) !== 'h5p') {
$this->h5pF
->setErrorMessage($this->h5pF
->t('The file you uploaded is not a valid HTML5 Package (It does not have the .h5p file extension)'), 'missing-h5p-extension');
H5PStorageImport::deleteFileTree($tmpDir);
return FALSE;
}
if ($zip
->open($tmpPath) === TRUE) {
$zip
->extractTo($tmpDir);
$zip
->close();
}
else {
$this->h5pF
->setErrorMessage($this->h5pF
->t('The file you uploaded is not a valid HTML5 Package (We are unable to unzip it)'), 'unable-to-unzip');
H5PStorageImport::deleteFileTree($tmpDir);
return FALSE;
}
unlink($tmpPath);
$valid = TRUE;
$libraries = [];
$files = scandir($tmpDir);
foreach ($files as $file) {
if (in_array(substr($file, 0, 1), [
'.',
'_',
])) {
continue;
}
$filePath = $tmpDir . DIRECTORY_SEPARATOR . $file;
if (strtolower($file) != 'h5p.json' && $file != 'content') {
$libraryH5PData = $this
->getLibraryData($file, $filePath, $tmpDir);
if ($libraryH5PData !== FALSE) {
$libraryH5PData['uploadDirectory'] = $filePath;
$libraries[self::libraryToString($libraryH5PData)] = $libraryH5PData;
}
else {
$valid = FALSE;
}
}
}
if ($valid) {
$this->h5pC->librariesJsonData = $libraries;
}
if (!$valid) {
H5PStorageImport::deleteFileTree($tmpDir);
}
return $valid;
}
public static function libraryToString(array $library, $folderName = FALSE) {
return (isset($library['machineName']) ? $library['machineName'] : $library['name']) . ($folderName ? '-' : ' ') . $library['majorVersion'] . '.' . $library['minorVersion'];
}
}