Fixes to H5P module

Regex for library names changed to be a lot less restrictive.
Fixes to validation code that assumed stuff. Less strict now too.
Allow multiple JS/CSS files per library.

Attach H5P JS object to targets
namespaces
Frank Ronny Larsen 2013-01-16 19:39:49 +01:00
parent c283cd83b8
commit 2d3abae249
1 changed files with 31 additions and 26 deletions

View File

@ -118,11 +118,11 @@ class h5pValidator {
'title' => '/^.{1,255}$/', 'title' => '/^.{1,255}$/',
'language' => '/^[a-z]{1,5}$/', 'language' => '/^[a-z]{1,5}$/',
'preloadedDependencies' => array( 'preloadedDependencies' => array(
'machineName' => '/^[a-z0-9\-]{1,255}$/i', 'machineName' => '/^[\w0-9\-\.]{1,255}$/i',
'majorVersion' => '/^[0-9]{1,5}$/', 'majorVersion' => '/^[0-9]{1,5}$/',
'minorVersion' => '/^[0-9]{1,5}$/', 'minorVersion' => '/^[0-9]{1,5}$/',
), ),
'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i', // 'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i',
'embedTypes' => array('iframe', 'div'), 'embedTypes' => array('iframe', 'div'),
); );
@ -130,9 +130,9 @@ class h5pValidator {
'contentType' => '/^.{1,255}$/', 'contentType' => '/^.{1,255}$/',
'description' => '/^.{1,}$/', 'description' => '/^.{1,}$/',
'author' => '/^.{1,255}$/', 'author' => '/^.{1,255}$/',
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr)$/', 'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr|MIT)$/',
'dynamicDependencies' => array( 'dynamicDependencies' => array(
'machineName' => '/^[a-z0-9\-]{1,255}$/i', 'machineName' => '/^[\w0-9\-\.]{1,255}$/i',
'majorVersion' => '/^[0-9]{1,5}$/', 'majorVersion' => '/^[0-9]{1,5}$/',
'minorVersion' => '/^[0-9]{1,5}$/', 'minorVersion' => '/^[0-9]{1,5}$/',
), ),
@ -148,21 +148,21 @@ class h5pValidator {
'majorVersion' => '/^[0-9]{1,5}$/', 'majorVersion' => '/^[0-9]{1,5}$/',
'minorVersion' => '/^[0-9]{1,5}$/', 'minorVersion' => '/^[0-9]{1,5}$/',
'patchVersion' => '/^[0-9]{1,5}$/', 'patchVersion' => '/^[0-9]{1,5}$/',
'machineName' => '/^[a-z0-9\-]{1,255}$/i', 'machineName' => '/^[\w0-9\-\.]{1,255}$/i',
); );
private $libraryOptional = array( private $libraryOptional = array(
'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i', // 'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i',
'author' => '/^.{1,255}$/', 'author' => '/^.{1,255}$/',
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr)$/', 'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr|MIT)$/',
'description' => '/^.{1,}$/', 'description' => '/^.{1,}$/',
'dynamicDependencies' => array( 'dynamicDependencies' => array(
'machineName' => '/^[a-z0-9\-]{1,255}$/i', 'machineName' => '/^[\w0-9\-\.]{1,255}$/i',
'majorVersion' => '/^[0-9]{1,5}$/', 'majorVersion' => '/^[0-9]{1,5}$/',
'minorVersion' => '/^[0-9]{1,5}$/', 'minorVersion' => '/^[0-9]{1,5}$/',
), ),
'preloadedDependencies' => array( 'preloadedDependencies' => array(
'machineName' => '/^[a-z0-9\-]{1,255}$/i', 'machineName' => '/^[\w0-9\-\.]{1,255}$/i',
'majorVersion' => '/^[0-9]{1,5}$/', 'majorVersion' => '/^[0-9]{1,5}$/',
'minorVersion' => '/^[0-9]{1,5}$/', 'minorVersion' => '/^[0-9]{1,5}$/',
), ),
@ -221,10 +221,12 @@ class h5pValidator {
$libraryJsonData; $libraryJsonData;
$mainH5pExists = $imageExists = $contentExists = FALSE; $mainH5pExists = $imageExists = $contentExists = FALSE;
foreach ($files as $file) { foreach ($files as $file) {
// TODO: Any reason not to just drop anything starting with .?
if (in_array($file, array('.', '..'))) { if (in_array($file, array('.', '..'))) {
continue; continue;
} }
$file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file; $file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file;
// Check for h5p.json file.
if (strtolower($file) == 'h5p.json') { if (strtolower($file) == 'h5p.json') {
$mainH5pData = $this->getJsonData($file_path); $mainH5pData = $this->getJsonData($file_path);
if ($mainH5pData === FALSE) { if ($mainH5pData === FALSE) {
@ -242,10 +244,11 @@ class h5pValidator {
} }
} }
} }
// Check for h5p.jpg?
elseif (strtolower($file) == 'h5p.jpg') { elseif (strtolower($file) == 'h5p.jpg') {
$imageExists = TRUE; $imageExists = TRUE;
} }
// Content directory holds content.
elseif ($file == 'content') { elseif ($file == 'content') {
if (!is_dir($file_path)) { if (!is_dir($file_path)) {
$this->h5pF->setErrorMessage($this->h5pF->t('Invalid content folder')); $this->h5pF->setErrorMessage($this->h5pF->t('Invalid content folder'));
@ -264,18 +267,20 @@ class h5pValidator {
} }
} }
elseif (strpos($file, '.') !== FALSE) { // elseif (strpos($file, '.') !== FALSE) {
// Illegal file fond. This is ignored. // // Illegal file fond. This is ignored.
continue; // continue;
} // }
// The rest should be library folders
else { else {
if (!is_dir($file_path)) { if (!is_dir($file_path)) {
$this->h5pF->setErrorMessage($this->h5pF->t('Invalid library folder: %name', array('%name' => $file))); // $this->h5pF->setErrorMessage($this->h5pF->t('Invalid library folder: %name', array('%name' => $file)));
$valid = FALSE; // $valid = FALSE;
// Ignore this. Probably a file that shouldn't have been included.
continue; continue;
} }
if (preg_match('/^[a-z0-9\-]{1,255}$/i', $file) === 0) { if (preg_match('/^[\w0-9\-\.]{1,255}$/i', $file) === 0) {
$this->h5pF->setErrorMessage($this->h5pF->t('Invalid library name: %name', array('%name' => $file))); $this->h5pF->setErrorMessage($this->h5pF->t('Invalid library name: %name', array('%name' => $file)));
$valid = FALSE; $valid = FALSE;
continue; continue;
@ -290,10 +295,10 @@ class h5pValidator {
$validLibrary = $this->isValidH5pData($h5pData, $file, $this->libraryRequired, $this->libraryOptional); $validLibrary = $this->isValidH5pData($h5pData, $file, $this->libraryRequired, $this->libraryOptional);
if (isset($h5pData['preloadedJs'])) { if (isset($h5pData['preloadedJs'])) {
$validLibrary = $this->isExcistingFiles($h5pData['preloadedJs'], $tmp_dir, $file) && $validLibrary; $validLibrary = $this->isExistingFiles($h5pData['preloadedJs'], $tmp_dir, $file) && $validLibrary;
} }
if (isset($h5pData['preloadedCss'])) { if (isset($h5pData['preloadedCss'])) {
$validLibrary = $this->isExcistingFiles($h5pData['preloadedCss'], $tmp_dir, $file) && $validLibrary; $validLibrary = $this->isExistingFiles($h5pData['preloadedCss'], $tmp_dir, $file) && $validLibrary;
} }
if ($validLibrary) { if ($validLibrary) {
$libraries[$file] = $h5pData; $libraries[$file] = $h5pData;
@ -392,11 +397,11 @@ class h5pValidator {
* @return boolean * @return boolean
* TRUE if all the files excists * TRUE if all the files excists
*/ */
private function isExcistingFiles($files, $tmp_dir, $library) { private function isExistingFiles($files, $tmp_dir, $library) {
foreach ($files as $file) { foreach ($files as $file) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file['path']); $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file['path']);
if (!file_exists($tmp_dir . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . $path)) { if (!file_exists($tmp_dir . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . $path)) {
$this->h5pF->setErrorMessage($this->h5pF->t('The JS file %file is missing from library: %name', array('%file' => $file_path, '%name' => $library))); $this->h5pF->setErrorMessage($this->h5pF->t('The file "%file" is missing from library: "%name"', array('%file' => $path, '%name' => $library)));
return FALSE; return FALSE;
} }
} }