2012-11-21 14:14:07 +01:00
< ? php
2012-12-22 06:35:16 +01:00
2012-11-21 14:14:07 +01:00
interface h5pFramework {
2012-11-22 14:00:06 +01:00
public function setErrorMessage ( $message );
public function setInfoMessage ( $message );
2012-12-03 00:19:25 +01:00
public function t ( $message , $replacements = array ());
2012-11-30 15:51:39 +01:00
public function getUploadedH5pFolderPath ();
public function getH5pPath ();
2012-11-22 14:00:06 +01:00
public function getUploadedH5pPath ();
2012-12-22 06:35:16 +01:00
public function getLibraryId ( $machineName , $majorVersion , $minorVersion );
public function isPatchedLibrary ( $library );
2012-12-03 00:19:25 +01:00
public function storeLibraryData ( & $libraryData );
2012-12-22 07:09:17 +01:00
public function storeContentData ( $contentId , $contentJson , $mainJsonData , $contentMainId = NULL );
public function deleteContentData ( $contentId );
2012-11-30 15:51:39 +01:00
public function saveLibraryUsage ( $contentId , $librariesInUse );
2012-12-04 22:26:20 +01:00
public function loadLibrary ( $machineName , $majorVersion , $minorVersion );
2012-11-21 14:14:07 +01:00
}
2012-11-26 22:48:51 +01:00
class h5pValidator {
2012-11-22 14:00:06 +01:00
public $h5pF ;
2012-11-29 22:52:41 +01:00
public $h5pC ;
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 (
2012-12-04 15:52:03 +01:00
'machineName' => '/^[a-z0-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
),
2012-11-28 14:06:55 +01:00
'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i' ,
2012-11-24 01:09:25 +01:00
'embedTypes' => array ( 'iframe' , 'div' ),
);
private $h5pOptional = array (
2012-11-28 14:06:55 +01:00
'contentType' => '/^.{1,255}$/' ,
2012-12-02 21:55:51 +01:00
'description' => '/^.{1,}$/' ,
2012-11-28 14:06:55 +01:00
'author' => '/^.{1,255}$/' ,
2012-12-04 20:47:55 +01:00
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr)$/' ,
2012-11-24 01:09:25 +01:00
'dynamicDependencies' => array (
2012-12-04 15:52:03 +01:00
'machineName' => '/^[a-z0-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
),
2012-12-02 21:55:51 +01:00
'w' => '/^[0-9]{1,4}$/' ,
'h' => '/^[0-9]{1,4}$/' ,
'metaKeywords' => '/^.{1,}$/' ,
'metaDescription' => '/^.{1,}$/k' ,
);
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}$/' ,
2012-12-04 15:52:03 +01:00
'machineName' => '/^[a-z0-9\-]{1,255}$/i' ,
2012-12-02 21:55:51 +01:00
);
private $libraryOptional = array (
'init' => '/^[$a-z_][0-9a-z_\.$]{1,254}$/i' ,
'author' => '/^.{1,255}$/' ,
2012-12-04 20:47:55 +01:00
'license' => '/^(cc-by|cc-by-sa|cc-by-nd|cc-by-nc|cc-by-nc-sa|cc-by-nc-nd|pd|cr)$/' ,
2012-12-02 21:55:51 +01:00
'description' => '/^.{1,}$/' ,
'dynamicDependencies' => array (
2012-12-04 15:52:03 +01:00
'machineName' => '/^[a-z0-9\-]{1,255}$/i' ,
2012-12-04 22:26:20 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
2012-12-02 21:55:51 +01:00
),
'preloadedDependencies' => array (
2012-12-04 15:52:03 +01:00
'machineName' => '/^[a-z0-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
),
2012-11-30 15:51:39 +01:00
'preloadedJs' => array (
2012-12-04 15:52:03 +01:00
'path' => '/^((\\\|\/)?[a-z_\-\s0-9]+)+\.js$/i' ,
2012-11-30 15:51:39 +01:00
),
'preloadedCss' => array (
2012-12-04 15:52:03 +01:00
'path' => '/^((\\\|\/)?[a-z_\-\s0-9]+)+\.css$/i' ,
2012-11-30 15:51:39 +01:00
),
2012-11-28 14:06: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' ),
2012-11-24 01:09:25 +01:00
);
2012-11-22 14:00:06 +01:00
2012-11-29 15:41:06 +01:00
/**
* Constructor for the h5pValidator
*
* @ param object $h5pFramework
* The frameworks implementation of the h5pFramework interface
*/
2012-11-29 22:52:41 +01:00
public function __construct ( $h5pFramework , $h5pCore ) {
2012-11-22 14:00:06 +01:00
$this -> h5pF = $h5pFramework ;
2012-11-29 22:52:41 +01:00
$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.
2012-12-03 00:19:25 +01:00
$tmp_dir = $this -> h5pF -> getUploadedH5pFolderPath ();
$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 ) {
$zip -> extractTo ( $tmp_dir );
$zip -> close ();
}
else {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The file you uploaded is not a valid HTML5 Pack.' ));
2012-12-22 06:35:16 +01:00
$this -> h5pC -> delTree ( $tmp_dir );
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 ();
2012-11-23 17:06:03 +01:00
$files = scandir ( $tmp_dir );
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 ) {
if ( in_array ( $file , array ( '.' , '..' ))) {
continue ;
}
$file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file ;
if ( strtolower ( $file ) == 'h5p.json' ) {
2012-11-29 15:41:06 +01:00
$mainH5pData = $this -> getJsonData ( $file_path );
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
}
2012-11-22 14:00:06 +01:00
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
}
elseif ( $file == 'content' ) {
2012-12-22 06:35:16 +01:00
if ( ! is_dir ( $file_path )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid content folder' ));
$valid = FALSE ;
continue ;
}
2012-11-30 15:51:39 +01:00
$contentJsonData = $this -> getJsonData ( $file_path . DIRECTORY_SEPARATOR . 'content.json' );
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 ;
2012-11-29 08:51:36 +01:00
// In the future we might let the librarys provide validation functions for content.json
2012-11-28 15:21:34 +01:00
}
2012-11-23 17:06:03 +01:00
}
elseif ( strpos ( $file , '.' ) !== FALSE ) {
// Illegal file fond. This is ignored.
continue ;
}
2012-11-22 14:00:06 +01:00
2012-11-23 17:06:03 +01:00
else {
2012-12-22 06:35:16 +01:00
if ( ! is_dir ( $file_path )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid library folder: %name' , array ( '%name' => $file )));
$valid = FALSE ;
continue ;
}
2012-12-04 15:52:03 +01:00
if ( preg_match ( '/^[a-z0-9\-]{1,255}$/i' , $file ) === 0 ) {
2012-11-29 22:52:41 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid library name: %name' , array ( '%name' => $file )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-22 14:00:06 +01:00
continue ;
}
2012-12-02 21:55:51 +01:00
$h5pData = $this -> getJsonData ( $file_path . DIRECTORY_SEPARATOR . 'library.json' );
2012-11-29 08:51:36 +01:00
if ( $h5pData === FALSE ) {
2012-12-02 21:55:51 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not find library.json file with valid json format for library %name' , array ( '%name' => $file )));
2012-11-28 15:21:34 +01:00
$valid = FALSE ;
2012-11-22 14:00:06 +01:00
continue ;
}
2012-11-29 08:51:36 +01:00
2012-12-04 15:52:03 +01:00
$validLibrary = $this -> isValidH5pData ( $h5pData , $file , $this -> libraryRequired , $this -> libraryOptional );
2012-11-28 15:31:55 +01:00
2012-12-04 15:52:03 +01:00
if ( isset ( $h5pData [ 'preloadedJs' ])) {
$validLibrary = $this -> isExcistingFiles ( $h5pData [ 'preloadedJs' ], $tmp_dir , $file ) && $validLibrary ;
2012-11-22 14:00:06 +01:00
}
2012-12-04 15:52:03 +01:00
if ( isset ( $h5pData [ 'preloadedCss' ])) {
$validLibrary = $this -> isExcistingFiles ( $h5pData [ 'preloadedCss' ], $tmp_dir , $file ) && $validLibrary ;
2012-11-22 14:00:06 +01:00
}
2012-11-28 15:31:55 +01:00
if ( $validLibrary ) {
2012-12-22 06:35:16 +01:00
$libraries [ $file ] = $h5pData ;
2012-11-28 15:31:55 +01:00
}
$valid = $validLibrary && $valid ;
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 ;
2012-12-22 06:35:16 +01:00
$libraries [ 'mainH5pData' ] = $mainH5pData ;
2012-11-28 23:29:22 +01:00
$missingLibraries = $this -> getMissingLibraries ( $libraries );
foreach ( $missingLibraries as $missing ) {
2012-12-22 06:35:16 +01:00
if ( $this -> h5pF -> getLibraryId ( $missing [ 'machineName' ], $missing [ 'majorVersion' ], $missing [ 'minorVersion' ])) {
2012-11-28 23:29:22 +01:00
unset ( $missingLibraries [ $missing [ 'machineName' ]]);
}
}
$valid = empty ( $missingLibraries ) && $valid ;
}
if ( ! $valid ) {
2012-12-22 06:35:16 +01:00
$this -> h5pC -> delTree ( $tmp_dir );
2012-11-28 23:29:22 +01:00
}
return $valid ;
}
2012-11-29 15:41:06 +01:00
/**
* Use the dependency declarations to find any missing libraries
*
* @ param array $libraries
2012-12-04 20:56:16 +01:00
* A multidimensional array of libraries keyed with machineName first and majorVersion second
2012-11-29 15:41:06 +01:00
* @ return array
* A list of libraries that are missing keyed with machineName and holds objects with
2012-12-04 22:26:20 +01:00
* 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' ])) {
array_merge ( $missing , $this -> getMissingDependencies ( $library [ 'preloadedDependencies' ], $libraries ));
}
if ( isset ( $library [ 'dynamicDependencies' ])) {
array_merge ( $missing , $this -> getMissingDependencies ( $library [ 'dynamicDependencies' ], $libraries ));
}
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
2012-12-04 22:26:20 +01:00
* A list of objects with machineName , majorVersion and minorVersion properties
2012-11-29 15:41:06 +01:00
* @ param array $libraries
2012-12-22 06:35:16 +01:00
* An array of libraries keyed with machineName
2012-11-29 15:41:06 +01:00
* @ return
* A list of libraries that are missing keyed with machineName and holds objects with
2012-12-04 22:26:20 +01:00
* 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 ) {
2012-11-28 23:29:22 +01:00
if ( isset ( $libraries [ $dependency [ 'machineName' ]])) {
2012-12-04 22:35:54 +01:00
if ( ! $this -> h5pC -> isSameVersion ( $libraries [ $dependency [ 'machineName' ]], $dependency )) {
2012-11-28 23:29:22 +01:00
$missing [ $dependency [ 'machineName' ]] = $dependency ;
}
}
else {
$missing [ $dependency [ 'machineName' ]] = $dependency ;
}
}
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
* List of file paths relative to $tmp_dir
* @ param string $tmp_dir
* 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
*/
2012-11-26 22:48:51 +01:00
private function isExcistingFiles ( $files , $tmp_dir , $library ) {
2012-12-04 15:52:03 +01:00
foreach ( $files as $file ) {
$path = str_replace ( array ( '/' , '\\' ), DIRECTORY_SEPARATOR , $file [ '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 )));
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
/**
*
*
* @ param < type > $h5pData
* @ param < type > $requirement
* @ param < type > $library_name
* @ param < type > $property_name
* @ return boolean
*/
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 )) {
// The requirement is a regexp, match it against the data
2012-12-03 00:19:25 +01:00
if ( is_string ( $h5pData ) || is_int ( $h5pData )) {
2012-11-28 13:56:42 +01:00
if ( preg_match ( $requirement , $h5pData ) === 0 ) {
2012-12-04 09:19:16 +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-12-04 09:19:16 +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
}
}
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
}
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
}
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
}
2012-11-29 08:51:36 +01:00
private function getJsonData ( $file_path ) {
$json = file_get_contents ( $file_path );
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 ;
}
return $jsonData ;
}
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
class h5pSaver {
public $h5pF ;
public $h5pC ;
/**
* Constructor for the h5pSaver
*
* @ param object $h5pFramework
* The frameworks implementation of the h5pFramework interface
*/
public function __construct ( $h5pFramework , $h5pCore ) {
$this -> h5pF = $h5pFramework ;
$this -> h5pC = $h5pCore ;
}
2012-12-22 07:09:17 +01:00
public function savePackage ( $contentId , $contentMainId = NULL ) {
2012-12-22 06:35:16 +01:00
foreach ( $this -> h5pC -> librariesJsonData as $key => & $library ) {
$libraryId = $this -> h5pF -> getLibraryId ( $key , $library [ 'majorVersion' ], $library [ 'minorVersion' ]);
if ( ! $libraryId ) {
$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 ;
$library [ 'libraryId' ] = $libraryId ;
}
else {
// We already have the same or a newer version of this library
continue ;
2012-12-15 15:45:29 +01:00
}
2012-12-22 06:35:16 +01:00
$this -> h5pF -> storeLibraryData ( $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 ;
$destination_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $library [ 'libraryId' ];
$this -> h5pC -> delTree ( $destination_path );
rename ( $current_path , $destination_path );
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 );
2012-12-04 15:52:03 +01:00
$contentJson = file_get_contents ( $destination_path . DIRECTORY_SEPARATOR . 'content.json' );
2012-12-22 07:09:17 +01:00
$this -> h5pF -> storeContentData ( $contentId , $contentJson , $this -> h5pC -> mainJsonData , $contentMainId );
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 ());
2012-11-30 15:51:39 +01:00
}
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 );
}
2012-11-30 15:51:39 +01:00
public function getLibraryUsage ( & $librariesInUse , $jsonData , $dynamic = FALSE ) {
if ( isset ( $jsonData [ 'preloadedDependencies' ])) {
foreach ( $jsonData [ 'preloadedDependencies' ] as $preloadedDependency ) {
2012-12-04 22:26:20 +01:00
$library = $this -> h5pF -> loadLibrary ( $preloadedDependency [ 'machineName' ], $preloadedDependency [ 'majorVersion' ], $preloadedDependency [ 'minorVersion' ]);
2012-11-30 15:51:39 +01:00
$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 ) {
if ( ! isset ( $librariesInUse [ $dynamicDependency [ 'machineName' ]])) {
2012-12-04 22:26:20 +01:00
$library = $this -> h5pF -> loadLibrary ( $dynamicDependency [ 'machineName' ], $dynamicDependency [ 'majorVersion' ], $dynamicDependency [ 'minorVersion' ]);
2012-11-30 15:51:39 +01:00
$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
}
}
}
}
class h5pCore {
public $h5pF ;
2012-11-30 15:51:39 +01:00
public $librariesJsonData ;
public $contentJsonData ;
public $mainJsonData ;
2012-11-29 22:52:41 +01:00
/**
* Constructor for the h5pSaver
*
* @ param object $h5pFramework
* The frameworks implementation of the h5pFramework interface
*/
public function __construct ( $h5pFramework ) {
$this -> h5pF = $h5pFramework ;
}
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 .
*
* @ param string $dir Directory .
* @ return boolean Indicates if the directory existed .
*/
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-11-29 22:52:41 +01:00
}
?>