Fix consistence in error reporting

namespaces
Svein-Tore Griff With 2012-11-28 15:21:34 +01:00
parent 44dfbbc4f6
commit 6a21c3101b
1 changed files with 60 additions and 53 deletions

View File

@ -55,10 +55,12 @@ class h5pValidator {
$tmp_dir = $this->h5pFramework->getUploadedH5pDir(); $tmp_dir = $this->h5pFramework->getUploadedH5pDir();
$tmp_path = $this->h5pFramework->getUploadedH5pPath(); $tmp_path = $this->h5pFramework->getUploadedH5pPath();
$valid = TRUE;
// Extract and then remove the package file. // Extract and then remove the package file.
$tar = new Archive_Tar($tmp_path, 'bz2'); $tar = new Archive_Tar($tmp_path, 'bz2');
if (!$tar->extract($tmp_dir)) { if (!$tar->extract($tmp_dir)) {
$this->setErrorMessage($this->t('The file you uploaded is not a valid HTML5 Pack.')); $this->h5pF->setErrorMessage($this->t('The file you uploaded is not a valid HTML5 Pack.'));
$this->rRmdir($tmp_dir); $this->rRmdir($tmp_dir);
return; return;
} }
@ -83,7 +85,19 @@ class h5pValidator {
} }
elseif ($file == 'content') { elseif ($file == 'content') {
$content_exists = TRUE; $content_exists = TRUE;
// TODO: Validate content $json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'content.json');
if (!$json) {
$this->h5pF->setErrorMessage($this->t('Could not find content.json file'));
$valid = FALSE;
continue;
}
$contentData = json_decode($json);
if (!$contentData) {
$this->h5pF->setErrorMessage('Invalid content.json file format. Json is required');
$valid = FALSE;
continue;
}
// In the future we might let the librarys provide validation functions for content.json
} }
elseif (strpos($file, '.') !== FALSE) { elseif (strpos($file, '.') !== FALSE) {
@ -92,38 +106,30 @@ class h5pValidator {
} }
else { else {
if (preg_match('/[^a-z0-9\-]/', $file)) { if (preg_match('/[^a-z0-9\-]/', $file) === 0) {
$this->setErrorMessage($this->t('Invalid library name: %name', array('%name' => $file))); $this->h5pF->setErrorMessage($this->t('Invalid library name: %name', array('%name' => $file)));
$this->rRmdir($content_path); $valid = FALSE;
continue; continue;
} }
$json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'h5p.json'); $json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'h5p.json');
if (!$json) { if (!$json) {
$this->setErrorMessage($this->t('Could not find h5p.json file: %name', array('%name' => $file))); $this->h5pF->setErrorMessage($this->t('Could not find h5p.json file: %name', array('%name' => $file)));
$this->rRmdir($file_path); $valid = FALSE;
continue; continue;
} }
$h5pData = json_decode($json); $h5pData = json_decode($json);
if (!$h5pData) { if (!$h5pData) {
$this->setErrorMessage($this->t('Invalid h5p.json file format: %name', array('%name' => $file))); $this->h5pF->setErrorMessage($this->t('Invalid h5p.json file format: %name', array('%name' => $file)));
$this->rRmdir($file_path); $valid = FALSE;
continue; continue;
} }
$errors = $this->validateH5pData($h5pData, $file); $valid = $this->isValidH5pData($h5pData, $file) && $valid;
if ($errors !== FALSE) {
// TODO: Print the (themed) errors
return;
}
if (isset($h5pData->preloadedJs)) { if (isset($h5pData->preloadedJs)) {
if (!$this->isExcistingFiles($h5pData->preloadedJs, $tmp_dir, $file)) { $valid = $this->isExcistingFiles($h5pData->preloadedJs, $tmp_dir, $file) && $valid;
// TODO: Handle the fact that we are missing js files
}
} }
if (isset($h5pData->preloadedCss)) { if (isset($h5pData->preloadedCss)) {
if (!$this->isExcistingFiles($h5pData->preloadedCss, $tmp_dir, $file)) { $valid = $this->isExcistingFiles($h5pData->preloadedCss, $tmp_dir, $file) && $valid;
// TODO: Handle the fact that we are missing css files
}
} }
} }
// TODO: Store library info in array // TODO: Store library info in array
@ -135,92 +141,93 @@ class h5pValidator {
foreach ($files as $file_path) { foreach ($files as $file_path) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file_path); $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file_path);
if (!file_exists($tmp_dir . DIRECTORY_SEPARATOR . $path)) { if (!file_exists($tmp_dir . DIRECTORY_SEPARATOR . $path)) {
$this->setErrorMessage($this->t('The JS file %file is missing from library: %name', array('%file' => $file_path, '%name' => $library))); $this->h5pF->setErrorMessage('The JS file %file is missing from library: %name', array('%file' => $file_path, '%name' => $library));
$this->rRmdir($tmp_dir);
return FALSE; return FALSE;
} }
} }
return TRUE; return TRUE;
} }
private function validateH5pData($h5pData, $library_name) { private function isValidH5pData($h5pData, $library_name) {
$errors = $this->validateRequiredH5pData($h5pData, $this->h5pRequired, $library_name); $valid = $this->isValidRequiredH5pData($h5pData, $this->h5pRequired, $library_name);
array_push($errors, $this->validateOptionalH5pData($h5pData, $this->h5pOptional, $library_name)); $valid = $this->isValidOptionalH5pData($h5pData, $this->h5pOptional, $library_name) && $valid;
if (!empty($errors)) { return $valid;
return $errors;
}
else {
return FALSE;
}
} }
private function validateOptionalH5pData($h5pData, $requirements, $library_name) { private function isValidOptionalH5pData($h5pData, $requirements, $library_name) {
$errors = array(); $valid = TRUE;
foreach ($h5pData as $key => $value) { foreach ($h5pData as $key => $value) {
if (isset($requirements[$key])) { if (isset($requirements[$key])) {
array_merge($errors, validateRequirement($h5pData, $requirement, $library_name, $property_name)); $valid = isValidRequirement($h5pData, $requirement, $library_name, $property_name) && $valid;
} }
// Else: ignore, a package can have parameters that this library doesn't care about, but that library // Else: ignore, a package can have parameters that this library doesn't care about, but that library
// specific implementations does care about... // specific implementations does care about...
} }
return $errors; return $valid;
} }
private function validateRequirement($h5pData, $requirement, $library_name, $property_name) { private function isValidRequirement($h5pData, $requirement, $library_name, $property_name) {
$errors = array(); $valid = TRUE;
if (is_string($requirement)) { if (is_string($requirement)) {
// The requirement is a regexp, match it against the data // The requirement is a regexp, match it against the data
if (is_string($h5pData)) { if (is_string($h5pData)) {
if (preg_match($requirement, $h5pData) === 0) { if (preg_match($requirement, $h5pData) === 0) {
$errors[] = $this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)));
$valid = FALSE;
} }
} }
else { else {
$errors[] = $this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)));
$valid = FALSE;
} }
} }
elseif (is_array($requirement)) { elseif (is_array($requirement)) {
// We have sub requirements // We have sub requirements
if (is_array($h5pData)) { if (is_array($h5pData)) {
array_merge($errors, $this->validateRequiredH5pData($h5pData, $requirement, $library_name)); $valid = $this->isValidRequiredH5pData($h5pData, $requirement, $library_name) && $valid;
} }
else { else {
$errors[] = $this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t("Ivalid data provided for %property in %library", array('%property' => $property_name, '%library' => $library_name)));
$valid = FALSE;
} }
} }
else { else {
$errors[] = $this->t("Can't read the property %property in %library", array('%property' => $property_name, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t("Can't read the property %property in %library", array('%property' => $property_name, '%library' => $library_name)));
$valid = FALSE;
} }
return $errors; return $valid;
} }
private function validateRequiredH5pData($h5pData, $requirements, $library_name) { private function isValidRequiredH5pData($h5pData, $requirements, $library_name) {
$errors = array(); $valid = TRUE;
foreach ($requirements as $required => $requirement) { foreach ($requirements as $required => $requirement) {
if (is_int($required)) { if (is_int($required)) {
// We have an array of allowed options // We have an array of allowed options
return validateH5pDataOptions($h5pData, $requirements, $library_name); return isValidH5pDataOptions($h5pData, $requirements, $library_name);
} }
if (isset($h5pData[$required])) { if (isset($h5pData[$required])) {
array_merge($errors, validateRequirement($h5pData[$required], $requirement, $library_name, $required)); $valid = validateRequirement($h5pData[$required], $requirement, $library_name, $required) && $valid;
} }
else { else {
$errors[] = $this->t('The required property %property is missing from %library', array('%property' => $required, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t('The required property %property is missing from %library', array('%property' => $required, '%library' => $library_name)));
$valid = FALSE;
} }
} }
return $errors; return $valid;
} }
private function validateH5pDataOptions($selected, $allowed, $library_name) { private function isValidH5pDataOptions($selected, $allowed, $library_name) {
$errors = array(); $valid = TRUE;
foreach ($selected as $value) { foreach ($selected as $value) {
if (!in_array($value, $allowed)) { if (!in_array($value, $allowed)) {
$errors[] = $this->t('Illegal option %option in %library', array('%option' => $value, '%library' => $library_name)); $this->h5pF->setErrorMessage($this->t('Illegal option %option in %library', array('%option' => $value, '%library' => $library_name)));
$valid = FALSE;
} }
} }
return $errors; return $valid;
} }
/** /**