2012-11-21 14:14:07 +01:00
< ? php
2013-01-14 23:47:50 +01:00
/**
* Interface defining functions the h5p library needs the framework to implement
*/
2013-02-17 15:39:26 +01:00
interface H5PFrameworkInterface {
2013-01-14 23:47:50 +01:00
/**
* Show the user an error message
*
* @ param string $message
* The error message
*/
2012-11-22 14:00:06 +01:00
public function setErrorMessage ( $message );
2013-01-14 23:47:50 +01:00
/**
* Show the user an information message
*
* @ param string $message
* The error message
*/
2012-11-22 14:00:06 +01:00
public function setInfoMessage ( $message );
2013-01-14 23:47:50 +01:00
/**
* Translation function
*
* @ param string $message
* The english string to be translated .
* @ param type $replacements
* An associative array of replacements to make after translation . Incidences
* of any key in this array are replaced with the corresponding value . Based
* on the first character of the key , the value is escaped and / or themed :
* - ! variable : inserted as is
* - @ variable : escape plain text to HTML
* - % variable : escape text and theme as a placeholder for user - submitted
* content
* @ return string Translated string
*/
2012-12-03 00:19:25 +01:00
public function t ( $message , $replacements = array ());
2013-01-14 23:47:50 +01:00
/**
* Get the Path to the last uploaded h5p
*
* @ return string Path to the folder where the last uploaded h5p for this session is located .
*/
2012-11-30 15:51:39 +01:00
public function getUploadedH5pFolderPath ();
2013-01-14 23:47:50 +01:00
/**
* @ return string Path to the folder where all h5p files are stored
*/
2012-11-30 15:51:39 +01:00
public function getH5pPath ();
2013-01-14 23:47:50 +01:00
/**
* Get the path to the last uploaded h5p file
*
* @ return string Path to the last uploaded h5p
*/
2012-11-22 14:00:06 +01:00
public function getUploadedH5pPath ();
2013-01-14 23:47:50 +01:00
/**
* Get id to an excisting library
*
2013-03-09 01:20:55 +01:00
* @ param string $machineName
2013-01-14 23:47:50 +01:00
* The librarys machine name
* @ param int $majorVersion
* The librarys major version
* @ param int $minorVersion
* The librarys minor version
* @ return int
* The id of the specified library or FALSE
*/
2013-03-09 01:20:55 +01:00
public function getLibraryId ( $machineName , $majorVersion , $minorVersion );
2013-01-14 23:47:50 +01:00
/**
2013-06-30 16:39:17 +02:00
* Is the library a patched version of an existing library ?
2013-01-14 23:47:50 +01:00
*
* @ param object $library
* The library data for a library we are checking
* @ return boolean
* TRUE if the library is a patched version of an excisting library
* FALSE otherwise
*/
2012-12-22 06:35:16 +01:00
public function isPatchedLibrary ( $library );
2013-01-14 23:47:50 +01:00
2013-06-30 16:39:17 +02:00
/**
* Is the current user allowed to update the library data ?
*
* @ param object $library
* The library data for a library we are checking
* @ return boolean
* TRUE if the user us allowed to update with the given library data OR the library already exists with the current version levels .
* FALSE if the user is not allowed to update or create the library .
*/
public function isAllowedLibraryUpdate ( $library );
2013-01-14 23:47:50 +01:00
/**
* Store data about a library
*
* Also fills in the libraryId in the libraryData object if the object is new
*
* @ param object $libraryData
* Object holding the information that is to be stored
*/
2013-02-17 14:05:02 +01:00
public function saveLibraryData ( & $libraryData );
2013-01-14 23:47:50 +01:00
/**
2013-02-15 11:38:49 +01:00
* Stores contentData
2013-01-14 23:47:50 +01:00
*
* @ param int $contentId
* Framework specific id identifying the content
* @ param string $contentJson
* The content data that is to be stored
* @ param array $mainJsonData
* The data extracted from the h5p . json file
* @ param int $contentMainId
* Any contentMainId defined by the framework , for instance to support revisioning
*/
2013-03-08 18:30:38 +01:00
public function saveContentData ( $contentId , $contentJson , $mainJsonData , $mainLibraryId , $contentMainId = NULL );
2013-06-30 22:14:16 +02:00
/**
* Validates content files
*
* @ param string $contentPath
* The path containg content files to validate .
* @ return boolean
* TRUE if all files are valid
* FALSE if one or more files fail validation . Error message should be set accordingly by validator .
*/
public function validateContentFiles ( $contentPath );
2013-03-29 16:35:54 +01:00
/**
* Save what libraries a library is dependending on
*
* @ param int $libraryId
* Library Id for the library we ' re saving dependencies for
* @ param array $dependencies
* List of dependencies in the format used in library . json
* @ param string $dependency_type
* What type of dependency this is , for instance it might be an editor dependency
*/
public function saveLibraryDependencies ( $libraryId , $dependencies , $dependency_type );
2013-02-15 11:38:49 +01:00
/**
2013-03-07 05:31:30 +01:00
* Copies library usage
2013-02-15 11:38:49 +01:00
*
* @ param int $contentId
* Framework specific id identifying the content
* @ param int $copyFromId
* Framework specific id identifying the content to be copied
* @ param int $contentMainId
* Framework specific main id for the content , typically used in frameworks
* That supports versioning . ( In this case the content id will typically be
* the version id , and the contentMainId will be the frameworks content id
*/
2013-03-07 05:31:30 +01:00
public function copyLibraryUsage ( $contentId , $copyFromId , $contentMainId = NULL );
2013-02-15 11:38:49 +01:00
/**
* Deletes content data
*
* @ param int $contentId
* Framework specific id identifying the content
*/
2012-12-22 07:09:17 +01:00
public function deleteContentData ( $contentId );
2013-02-15 11:38:49 +01:00
2013-03-29 16:35:54 +01:00
/**
* Delete what libraries a content item is using
*
* @ param int $contentId
* Content Id of the content we ' ll be deleting library usage for
*/
public function deleteLibraryUsage ( $contentId );
2013-02-15 11:38:49 +01:00
/**
* Saves what libraries the content uses
*
* @ param int $contentId
* Framework specific id identifying the content
* @ param array $librariesInUse
* List of libraries the content uses . Libraries consist of arrays with :
* - libraryId stored in $librariesInUse [ < place > ][ 'library' ][ 'libraryId' ]
* - libraryId stored in $librariesInUse [ < place > ][ 'preloaded' ]
*/
2012-11-30 15:51:39 +01:00
public function saveLibraryUsage ( $contentId , $librariesInUse );
2013-02-15 11:38:49 +01:00
/**
* Loads a library
*
2013-03-09 01:20:55 +01:00
* @ param string $machineName
2013-02-15 11:38:49 +01:00
* @ param int $majorVersion
* @ param int $minorVersion
* @ return array | FALSE
* Array representing the library with dependency descriptions
* FALSE if the library doesn ' t exist
*/
2013-03-09 01:20:55 +01:00
public function loadLibrary ( $machineName , $majorVersion , $minorVersion );
2013-04-13 14:11:25 +02:00
/**
* Delete all dependencies belonging to given library
*
* @ param int $libraryId
* Library Id
*/
public function deleteLibraryDependencies ( $libraryId );
2012-11-21 14:14:07 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* This class is used for validating H5P files
*/
2013-02-17 15:04:30 +01:00
class H5PValidator {
2012-11-22 14:00:06 +01:00
public $h5pF ;
2012-11-29 22:52:41 +01:00
public $h5pC ;
2013-01-16 19:39:49 +01:00
2012-11-24 01:09:25 +01:00
// Schemas used to validate the h5p files
private $h5pRequired = array (
2012-11-28 14:06:55 +01:00
'title' => '/^.{1,255}$/' ,
'language' => '/^[a-z]{1,5}$/' ,
2012-11-24 01:09:25 +01:00
'preloadedDependencies' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2012-12-04 22:26:20 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
2012-11-24 01:09:25 +01:00
),
2013-03-09 01:20:55 +01:00
'mainLibrary' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i' ,
2012-11-24 01:09:25 +01:00
'embedTypes' => array ( 'iframe' , 'div' ),
);
2013-01-16 19:39:49 +01:00
2012-11-24 01:09:25 +01:00
private $h5pOptional = array (
2012-11-28 14:06:55 +01:00
'contentType' => '/^.{1,255}$/' ,
'author' => '/^.{1,255}$/' ,
2013-01-16 19:39:49 +01:00
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr|MIT)$/' ,
2012-11-24 01:09:25 +01:00
'dynamicDependencies' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2012-12-04 22:26:20 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
2012-11-24 01:09:25 +01:00
),
2013-03-09 01:20:55 +01:00
'w' => '/^[0-9]{1,4}$/' ,
'h' => '/^[0-9]{1,4}$/' ,
2012-12-02 21:55:51 +01:00
'metaKeywords' => '/^.{1,}$/' ,
'metaDescription' => '/^.{1,}$/k' ,
);
2013-01-14 23:47:50 +01:00
// Schemas used to validate the library files
2012-12-02 21:55:51 +01:00
private $libraryRequired = array (
'title' => '/^.{1,255}$/' ,
2012-12-04 20:56:16 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
2012-12-04 22:26:20 +01:00
'minorVersion' => '/^[0-9]{1,5}$/' ,
'patchVersion' => '/^[0-9]{1,5}$/' ,
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2013-01-20 16:13:11 +01:00
'runnable' => '/^(0|1)$/' ,
2012-12-02 21:55:51 +01:00
);
2013-01-16 19:39:49 +01:00
2012-12-02 21:55:51 +01:00
private $libraryOptional = array (
'author' => '/^.{1,255}$/' ,
2013-01-16 19:39:49 +01:00
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr|MIT)$/' ,
2012-12-02 21:55:51 +01:00
'description' => '/^.{1,}$/' ,
'dynamicDependencies' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2012-12-04 22:26:20 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
2013-01-16 19:39:49 +01:00
),
2012-12-02 21:55:51 +01:00
'preloadedDependencies' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2012-12-04 22:26:20 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
2012-12-22 06:35:16 +01:00
'minorVersion' => '/^[0-9]{1,5}$/' ,
2012-12-02 21:55:51 +01:00
),
2013-02-06 17:39:45 +01:00
'editorDependencies' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2013-02-06 17:39:45 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
),
2012-11-30 15:51:39 +01:00
'preloadedJs' => array (
2013-04-14 15:31:29 +02:00
'path' => '/^((\\\|\/)?[a-z_\-\s0-9\.]+)+\.js$/i' ,
2012-11-30 15:51:39 +01:00
),
'preloadedCss' => array (
2013-04-14 15:31:29 +02:00
'path' => '/^((\\\|\/)?[a-z_\-\s0-9\.]+)+\.css$/i' ,
2012-11-30 15:51:39 +01:00
),
2013-02-28 20:06:18 +01:00
'dropLibraryCss' => array (
2013-03-09 01:20:55 +01:00
'machineName' => '/^[\w0-9\-\.]{1,255}$/i' ,
2013-02-28 20:06:18 +01:00
),
2013-03-09 01:20:55 +01:00
'w' => '/^[0-9]{1,4}$/' ,
'h' => '/^[0-9]{1,4}$/' ,
2012-12-02 21:55:51 +01:00
'embedTypes' => array ( 'iframe' , 'div' ),
2013-04-11 14:59:33 +02:00
'fullscreen' => '/^(0|1)$/' ,
2012-11-24 01:09:25 +01:00
);
2012-11-22 14:00:06 +01:00
2012-11-29 15:41:06 +01:00
/**
2013-02-17 15:04:30 +01:00
* Constructor for the H5PValidator
2012-11-29 15:41:06 +01:00
*
2013-02-17 15:04:30 +01:00
* @ param object $H5PFramework
2013-02-17 15:39:26 +01:00
* The frameworks implementation of the H5PFrameworkInterface
2012-11-29 15:41:06 +01:00
*/
2013-02-17 15:04:30 +01:00
public function __construct ( $H5PFramework , $H5PCore ) {
$this -> h5pF = $H5PFramework ;
$this -> h5pC = $H5PCore ;
2012-11-22 14:00:06 +01:00
}
2012-11-29 15:41:06 +01:00
/**
* Validates a . h5p file
*
* @ return boolean
* TRUE if the . h5p file is valid
*/
2012-11-30 15:51:39 +01:00
public function isValidPackage () {
2012-11-22 14:00:06 +01:00
// Create a temporary dir to extract package in.
2013-05-01 23:24:58 +02:00
$tmpDir = $this -> h5pF -> getUploadedH5pFolderPath ();
2012-12-03 00:19:25 +01:00
$tmp_path = $this -> h5pF -> getUploadedH5pPath ();
2012-11-22 14:00:06 +01:00
2012-11-28 15:21:34 +01:00
$valid = TRUE ;
2012-11-22 14:00:06 +01:00
// Extract and then remove the package file.
2012-12-03 00:19:25 +01:00
$zip = new ZipArchive ;
if ( $zip -> open ( $tmp_path ) === true ) {
2013-05-01 23:24:58 +02:00
$zip -> extractTo ( $tmpDir );
2012-12-03 00:19:25 +01:00
$zip -> close ();
}
else {
2013-02-28 20:06:18 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The file you uploaded is not a valid HTML5 Package.' ));
2013-05-01 23:24:58 +02:00
$this -> h5pC -> delTree ( $tmpDir );
2012-11-22 14:00:06 +01:00
return ;
}
unlink ( $tmp_path );
// Process content and libraries
2012-11-28 15:31:55 +01:00
$libraries = array ();
2013-05-01 23:24:58 +02:00
$files = scandir ( $tmpDir );
2012-11-29 15:41:06 +01:00
$mainH5pData ;
2012-11-30 15:51:39 +01:00
$libraryJsonData ;
2012-11-29 08:57:01 +01:00
$mainH5pExists = $imageExists = $contentExists = FALSE ;
2012-11-23 17:06:03 +01:00
foreach ( $files as $file ) {
2013-01-16 22:00:12 +01:00
if ( in_array ( substr ( $file , 0 , 1 ), array ( '.' , '_' ))) {
2012-11-23 17:06:03 +01:00
continue ;
}
2013-05-01 23:24:58 +02:00
$filePath = $tmpDir . DIRECTORY_SEPARATOR . $file ;
2013-01-16 19:39:49 +01:00
// Check for h5p.json file.
2012-11-23 17:06:03 +01:00
if ( strtolower ( $file ) == 'h5p.json' ) {
2013-05-01 17:22:09 +02:00
$mainH5pData = $this -> getJsonData ( $filePath );
2012-11-29 15:41:06 +01:00
if ( $mainH5pData === FALSE ) {
2012-11-29 08:51:36 +01:00
$valid = FALSE ;
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not find or parse the main h5p.json file' ));
2012-11-29 08:51:36 +01:00
}
else {
2012-11-29 15:41:06 +01:00
$validH5p = $this -> isValidH5pData ( $mainH5pData , $file , $this -> h5pRequired , $this -> h5pOptional );
2012-11-29 08:51:36 +01:00
if ( $validH5p ) {
$mainH5pExists = TRUE ;
}
else {
$valid = FALSE ;
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not find or parse the main h5p.json file' ));
2012-11-29 08:51:36 +01:00
}
}
2012-11-23 17:06:03 +01:00
}
2013-01-16 19:39:49 +01:00
// Check for h5p.jpg?
2012-11-23 17:06:03 +01:00
elseif ( strtolower ( $file ) == 'h5p.jpg' ) {
2012-11-29 08:57:01 +01:00
$imageExists = TRUE ;
2012-11-23 17:06:03 +01:00
}
2013-01-16 19:39:49 +01:00
// Content directory holds content.
2012-11-23 17:06:03 +01:00
elseif ( $file == 'content' ) {
2013-05-01 17:22:09 +02:00
if ( ! is_dir ( $filePath )) {
2012-12-22 06:35:16 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid content folder' ));
$valid = FALSE ;
continue ;
}
2013-05-01 17:22:09 +02:00
$contentJsonData = $this -> getJsonData ( $filePath . DIRECTORY_SEPARATOR . 'content.json' );
2012-11-30 15:51:39 +01:00
if ( $contentJsonData === FALSE ) {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not find or parse the content.json file' ));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
continue ;
}
2012-11-29 08:51:36 +01:00
else {
2012-11-29 08:57:01 +01:00
$contentExists = TRUE ;
2013-06-30 22:14:16 +02:00
// In the future we might let the libraries provide validation functions for content.json
}
if ( ! $this -> h5pF -> validateContentFiles ( $filePath )) {
$valid = FALSE ;
continue ;
2012-11-28 15:21:34 +01:00
}
2012-11-23 17:06:03 +01:00
}
2012-11-22 14:00:06 +01:00
2013-01-16 19:39:49 +01:00
// The rest should be library folders
2012-11-23 17:06:03 +01:00
else {
2013-05-01 17:22:09 +02:00
if ( ! is_dir ( $filePath )) {
2013-01-16 19:39:49 +01:00
// Ignore this. Probably a file that shouldn't have been included.
2012-12-22 06:35:16 +01:00
continue ;
}
2013-06-30 15:32:12 +02:00
2013-05-01 23:24:58 +02:00
$libraryH5PData = $this -> getLibraryData ( $file , $filePath , $tmpDir );
2012-11-28 15:31:55 +01:00
2013-05-01 23:24:58 +02:00
if ( $libraryH5PData ) {
$libraries [ $file ] = $libraryH5PData ;
2012-11-28 15:31:55 +01:00
}
2013-05-01 17:22:09 +02:00
else {
$valid = FALSE ;
}
2012-11-22 14:00:06 +01:00
}
}
2012-11-29 15:41:06 +01:00
if ( ! $contentExists ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'A valid content folder is missing' ));
$valid = FALSE ;
}
if ( ! $mainH5pExists ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'A valid main h5p.json file is missing' ));
$valid = FALSE ;
}
2012-11-28 15:31:55 +01:00
if ( $valid ) {
2012-11-30 15:51:39 +01:00
$this -> h5pC -> librariesJsonData = $libraries ;
$this -> h5pC -> mainJsonData = $mainH5pData ;
$this -> h5pC -> contentJsonData = $contentJsonData ;
2013-04-13 13:28:48 +02:00
$libraries [ 'mainH5pData' ] = $mainH5pData ; // Check for the dependencies in h5p.json as well as in the libraries
2012-11-28 23:29:22 +01:00
$missingLibraries = $this -> getMissingLibraries ( $libraries );
foreach ( $missingLibraries as $missing ) {
2013-03-09 01:20:55 +01:00
if ( $this -> h5pF -> getLibraryId ( $missing [ 'machineName' ], $missing [ 'majorVersion' ], $missing [ 'minorVersion' ])) {
unset ( $missingLibraries [ $missing [ 'machineName' ]]);
2012-11-28 23:29:22 +01:00
}
}
2013-04-13 13:28:48 +02:00
if ( ! empty ( $missingLibraries )) {
foreach ( $missingLibraries as $library ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Missing required library @library' , array ( '@library' => $this -> h5pC -> libraryToString ( $library ))));
}
}
2012-11-28 23:29:22 +01:00
$valid = empty ( $missingLibraries ) && $valid ;
}
if ( ! $valid ) {
2013-05-01 23:24:58 +02:00
$this -> h5pC -> delTree ( $tmpDir );
2012-11-28 23:29:22 +01:00
}
return $valid ;
}
2012-11-29 15:41:06 +01:00
2013-05-01 17:22:09 +02:00
/**
* Validates a H5P library
*
* @ param string $file
* Name of the library folder
* @ param string $filePath
* Path to the library folder
2013-05-01 23:24:58 +02:00
* @ param string $tmpDir
* Path to the temporary upload directory
2013-05-01 17:22:09 +02:00
* @ return object | boolean
* H5P data from library . json and semantics if the library is valid
* FALSE if the library isn ' t valid
*/
2013-05-01 23:24:58 +02:00
public function getLibraryData ( $file , $filePath , $tmpDir ) {
2013-05-01 17:22:09 +02:00
if ( preg_match ( '/^[\w0-9\-\.]{1,255}$/i' , $file ) === 0 ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid library name: %name' , array ( '%name' => $file )));
2013-05-01 23:24:58 +02:00
return FALSE ;
2013-05-01 17:22:09 +02:00
}
$h5pData = $this -> getJsonData ( $filePath . DIRECTORY_SEPARATOR . 'library.json' );
if ( $h5pData === FALSE ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not find library.json file with valid json format for library %name' , array ( '%name' => $file )));
2013-05-01 23:24:58 +02:00
return FALSE ;
2013-05-01 17:22:09 +02:00
}
2013-06-30 16:39:17 +02:00
// check if allowed to update this library
if ( ! $this -> h5pF -> isAllowedLibraryUpdate ( $h5pData )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Not allowed to update library %name' , array ( '%name' => $h5pData [ 'machineName' ])));
return FALSE ;
}
2013-05-01 17:22:09 +02:00
// validate json if a semantics file is provided
$semanticsPath = $filePath . DIRECTORY_SEPARATOR . 'semantics.json' ;
if ( file_exists ( $semanticsPath )) {
$semantics = $this -> getJsonData ( $semanticsPath , TRUE );
if ( $semantics === FALSE ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid semantics.json file has been included in the library %name' , array ( '%name' => $file )));
2013-05-01 23:24:58 +02:00
return FALSE ;
2013-05-01 17:22:09 +02:00
}
else {
$h5pData [ 'semantics' ] = $semantics ;
}
}
2013-05-02 17:09:48 +02:00
// validate language folder if it exists
$languagePath = $filePath . DIRECTORY_SEPARATOR . 'language' ;
if ( is_dir ( $languagePath )) {
$languageFiles = scandir ( $languagePath );
foreach ( $languageFiles as $languageFile ) {
2013-05-03 11:21:08 +02:00
if ( in_array ( $languageFile , array ( '.' , '..' ))) {
continue ;
}
if ( preg_match ( '/^(-?[a-z]+){1,7}\.json$/i' , $languageFile ) === 0 ) {
2013-05-02 17:09:48 +02:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid language file %file in library %library' , array ( '%file' => $languageFile , '%library' => $file )));
return FALSE ;
}
2013-05-03 11:21:08 +02:00
$languageJson = $this -> getJsonData ( $languagePath . DIRECTORY_SEPARATOR . $languageFile , TRUE );
2013-05-02 17:09:48 +02:00
if ( $languageJson === FALSE ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid language file %languageFile has been included in the library %name' , array ( '%languageFile' => $languageFile , '%name' => $file )));
return FALSE ;
}
$parts = explode ( '.' , $languageFile ); // $parts[0] is the language code
$h5pData [ 'language' ][ $parts [ 0 ]] = $languageJson ;
}
}
2013-05-01 17:22:09 +02:00
$validLibrary = $this -> isValidH5pData ( $h5pData , $file , $this -> libraryRequired , $this -> libraryOptional );
if ( isset ( $h5pData [ 'preloadedJs' ])) {
2013-05-01 23:24:58 +02:00
$validLibrary = $this -> isExistingFiles ( $h5pData [ 'preloadedJs' ], $tmpDir , $file ) && $validLibrary ;
2013-05-01 17:22:09 +02:00
}
if ( isset ( $h5pData [ 'preloadedCss' ])) {
2013-05-01 23:24:58 +02:00
$validLibrary = $this -> isExistingFiles ( $h5pData [ 'preloadedCss' ], $tmpDir , $file ) && $validLibrary ;
2013-05-01 17:22:09 +02:00
}
if ( $validLibrary ) {
return $h5pData ;
}
else {
return FALSE ;
}
}
2012-11-29 15:41:06 +01:00
/**
* Use the dependency declarations to find any missing libraries
*
* @ param array $libraries
2013-03-09 01:20:55 +01:00
* A multidimensional array of libraries keyed with machineName first and majorVersion second
2012-11-29 15:41:06 +01:00
* @ return array
2013-03-09 01:20:55 +01:00
* A list of libraries that are missing keyed with machineName and holds objects with
* machineName , majorVersion and minorVersion properties
2012-11-29 15:41:06 +01:00
*/
2012-11-28 23:29:22 +01:00
private function getMissingLibraries ( $libraries ) {
$missing = array ();
foreach ( $libraries as $library ) {
if ( isset ( $library [ 'preloadedDependencies' ])) {
2013-04-13 13:28:48 +02:00
$missing = array_merge ( $missing , $this -> getMissingDependencies ( $library [ 'preloadedDependencies' ], $libraries ));
2012-11-28 23:29:22 +01:00
}
if ( isset ( $library [ 'dynamicDependencies' ])) {
2013-04-13 13:28:48 +02:00
$missing = array_merge ( $missing , $this -> getMissingDependencies ( $library [ 'dynamicDependencies' ], $libraries ));
2012-11-28 23:29:22 +01:00
}
2013-02-08 04:38:27 +01:00
if ( isset ( $library [ 'editorDependencies' ])) {
2013-04-13 13:28:48 +02:00
$missing = array_merge ( $missing , $this -> getMissingDependencies ( $library [ 'editorDependencies' ], $libraries ));
2013-02-08 04:38:27 +01:00
}
2012-11-28 15:31:55 +01:00
}
2012-11-28 23:29:22 +01:00
return $missing ;
}
2012-11-29 15:41:06 +01:00
/**
* Helper function for getMissingLibraries , searches for dependency required libraries in
* the provided list of libraries
*
* @ param array $dependencies
2013-03-09 01:20:55 +01:00
* A list of objects with machineName , majorVersion and minorVersion properties
2012-11-29 15:41:06 +01:00
* @ param array $libraries
2013-03-09 01:20:55 +01:00
* An array of libraries keyed with machineName
2012-11-29 15:41:06 +01:00
* @ return
2013-03-09 01:20:55 +01:00
* A list of libraries that are missing keyed with machineName and holds objects with
* machineName , majorVersion and minorVersion properties
2012-11-29 15:41:06 +01:00
*/
2012-11-28 23:29:22 +01:00
private function getMissingDependencies ( $dependencies , $libraries ) {
$missing = array ();
2012-12-04 15:52:03 +01:00
foreach ( $dependencies as $dependency ) {
2013-03-09 01:20:55 +01:00
if ( isset ( $libraries [ $dependency [ 'machineName' ]])) {
if ( ! $this -> h5pC -> isSameVersion ( $libraries [ $dependency [ 'machineName' ]], $dependency )) {
$missing [ $dependency [ 'machineName' ]] = $dependency ;
2012-11-28 23:29:22 +01:00
}
}
else {
2013-03-09 01:20:55 +01:00
$missing [ $dependency [ 'machineName' ]] = $dependency ;
2012-11-28 23:29:22 +01:00
}
}
return $missing ;
2012-11-26 22:48:51 +01:00
}
2012-11-29 15:41:06 +01:00
/**
* Figure out if the provided file paths exists
*
* Triggers error messages if files doesn ' t exist
*
* @ param array $files
2013-05-01 23:24:58 +02:00
* List of file paths relative to $tmpDir
* @ param string $tmpDir
2012-11-29 15:41:06 +01:00
* Path to the directory where the $files are stored .
* @ param string $library
* Name of the library we are processing
* @ return boolean
* TRUE if all the files excists
*/
2013-05-01 23:24:58 +02:00
private function isExistingFiles ( $files , $tmpDir , $library ) {
2012-12-04 15:52:03 +01:00
foreach ( $files as $file ) {
$path = str_replace ( array ( '/' , '\\' ), DIRECTORY_SEPARATOR , $file [ 'path' ]);
2013-05-01 23:24:58 +02:00
if ( ! file_exists ( $tmpDir . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . $path )) {
2013-01-16 19:39:49 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The file "%file" is missing from library: "%name"' , array ( '%file' => $path , '%name' => $library )));
2012-11-26 22:48:51 +01:00
return FALSE ;
2012-11-22 14:00:06 +01:00
}
}
2012-11-26 22:48:51 +01:00
return TRUE ;
}
2012-11-29 15:41:06 +01:00
/**
2012-12-02 21:55:51 +01:00
* Validates h5p . json and library . json data
2012-11-29 15:41:06 +01:00
*
* Error messages are triggered if the data isn ' t valid
*
* @ param array $h5pData
* h5p data
* @ param string $library_name
* Name of the library we are processing
* @ param array $required
* Validation pattern for required properties
* @ param array $optional
* Validation pattern for optional properties
* @ return boolean
* TRUE if the $h5pData is valid
*/
2012-11-29 08:51:36 +01:00
private function isValidH5pData ( $h5pData , $library_name , $required , $optional ) {
$valid = $this -> isValidRequiredH5pData ( $h5pData , $required , $library_name );
$valid = $this -> isValidOptionalH5pData ( $h5pData , $optional , $library_name ) && $valid ;
2012-11-28 15:21:34 +01:00
return $valid ;
2012-11-28 13:56:42 +01:00
}
2012-11-29 15:41:06 +01:00
/**
* Helper function for isValidH5pData
*
* Validates the optional part of the h5pData
*
* Triggers error messages
*
* @ param array $h5pData
* h5p data
* @ param array $requirements
* Validation pattern
* @ param string $library_name
* Name of the library we are processing
* @ return boolean
* TRUE if the optional part of the $h5pData is valid
*/
2012-11-28 15:21:34 +01:00
private function isValidOptionalH5pData ( $h5pData , $requirements , $library_name ) {
$valid = TRUE ;
2012-11-28 13:56:42 +01:00
foreach ( $h5pData as $key => $value ) {
if ( isset ( $requirements [ $key ])) {
2012-12-04 15:52:03 +01:00
$valid = $this -> isValidRequirement ( $value , $requirements [ $key ], $library_name , $key ) && $valid ;
2012-11-28 13:56:42 +01:00
}
// Else: ignore, a package can have parameters that this library doesn't care about, but that library
// specific implementations does care about...
}
2012-11-28 15:21:34 +01:00
return $valid ;
2012-11-28 13:56:42 +01:00
}
2012-11-29 15:41:06 +01:00
/**
2013-03-29 16:10:29 +01:00
* Va ( lidate a requirement given as regexp or an array of requirements
2012-11-29 15:41:06 +01:00
*
2013-03-29 16:10:29 +01:00
* @ param mixed $h5pData
* The data to be validated
* @ param mixed $requirement
* The requirement the data is to be validated against , regexp or array of requirements
* @ param string $library_name
* Name of the library we are validating ( used in error messages )
* @ param string $property_name
* Name of the property we are validating ( used in error messages )
* @ return boolean
* TRUE if valid , FALSE if invalid
2012-11-29 15:41:06 +01:00
*/
2012-11-28 15:21:34 +01:00
private function isValidRequirement ( $h5pData , $requirement , $library_name , $property_name ) {
$valid = TRUE ;
2012-11-28 13:56:42 +01:00
if ( is_string ( $requirement )) {
2013-02-23 23:29:41 +01:00
if ( $requirement == 'boolean' ) {
if ( ! is_bool ( $h5pData )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( " Invalid data provided for %property in %library. Boolean expected. " , array ( '%property' => $property_name , '%library' => $library_name )));
$valid = FALSE ;
2012-11-28 13:56:42 +01:00
}
}
else {
2013-02-23 23:29:41 +01:00
// The requirement is a regexp, match it against the data
if ( is_string ( $h5pData ) || is_int ( $h5pData )) {
if ( preg_match ( $requirement , $h5pData ) === 0 ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( " Invalid data provided for %property in %library " , array ( '%property' => $property_name , '%library' => $library_name )));
$valid = FALSE ;
}
}
else {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( " Invalid data provided for %property in %library " , array ( '%property' => $property_name , '%library' => $library_name )));
$valid = FALSE ;
}
2012-11-28 13:56:42 +01:00
}
}
elseif ( is_array ( $requirement )) {
// We have sub requirements
if ( is_array ( $h5pData )) {
2012-12-04 15:52:03 +01:00
if ( is_array ( current ( $h5pData ))) {
foreach ( $h5pData as $sub_h5pData ) {
$valid = $this -> isValidRequiredH5pData ( $sub_h5pData , $requirement , $library_name ) && $valid ;
}
}
else {
$valid = $this -> isValidRequiredH5pData ( $h5pData , $requirement , $library_name ) && $valid ;
}
2012-11-28 13:56:42 +01:00
}
else {
2012-12-04 09:19:42 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( " Invalid data provided for %property in %library " , array ( '%property' => $property_name , '%library' => $library_name )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-28 13:56:42 +01:00
}
}
else {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( " Can't read the property %property in %library " , array ( '%property' => $property_name , '%library' => $library_name )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-28 13:56:42 +01:00
}
2012-11-28 15:21:34 +01:00
return $valid ;
2012-11-28 13:56:42 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Validates the required h5p data in libraray . json and h5p . json
*
* @ param mixed $h5pData
* Data to be validated
* @ param array $requirements
* Array with regexp to validate the data against
* @ param string $library_name
* Name of the library we are validating ( used in error messages )
* @ return boolean
* TRUE if all the required data exists and is valid , FALSE otherwise
*/
2012-11-28 15:21:34 +01:00
private function isValidRequiredH5pData ( $h5pData , $requirements , $library_name ) {
$valid = TRUE ;
2012-11-28 13:56:42 +01:00
foreach ( $requirements as $required => $requirement ) {
2012-11-28 14:45:54 +01:00
if ( is_int ( $required )) {
// We have an array of allowed options
2012-12-03 00:19:25 +01:00
return $this -> isValidH5pDataOptions ( $h5pData , $requirements , $library_name );
2012-11-28 14:45:54 +01:00
}
2012-11-28 13:56:42 +01:00
if ( isset ( $h5pData [ $required ])) {
2012-12-03 00:19:25 +01:00
$valid = $this -> isValidRequirement ( $h5pData [ $required ], $requirement , $library_name , $required ) && $valid ;
2012-11-28 13:56:42 +01:00
}
else {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The required property %property is missing from %library' , array ( '%property' => $required , '%library' => $library_name )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-28 13:56:42 +01:00
}
}
2012-11-28 15:21:34 +01:00
return $valid ;
2012-11-22 14:00:06 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Validates h5p data against a set of allowed values ( options )
*
* @ param array $selected
* The option ( s ) that has been specified
* @ param array $allowed
* The allowed options
* @ param string $library_name
* Name of the library we are validating ( used in error messages )
* @ return boolean
* TRUE if the specified data is valid , FALSE otherwise
*/
2012-11-28 15:21:34 +01:00
private function isValidH5pDataOptions ( $selected , $allowed , $library_name ) {
$valid = TRUE ;
2012-11-28 14:45:54 +01:00
foreach ( $selected as $value ) {
if ( ! in_array ( $value , $allowed )) {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Illegal option %option in %library' , array ( '%option' => $value , '%library' => $library_name )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-28 14:45:54 +01:00
}
}
2012-11-28 15:21:34 +01:00
return $valid ;
2012-11-28 14:45:54 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Fetch json data from file
*
2013-05-01 17:22:09 +02:00
* @ param string $filePath
2013-03-29 16:10:29 +01:00
* Path to the file holding the json string
* @ param boolean $return_as_string
* If true the json data will be decoded in order to validate it , but will be
* returned as string
* @ return mixed
* FALSE if the file can 't be read or the contents can' t be decoded
* string if the $return as string parameter is set
* array otherwise
*/
2013-05-01 17:22:09 +02:00
private function getJsonData ( $filePath , $return_as_string = FALSE ) {
$json = file_get_contents ( $filePath );
2012-11-29 08:51:36 +01:00
if ( ! $json ) {
return FALSE ;
}
2012-12-03 00:19:25 +01:00
$jsonData = json_decode ( $json , TRUE );
2012-11-29 08:51:36 +01:00
if ( ! $jsonData ) {
return FALSE ;
}
2013-01-27 23:08:48 +01:00
return $return_as_string ? $json : $jsonData ;
2012-11-29 08:51:36 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Helper function that copies an array
*
* @ param array $array
* The array to be copied
* @ return array
* Copy of $array . All objects are cloned
*/
2012-11-29 08:51:36 +01:00
private function arrayCopy ( array $array ) {
$result = array ();
foreach ( $array as $key => $val ) {
if ( is_array ( $val )) {
$result [ $key ] = arrayCopy ( $val );
}
elseif ( is_object ( $val )) {
$result [ $key ] = clone $val ;
}
else {
$result [ $key ] = $val ;
}
}
return $result ;
}
2012-11-21 14:14:07 +01:00
}
2012-11-29 22:52:41 +01:00
2013-03-29 16:10:29 +01:00
/**
* This class is used for saving H5P files
*/
2013-02-17 15:04:30 +01:00
class H5PStorage {
2012-11-29 22:52:41 +01:00
public $h5pF ;
public $h5pC ;
/**
2013-02-17 15:04:30 +01:00
* Constructor for the H5PStorage
2012-11-29 22:52:41 +01:00
*
2013-02-17 15:04:30 +01:00
* @ param object $H5PFramework
2013-02-17 15:39:26 +01:00
* The frameworks implementation of the H5PFrameworkInterface
2012-11-29 22:52:41 +01:00
*/
2013-02-17 15:04:30 +01:00
public function __construct ( $H5PFramework , $H5PCore ) {
$this -> h5pF = $H5PFramework ;
$this -> h5pC = $H5PCore ;
2012-11-29 22:52:41 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Saves a H5P file
*
* @ param int $contentId
* The id of the content we are saving
* @ param int $contentMainId
* The main id for the content we are saving . This is used if the framework
* we 're integrating with uses content id' s and version id ' s
*/
2012-12-22 07:09:17 +01:00
public function savePackage ( $contentId , $contentMainId = NULL ) {
2013-03-29 16:10:29 +01:00
// Save the libraries we processed during validation
2012-12-22 06:35:16 +01:00
foreach ( $this -> h5pC -> librariesJsonData as $key => & $library ) {
2013-03-08 18:30:53 +01:00
$libraryId = $this -> h5pF -> getLibraryId ( $key , $library [ 'majorVersion' ], $library [ 'minorVersion' ]);
2013-04-13 14:11:25 +02:00
$library [ 'saveDependencies' ] = TRUE ;
2013-03-08 18:30:53 +01:00
if ( ! $libraryId ) {
2012-12-22 06:35:16 +01:00
$new = TRUE ;
2012-11-29 22:52:41 +01:00
}
2012-12-22 06:35:16 +01:00
elseif ( $this -> h5pF -> isPatchedLibrary ( $library )) {
$new = FALSE ;
2013-03-08 18:30:53 +01:00
$library [ 'libraryId' ] = $libraryId ;
2012-12-22 06:35:16 +01:00
}
else {
2013-04-13 14:11:25 +02:00
$library [ 'libraryId' ] = $libraryId ;
2012-12-22 06:35:16 +01:00
// We already have the same or a newer version of this library
2013-04-13 14:11:25 +02:00
$library [ 'saveDependencies' ] = FALSE ;
2012-12-22 06:35:16 +01:00
continue ;
2012-12-15 15:45:29 +01:00
}
2013-06-30 16:39:17 +02:00
// If we're not allowed to save, we should not be here at all. But just
// in case, we check again.
if ( ! $this -> h5pF -> isAllowedLibraryUpdate ( $library )) {
continue ;
}
2013-02-17 14:05:02 +01:00
$this -> h5pF -> saveLibraryData ( $library , $new );
2012-12-22 06:36:42 +01:00
2012-12-22 06:35:16 +01:00
$current_path = $this -> h5pF -> getUploadedH5pFolderPath () . DIRECTORY_SEPARATOR . $key ;
2013-04-13 14:55:33 +02:00
$destination_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $this -> h5pC -> libraryToString ( $library , TRUE );
2012-12-22 06:35:16 +01:00
$this -> h5pC -> delTree ( $destination_path );
rename ( $current_path , $destination_path );
2012-11-30 15:51:39 +01:00
}
2013-04-13 14:11:25 +02:00
2013-02-24 00:12:57 +01:00
foreach ( $this -> h5pC -> librariesJsonData as $key => & $library ) {
2013-04-13 14:11:25 +02:00
if ( $library [ 'saveDependencies' ]) {
$this -> h5pF -> deleteLibraryDependencies ( $library [ 'libraryId' ]);
if ( isset ( $library [ 'preloadedDependencies' ])) {
$this -> h5pF -> saveLibraryDependencies ( $library [ 'libraryId' ], $library [ 'preloadedDependencies' ], 'preloaded' );
}
if ( isset ( $library [ 'dynamicDependencies' ])) {
$this -> h5pF -> saveLibraryDependencies ( $library [ 'libraryId' ], $library [ 'dynamicDependencies' ], 'dynamic' );
}
if ( isset ( $library [ 'editorDependencies' ])) {
$this -> h5pF -> saveLibraryDependencies ( $library [ 'libraryId' ], $library [ 'editorDependencies' ], 'editor' );
}
2013-02-24 00:12:57 +01:00
}
}
2013-03-29 16:10:29 +01:00
// Move the content folder
2012-11-30 15:51:39 +01:00
$current_path = $this -> h5pF -> getUploadedH5pFolderPath () . DIRECTORY_SEPARATOR . 'content' ;
$destination_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . $contentId ;
rename ( $current_path , $destination_path );
2013-03-29 16:10:29 +01:00
// Save what libraries is beeing used by this package/content
2012-11-30 15:51:39 +01:00
$librariesInUse = array ();
$this -> getLibraryUsage ( $librariesInUse , $this -> h5pC -> mainJsonData );
$this -> h5pF -> saveLibraryUsage ( $contentId , $librariesInUse );
2012-12-22 06:35:16 +01:00
$this -> h5pC -> delTree ( $this -> h5pF -> getUploadedH5pFolderPath ());
2013-03-07 04:12:59 +01:00
2013-03-29 16:10:29 +01:00
// Save the data in content.json
2013-03-07 04:12:59 +01:00
$contentJson = file_get_contents ( $destination_path . DIRECTORY_SEPARATOR . 'content.json' );
2013-03-08 18:30:53 +01:00
$mainLibraryId = $librariesInUse [ $this -> h5pC -> mainJsonData [ 'mainLibrary' ]][ 'library' ][ 'libraryId' ];
$this -> h5pF -> saveContentData ( $contentId , $contentJson , $this -> h5pC -> mainJsonData , $mainLibraryId , $contentMainId );
2012-11-30 15:51:39 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Delete an H5P package
*
* @ param int $contentId
* The content id
*/
2012-12-22 07:09:17 +01:00
public function deletePackage ( $contentId ) {
$this -> h5pC -> delTree ( $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . $contentId );
$this -> h5pF -> deleteContentData ( $contentId );
}
2013-03-29 16:10:29 +01:00
/**
* Update an H5P package
*
* @ param int $contentId
* The content id
* @ param int $contentMainId
* The content main id ( used by frameworks supporting revisioning )
*/
2012-12-22 08:04:09 +01:00
public function updatePackage ( $contentId , $contentMainId = NULL ) {
$this -> deletePackage ( $contentId );
$this -> savePackage ( $contentId , $contentMainId );
}
2013-03-29 16:10:29 +01:00
/**
* Copy / clone an H5P package
*
* May for instance be used if the content is beeing revisioned without
* uploading a new H5P package
*
* @ param int $contentId
* The new content id
* @ param int $copyFromId
* The content id of the content that should be cloned
* @ param int $contentMainId
* The main id of the new content ( used in frameworks that support revisioning )
*/
2012-12-22 08:04:09 +01:00
public function copyPackage ( $contentId , $copyFromId , $contentMainId = NULL ) {
$source_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . $copyFromId ;
$destination_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . $contentId ;
$this -> h5pC -> copyTree ( $source_path , $destination_path );
2013-03-07 05:31:30 +01:00
$this -> h5pF -> copyLibraryUsage ( $contentId , $copyFromId , $contentMainId );
2012-12-22 08:04:09 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Identify what libraries are beeing used taking all dependencies into account
*
* @ param array $librariesInUse
* List of libraries in use , indexed by machineName
* @ param array $jsonData
* library . json og h5p . json data holding dependency information
* @ param boolean $dynamic
* Whether or not the current library is a dynamic dependency
*/
2012-11-30 15:51:39 +01:00
public function getLibraryUsage ( & $librariesInUse , $jsonData , $dynamic = FALSE ) {
if ( isset ( $jsonData [ 'preloadedDependencies' ])) {
foreach ( $jsonData [ 'preloadedDependencies' ] as $preloadedDependency ) {
2013-03-09 01:20:55 +01:00
$library = $this -> h5pF -> loadLibrary ( $preloadedDependency [ 'machineName' ], $preloadedDependency [ 'majorVersion' ], $preloadedDependency [ 'minorVersion' ]);
$librariesInUse [ $preloadedDependency [ 'machineName' ]] = array (
2012-12-01 22:18:48 +01:00
'library' => $library ,
2012-11-30 15:51:39 +01:00
'preloaded' => $dynamic ? 0 : 1 ,
);
2012-12-01 22:18:48 +01:00
$this -> getLibraryUsage ( $librariesInUse , $library , $dynamic );
2012-11-30 15:51:39 +01:00
}
}
if ( isset ( $jsonData [ 'dynamicDependencies' ])) {
foreach ( $jsonData [ 'dynamicDependencies' ] as $dynamicDependency ) {
2013-03-09 01:20:55 +01:00
if ( ! isset ( $librariesInUse [ $dynamicDependency [ 'machineName' ]])) {
$library = $this -> h5pF -> loadLibrary ( $dynamicDependency [ 'machineName' ], $dynamicDependency [ 'majorVersion' ], $dynamicDependency [ 'minorVersion' ]);
$librariesInUse [ $dynamicDependency [ 'machineName' ]] = array (
2012-12-01 22:18:48 +01:00
'library' => $library ,
2012-11-30 15:51:39 +01:00
'preloaded' => 0 ,
);
}
2012-12-01 22:18:48 +01:00
$this -> getLibraryUsage ( $librariesInUse , $library , TRUE );
2012-11-29 22:52:41 +01:00
}
}
}
}
2013-03-29 16:10:29 +01:00
/**
* Functions and storage shared by the other H5P classes
*/
2013-02-17 15:04:30 +01:00
class H5PCore {
2013-04-03 15:39:59 +02:00
2013-04-10 17:08:57 +02:00
public static $styles = array (
'styles/h5p.css' ,
);
2013-04-03 15:39:59 +02:00
public static $scripts = array (
'js/jquery.js' ,
'js/h5p.js' ,
'js/flowplayer-3.2.12.min.js' ,
);
2012-11-29 22:52:41 +01:00
public $h5pF ;
2012-11-30 15:51:39 +01:00
public $librariesJsonData ;
public $contentJsonData ;
public $mainJsonData ;
2012-11-29 22:52:41 +01:00
/**
2013-02-17 15:04:30 +01:00
* Constructor for the H5PCore
2012-11-29 22:52:41 +01:00
*
2013-02-17 15:04:30 +01:00
* @ param object $H5PFramework
2013-02-17 15:39:26 +01:00
* The frameworks implementation of the H5PFrameworkInterface
2012-11-29 22:52:41 +01:00
*/
2013-02-17 15:04:30 +01:00
public function __construct ( $H5PFramework ) {
$this -> h5pF = $H5PFramework ;
2012-11-29 22:52:41 +01:00
}
2012-12-04 22:26:20 +01:00
2013-03-29 16:10:29 +01:00
/**
* Check if a library is of the version we ' re looking for
*
* Same verision means that the majorVersion and minorVersion is the same
*
* @ param array $library
* Data from library . json
* @ param array $dependency
* Definition of what library we ' re looking for
* @ return boolean
* TRUE if the library is the same version as the dependency
* FALSE otherwise
*/
2012-12-04 22:26:20 +01:00
public function isSameVersion ( $library , $dependency ) {
if ( $library [ 'majorVersion' ] != $dependency [ 'majorVersion' ]) {
return FALSE ;
}
if ( $library [ 'minorVersion' ] != $dependency [ 'minorVersion' ]) {
return FALSE ;
}
return TRUE ;
}
2012-12-22 06:35:16 +01:00
/**
* Recursive function for removing directories .
*
2013-03-29 16:10:29 +01:00
* @ param string $dir
* Path to the directory we ' ll be deleting
* @ return boolean
* Indicates if the directory existed .
2012-12-22 06:35:16 +01:00
*/
public function delTree ( $dir ) {
if ( ! is_dir ( $dir )) {
return ;
}
$files = array_diff ( scandir ( $dir ), array ( '.' , '..' ));
foreach ( $files as $file ) {
( is_dir ( " $dir / $file " )) ? $this -> delTree ( " $dir / $file " ) : unlink ( " $dir / $file " );
}
return rmdir ( $dir );
}
2012-12-22 08:04:09 +01:00
2013-03-29 16:10:29 +01:00
/**
* Recursive function for copying directories .
*
* @ param string $source
* Path to the directory we ' ll be copying
* @ return boolean
* Indicates if the directory existed .
*/
2012-12-22 08:04:09 +01:00
public function copyTree ( $source , $destination ) {
$dir = opendir ( $source );
@ mkdir ( $destination );
while ( false !== ( $file = readdir ( $dir ))) {
if (( $file != '.' ) && ( $file != '..' )) {
2013-06-30 15:32:12 +02:00
if ( is_dir ( $source . DIRECTORY_SEPARATOR . $file )) {
$this -> copyTree ( $source . DIRECTORY_SEPARATOR . $file , $destination . DIRECTORY_SEPARATOR . $file );
2012-12-22 08:04:09 +01:00
}
else {
2013-06-30 15:32:12 +02:00
copy ( $source . DIRECTORY_SEPARATOR . $file , $destination . DIRECTORY_SEPARATOR . $file );
2012-12-22 08:04:09 +01:00
}
}
}
closedir ( $dir );
2013-03-29 16:10:29 +01:00
}
2013-04-13 13:28:48 +02:00
/**
* Writes library data as string on the form { machineName } { majorVersion } . { minorVersion }
*
* @ param array $library
* With keys machineName , majorVersion and minorVersion
2013-07-05 17:35:59 +02:00
* @ param boolean $folderName
* Use hyphen instead of space in returned string .
2013-04-13 13:28:48 +02:00
* @ return string
* On the form { machineName } { majorVersion } . { minorVersion }
*/
2013-04-13 14:55:33 +02:00
public function libraryToString ( $library , $folderName = FALSE ) {
2013-04-13 15:03:04 +02:00
return $library [ 'machineName' ] . ( $folderName ? '-' : ' ' ) . $library [ 'majorVersion' ] . '.' . $library [ 'minorVersion' ];
2013-04-13 13:28:48 +02:00
}
2013-07-05 17:35:59 +02:00
/**
* Writes library data as string on the form { machineName } { majorVersion } . { minorVersion }
*
* @ param string $libraryString
* On the form { machineName } { majorVersion } . { minorVersion }
* @ return array | FALSE
* With keys machineName , majorVersion and minorVersion .
* Returns FALSE only if string is not parsable in the normal library
* string formats " Lib.Name-x.y " or " Lib.Name x.y "
*/
public function libraryFromString ( $libraryString ) {
$re = '/^([\w0-9\-\.]{1,255})[\-\ ]([0-9]{1,5})\.([0-9]{1,5})$/i' ;
$matches = array ();
$res = preg_match ( $re , $libraryString , $matches );
if ( $res ) {
return array (
'machineName' => $matches [ 1 ],
'majorVersion' => $matches [ 2 ],
'minorVersion' => $matches [ 3 ]
);
}
return FALSE ;
}
2012-11-29 22:52:41 +01:00
}
2013-07-05 17:35:59 +02:00
/**
* Functions for validating basic types from H5P library semantics .
*/
class H5PContentValidator {
public $h5pF ;
public $h5pC ;
private $typeMap ;
private $semanticsCache ;
/**
* Constructor for the H5PContentValidator
*
* @ param object $H5PFramework
* The frameworks implementation of the H5PFrameworkInterface
* @ param object $H5PCore
* The main H5PCore instance
*/
public function __construct ( $H5PFramework , $H5PCore ) {
$this -> h5pF = $H5PFramework ;
$this -> h5pC = $H5PCore ;
$this -> typeMap = array (
'text' => 'validateText' ,
'number' => 'validateNumber' ,
'boolean' => 'validateBoolean' ,
'list' => 'validateList' ,
'group' => 'validateGroup' ,
'image' => 'validateImage' ,
'video' => 'validateVideo' ,
'audio' => 'validateAudio' ,
'select' => 'validateSelect' ,
'library' => 'validateLibrary' ,
);
// Cache for semantics used within this validation to avoid unneccessary
// json_decodes if a library is used multiple times.
$this -> semanticsCache = array ();
}
/**
* Validate the given value from content with the matching semantics
* object from semantics
*
* Function will recurse via external functions for container objects like
* 'list' , 'group' and 'library' .
*
* @ param object $value
* Object to be verified . May be a string or an array . ( normal or keyed )
* @ param object $semantics
* Semantics object from semantics . json for main library . Further
* semantics will be loaded from H5PFramework if any libraries are
* found within the value data .
*/
public function validateBySemantics ( & $value , $semantics ) {
// dd('validateBySemantics');
$fakebaseobject = ( object ) array (
'type' => 'group' ,
'fields' => $semantics ,
);
$this -> validateGroup ( $value , $fakebaseobject );
}
/**
* Validate given text value against text semantics .
*/
public function validateText ( & $text , $semantics ) {
// dd('validateText');
if ( $semantics -> widget && $semantics -> widget == 'html' ) {
// FIXME: Implicit tags added in javascript are NOT vissible in
// $semantics->tags (such as mathml etc)
$allowedtags = implode ( '' , array_map ( array ( $this , 'bracketTags' ), $semantics -> tags ));
// Strip invalid HTML tags.
$text = strip_tags ( $text , $allowedtags );
}
else {
// Filter text to plain text.
$text = htmlspecialchars ( $text , ENT_QUOTES , 'UTF-8' );
}
// TODO: Check if string is within allowed length
// TODO: Check if string is according to optional regexp in semantics
}
private function bracketTags ( $tag ) {
return '<' . $tag . '>' ;
}
/**
* Validate given value against number semantics
*/
public function validateNumber ( & $number , $semantics ) {
// Validate that $number is indeed a number
if ( ! is_numeric ( $number )) {
$number = 0 ;
}
// TODO: Check if number is within valid bounds.
// TODO: Check if number is within allowed bounds even if step value is set.
// TODO: Check if number has proper number of decimals.
}
/**
* Validate given value against boolean semantics
*/
public function validateBoolean ( & $bool , $semantics ) {
if ( ! is_bool ( $bool )) {
$bool = FALSE ;
}
}
/**
* Validate select values
*/
public function validateSelect ( & $select , $semantics ) {
if ( ! in_array ( $select , array_map ( array ( $this , 'map_object_value' ), $semantics -> options ))) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid selected option in select.' ));
$select = $semantics -> options [ 0 ] -> value ;
}
}
private function map_object_value ( $o ) {
return $o -> value ;
}
/**
* Validate given list value agains list semantics .
* Will recurse into validating each item in the list according to the type .
*/
public function validateList ( & $list , $semantics ) {
// dd('validateList');
$field = $semantics -> field ;
// WTF happens in content with lists of libraries?
if ( $semantics -> field -> type == 'group'
&& $semantics -> field -> fields [ 0 ] -> type == 'library' ) {
$field = $semantics -> field -> fields [ 0 ];
}
$function = $this -> typeMap [ $field -> type ];
foreach ( $list as $key => $value ) {
$this -> $function ( $value , $field );
}
// TODO: Check that list is not longer than allowed length
}
/**
* Validate given image data
*/
public function validateImage ( & $image , $semantics ) {
$image -> path = htmlspecialchars ( $image -> path );
if ( $image -> mime && substr ( $image -> mime , 0 , 5 ) !== 'image' ) {
unset ( $image -> mime );
}
}
/**
* Validate given video data
*/
public function validateVideo ( & $video , $semantics ) {
$video -> path = htmlspecialchars ( $video -> path );
if ( $video -> mime && substr ( $video -> mime , 0 , 5 ) !== 'video' ) {
unset ( $video -> mime );
}
}
/**
* Validate given audio data
*/
public function validateAudio ( & $audio , $semantics ) {
$audio -> path = htmlspecialchars ( $audio -> path );
if ( $audio -> mime && substr ( $audio -> mime , 0 , 5 ) !== 'audio' ) {
unset ( $audio -> mime );
}
}
/**
* Validate given group value against group semantics .
* Will recurse into validating each group member .
*/
public function validateGroup ( & $group , $semantics ) {
// dd('validateGroup');
// dd(print_r($group, TRUE));
// dd(print_r($semantics, TRUE));
foreach ( $group as $key => & $value ) {
// dd("time for $key");
// Find semantics for name=$key
$found = FALSE ;
foreach ( $semantics -> fields as $field ) {
if ( $field -> name == $key ) {
$function = $this -> typeMap [ $field -> type ];
$found = TRUE ;
// dd(print_r($field, TRUE));
// dd("calling dr. $function");
break ;
}
}
if ( $found ) {
$this -> $function ( $value , $field );
}
else {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'H5P internal error: no validator exists for ' . $key ));
// dd('WTF!?');
// dd(print_r($group, TRUE));
}
}
}
/**
* Validate given library value against library semantics .
*
* Will recurse into validating the library ' s semantics too .
*/
public function validateLibrary ( & $value , $semantics ) {
// dd('validLibrary');
// Check if provided library is within allowed options
if ( in_array ( $value -> library , $semantics -> options )) {
if ( isset ( $semanticsCache [ $value -> library ])) {
$librarySemantics = $semanticsCache [ $value -> library ];
}
else {
$libspec = $this -> h5pC -> libraryFromString ( $value -> library );
$library = $this -> h5pF -> loadLibrary ( $libspec [ 'machineName' ], $libspec [ 'majorVersion' ], $libspec [ 'minorVersion' ]);
$librarySemantics = json_decode ( $library [ 'semantics' ]);
$semanticsCache [ $value -> library ] = $librarySemantics ;
}
return $this -> validateBySemantics ( $value -> params , $librarySemantics );
}
else {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Library used in content is not a valid library according to semantics' ));
// dd('Value: '.print_r($value, TRUE));
// dd('Semantics: '.print_r($semantics, TRUE));
}
}
}
2012-11-29 22:52:41 +01:00
?>