Starting to rewrite the validation according to new content specification

namespaces
Svein-Tore Griff With 2012-11-23 17:06:03 +01:00
parent 269339228e
commit adcf4288a9
1 changed files with 35 additions and 30 deletions

View File

@ -20,8 +20,8 @@ class h5p {
require_once 'Archive/Tar.php'; require_once 'Archive/Tar.php';
// Create a temporary dir to extract package in. // Create a temporary dir to extract package in.
$tmp_dir = $this->h5pFramework->getTempDir(); $tmp_dir = $this->h5pFramework->getUploadedH5pDir();
$tmp_path = $this->h5pFramework->getTempPath(); $tmp_path = $this->h5pFramework->getUploadedH5pPath();
// 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');
@ -34,48 +34,53 @@ class h5p {
// Process content and libraries // Process content and libraries
$contents = array(); $contents = array();
$tars = scandir($tmp_dir); $files = scandir($tmp_dir);
for ($i = 2, $s = count($tars); $i < $s; $i++) {
if (substr($tars[$i], -4) == '.tar') {
$tar_file = $tmp_dir . DIRECTORY_SEPARATOR . $tars[$i];
$tar_name = substr($tars[$i], 0, -4);
$tar_id = md5_file($tar_file);
$tar_time = filemtime($tar_file);
$content_path = $files_path . DIRECTORY_SEPARATOR . $tar_id; $json_exists = $image_exists = $content_exists = FALSE;
foreach ($files as $file) {
if (in_array($file, array('.', '..'))) {
continue;
}
$file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file;
if (strtolower($file) == 'h5p.json') {
$json_exists = TRUE;
}
// Extract content to our files dir. elseif (strtolower($file) == 'h5p.jpg') {
if (is_dir($content_path)) { $image_exists = TRUE;
continue; // We already have this content/library. }
} elseif ($file == 'content') {
$content_exists = TRUE;
// TODO: Validate content
}
mkdir($content_path); elseif (strpos($file, '.') !== FALSE) {
$tar = new Archive_Tar($tar_file); // Illegal file fond. This is ignored.
if (!$tar->extract($content_path)) { continue;
continue; // Wasn't a valid tar file. }
}
if (preg_match('/[^a-z0-9\-]/', $tar_name)) { else {
$this->setErrorMessage($this->t('Invalid name: %name', array('%name' => $tar_name))); // TODO: Validate library
if (preg_match('/[^a-z0-9\-]/', $file)) {
$this->setErrorMessage($this->t('Invalid name: %name', array('%name' => $file)));
$this->rRmdir($content_path); $this->rRmdir($content_path);
continue; continue;
} }
$json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'h5p.json');
// Go to extracted stuff and check it.
$json = file_get_contents($content_path . DIRECTORY_SEPARATOR . 'index.js');
if (!$json) { if (!$json) {
$this->setErrorMessage($this->t('Could not find index.js file: %name', array('%name' => $tar_name))); $this->setErrorMessage($this->t('Could not find h5p.json file: %name', array('%name' => $file)));
$this->rRmdir($content_path); $this->rRmdir($file_path);
continue; continue;
} }
$content = json_decode($json); $content = json_decode($json);
if (!$content) { if (!$content) {
$this->setErrorMessage($this->t('Invalid index.js file format: %name', array('%name' => $tar_name))); $this->setErrorMessage($this->t('Invalid h5p.json file format: %name', array('%name' => $file)));
$this->rRmdir($content_path); $this->rRmdir($file_path);
continue; continue;
} }
}
if ($content->type != H5P_CONTENT && $content->type != H5P_LIBRARY) { if ($content->type != H5P_CONTENT && $content->type != H5P_LIBRARY) {
$this->setErrorMessage($this->t('Invalid content type: %name', array('%name' => $tar_name))); $this->setErrorMessage($this->t('Invalid content type: %name', array('%name' => $tar_name)));
$this->rRmdir($content_path); $this->rRmdir($content_path);