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 {
2014-03-26 08:43:29 +01:00
2014-09-05 09:59:16 +02:00
/**
* Returns info for the current platform
2014-10-10 13:56:48 +02:00
*
* @ return array
2014-10-08 10:16:16 +02:00
* An associative array containing :
* - name : The name of the plattform , for instance " Wordpress "
* - version : The version of the pattform , for instance " 4.0 "
2014-11-02 13:46:09 +01:00
* - h5pVersion : The version of the H5P plugin / module
2014-09-05 09:59:16 +02:00
*/
public function getPlatformInfo ();
2014-10-10 13:56:48 +02:00
2014-11-02 14:29:13 +01:00
/**
* Fetches a file from a remote server using HTTP GET
*
* @ param $url
* @ return string The content ( response body ) . NULL if something went wrong
*/
public function fetchExternalData ( $url );
2014-11-01 17:05:15 +01:00
/**
* Set the tutorial URL for a library . All versions of the library is set
*
* @ param string $machineName
* @ param string $tutorialUrl
*/
public function setLibraryTutorialUrl ( $machineName , $tutorialUrl );
2013-01-14 23:47:50 +01:00
/**
* Show the user an error message
2013-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* @ param string $message
2014-10-08 10:16:16 +02:00
* The error message
2013-01-14 23:47:50 +01:00
*/
2012-11-22 14:00:06 +01:00
public function setErrorMessage ( $message );
2013-07-09 10:14:42 +02:00
2013-01-14 23:47:50 +01:00
/**
* Show the user an information message
2013-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* @ 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
2013-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* @ param string $message
2013-07-09 10:14:42 +02:00
* The english string to be translated .
2013-01-14 23:47:50 +01:00
* @ 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
2014-10-08 10:16:16 +02:00
* @ return string
* Translated string
2013-01-14 23:47:50 +01:00
*/
2012-12-03 00:19:25 +01:00
public function t ( $message , $replacements = array ());
2013-07-09 10:14:42 +02:00
2013-01-14 23:47:50 +01:00
/**
* Get the Path to the last uploaded h5p
2013-07-09 10:14:42 +02:00
*
2014-10-08 10:16:16 +02:00
* @ return string
* Path to the folder where the last uploaded h5p for this session is located .
2013-01-14 23:47:50 +01:00
*/
2012-11-30 15:51:39 +01:00
public function getUploadedH5pFolderPath ();
2013-07-09 10:14:42 +02:00
2013-01-14 23:47:50 +01:00
/**
2014-10-08 10:16:16 +02:00
* @ return string
* Path to the folder where all h5p files are stored
2013-01-14 23:47:50 +01:00
*/
2012-11-30 15:51:39 +01:00
public function getH5pPath ();
2013-07-09 10:14:42 +02:00
2013-01-14 23:47:50 +01:00
/**
* Get the path to the last uploaded h5p file
2013-07-09 10:14:42 +02:00
*
2014-10-08 10:16:16 +02:00
* @ return string
* Path to the last uploaded h5p
2013-01-14 23:47:50 +01:00
*/
2012-11-22 14:00:06 +01:00
public function getUploadedH5pPath ();
2014-10-10 13:56:48 +02:00
2014-08-19 10:14:55 +02:00
/**
2014-10-08 10:16:16 +02:00
* Get a list of the current installed libraries
2014-10-10 13:56:48 +02:00
*
* @ return array
2014-10-08 10:16:16 +02:00
* Associative array containg one entry per machine name .
* For each machineName there is a list of libraries ( with different versions )
2014-08-19 10:14:55 +02:00
*/
public function loadLibraries ();
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
* Saving the unsupported library list
2014-10-10 13:56:48 +02:00
*
2014-10-08 10:16:16 +02:00
* @ param array
* A list of unsupported libraries . Each list entry contains :
* - name : MachineName for the library
* - downloadUrl : URL to a location a new version of the library may be downloaded from
* - currentVersion : The unsupported version of the library installed on the system .
* This is an associative array containing :
* - major : The major version of the library
* - minor : The minor version of the library
* - patch : The patch version of the library
2014-08-22 14:03:00 +02:00
*/
public function setUnsupportedLibraries ( $libraries );
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
* Returns unsupported libraries
2014-10-10 13:56:48 +02:00
*
2014-10-08 10:16:16 +02:00
* @ return array
* A list of unsupported libraries . Each entry contains an associative array with :
* - name : MachineName for the library
* - downloadUrl : URL to a location a new version of the library may be downloaded from
* - currentVersion : The unsupported version of the library installed on the system .
* This is an associative array containing :
* - major : The major version of the library
* - minor : The minor version of the library
* - patch : The patch version of the library
2014-08-22 14:03:00 +02:00
*/
public function getUnsupportedLibraries ();
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
* Returns the URL to the library admin page
2014-10-10 13:56:48 +02:00
*
2014-10-08 10:16:16 +02:00
* @ return string
* URL to admin page
2014-08-22 14:03:00 +02:00
*/
public function getAdminUrl ();
2013-07-09 10:14:42 +02:00
2013-01-14 23:47:50 +01:00
/**
2014-10-10 13:57:43 +02:00
* Get id to an existing library
2013-07-09 10:14:42 +02:00
*
2013-03-09 01:20:55 +01:00
* @ param string $machineName
2014-10-08 10:16:16 +02:00
* The librarys machine name
2013-01-14 23:47:50 +01:00
* @ param int $majorVersion
2014-10-08 10:16:16 +02:00
* The librarys major version
2013-01-14 23:47:50 +01:00
* @ param int $minorVersion
2014-10-08 10:16:16 +02:00
* The librarys minor version
2013-01-14 23:47:50 +01:00
* @ return int
2014-10-08 10:16:16 +02:00
* The id of the specified library or FALSE
2013-01-14 23:47:50 +01:00
*/
2013-03-09 01:20:55 +01:00
public function getLibraryId ( $machineName , $majorVersion , $minorVersion );
2013-07-15 16:25:10 +02:00
2013-07-13 22:25:18 +02:00
/**
* Get file extension whitelist
2013-07-15 16:25:10 +02:00
*
2013-07-13 22:25:18 +02:00
* The default extension list is part of h5p , but admins should be allowed to modify it
2013-07-15 16:25:10 +02:00
*
2013-07-13 22:25:18 +02:00
* @ param boolean $isLibrary
2014-10-08 10:16:16 +02:00
* TRUE if this is the whitelist for a library . FALSE if it is the whitelist
* for the content folder we are getting
2013-07-13 22:25:18 +02:00
* @ param string $defaultContentWhitelist
2014-10-08 10:16:16 +02:00
* A string of file extensions separated by whitespace
2013-07-13 22:25:18 +02:00
* @ param string $defaultLibraryWhitelist
2014-10-08 10:16:16 +02:00
* A string of file extensions separated by whitespace
2013-07-13 22:25:18 +02:00
*/
public function getWhitelist ( $isLibrary , $defaultContentWhitelist , $defaultLibraryWhitelist );
2014-10-10 13:56:48 +02:00
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-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* @ param object $library
2014-10-08 10:16:16 +02:00
* An associateve array containing :
* - machineName : The library machineName
* - majorVersion : The librarys majorVersion
* - minorVersion : The librarys minorVersion
* - patchVersion : The librarys patchVersion
2013-01-14 23:47:50 +01:00
* @ return boolean
2014-10-10 13:57:43 +02:00
* TRUE if the library is a patched version of an existing library
2014-10-08 10:16:16 +02:00
* FALSE otherwise
2013-01-14 23:47:50 +01:00
*/
2012-12-22 06:35:16 +01:00
public function isPatchedLibrary ( $library );
2013-10-21 13:22:13 +02:00
2013-08-12 08:11:11 +02:00
/**
* Is H5P in development mode ?
2013-10-21 13:22:13 +02:00
*
2013-08-12 08:11:11 +02:00
* @ return boolean
* TRUE if H5P development mode is active
* FALSE otherwise
*/
public function isInDevMode ();
2013-07-09 10:14:42 +02:00
2013-06-30 16:39:17 +02:00
/**
2013-07-15 17:36:56 +02:00
* Is the current user allowed to update libraries ?
2013-06-30 16:39:17 +02:00
*
* @ return boolean
2013-07-15 17:36:56 +02:00
* TRUE if the user is allowed to update libraries
* FALSE if the user is not allowed to update libraries
2013-06-30 16:39:17 +02:00
*/
2013-07-15 17:36:56 +02:00
public function mayUpdateLibraries ();
2013-06-30 16:39:17 +02:00
2013-01-14 23:47:50 +01:00
/**
* Store data about a library
2013-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* Also fills in the libraryId in the libraryData object if the object is new
2013-07-09 10:14:42 +02:00
*
2013-01-14 23:47:50 +01:00
* @ param object $libraryData
2014-10-08 10:16:16 +02:00
* Associative array containing :
* - libraryId : The id of the library if it is an existing library .
* - title : The library ' s name
* - machineName : The library machineName
* - majorVersion : The library ' s majorVersion
* - minorVersion : The library ' s minorVersion
* - patchVersion : The library ' s patchVersion
* - runnable : 1 if the library is a content type , 0 otherwise
* - fullscreen ( optional ) : 1 if the library supports fullscreen , 0 otherwise
* - embedTypes ( optional ) : list of supported embed types
* - preloadedJs ( optional ) : list of associative arrays containing :
* - path : path to a js file relative to the library root folder
* - preloadedCss ( optional ) : list of associative arrays containing :
* - path : path to css file relative to the library root folder
* - dropLibraryCss ( optional ) : list of associative arrays containing :
* - machineName : machine name for the librarys that are to drop their css
* - semantics ( optional ) : Json describing the content structure for the library
* - language ( optional ) : associative array containing :
2014-10-10 13:56:48 +02:00
* - languageCode : Translation in json format
2013-01-14 23:47:50 +01:00
*/
2013-09-10 22:43:31 +02:00
public function saveLibraryData ( & $libraryData , $new = TRUE );
2014-10-10 13:56:48 +02:00
2013-01-14 23:47:50 +01:00
/**
2014-05-27 10:55:46 +02:00
* Insert new content .
2014-10-10 13:56:48 +02:00
*
2014-10-08 10:16:16 +02:00
* @ param array $content
* An associative array containing :
* - id : The content id
* - params : The content in json format
* - library : An associative array containing :
* - libraryId : The id of the main library for this content
2013-01-14 23:47:50 +01:00
* @ param int $contentMainId
2014-10-08 10:16:16 +02:00
* Main id for the content if this is a system that supports versioning
2013-01-14 23:47:50 +01:00
*/
2014-05-27 10:55:46 +02:00
public function insertContent ( $content , $contentMainId = NULL );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
* Update old content .
2014-10-10 13:56:48 +02:00
*
2014-10-08 10:16:16 +02:00
* @ param array $content
* An associative array containing :
* - id : The content id
* - params : The content in json format
* - library : An associative array containing :
* - libraryId : The id of the main library for this content
2014-05-27 10:55:46 +02:00
* @ param int $contentMainId
2014-10-08 10:16:16 +02:00
* Main id for the content if this is a system that supports versioning
2014-05-27 10:55:46 +02:00
*/
public function updateContent ( $content , $contentMainId = NULL );
2014-10-10 13:56:48 +02:00
2013-03-29 16:35:54 +01:00
/**
* Save what libraries a library is dependending on
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:35:54 +01:00
* @ param int $libraryId
2014-10-08 10:16:16 +02:00
* Library Id for the library we ' re saving dependencies for
2013-03-29 16:35:54 +01:00
* @ param array $dependencies
2014-10-08 10:16:16 +02:00
* List of dependencies as associative arrays containing :
* - machineName : The library machineName
* - majorVersion : The library ' s majorVersion
* - minorVersion : The library ' s minorVersion
2013-03-29 16:35:54 +01:00
* @ param string $dependency_type
2014-10-08 10:16:16 +02:00
* What type of dependency this is , the following values are allowed :
* - editor
* - preloaded
* - dynamic
2013-03-29 16:35:54 +01:00
*/
public function saveLibraryDependencies ( $libraryId , $dependencies , $dependency_type );
2013-02-15 11:38:49 +01:00
/**
2014-10-08 10:16:16 +02:00
* Give an H5P the same library dependencies as a given H5P
2013-02-15 11:38:49 +01:00
*
* @ param int $contentId
2014-10-08 10:16:16 +02:00
* Id identifying the content
2013-02-15 11:38:49 +01:00
* @ param int $copyFromId
2014-10-08 10:16:16 +02:00
* Id identifying the content to be copied
2013-02-15 11:38:49 +01:00
* @ param int $contentMainId
2014-10-08 10:16:16 +02:00
* 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-02-15 11:38:49 +01:00
*/
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
2014-10-08 10:16:16 +02:00
* Id identifying the content
2013-02-15 11:38:49 +01:00
*/
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
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:35:54 +01:00
* @ param int $contentId
2014-10-08 10:16:16 +02:00
* Content Id of the content we ' ll be deleting library usage for
2013-03-29 16:35:54 +01:00
*/
public function deleteLibraryUsage ( $contentId );
2013-07-09 10:14:42 +02:00
2013-02-15 11:38:49 +01:00
/**
* Saves what libraries the content uses
*
* @ param int $contentId
2014-10-08 10:16:16 +02:00
* Id identifying the content
2013-02-15 11:38:49 +01:00
* @ param array $librariesInUse
2014-10-08 10:16:16 +02:00
* List of libraries the content uses . Libraries consist of associative arrays with :
* - library : Associative array containing :
* - dropLibraryCss ( optional ) : commasepareted list of machineNames
* - machineName : Machine name for the library
* - libraryId : Id of the library
2014-10-09 19:29:20 +02:00
* - type : The dependency type . Allowed values :
* - editor
* - dynamic
* - preloaded
2013-02-15 11:38:49 +01:00
*/
2012-11-30 15:51:39 +01:00
public function saveLibraryUsage ( $contentId , $librariesInUse );
2013-02-15 11:38:49 +01:00
2014-03-26 08:43:29 +01:00
/**
2014-10-10 13:56:48 +02:00
* Get number of content / nodes using a library , and the number of
2014-03-26 08:43:29 +01:00
* dependencies to other libraries
2014-10-10 13:56:48 +02:00
*
2014-10-10 13:57:43 +02:00
* @ param int $libraryId
2014-10-09 19:29:20 +02:00
* Library identifier
* @ return array
* Associative array containing :
* - content : Number of content using the library
* - libraries : Number of libraries depending on the library
2014-03-26 08:43:29 +01:00
*/
public function getLibraryUsage ( $libraryId );
2013-02-15 11:38:49 +01:00
/**
* Loads a library
*
2013-03-09 01:20:55 +01:00
* @ param string $machineName
2014-10-09 19:29:20 +02:00
* The library ' s machine name
2013-02-15 11:38:49 +01:00
* @ param int $majorVersion
2014-10-09 19:29:20 +02:00
* The library ' s major version
2013-02-15 11:38:49 +01:00
* @ param int $minorVersion
2014-10-09 19:29:20 +02:00
* The library ' s minor version
2013-02-15 11:38:49 +01:00
* @ return array | FALSE
2014-10-09 19:29:20 +02:00
* FALSE if the library doesn ' t exist .
* Otherwise an associative array containing :
* - libraryId : The id of the library if it is an existing library .
* - title : The library ' s name
* - machineName : The library machineName
* - majorVersion : The library ' s majorVersion
* - minorVersion : The library ' s minorVersion
* - patchVersion : The library ' s patchVersion
* - runnable : 1 if the library is a content type , 0 otherwise
* - fullscreen ( optional ) : 1 if the library supports fullscreen , 0 otherwise
* - embedTypes ( optional ) : list of supported embed types
* - preloadedJs ( optional ) : comma separated string with js file paths
* - preloadedCss ( optional ) : comma separated sting with css file paths
* - dropLibraryCss ( optional ) : list of associative arrays containing :
* - machineName : machine name for the librarys that are to drop their css
* - semantics ( optional ) : Json describing the content structure for the library
* - preloadedDependencies ( optional ) : list of associative arrays containing :
* - machineName : Machine name for a library this library is depending on
* - majorVersion : Major version for a library this library is depending on
* - minorVersion : Minor for a library this library is depending on
* - dynamicDependencies ( optional ) : list of associative arrays containing :
* - machineName : Machine name for a library this library is depending on
* - majorVersion : Major version for a library this library is depending on
* - minorVersion : Minor for a library this library is depending on
* - editorDependencies ( optional ) : list of associative arrays containing :
* - machineName : Machine name for a library this library is depending on
* - majorVersion : Major version for a library this library is depending on
* - minorVersion : Minor for a library this library is depending on
2013-02-15 11:38:49 +01:00
*/
2013-03-09 01:20:55 +01:00
public function loadLibrary ( $machineName , $majorVersion , $minorVersion );
2013-04-13 14:11:25 +02:00
2013-07-09 15:13:09 +02:00
/**
2014-03-26 08:43:29 +01:00
* Loads library semantics .
2013-07-09 15:13:09 +02:00
*
2014-10-09 19:29:20 +02:00
* @ param string $machineName
* Machine name for the library
* @ param int $majorVersion
* The library ' s major version
* @ param int $minorVersion
* The library ' s minor version
* @ return string
* The library ' s semantics as json
2013-07-09 15:13:09 +02:00
*/
2014-10-09 19:29:20 +02:00
public function loadLibrarySemantics ( $machineName , $majorVersion , $minorVersion );
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Makes it possible to alter the semantics , adding custom fields , etc .
*
* @ param array $semantics
2014-10-09 19:29:20 +02:00
* Associative array representing the semantics
* @ param string $machineName
* The library ' s machine name
* @ param int $majorVersion
* The library ' s major version
2014-10-10 13:56:48 +02:00
* @ param int $minorVersion
2014-10-09 19:29:20 +02:00
* The library ' s minor version
2014-03-26 08:43:29 +01:00
*/
2014-10-09 19:29:20 +02:00
public function alterLibrarySemantics ( & $semantics , $machineName , $majorVersion , $minorVersion );
2013-07-09 15:13:09 +02:00
2013-04-13 14:11:25 +02:00
/**
* Delete all dependencies belonging to given library
*
* @ param int $libraryId
2014-10-09 19:29:20 +02:00
* Library identifier
2013-04-13 14:11:25 +02:00
*/
public function deleteLibraryDependencies ( $libraryId );
2014-10-10 13:56:48 +02:00
2014-11-27 23:14:15 +01:00
/**
* Start an atomic operation against the dependency storage
*/
public function lockDependencyStorage ();
2015-01-20 14:48:22 +01:00
2014-11-27 23:14:15 +01:00
/**
* Stops an atomic operation against the dependency storage
*/
public function unlockDependencyStorage ();
2015-01-20 14:48:22 +01:00
2014-03-26 08:43:29 +01:00
/**
* Delete a library from database and file system
2014-10-10 13:56:48 +02:00
*
2014-10-23 15:18:03 +02:00
* @ param stdClass $library
* Library object with id , name , major version and minor version .
2014-03-26 08:43:29 +01:00
*/
2014-08-22 11:33:47 +02:00
public function deleteLibrary ( $library );
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Load content .
*
2014-10-09 19:29:20 +02:00
* @ param int $id
* Content identifier
* @ return array
* Associative array containing :
* - contentId : Identifier for the content
* - params : json content as string
* - embedType : csv of embed types
* - title : The contents title
* - language : Language code for the content
* - libraryId : Id for the main library
* - libraryName : The library machine name
* - libraryMajorVersion : The library ' s majorVersion
* - libraryMinorVersion : The library ' s minorVersion
* - libraryEmbedTypes : CSV of the main library ' s embed types
* - libraryFullscreen : 1 if fullscreen is supported . 0 otherwise .
2014-03-26 08:43:29 +01:00
*/
public function loadContent ( $id );
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Load dependencies for the given content of the given type .
*
2014-10-09 19:29:20 +02:00
* @ param int $id
* Content identifier
* @ param int $type
* Dependency types . Allowed values :
* - editor
* - preloaded
* - dynamic
2014-03-26 08:43:29 +01:00
* @ return array
2014-10-09 19:29:20 +02:00
* List of associative arrays containing :
* - libraryId : The id of the library if it is an existing library .
* - machineName : The library machineName
* - majorVersion : The library ' s majorVersion
* - minorVersion : The library ' s minorVersion
* - patchVersion : The library ' s patchVersion
* - preloadedJs ( optional ) : comma separated string with js file paths
* - preloadedCss ( optional ) : comma separated sting with css file paths
* - dropCss ( optional ) : csv of machine names
2014-03-26 08:43:29 +01:00
*/
public function loadContentDependencies ( $id , $type = NULL );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-09-26 15:56:42 +02:00
* Get stored setting .
2014-10-10 13:56:48 +02:00
*
2014-10-23 15:18:03 +02:00
* @ param string $name
* Identifier for the setting
* @ param string $default
* Optional default value if settings is not set
2014-10-09 19:29:20 +02:00
* @ return mixed
2014-10-23 15:18:03 +02:00
* Whatever has been stored as the setting
2014-05-27 10:55:46 +02:00
*/
2014-09-26 15:56:42 +02:00
public function getOption ( $name , $default = NULL );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-09-26 15:56:42 +02:00
* Stores the given setting .
* For example when did we last check h5p . org for updates to our libraries .
2014-10-10 13:56:48 +02:00
*
2014-10-23 15:18:03 +02:00
* @ param string $name
* Identifier for the setting
2014-09-26 15:56:42 +02:00
* @ param mixed $value Data
2014-10-23 15:18:03 +02:00
* Whatever we want to store as the setting
2014-05-27 10:55:46 +02:00
*/
2014-09-26 15:56:42 +02:00
public function setOption ( $name , $value );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-09-26 15:56:42 +02:00
* This will set the filtered parameters for the given content .
2014-10-10 13:56:48 +02:00
*
2014-09-26 15:56:42 +02:00
* @ param int $content_id
* @ param string $parameters filtered
2014-05-27 10:55:46 +02:00
*/
2014-09-26 15:56:42 +02:00
public function setFilteredParameters ( $content_id , $parameters = '' );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-09-26 15:56:42 +02:00
* Will clear filtered params for all the content that uses the specified
* library . This means that the content dependencies will have to be rebuilt ,
* and the parameters refiltered .
2014-10-09 19:29:20 +02:00
*
2014-05-27 10:55:46 +02:00
* @ param int $library_id
*/
2014-09-26 15:56:42 +02:00
public function clearFilteredParameters ( $library_id );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-09-26 15:56:42 +02:00
* Get number of contents that has to get their content dependencies rebuilt
* and parameters refiltered .
*
* @ return int
2014-05-27 10:55:46 +02:00
*/
2014-09-26 15:56:42 +02:00
public function getNumNotFiltered ();
2014-10-10 13:56:48 +02:00
2014-06-03 16:07:50 +02:00
/**
* Get number of contents using library as main library .
2014-10-10 13:56:48 +02:00
*
2014-10-10 13:57:43 +02:00
* @ param int $libraryId
2014-09-26 15:56:42 +02:00
* @ return int
2014-06-03 16:07:50 +02:00
*/
2014-10-10 13:57:43 +02:00
public function getNumContent ( $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-11-08 18:16:41 +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|GPL1|GPL2|GPL3|MPL|MPL2)$/' ,
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,}$/' ,
2013-12-18 11:32:18 +01:00
'metaDescription' => '/^.{1,}$/' ,
2012-12-02 21:55:51 +01:00
);
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-11-08 18:16:41 +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|GPL1|GPL2|GPL3|MPL|MPL2)$/' ,
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)$/' ,
2013-12-05 12:15:14 +01:00
'coreApi' => array (
2013-11-27 10:09:57 +01:00
'majorVersion' => '/^[0-9]{1,5}$/' ,
'minorVersion' => '/^[0-9]{1,5}$/' ,
),
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 ;
2013-07-13 22:25:18 +02:00
$this -> h5pCV = new H5PContentValidator ( $this -> h5pF , $this -> h5pC );
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
*/
2014-08-26 12:00:14 +02:00
public function isValidPackage ( $skipContent = FALSE , $upgradeOnly = FALSE ) {
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 ();
2014-03-26 08:43:29 +01:00
$tmpPath = $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 ;
2014-10-10 13:56:48 +02:00
2014-04-13 15:52:12 +02:00
// Only allow files with the .h5p extension:
if ( strtolower ( substr ( $tmpPath , - 3 )) !== 'h5p' ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The file you uploaded is not a valid HTML5 Package (It does not have the .h5p file extension)' ));
H5PCore :: deleteFileTree ( $tmpDir );
return ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $zip -> open ( $tmpPath ) === true ) {
2013-05-01 23:24:58 +02:00
$zip -> extractTo ( $tmpDir );
2012-12-03 00:19:25 +01:00
$zip -> close ();
}
else {
2014-04-13 15:52:12 +02:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The file you uploaded is not a valid HTML5 Package (We are unable to unzip it)' ));
2014-03-26 08:43:29 +01:00
H5PCore :: deleteFileTree ( $tmpDir );
2012-11-22 14:00:06 +01:00
return ;
}
2014-03-26 08:43:29 +01:00
unlink ( $tmpPath );
2012-11-22 14:00:06 +01:00
// 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' ) {
2014-03-26 08:43:29 +01:00
if ( $skipContent === TRUE ) {
continue ;
}
2014-10-10 13:56:48 +02:00
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 ;
2014-04-13 15:52:12 +02:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Could not 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 ;
2014-04-13 15:52:12 +02:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'The main h5p.json file is not valid' ));
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' ) {
2014-03-26 08:43:29 +01:00
// We do a separate skipContent check to avoid having the content folder being treated as a library
if ( $skipContent ) {
continue ;
}
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
}
2013-07-15 16:25:10 +02:00
2013-07-13 22:25:18 +02:00
if ( ! $this -> h5pCV -> validateContentFiles ( $filePath )) {
2014-03-26 08:43:29 +01:00
// validateContentfiles prints error messages itself
2013-06-30 22:14:16 +02:00
$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
2013-07-15 17:36:56 +02:00
elseif ( $this -> h5pF -> mayUpdateLibraries ()) {
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 );
2014-10-10 13:56:48 +02:00
2014-04-14 10:50:37 +02:00
if ( $libraryH5PData !== FALSE ) {
2014-08-26 12:00:14 +02:00
// Library's directory name must be:
// - <machineName>
// - or -
// - <machineName>-<majorVersion>.<minorVersion>
// where mcahineName, majorVersion and minorVersion is read from library.json
if ( $libraryH5PData [ 'machineName' ] !== $file && H5PCore :: libraryToString ( $libraryH5PData , TRUE ) !== $file ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Library directory name must match machineName or machineName-majorVersion.minorVersion (from library.json). (Directory: %directoryName , machineName: %machineName, majorVersion: %majorVersion, minorVersion: %minorVersion)' , array (
'%directoryName' => $file ,
'%machineName' => $libraryH5PData [ 'machineName' ],
'%majorVersion' => $libraryH5PData [ 'majorVersion' ],
'%minorVersion' => $libraryH5PData [ 'minorVersion' ])));
$valid = FALSE ;
continue ;
}
$libraryH5PData [ 'uploadDirectory' ] = $filePath ;
$libraries [ H5PCore :: libraryToString ( $libraryH5PData )] = $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
}
}
2014-03-26 08:43:29 +01:00
if ( $skipContent === FALSE ) {
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-29 15:41:06 +01:00
}
2012-11-28 15:31:55 +01:00
if ( $valid ) {
2014-08-26 12:00:14 +02:00
if ( $upgradeOnly ) {
2014-10-10 13:56:48 +02:00
// When upgrading, we opnly add allready installed libraries,
2014-08-26 12:00:14 +02:00
// and new dependent libraries
$upgrades = array ();
foreach ( $libraries as & $library ) {
// Is this library already installed?
if ( $this -> h5pF -> getLibraryId ( $library [ 'machineName' ], $library [ 'majorVersion' ], $library [ 'minorVersion' ]) !== FALSE ) {
$upgrades [ H5PCore :: libraryToString ( $library )] = $library ;
}
}
while ( $missingLibraries = $this -> getMissingLibraries ( $upgrades )) {
foreach ( $missingLibraries as $missing ) {
$libString = H5PCore :: libraryToString ( $missing );
$library = $libraries [ $libString ];
if ( $library ) {
$upgrades [ $libString ] = $library ;
}
}
}
2014-10-10 13:56:48 +02:00
2014-08-26 12:00:14 +02:00
$libraries = $upgrades ;
}
2014-10-10 13:56:48 +02:00
2012-11-30 15:51:39 +01:00
$this -> h5pC -> librariesJsonData = $libraries ;
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $skipContent === FALSE ) {
$this -> h5pC -> mainJsonData = $mainH5pData ;
$this -> h5pC -> contentJsonData = $contentJsonData ;
$libraries [ 'mainH5pData' ] = $mainH5pData ; // Check for the dependencies in h5p.json as well as in the libraries
}
2014-10-10 13:56:48 +02:00
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' ])) {
2014-09-05 14:12:46 +02:00
unset ( $missingLibraries [ H5PCore :: libraryToString ( $missing )]);
2012-11-28 23:29:22 +01:00
}
}
2014-10-10 13:56:48 +02:00
2013-04-13 13:28:48 +02:00
if ( ! empty ( $missingLibraries )) {
foreach ( $missingLibraries as $library ) {
2014-03-26 08:43:29 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Missing required library @library' , array ( '@library' => H5PCore :: libraryToString ( $library ))));
2013-04-13 13:28:48 +02:00
}
2013-07-15 17:36:56 +02:00
if ( ! $this -> h5pF -> mayUpdateLibraries ()) {
2014-08-26 11:26:41 +02:00
$this -> h5pF -> setInfoMessage ( $this -> h5pF -> t ( " Note that the libraries may exist in the file you uploaded, but you're not allowed to upload new libraries. Contact the site administrator about this. " ));
2013-07-15 17:36:56 +02:00
}
2013-04-13 13:28:48 +02:00
}
2012-11-28 23:29:22 +01:00
$valid = empty ( $missingLibraries ) && $valid ;
}
if ( ! $valid ) {
2014-03-26 08:43:29 +01:00
H5PCore :: deleteFileTree ( $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
}
// 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 );
2013-07-13 22:25:18 +02:00
$validLibrary = $this -> h5pCV -> validateContentFiles ( $filePath , TRUE ) && $validLibrary ;
2013-07-08 18:22:38 +02:00
2013-05-01 17:22:09 +02:00
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 ) {
2014-08-26 12:00:14 +02:00
if ( ! isset ( $libraries [ H5PCore :: libraryToString ( $dependency )])) {
$missing [ H5PCore :: libraryToString ( $dependency )] = $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 ;
2013-11-27 10:09:57 +01:00
// Test library core version requirement. If no requirement is set,
// this implicitly means 1.0, which shall work on newer versions
// too.
2013-12-05 12:15:14 +01:00
if ( isset ( $h5pData [ 'coreApi' ]) && ! empty ( $h5pData [ 'coreApi' ])) {
if (( $h5pData [ 'coreApi' ][ 'majorVersion' ] > H5PCore :: $coreApi [ 'majorVersion' ]) ||
(( $h5pData [ 'coreApi' ][ 'majorVersion' ] == H5PCore :: $coreApi [ 'majorVersion' ]) &&
( $h5pData [ 'coreApi' ][ 'minorVersion' ] > H5PCore :: $coreApi [ 'minorVersion' ])))
2013-11-27 10:09:57 +01:00
{
$this -> h5pF -> setErrorMessage (
2013-12-05 12:15:14 +01:00
$this -> h5pF -> t ( 'The library "%library_name" requires H5P %requiredVersion, but only H5P %coreApi is installed.' ,
2013-11-27 10:09:57 +01:00
array (
'%library_name' => $library_name ,
2013-12-05 12:15:14 +01:00
'%requiredVersion' => $h5pData [ 'coreApi' ][ 'majorVersion' ] . '.' . $h5pData [ 'coreApi' ][ 'minorVersion' ],
'%coreApi' => H5PCore :: $coreApi [ 'majorVersion' ] . '.' . H5PCore :: $coreApi [ 'minorVersion' ]
2013-11-27 10:09:57 +01:00
)));
$valid = false ;
}
}
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-11-27 10:09:57 +01:00
* Validate 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
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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 )
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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-07-09 10:14:42 +02:00
*
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 );
2013-10-21 13:22:13 +02:00
if ( $json === FALSE ) {
return FALSE ; // Cannot read from file.
2012-11-29 08:51:36 +01:00
}
2012-12-03 00:19:25 +01:00
$jsonData = json_decode ( $json , TRUE );
2013-10-21 13:22:13 +02:00
if ( $jsonData === NULL ) {
return FALSE ; // JSON cannot be decoded or the recursion limit has been reached.
2012-11-29 08:51:36 +01:00
}
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
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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 {
2013-07-09 10:14:42 +02:00
2012-11-29 22:52:41 +01:00
public $h5pF ;
public $h5pC ;
2014-10-10 13:56:48 +02:00
2014-04-28 15:09:07 +02:00
public $contentId = NULL ; // Quick fix so WP can get ID of new content.
2012-11-29 22:52:41 +01:00
/**
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-07-09 10:14:42 +02:00
2013-03-29 16:10:29 +01:00
/**
* Saves a H5P file
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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
2013-07-08 17:12:40 +02:00
* @ return boolean
* TRUE if one or more libraries were updated
* FALSE otherwise
2013-03-29 16:10:29 +01:00
*/
2014-08-22 14:03:00 +02:00
public function savePackage ( $content = NULL , $contentMainId = NULL , $skipContent = FALSE , $upgradeOnly = FALSE ) {
2013-03-29 16:10:29 +01:00
// Save the libraries we processed during validation
2013-07-08 17:12:40 +02:00
$library_saved = FALSE ;
2014-08-22 14:03:00 +02:00
$upgradedLibsCount = 0 ;
2013-07-15 17:36:56 +02:00
$mayUpdateLibraries = $this -> h5pF -> mayUpdateLibraries ();
2014-10-10 13:56:48 +02:00
2014-08-26 12:00:14 +02:00
foreach ( $this -> h5pC -> librariesJsonData as & $library ) {
$libraryId = $this -> h5pF -> getLibraryId ( $library [ 'machineName' ], $library [ 'majorVersion' ], $library [ 'minorVersion' ]);
2013-04-13 14:11:25 +02:00
$library [ 'saveDependencies' ] = TRUE ;
2014-10-10 13:56:48 +02:00
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
2013-07-15 17:36:56 +02:00
if ( ! $mayUpdateLibraries ) {
// This shouldn't happen, but just to be safe...
continue ;
}
2013-02-17 14:05:02 +01:00
$this -> h5pF -> saveLibraryData ( $library , $new );
2013-07-08 17:02:05 +02:00
2014-04-22 16:33:01 +02:00
$libraries_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'libraries' ;
if ( ! is_dir ( $libraries_path )) {
mkdir ( $libraries_path , 0777 , true );
}
$destination_path = $libraries_path . DIRECTORY_SEPARATOR . H5PCore :: libraryToString ( $library , TRUE );
2014-03-26 08:43:29 +01:00
H5PCore :: deleteFileTree ( $destination_path );
2014-08-26 12:00:14 +02:00
rename ( $library [ 'uploadDirectory' ], $destination_path );
2013-07-09 10:14:42 +02:00
2013-07-08 17:12:40 +02:00
$library_saved = TRUE ;
2012-11-30 15:51:39 +01:00
}
2013-04-13 14:11:25 +02:00
2014-08-26 12:00:14 +02:00
foreach ( $this -> h5pC -> librariesJsonData as & $library ) {
if ( $library [ 'saveDependencies' ]) {
2013-04-13 14:11:25 +02:00
$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' );
}
2014-10-10 13:56:48 +02:00
2014-05-27 14:12:54 +02:00
// Make sure libraries dependencies, parameter filtering and export files gets regenerated for all content who uses this library.
2014-09-26 15:56:42 +02:00
$this -> h5pF -> clearFilteredParameters ( $library [ 'libraryId' ]);
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
$upgradedLibsCount ++ ;
2013-02-24 00:12:57 +01:00
}
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( ! $skipContent ) {
$current_path = $this -> h5pF -> getUploadedH5pFolderPath () . DIRECTORY_SEPARATOR . 'content' ;
2014-10-10 13:56:48 +02:00
2014-04-22 16:33:01 +02:00
// Find out which libraries are used by this package/content
2014-03-26 08:43:29 +01:00
$librariesInUse = array ();
$this -> h5pC -> findLibraryDependencies ( $librariesInUse , $this -> h5pC -> mainJsonData );
2014-10-10 13:56:48 +02:00
2014-05-06 16:52:43 +02:00
// Save content
if ( $content === NULL ) {
$content = array ();
}
if ( ! is_array ( $content )) {
$content = array ( 'id' => $content );
}
$content [ 'library' ] = $librariesInUse [ 'preloaded-' . $this -> h5pC -> mainJsonData [ 'mainLibrary' ]][ 'library' ];
$content [ 'params' ] = file_get_contents ( $current_path . DIRECTORY_SEPARATOR . 'content.json' );
2014-05-27 10:55:46 +02:00
$contentId = $this -> h5pC -> saveContent ( $content , $contentMainId );
2014-04-28 15:09:07 +02:00
$this -> contentId = $contentId ;
2014-04-22 16:33:01 +02:00
$contents_path = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' ;
if ( ! is_dir ( $contents_path )) {
mkdir ( $contents_path , 0777 , true );
}
2014-10-10 13:56:48 +02:00
2014-04-22 16:33:01 +02:00
// Move the content folder
$destination_path = $contents_path . DIRECTORY_SEPARATOR . $contentId ;
2014-05-08 16:28:36 +02:00
@ rename ( $current_path , $destination_path );
2014-10-10 13:56:48 +02:00
2014-04-22 16:33:01 +02:00
// Save the content library dependencies
$this -> h5pF -> saveLibraryUsage ( $contentId , $librariesInUse );
H5PCore :: deleteFileTree ( $this -> h5pF -> getUploadedH5pFolderPath ());
2014-03-26 08:43:29 +01:00
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
// Update supported library list if neccessary:
$this -> h5pC -> validateLibrarySupport ( TRUE );
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
if ( $upgradeOnly ) {
// TODO - support translation
2014-09-23 15:10:23 +02:00
$this -> h5pF -> setInfoMessage ( $this -> h5pF -> t ( '@num libraries were upgraded!' , array ( '@num' => $upgradedLibsCount )));
2014-08-22 14:03:00 +02:00
}
2013-07-09 10:14:42 +02:00
2013-07-08 17:12:40 +02:00
return $library_saved ;
2012-11-30 15:51:39 +01:00
}
2014-10-10 13:56:48 +02:00
2013-03-29 16:10:29 +01:00
/**
* Delete an H5P package
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ param int $contentId
* The content id
*/
2012-12-22 07:09:17 +01:00
public function deletePackage ( $contentId ) {
2014-03-26 08:43:29 +01:00
H5PCore :: deleteFileTree ( $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . $contentId );
2012-12-22 07:09:17 +01:00
$this -> h5pF -> deleteContentData ( $contentId );
}
2013-03-29 16:10:29 +01:00
/**
* Update an H5P package
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ param int $contentId
* The content id
* @ param int $contentMainId
* The content main id ( used by frameworks supporting revisioning )
2013-07-08 17:12:40 +02:00
* @ return boolean
* TRUE if one or more libraries were updated
* FALSE otherwise
2013-03-29 16:10:29 +01:00
*/
2012-12-22 08:04:09 +01:00
public function updatePackage ( $contentId , $contentMainId = NULL ) {
$this -> deletePackage ( $contentId );
2013-07-08 17:12:40 +02:00
return $this -> savePackage ( $contentId , $contentMainId );
2012-12-22 08:04:09 +01:00
}
2013-03-29 16:10:29 +01:00
/**
* Copy / clone an H5P package
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* May for instance be used if the content is beeing revisioned without
* uploading a new H5P package
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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 ;
2014-03-26 08:43:29 +01:00
$this -> h5pC -> copyFileTree ( $source_path , $destination_path );
2012-12-22 08:04:09 +01:00
2013-03-07 05:31:30 +01:00
$this -> h5pF -> copyLibraryUsage ( $contentId , $copyFromId , $contentMainId );
2012-12-22 08:04:09 +01:00
}
2012-11-29 22:52:41 +01:00
}
2013-11-14 23:42:10 +01:00
/**
* This class is used for exporting zips
*/
Class H5PExport {
public $h5pF ;
public $h5pC ;
/**
2013-11-15 01:13:00 +01:00
* Constructor for the H5PExport
2013-11-14 23:42:10 +01:00
*
* @ param object $H5PFramework
* The frameworks implementation of the H5PFrameworkInterface
2013-11-15 01:13:00 +01:00
* @ param H5PCore
* Reference to an insance of H5PCore
2013-11-14 23:42:10 +01:00
*/
public function __construct ( $H5PFramework , $H5PCore ) {
$this -> h5pF = $H5PFramework ;
$this -> h5pC = $H5PCore ;
}
2014-10-10 13:56:48 +02:00
2013-11-14 23:42:10 +01:00
/**
2013-11-18 18:03:01 +01:00
* Return path to h5p package .
*
* Creates package if not already created
2013-11-14 23:42:10 +01:00
*
2014-05-27 14:05:20 +02:00
* @ param array $content
2013-11-18 18:03:01 +01:00
* @ return string
2013-11-14 23:42:10 +01:00
*/
2014-05-27 14:05:20 +02:00
public function createExportFile ( $content ) {
2013-11-14 23:42:10 +01:00
$h5pDir = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR ;
2014-05-08 16:28:36 +02:00
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content [ 'id' ];
2014-05-07 16:43:48 +02:00
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content [ 'id' ] . '.h5p' ;
2014-10-10 13:56:48 +02:00
2014-05-27 14:05:20 +02:00
// Temp dir to put the h5p files in
@ mkdir ( $tempPath , 0777 , TRUE );
@ mkdir ( $h5pDir . 'exports' , 0777 , TRUE );
// Create content folder
if ( $this -> h5pC -> copyFileTree ( $h5pDir . 'content' . DIRECTORY_SEPARATOR . $content [ 'id' ], $tempPath . DIRECTORY_SEPARATOR . 'content' ) === FALSE ) {
return FALSE ;
}
file_put_contents ( $tempPath . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'content.json' , $content [ 'params' ]);
// Make embedTypes into an array
$embedTypes = explode ( ', ' , $content [ 'embedType' ]); // Won't content always be embedded in one way?
// Build h5p.json
$h5pJson = array (
'title' => $content [ 'title' ],
2014-10-10 13:56:48 +02:00
// TODO - stop using 'und', this is not the preferred way.
// Either remove language from the json if not existing, or use "language": null
2014-11-06 11:13:24 +01:00
'language' => ( isset ( $content [ 'language' ]) && strlen ( trim ( $content [ 'language' ])) !== 0 ) ? $content [ 'language' ] : 'und' ,
2014-05-27 14:05:20 +02:00
'mainLibrary' => $content [ 'library' ][ 'name' ],
'embedTypes' => $embedTypes ,
);
// Add dependencies to h5p
foreach ( $content [ 'dependencies' ] as $dependency ) {
$library = $dependency [ 'library' ];
2014-10-10 13:56:48 +02:00
2014-05-27 14:05:20 +02:00
// Copy library to h5p
$source = isset ( $library [ 'path' ]) ? $library [ 'path' ] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore :: libraryToString ( $library , TRUE );
$destination = $tempPath . DIRECTORY_SEPARATOR . $library [ 'machineName' ];
$this -> h5pC -> copyFileTree ( $source , $destination );
// Do not add editor dependencies to h5p json.
if ( $dependency [ 'type' ] === 'editor' ) {
2014-10-10 13:56:48 +02:00
continue ;
2013-12-20 13:31:08 +01:00
}
2014-05-27 14:05:20 +02:00
$h5pJson [ $dependency [ 'type' ] . 'Dependencies' ][] = array (
'machineName' => $library [ 'machineName' ],
'majorVersion' => $library [ 'majorVersion' ],
'minorVersion' => $library [ 'minorVersion' ]
2013-11-17 18:04:39 +01:00
);
2014-05-27 14:05:20 +02:00
}
2013-11-14 23:42:10 +01:00
2014-05-27 14:05:20 +02:00
// Save h5p.json
$results = print_r ( json_encode ( $h5pJson ), true );
file_put_contents ( $tempPath . DIRECTORY_SEPARATOR . 'h5p.json' , $results );
// Create new zip instance.
$zip = new ZipArchive ();
2014-11-25 10:55:32 +01:00
$zip -> open ( $zipPath , ZIPARCHIVE :: CREATE | ZIPARCHIVE :: OVERWRITE );
2014-05-27 14:05:20 +02:00
// Get all files and folders in $tempPath
$iterator = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $tempPath . DIRECTORY_SEPARATOR ));
// Add files to zip
foreach ( $iterator as $key => $value ) {
$test = '.' ;
// Do not add the folders '.' and '..' to the zip. This will make zip invalid.
if ( substr_compare ( $key , $test , - strlen ( $test ), strlen ( $test )) !== 0 ) {
// Get files path in $tempPath
$filePath = explode ( $tempPath . DIRECTORY_SEPARATOR , $key );
// Add files to the zip with the intended file-structure
$zip -> addFile ( $key , $filePath [ 1 ]);
2013-11-14 23:42:10 +01:00
}
}
2014-05-27 14:05:20 +02:00
// Close zip and remove temp dir
$zip -> close ();
H5PCore :: deleteFileTree ( $tempPath );
2013-11-14 23:42:10 +01:00
}
2013-11-18 17:57:32 +01:00
2013-11-18 18:03:01 +01:00
/**
* Delete . h5p file
*
* @ param int / string $contentId
* Identifier for the H5P
*/
2013-11-18 17:57:32 +01:00
public function deleteExport ( $contentId ) {
$h5pDir = $this -> h5pF -> getH5pPath () . DIRECTORY_SEPARATOR ;
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $contentId . '.h5p' ;
if ( file_exists ( $zipPath )) {
2014-03-26 08:43:29 +01:00
unlink ( $zipPath );
2013-11-18 17:57:32 +01:00
}
}
2013-11-18 18:03:01 +01:00
/**
* Add editor libraries to the list of libraries
*
* These aren ' t supposed to go into h5p . json , but must be included with the rest
* of the libraries
*
* @ param array $libraries
* List of libraries keyed by machineName
* @ param array $editorLibraries
* List of libraries keyed by machineName
* @ return List of libraries keyed by machineName
*/
2013-11-18 17:45:31 +01:00
private function addEditorLibraries ( $libraries , $editorLibraries ) {
foreach ( $editorLibraries as $editorLibrary ) {
2014-10-10 13:56:48 +02:00
$libraries [ $editorLibrary [ 'machineName' ]] = $editorLibrary ;
2013-11-17 18:04:39 +01:00
}
return $libraries ;
}
2013-11-14 23:42:10 +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 {
2014-03-26 08:43:29 +01:00
2013-12-05 12:15:14 +01:00
public static $coreApi = array (
2013-11-27 10:09:57 +01:00
'majorVersion' => 1 ,
2014-11-10 11:35:15 +01:00
'minorVersion' => 4
2013-11-27 10:09:57 +01: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' ,
2014-11-10 11:31:23 +01:00
'js/h5p-event-dispatcher.js' ,
2013-04-03 15:39:59 +02:00
);
2014-03-26 08:43:29 +01:00
public static $adminScripts = array (
'js/jquery.js' ,
'js/h5p-utils.js' ,
);
2013-07-15 16:25:10 +02:00
2014-04-22 16:33:01 +02:00
public static $defaultContentWhitelist = 'json png jpg jpeg gif bmp tif tiff svg eot ttf woff otf webm mp4 ogg mp3 txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp xml csv diff patch swf md textile' ;
2013-07-13 22:25:18 +02:00
public static $defaultLibraryWhitelistExtras = 'js css' ;
2014-10-10 13:56:48 +02:00
2014-10-21 15:42:47 +02:00
public $librariesJsonData , $contentJsonData , $mainJsonData , $h5pF , $path , $development_mode , $h5pD , $disableFileCheck ;
2014-10-10 13:56:48 +02:00
const SECONDS_IN_WEEK = 604800 ;
2014-05-27 14:05:20 +02:00
private $exportEnabled ;
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
2014-03-26 08:43:29 +01:00
* @ param string $path H5P file storage directory .
* @ param string $language code . Defaults to english .
2014-05-27 14:05:20 +02:00
* @ param boolean $export enabled ?
2014-03-26 08:43:29 +01:00
* @ param int $development_mode mode .
2012-11-29 22:52:41 +01:00
*/
2014-05-27 14:05:20 +02:00
public function __construct ( $H5PFramework , $path , $language = 'en' , $export = FALSE , $development_mode = H5PDevelopment :: MODE_NONE ) {
2013-02-17 15:04:30 +01:00
$this -> h5pF = $H5PFramework ;
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
$this -> h5pF = $H5PFramework ;
$this -> path = $path ;
2014-05-27 14:05:20 +02:00
$this -> exportEnabled = $export ;
2014-03-26 08:43:29 +01:00
$this -> development_mode = $development_mode ;
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $development_mode & H5PDevelopment :: MODE_LIBRARY ) {
$this -> h5pD = new H5PDevelopment ( $this -> h5pF , $path , $language );
}
}
2014-08-22 11:34:38 +02:00
/**
* Save content and clear cache .
2014-10-10 13:56:48 +02:00
*
2014-08-22 11:34:38 +02:00
* @ param array $content
* @ return int Content ID
*/
public function saveContent ( $content , $contentMainId = NULL ) {
if ( isset ( $content [ 'id' ])) {
$this -> h5pF -> updateContent ( $content , $contentMainId );
}
else {
2014-10-10 13:56:48 +02:00
$content [ 'id' ] = $this -> h5pF -> insertContent ( $content , $contentMainId );
2014-08-22 11:34:38 +02:00
}
2014-10-10 13:56:48 +02:00
2014-08-22 11:34:38 +02:00
return $content [ 'id' ];
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Load content .
*
* @ param int $id for content .
* @ return object
*/
public function loadContent ( $id ) {
$content = $this -> h5pF -> loadContent ( $id );
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $content !== NULL ) {
$content [ 'library' ] = array (
'id' => $content [ 'libraryId' ],
'name' => $content [ 'libraryName' ],
'majorVersion' => $content [ 'libraryMajorVersion' ],
'minorVersion' => $content [ 'libraryMinorVersion' ],
'embedTypes' => $content [ 'libraryEmbedTypes' ],
'fullscreen' => $content [ 'libraryFullscreen' ],
);
unset ( $content [ 'libraryId' ], $content [ 'libraryName' ], $content [ 'libraryEmbedTypes' ], $content [ 'libraryFullscreen' ]);
2014-10-10 13:56:48 +02:00
2014-05-27 13:07:53 +02:00
// // TODO: Move to filterParameters?
// if ($this->development_mode & H5PDevelopment::MODE_CONTENT) {
// // TODO: Remove Drupal specific stuff
// $json_content_path = file_create_path(file_directory_path() . '/' . variable_get('h5p_default_path', 'h5p') . '/content/' . $id . '/content.json');
// if (file_exists($json_content_path) === TRUE) {
// $json_content = file_get_contents($json_content_path);
// if (json_decode($json_content, TRUE) !== FALSE) {
// drupal_set_message(t('Invalid json in json content'), 'warning');
// }
// $content['params'] = $json_content;
// }
// }
2014-03-26 08:43:29 +01:00
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
return $content ;
}
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
/**
2014-05-27 13:07:53 +02:00
* Filter content run parameters , rebuild content dependecy cache and export file .
2014-10-10 13:56:48 +02:00
*
2014-05-27 10:55:46 +02:00
* @ param Object $content
* @ return Object NULL on failure .
*/
public function filterParameters ( $content ) {
2014-09-26 15:56:42 +02:00
if ( isset ( $content [ 'filtered' ]) && $content [ 'filtered' ] !== '' ) {
return $content [ 'filtered' ];
2014-05-27 10:55:46 +02:00
}
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
// Validate and filter against main library semantics.
$validator = new H5PContentValidator ( $this -> h5pF , $this );
$params = ( object ) array (
'library' => H5PCore :: libraryToString ( $content [ 'library' ]),
'params' => json_decode ( $content [ 'params' ])
);
$validator -> validateLibrary ( $params , ( object ) array ( 'options' => array ( $params -> library )));
$params = json_encode ( $params -> params );
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
// Update content dependencies.
2014-05-27 14:05:20 +02:00
$content [ 'dependencies' ] = $validator -> getDependencies ();
2014-10-10 13:56:48 +02:00
2014-11-02 19:13:14 +01:00
// Sometimes the parameters are filtered before content has been created
if ( $content [ 'id' ]) {
$this -> h5pF -> deleteLibraryUsage ( $content [ 'id' ]);
$this -> h5pF -> saveLibraryUsage ( $content [ 'id' ], $content [ 'dependencies' ]);
2014-10-10 13:56:48 +02:00
2014-11-02 19:13:14 +01:00
if ( $this -> exportEnabled ) {
// Recreate export file
$exporter = new H5PExport ( $this -> h5pF , $this );
$exporter -> createExportFile ( $content );
// TODO: Should we rather create the file once first accessed, like imagecache?
}
2014-05-27 10:55:46 +02:00
2014-11-02 19:13:14 +01:00
// Cache.
$this -> h5pF -> setFilteredParameters ( $content [ 'id' ], $params );
}
2014-05-27 10:55:46 +02:00
return $params ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Find the files required for this content to work .
*
* @ param int $id for content .
* @ return array
*/
public function loadContentDependencies ( $id , $type = NULL ) {
$dependencies = $this -> h5pF -> loadContentDependencies ( $id , $type );
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $this -> development_mode & H5PDevelopment :: MODE_LIBRARY ) {
$developmentLibraries = $this -> h5pD -> getLibraries ();
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
foreach ( $dependencies as $key => $dependency ) {
$libraryString = H5PCore :: libraryToString ( $dependency );
if ( isset ( $developmentLibraries [ $libraryString ])) {
2014-05-07 16:43:48 +02:00
$developmentLibraries [ $libraryString ][ 'dependencyType' ] = $dependencies [ $key ][ 'dependencyType' ];
2014-03-26 08:43:29 +01:00
$dependencies [ $key ] = $developmentLibraries [ $libraryString ];
}
}
}
2014-10-10 13:56:48 +02:00
2014-05-07 16:43:48 +02:00
return $dependencies ;
2014-03-26 08:43:29 +01:00
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
/**
* Get all dependency assets of the given type
2014-10-10 13:56:48 +02:00
*
2014-05-09 10:48:05 +02:00
* @ param array $dependency
* @ param string $type
* @ param array $assets
*/
private function getDependencyAssets ( $dependency , $type , & $assets ) {
// Check if dependency has any files of this type
if ( empty ( $dependency [ $type ]) || $dependency [ $type ][ 0 ] === '' ) {
return ;
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
// Check if we should skip CSS.
if ( $type === 'preloadedCss' && ( isset ( $dependency [ 'dropCss' ]) && $dependency [ 'dropCss' ] === '1' )) {
return ;
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
foreach ( $dependency [ $type ] as $file ) {
$assets [] = ( object ) array (
'path' => $dependency [ 'path' ] . '/' . trim ( is_array ( $file ) ? $file [ 'path' ] : $file ),
'version' => $dependency [ 'version' ]
);
}
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
/**
* Combines path with cache buster / version .
2014-10-10 13:56:48 +02:00
*
2014-05-09 10:48:05 +02:00
* @ param array $assets
* @ return array
*/
public function getAssetsUrls ( $assets ) {
$urls = array ();
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
foreach ( $assets as $asset ) {
$urls [] = $asset -> path . $asset -> version ;
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
return $urls ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Return file paths for all dependecies files .
*
* @ param array $dependencies
* @ return array files .
*/
2014-05-09 10:48:05 +02:00
public function getDependenciesFiles ( $dependencies ) {
2014-03-26 08:43:29 +01:00
$files = array (
'scripts' => array (),
2014-05-06 11:46:19 +02:00
'styles' => array ()
2014-03-26 08:43:29 +01:00
);
foreach ( $dependencies as $dependency ) {
if ( isset ( $dependency [ 'path' ]) === FALSE ) {
$dependency [ 'path' ] = $this -> path . '/libraries/' . H5PCore :: libraryToString ( $dependency , TRUE );
$dependency [ 'preloadedJs' ] = explode ( ',' , $dependency [ 'preloadedJs' ]);
$dependency [ 'preloadedCss' ] = explode ( ',' , $dependency [ 'preloadedCss' ]);
}
2014-10-10 13:56:48 +02:00
2014-05-09 10:48:05 +02:00
$dependency [ 'version' ] = " ?ver= { $dependency [ 'majorVersion' ] } . { $dependency [ 'minorVersion' ] } . { $dependency [ 'patchVersion' ] } " ;
$this -> getDependencyAssets ( $dependency , 'preloadedJs' , $files [ 'scripts' ]);
$this -> getDependencyAssets ( $dependency , 'preloadedCss' , $files [ 'styles' ]);
2014-03-26 08:43:29 +01:00
}
return $files ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Load library semantics .
*
2014-05-27 10:55:46 +02:00
* @ return string
2014-03-26 08:43:29 +01:00
*/
public function loadLibrarySemantics ( $name , $majorVersion , $minorVersion ) {
2014-03-26 08:47:44 +01:00
$semantics = NULL ;
2014-03-26 08:43:29 +01:00
if ( $this -> development_mode & H5PDevelopment :: MODE_LIBRARY ) {
// Try to load from dev lib
$semantics = $this -> h5pD -> getSemantics ( $name , $majorVersion , $minorVersion );
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $semantics === NULL ) {
// Try to load from DB.
$semantics = $this -> h5pF -> loadLibrarySemantics ( $name , $majorVersion , $minorVersion );
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $semantics !== NULL ) {
$semantics = json_decode ( $semantics );
$this -> h5pF -> alterLibrarySemantics ( $semantics , $name , $majorVersion , $minorVersion );
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
return $semantics ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Load library .
*
* @ return array or null .
*/
public function loadLibrary ( $name , $majorVersion , $minorVersion ) {
2014-03-26 08:47:44 +01:00
$library = NULL ;
2014-03-26 08:43:29 +01:00
if ( $this -> development_mode & H5PDevelopment :: MODE_LIBRARY ) {
// Try to load from dev
$library = $this -> h5pD -> getLibrary ( $name , $majorVersion , $minorVersion );
2014-05-08 16:28:36 +02:00
if ( $library !== NULL ) {
$library [ 'semantics' ] = $this -> h5pD -> getSemantics ( $name , $majorVersion , $minorVersion );
}
2014-03-26 08:43:29 +01:00
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $library === NULL ) {
// Try to load from DB.
$library = $this -> h5pF -> loadLibrary ( $name , $majorVersion , $minorVersion );
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
return $library ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
2014-08-22 14:03:00 +02:00
* Deletes a library
2014-10-10 13:56:48 +02:00
*
2014-08-22 14:03:00 +02:00
* @ param unknown $libraryId
*/
public function deleteLibrary ( $libraryId ) {
$this -> h5pF -> deleteLibrary ( $libraryId );
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
// Force update of unsupported libraries list:
$this -> validateLibrarySupport ( TRUE );
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
2014-10-10 13:56:48 +02:00
* Recursive . Goes through the dependency tree for the given library and
2014-03-26 08:43:29 +01:00
* adds all the dependencies to the given array in a flat format .
2014-10-10 13:56:48 +02:00
*
2014-03-26 08:43:29 +01:00
* @ param array $librariesUsed Flat list of all dependencies .
* @ param array $library To find all dependencies for .
* @ param bool $editor Used interally to force all preloaded sub dependencies of an editor dependecy to be editor dependencies .
*/
public function findLibraryDependencies ( & $dependencies , $library , $editor = FALSE ) {
foreach ( array ( 'dynamic' , 'preloaded' , 'editor' ) as $type ) {
$property = $type . 'Dependencies' ;
2014-03-26 08:47:44 +01:00
if ( ! isset ( $library [ $property ])) {
2014-03-26 08:43:29 +01:00
continue ; // Skip, no such dependencies.
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $type === 'preloaded' && $editor === TRUE ) {
// All preloaded dependencies of an editor library is set to editor.
$type = 'editor' ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
foreach ( $library [ $property ] as $dependency ) {
$dependencyKey = $type . '-' . $dependency [ 'machineName' ];
if ( isset ( $dependencies [ $dependencyKey ]) === TRUE ) {
continue ; // Skip, already have this.
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
$dependencyLibrary = $this -> loadLibrary ( $dependency [ 'machineName' ], $dependency [ 'majorVersion' ], $dependency [ 'minorVersion' ]);
2014-04-14 10:50:37 +02:00
if ( $dependencyLibrary ) {
$dependencies [ $dependencyKey ] = array (
'library' => $dependencyLibrary ,
'type' => $type
);
$this -> findLibraryDependencies ( $dependencies , $dependencyLibrary , $type === 'editor' );
}
else {
// This site is missing a dependency!
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Missing dependency @dep required by @lib.' , array ( '@dep' => H5PCore :: libraryToString ( $dependency ), '@lib' => H5PCore :: libraryToString ( $library ))));
}
2014-03-26 08:43:29 +01:00
}
}
2012-11-29 22:52:41 +01:00
}
2013-07-09 10:14:42 +02:00
2013-03-29 16:10:29 +01:00
/**
* Check if a library is of the version we ' re looking for
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* Same verision means that the majorVersion and minorVersion is the same
2013-07-09 10:14:42 +02:00
*
2013-03-29 16:10:29 +01:00
* @ 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 ) {
2014-08-26 12:00:14 +02:00
if ( $library [ 'machineName' ] != $dependency [ 'machineName' ]) {
return FALSE ;
}
2012-12-04 22:26:20 +01:00
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
*/
2014-03-26 08:43:29 +01:00
public static function deleteFileTree ( $dir ) {
2012-12-22 06:35:16 +01:00
if ( ! is_dir ( $dir )) {
return ;
}
$files = array_diff ( scandir ( $dir ), array ( '.' , '..' ));
foreach ( $files as $file ) {
2014-03-26 08:43:29 +01:00
( is_dir ( " $dir / $file " )) ? self :: deleteFileTree ( " $dir / $file " ) : unlink ( " $dir / $file " );
2012-12-22 06:35:16 +01:00
}
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 .
*/
2014-03-26 08:43:29 +01:00
public function copyFileTree ( $source , $destination ) {
2012-12-22 08:04:09 +01:00
$dir = opendir ( $source );
2013-12-20 13:31:08 +01:00
if ( $dir === FALSE ) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Unable to copy tree, no such directory: @dir' , array ( '@dir' => $source )));
return FALSE ;
}
2012-12-22 08:04:09 +01:00
@ 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 )) {
2014-03-26 08:43:29 +01:00
$this -> copyFileTree ( $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 }
*/
2014-03-26 08:43:29 +01:00
public static function libraryToString ( $library , $folderName = FALSE ) {
return ( isset ( $library [ 'machineName' ]) ? $library [ 'machineName' ] : $library [ 'name' ]) . ( $folderName ? '-' : ' ' ) . $library [ 'majorVersion' ] . '.' . $library [ 'minorVersion' ];
2013-04-13 13:28:48 +02:00
}
2013-07-05 17:35:59 +02:00
/**
2013-07-09 11:01:29 +02:00
* Parses library data from a string on the form { machineName } { majorVersion } . { minorVersion }
2013-07-05 17:35:59 +02:00
*
* @ 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 "
*/
2014-08-18 14:09:38 +02:00
public static function libraryFromString ( $libraryString ) {
2013-07-05 17:35:59 +02:00
$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 ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
/**
* Determine the correct embed type to use .
*
* @ return string 'div' or 'iframe' .
*/
public static function determineEmbedType ( $contentEmbedType , $libraryEmbedTypes ) {
// Detect content embed type
$embedType = strpos ( strtolower ( $contentEmbedType ), 'div' ) !== FALSE ? 'div' : 'iframe' ;
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $libraryEmbedTypes !== NULL && $libraryEmbedTypes !== '' ) {
// Check that embed type is available for library
$embedTypes = strtolower ( $libraryEmbedTypes );
if ( strpos ( $embedTypes , $embedType ) === FALSE ) {
// Not available, pick default.
$embedType = strpos ( $embedTypes , 'div' ) !== FALSE ? 'div' : 'iframe' ;
2014-10-10 13:56:48 +02:00
}
2014-03-26 08:43:29 +01:00
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
return $embedType ;
}
2014-10-10 13:56:48 +02:00
2014-06-03 16:07:50 +02:00
/**
* Get the absolute version for the library as a human readable string .
2014-10-10 13:56:48 +02:00
*
2014-06-03 16:07:50 +02:00
* @ param object $library
* @ return string
*/
public static function libraryVersion ( $library ) {
return $library -> major_version . '.' . $library -> minor_version . '.' . $library -> patch_version ;
}
2014-10-10 13:56:48 +02:00
2014-06-03 16:07:50 +02:00
/**
* Detemine which versions content with the given library can be upgraded to .
2014-10-10 13:56:48 +02:00
*
2014-06-03 16:07:50 +02:00
* @ param object $library
* @ param array $versions
* @ return array
*/
2014-06-13 15:02:26 +02:00
public function getUpgrades ( $library , $versions ) {
2014-06-03 16:07:50 +02:00
$upgrades = array ();
foreach ( $versions as $upgrade ) {
if ( $upgrade -> major_version > $library -> major_version || $upgrade -> major_version === $library -> major_version && $upgrade -> minor_version > $library -> minor_version ) {
$upgrades [ $upgrade -> id ] = H5PCore :: libraryVersion ( $upgrade );
}
}
return $upgrades ;
}
2014-10-10 13:56:48 +02:00
2014-07-18 11:26:46 +02:00
/**
* Converts all the properties of the given object or array from
* snake_case to camelCase . Useful after fetching data from the database .
2014-10-10 13:56:48 +02:00
*
2014-07-18 11:26:46 +02:00
* Note that some databases does not support camelCase .
2014-10-10 13:56:48 +02:00
*
2014-07-18 11:26:46 +02:00
* @ param mixed $arr input
* @ param boolean $obj return object
* @ return mixed object or array
*/
public static function snakeToCamel ( $arr , $obj = false ) {
$newArr = array ();
foreach ( $arr as $key => $val ) {
$next = - 1 ;
while (( $next = strpos ( $key , '_' , $next + 1 )) !== FALSE ) {
$key = substr_replace ( $key , strtoupper ( $key { $next + 1 }), $next , 2 );
}
$newArr [ $key ] = $val ;
}
return $obj ? ( object ) $newArr : $newArr ;
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
* Check if currently installed H5P libraries are supported by
* the current versjon of core . Which versions of which libraries are supported is
* defined in the library - support . json file .
*
2014-10-10 13:56:48 +02:00
* @ param boolean If TRUE , unsupported libraries list are rebuilt . If FALSE , list is
2014-08-22 14:03:00 +02:00
* rebuilt only if non - existing
*/
public function validateLibrarySupport ( $force = false ) {
if ( ! ( $this -> h5pF -> getUnsupportedLibraries () === NULL || $force )) {
return ;
}
2014-10-10 13:56:48 +02:00
2014-08-26 11:45:00 +02:00
$minVersions = $this -> getMinimumVersionsSupported ( realpath ( dirname ( __FILE__ )) . '/library-support.json' );
2014-08-26 11:26:41 +02:00
if ( $minVersions === NULL ) {
return ;
}
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
// Get all libraries installed, check if any of them is not supported:
$libraries = $this -> h5pF -> loadLibraries ();
$unsupportedLibraries = array ();
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
// Iterate over all installed libraries
foreach ( $libraries as $library_name => $versions ) {
if ( ! isset ( $minVersions [ $library_name ])) {
continue ;
2014-08-22 14:03:00 +02:00
}
2014-08-26 11:26:41 +02:00
$min = $minVersions [ $library_name ];
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
// For each version of this library, check if it is supported
foreach ( $versions as $library ) {
if ( ! $this -> isLibraryVersionSupported ( $library , $min -> versions )) {
// Current version of this library is not supported
$unsupportedLibraries [] = array (
'name' => $library_name ,
'downloadUrl' => $min -> downloadUrl ,
'currentVersion' => array (
'major' => $library -> major_version ,
'minor' => $library -> minor_version ,
'patch' => $library -> patch_version ,
2014-08-26 12:08:35 +02:00
)
2014-08-26 11:26:41 +02:00
);
2014-08-22 14:03:00 +02:00
}
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
$this -> h5pF -> setUnsupportedLibraries ( empty ( $unsupportedLibraries ) ? NULL : $unsupportedLibraries );
}
}
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
/**
* Returns a list of the minimum version of libraries that are supported .
* This is needed because some old libraries are no longer supported by core .
2014-10-10 13:56:48 +02:00
*
2014-08-26 11:26:41 +02:00
* TODO : Make it possible for the systems to cache this list between requests .
2014-10-10 13:56:48 +02:00
*
2014-08-26 11:26:41 +02:00
* @ param string $path to json file
* @ return array indexed using library names
*/
public function getMinimumVersionsSupported ( $path ) {
$minSupported = array ();
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
// Get list of minimum version for libraries. Some old libraries are no longer supported.
$libraries = file_get_contents ( $path );
if ( $libraries !== FALSE ) {
$libraries = json_decode ( $libraries );
if ( $libraries !== NULL ) {
foreach ( $libraries as $library ) {
$minSupported [ $library -> machineName ] = ( object ) array (
'versions' => $library -> minimumVersions ,
'downloadUrl' => $library -> downloadUrl
);
}
}
}
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
return empty ( $minSupported ) ? NULL : $minSupported ;
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
* Check if a specific version of a library is supported
2014-10-10 13:56:48 +02:00
*
2014-08-22 14:03:00 +02:00
* @ param object library
* @ param array An array containing versions
* @ return boolean TRUE if supported , otherwise FALSE
*/
2014-08-26 11:26:41 +02:00
public function isLibraryVersionSupported ( $library , $minimumVersions ) {
2014-08-22 14:03:00 +02:00
$major_supported = $minor_supported = $patch_supported = false ;
foreach ( $minimumVersions as $minimumVersion ) {
// A library is supported if:
// --- major is higher than any minimumversion
// --- minor is higher than any minimumversion for a given major
// --- major and minor equals and patch is >= supported
2014-08-26 11:26:41 +02:00
$major_supported |= ( $library -> major_version > $minimumVersion -> major );
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
if ( $library -> major_version == $minimumVersion -> major ) {
$minor_supported |= ( $library -> minor_version > $minimumVersion -> minor );
2014-08-22 14:03:00 +02:00
}
2014-10-10 13:56:48 +02:00
2014-08-26 11:26:41 +02:00
if ( $library -> major_version == $minimumVersion -> major &&
$library -> minor_version == $minimumVersion -> minor ) {
$patch_supported |= ( $library -> patch_version >= $minimumVersion -> patch );
2014-08-22 14:03:00 +02:00
}
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
return ( $patch_supported || $minor_supported || $major_supported );
}
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
/**
2014-10-10 13:56:48 +02:00
* Helper function for creating markup for the unsupported libraries list
*
2014-08-22 14:03:00 +02:00
* TODO : Make help text translatable
2014-10-10 13:56:48 +02:00
*
2014-08-22 14:03:00 +02:00
* @ return string Html
* */
public function createMarkupForUnsupportedLibraryList ( $libraries ) {
$html = '<div><span>The following versions of H5P libraries are not supported anymore:<span><ul>' ;
2014-10-10 13:56:48 +02:00
2014-08-22 14:03:00 +02:00
foreach ( $libraries as $library ) {
$downloadUrl = $library [ 'downloadUrl' ];
$libraryName = $library [ 'name' ];
$currentVersion = $library [ 'currentVersion' ][ 'major' ] . '.' . $library [ 'currentVersion' ][ 'minor' ] . '.' . $library [ 'currentVersion' ][ 'patch' ];
2014-08-26 12:00:14 +02:00
$html .= " <li><a href= \" $downloadUrl\ " > $libraryName </ a > ( $currentVersion ) </ li > " ;
2014-08-22 14:03:00 +02:00
}
2014-10-10 13:56:48 +02:00
2014-08-26 12:00:14 +02:00
$html .= '</ul><span><br>These libraries may cause problems on this site. See <a href="http://h5p.org/releases/h5p-core-1.3">here</a> for more info</div>' ;
2014-08-22 14:03:00 +02:00
return $html ;
}
2014-10-10 13:56:48 +02:00
2014-09-05 09:59:16 +02:00
/**
2014-11-01 17:05:15 +01:00
* Fetch a list of libraries ' metadata from h5p . org .
* Save URL tutorial to database . Each platform implementation
* is responsible for invoking this , eg using cron
2014-09-05 09:59:16 +02:00
*/
2014-11-01 17:05:15 +01:00
public function fetchLibrariesMetadata ( $fetchingDisabled = FALSE ) {
$platformInfo = $this -> h5pF -> getPlatformInfo ();
$platformInfo [ 'autoFetchingDisabled' ] = $fetchingDisabled ;
2014-11-02 13:46:09 +01:00
$platformInfo [ 'uuid' ] = $this -> h5pF -> getOption ( 'h5p_site_uuid' , '' );
// Adding random string to GET to be sure nothing is cached
$random = substr ( str_shuffle ( " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " ), 0 , 5 );
2014-11-03 07:02:45 +01:00
$json = $this -> h5pF -> fetchExternalData ( 'http://h5p.org/libraries-metadata.json?api=1&platform=' . urlencode ( json_encode ( $platformInfo )) . '&x=' . urlencode ( $random ));
2014-11-01 17:05:15 +01:00
if ( $json !== NULL ) {
2014-11-02 14:29:13 +01:00
$json = json_decode ( $json );
2014-11-02 13:46:09 +01:00
if ( isset ( $json -> libraries )) {
foreach ( $json -> libraries as $machineName => $libInfo ) {
$this -> h5pF -> setLibraryTutorialUrl ( $machineName , $libInfo -> tutorialUrl );
}
}
if ( $platformInfo [ 'uuid' ] === '' && isset ( $json -> uuid )) {
$this -> h5pF -> setOption ( 'h5p_site_uuid' , $json -> uuid );
2014-11-01 17:05:15 +01:00
}
}
}
}
2014-10-10 13:56:48 +02: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 ;
2014-05-27 10:55:46 +02:00
private $libraries , $dependencies ;
2013-07-05 17:35:59 +02:00
/**
* 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' ,
2013-07-10 09:59:35 +02:00
'file' => 'validateFile' ,
2013-07-05 17:35:59 +02:00
'image' => 'validateImage' ,
'video' => 'validateVideo' ,
'audio' => 'validateAudio' ,
'select' => 'validateSelect' ,
'library' => 'validateLibrary' ,
);
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
// Keep track of the libraries we load to avoid loading it multiple times.
$this -> libraries = array ();
// TODO: Should this possible be done in core's loadLibrary? This might be done multiple places.
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
// Keep track of all dependencies for the given content.
$this -> dependencies = array ();
2013-07-05 17:35:59 +02:00
}
/**
2014-05-27 10:55:46 +02:00
* Get the flat dependecy tree .
2014-10-10 13:56:48 +02:00
*
2014-05-27 10:55:46 +02:00
* @ return array
2013-07-05 17:35:59 +02:00
*/
2014-05-27 10:55:46 +02:00
public function getDependencies () {
return $this -> dependencies ;
2013-07-05 17:35:59 +02:00
}
2014-10-10 13:56:48 +02:00
2013-07-05 17:35:59 +02:00
/**
* Validate given text value against text semantics .
*/
public function validateText ( & $text , $semantics ) {
2013-07-24 18:01:07 +02:00
if ( ! is_string ( $text )) {
$text = '' ;
}
2013-07-11 14:36:31 +02:00
if ( isset ( $semantics -> tags )) {
// Not testing for empty array allows us to use the 4 defaults without
// specifying them in semantics.
$tags = array_merge ( array ( 'div' , 'span' , 'p' , 'br' ), $semantics -> tags );
2013-07-10 09:59:35 +02:00
// Add related tags for table etc.
2013-07-11 13:12:17 +02:00
if ( in_array ( 'table' , $tags )) {
2013-07-10 09:59:35 +02:00
$tags = array_merge ( $tags , array ( 'tr' , 'td' , 'th' , 'colgroup' , 'thead' , 'tbody' , 'tfoot' ));
}
2013-07-11 14:36:31 +02:00
if ( in_array ( 'b' , $tags ) && ! in_array ( 'strong' , $tags )) {
2013-07-10 09:59:35 +02:00
$tags [] = 'strong' ;
}
2013-07-11 14:36:31 +02:00
if ( in_array ( 'i' , $tags ) && ! in_array ( 'em' , $tags )) {
2013-07-10 09:59:35 +02:00
$tags [] = 'em' ;
}
2013-07-11 14:36:31 +02:00
if ( in_array ( 'ul' , $tags ) || in_array ( 'ol' , $tags ) && ! in_array ( 'li' , $tags )) {
2013-07-10 09:59:35 +02:00
$tags [] = 'li' ;
}
2013-11-08 18:46:47 +01:00
if ( in_array ( 'del' , $tags ) || in_array ( 'strike' , $tags ) && ! in_array ( 's' , $tags )) {
$tags [] = 's' ;
}
2013-07-05 17:35:59 +02:00
// Strip invalid HTML tags.
2013-07-12 14:49:37 +02:00
$text = $this -> filter_xss ( $text , $tags );
2013-07-05 17:35:59 +02:00
}
else {
// Filter text to plain text.
2013-07-17 15:11:37 +02:00
$text = htmlspecialchars ( $text , ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-05 17:35:59 +02:00
}
2013-07-11 14:36:31 +02:00
2013-07-08 15:28:45 +02:00
// Check if string is within allowed length
if ( isset ( $semantics -> maxLength )) {
$text = mb_substr ( $text , 0 , $semantics -> maxLength );
}
2013-07-11 14:36:31 +02:00
2013-07-08 15:28:45 +02:00
// Check if string is according to optional regexp in semantics
2014-03-26 08:43:29 +01:00
if ( ! ( $text === '' && $semantics -> optional ) && isset ( $semantics -> regexp )) {
2013-11-26 14:40:38 +01:00
// Escaping '/' found in patterns, so that it does not break regexp fencing.
2014-02-11 13:47:24 +01:00
$pattern = '/' . str_replace ( '/' , '\\/' , $semantics -> regexp -> pattern ) . '/' ;
2013-07-08 15:28:45 +02:00
$pattern .= isset ( $semantics -> regexp -> modifiers ) ? $semantics -> regexp -> modifiers : '' ;
if ( preg_match ( $pattern , $text ) === 0 ) {
// Note: explicitly ignore return value FALSE, to avoid removing text
// if regexp is invalid...
2014-03-26 08:43:29 +01:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Provided string is not valid according to regexp in semantics. (value: "%value", regexp: "%regexp")' , array ( '%value' => $text , '%regexp' => $pattern )));
2013-07-08 15:28:45 +02:00
$text = '' ;
}
}
2013-07-05 17:35:59 +02:00
}
2013-07-15 16:25:10 +02:00
2013-07-13 22:25:18 +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 , $isLibrary = FALSE ) {
2014-10-21 15:42:47 +02:00
if ( $this -> h5pC -> disableFileCheck === TRUE ) {
2014-11-03 07:02:45 +01:00
return TRUE ;
2014-10-21 15:42:47 +02:00
}
2013-07-13 22:25:18 +02:00
// Scan content directory for files, recurse into sub directories.
$files = array_diff ( scandir ( $contentPath ), array ( '.' , '..' ));
$valid = TRUE ;
$whitelist = $this -> h5pF -> getWhitelist ( $isLibrary , H5PCore :: $defaultContentWhitelist , H5PCore :: $defaultLibraryWhitelistExtras );
$wl_regex = '/\.(' . preg_replace ( '/ +/i' , '|' , preg_quote ( $whitelist )) . ')$/i' ;
foreach ( $files as $file ) {
$filePath = $contentPath . DIRECTORY_SEPARATOR . $file ;
if ( is_dir ( $filePath )) {
$valid = $this -> validateContentFiles ( $filePath , $isLibrary ) && $valid ;
}
else {
// Snipped from drupal 6 "file_validate_extensions". Using own code
// to avoid 1. creating a file-like object just to test for the known
// file name, 2. testing against a returned error array that could
// never be more than 1 element long anyway, 3. recreating the regex
// for every file.
if ( ! preg_match ( $wl_regex , mb_strtolower ( $file ))) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'File "%filename" not allowed. Only files with the following extensions are allowed: %files-allowed.' , array ( '%filename' => $file , '%files-allowed' => $whitelist )), 'error' );
$valid = FALSE ;
}
}
}
return $valid ;
}
2013-07-15 16:25:10 +02:00
2013-07-05 17:35:59 +02:00
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 ;
}
2013-07-08 15:28:45 +02:00
// Check if number is within valid bounds. Move within bounds if not.
if ( isset ( $semantics -> min ) && $number < $semantics -> min ) {
$number = $semantics -> min ;
}
if ( isset ( $semantics -> max ) && $number > $semantics -> max ) {
$number = $semantics -> max ;
}
// Check if number is within allowed bounds even if step value is set.
if ( isset ( $semantics -> step )) {
$testnumber = $number - ( isset ( $semantics -> min ) ? $semantics -> min : 0 );
$rest = $testnumber % $semantics -> step ;
if ( $rest !== 0 ) {
$number -= $rest ;
}
}
// Check if number has proper number of decimals.
if ( isset ( $semantics -> decimals )) {
$number = round ( $number , $semantics -> decimals );
}
2013-07-05 17:35:59 +02:00
}
/**
* 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 ) {
2013-07-15 15:07:53 +02:00
$strict = FALSE ;
if ( isset ( $semantics -> options ) && ! empty ( $semantics -> options )) {
// We have a strict set of options to choose from.
$strict = TRUE ;
$options = array ();
foreach ( $semantics -> options as $option ) {
$options [ $option -> value ] = TRUE ;
}
}
2013-07-15 16:25:10 +02:00
if ( isset ( $semantics -> multiple ) && $semantics -> multiple ) {
// Multichoice generates array of values. Test each one against valid
// options, if we are strict. First make sure we are working on an
// array.
if ( ! is_array ( $select )) {
$select = array ( $select );
}
2013-07-17 12:55:51 +02:00
foreach ( $select as $key => & $value ) {
2013-07-15 15:07:53 +02:00
if ( $strict && ! isset ( $options [ $value ])) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid selected option in multiselect.' ));
unset ( $select [ $key ]);
}
else {
2013-07-17 15:11:37 +02:00
$select [ $key ] = htmlspecialchars ( $value , ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-15 15:07:53 +02:00
}
2013-07-08 14:56:48 +02:00
}
}
2013-07-15 15:07:53 +02:00
else {
2013-07-15 16:25:10 +02:00
// Single mode. If we get an array in here, we chop off the first
// element and use that instead.
if ( is_array ( $select )) {
$select = $select [ 0 ];
}
2013-07-15 15:07:53 +02:00
if ( $strict && ! isset ( $options [ $select ])) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Invalid selected option in select.' ));
$select = $semantics -> options [ 0 ] -> value ;
}
2013-07-17 15:11:37 +02:00
$select = htmlspecialchars ( $select , ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-05 17:35:59 +02:00
}
}
/**
* 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 ) {
$field = $semantics -> field ;
$function = $this -> typeMap [ $field -> type ];
2013-07-08 15:28:45 +02:00
// Check that list is not longer than allowed length. We do this before
// iterating to avoid unneccessary work.
if ( isset ( $semantics -> max )) {
array_splice ( $list , $semantics -> max );
}
2013-07-17 11:41:23 +02:00
if ( ! is_array ( $list )) {
$list = array ();
}
2013-07-08 15:28:45 +02:00
// Validate each element in list.
2013-07-17 11:41:23 +02:00
foreach ( $list as $key => & $value ) {
if ( ! is_int ( $key )) {
unset ( $list [ $key ]);
continue ;
}
2013-07-05 17:35:59 +02:00
$this -> $function ( $value , $field );
}
}
2013-07-11 15:17:26 +02:00
// Validate a filelike object, such as video, image, audio and file.
private function _validateFilelike ( & $file , $semantics , $typevalidkeys = array ()) {
// Make sure path and mime does not have any special chars
2013-07-17 15:11:37 +02:00
$file -> path = htmlspecialchars ( $file -> path , ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-11 15:17:26 +02:00
if ( isset ( $file -> mime )) {
2013-07-17 15:11:37 +02:00
$file -> mime = htmlspecialchars ( $file -> mime , ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-11 15:17:26 +02:00
}
2013-07-10 09:59:35 +02:00
// Remove attributes that should not exist, they may contain JSON escape
// code.
2014-03-26 08:43:29 +01:00
$validkeys = array_merge ( array ( 'path' , 'mime' , 'copyright' ), $typevalidkeys );
2013-07-10 09:59:35 +02:00
if ( isset ( $semantics -> extraAttributes )) {
2013-12-03 11:36:31 +01:00
$validkeys = array_merge ( $validkeys , $semantics -> extraAttributes ); // TODO: Validate extraAttributes
2013-07-10 09:59:35 +02:00
}
2013-07-17 12:55:51 +02:00
$this -> filterParams ( $file , $validkeys );
2014-10-10 13:56:48 +02:00
2013-12-03 11:36:31 +01:00
if ( isset ( $file -> width )) {
$file -> width = intval ( $file -> width );
}
2014-10-10 13:56:48 +02:00
2013-12-03 11:36:31 +01:00
if ( isset ( $file -> height )) {
$file -> height = intval ( $file -> height );
}
2014-10-10 13:56:48 +02:00
2013-12-03 11:36:31 +01:00
if ( isset ( $file -> codecs )) {
$file -> codecs = htmlspecialchars ( $file -> codecs , ENT_QUOTES , 'UTF-8' , FALSE );
}
2014-10-10 13:56:48 +02:00
2013-12-03 11:36:31 +01:00
if ( isset ( $file -> quality )) {
if ( ! is_object ( $file -> quality ) || ! isset ( $file -> quality -> level ) || ! isset ( $file -> quality -> label )) {
unset ( $file -> quality );
}
else {
$this -> filterParams ( $file -> quality , array ( 'level' , 'label' ));
$file -> quality -> level = intval ( $file -> quality -> level );
$file -> quality -> label = htmlspecialchars ( $file -> quality -> label , ENT_QUOTES , 'UTF-8' , FALSE );
}
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( isset ( $file -> copyright )) {
$this -> validateGroup ( $file -> copyright , H5PContentValidator :: getCopyrightSemantics ());
}
2013-07-10 09:59:35 +02:00
}
2013-07-11 15:17:26 +02:00
/**
* Validate given file data
*/
public function validateFile ( & $file , $semantics ) {
$this -> _validateFilelike ( $file , $semantics );
}
2013-07-05 17:35:59 +02:00
/**
* Validate given image data
*/
public function validateImage ( & $image , $semantics ) {
2013-07-11 15:17:26 +02:00
$this -> _validateFilelike ( $image , $semantics , array ( 'width' , 'height' ));
2013-07-05 17:35:59 +02:00
}
/**
* Validate given video data
*/
public function validateVideo ( & $video , $semantics ) {
2013-07-17 12:55:51 +02:00
foreach ( $video as & $variant ) {
2013-12-03 11:36:31 +01:00
$this -> _validateFilelike ( $variant , $semantics , array ( 'width' , 'height' , 'codecs' , 'quality' ));
2013-07-05 17:35:59 +02:00
}
}
/**
* Validate given audio data
*/
public function validateAudio ( & $audio , $semantics ) {
2013-07-17 12:55:51 +02:00
foreach ( $audio as & $variant ) {
2013-07-11 15:17:26 +02:00
$this -> _validateFilelike ( $variant , $semantics );
2013-07-05 17:35:59 +02:00
}
}
/**
* Validate given group value against group semantics .
* Will recurse into validating each group member .
*/
2013-07-08 14:56:48 +02:00
public function validateGroup ( & $group , $semantics , $flatten = TRUE ) {
// Groups with just one field are compressed in the editor to only output
// the child content. (Exemption for fake groups created by
// "validateBySemantics" above)
if ( count ( $semantics -> fields ) == 1 && $flatten ) {
$field = $semantics -> fields [ 0 ];
$function = $this -> typeMap [ $field -> type ];
$this -> $function ( $group , $field );
}
else {
foreach ( $group as $key => & $value ) {
// Find semantics for name=$key
$found = FALSE ;
foreach ( $semantics -> fields as $field ) {
if ( $field -> name == $key ) {
2015-01-20 14:48:22 +01:00
if ( isset ( $semantics -> optional ) && $semantics -> optional ) {
$field -> optional = TRUE ;
}
2013-07-08 14:56:48 +02:00
$function = $this -> typeMap [ $field -> type ];
$found = TRUE ;
break ;
}
}
if ( $found ) {
2013-07-15 15:07:53 +02:00
if ( $function ) {
$this -> $function ( $value , $field );
}
else {
// We have a field type in semantics for which we don't have a
// known validator.
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'H5P internal error: unknown content type "@type" in semantics. Removing content!' , array ( '@type' => $field -> type )));
unset ( $group -> $key );
}
2013-07-08 14:56:48 +02:00
}
else {
// If validator is not found, something exists in content that does
// not have a corresponding semantics field. Remove it.
2013-07-11 15:13:15 +02:00
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'H5P internal error: no validator exists for @key' , array ( '@key' => $key )));
2013-07-08 14:56:48 +02:00
unset ( $group -> $key );
2013-07-05 17:35:59 +02:00
}
2013-07-09 10:10:32 +02:00
}
}
2015-01-20 14:48:22 +01:00
if ( ! ( isset ( $semantics -> optional ) && $semantics -> optional )) {
foreach ( $semantics -> fields as $field ) {
if ( ! ( isset ( $field -> optional ) && $field -> optional )) {
// Check if field is in group.
if ( ! property_exists ( $group , $field -> name )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'No value given for mandatory field ' . $field -> name ));
}
2013-07-09 10:10:32 +02:00
}
2013-07-05 17:35:59 +02:00
}
}
}
/**
* Validate given library value against library semantics .
2014-05-27 10:55:46 +02:00
* Check if provided library is within allowed options .
2013-07-05 17:35:59 +02:00
*
* Will recurse into validating the library ' s semantics too .
*/
public function validateLibrary ( & $value , $semantics ) {
2014-05-27 10:55:46 +02:00
if ( ! isset ( $value -> library ) || ! in_array ( $value -> library , $semantics -> options )) {
$this -> h5pF -> setErrorMessage ( $this -> h5pF -> t ( 'Library used in content is not a valid library according to semantics' ));
$value = new stdClass ();
return ;
}
2014-10-10 13:56:48 +02:00
2014-05-27 10:55:46 +02:00
if ( ! isset ( $this -> libraries [ $value -> library ])) {
2014-08-18 14:09:38 +02:00
$libspec = H5PCore :: libraryFromString ( $value -> library );
2014-05-27 10:55:46 +02:00
$library = $this -> h5pC -> loadLibrary ( $libspec [ 'machineName' ], $libspec [ 'majorVersion' ], $libspec [ 'minorVersion' ]);
2014-10-14 14:06:49 +02:00
$library [ 'semantics' ] = $this -> h5pC -> loadLibrarySemantics ( $libspec [ 'machineName' ], $libspec [ 'majorVersion' ], $libspec [ 'minorVersion' ]);
2014-05-27 10:55:46 +02:00
$this -> libraries [ $value -> library ] = $library ;
// Find all dependencies for this library
$depkey = 'preloaded-' . $libspec [ 'machineName' ];
if ( ! isset ( $this -> dependencies [ $depkey ])) {
$this -> dependencies [ $depkey ] = array (
'library' => $library ,
'type' => 'preloaded'
);
$this -> h5pC -> findLibraryDependencies ( $this -> dependencies , $library );
2013-09-17 15:53:37 +02:00
}
2013-07-05 17:35:59 +02:00
}
else {
2014-05-27 10:55:46 +02:00
$library = $this -> libraries [ $value -> library ];
}
$this -> validateGroup ( $value -> params , ( object ) array (
'type' => 'group' ,
'fields' => $library [ 'semantics' ],
), FALSE );
$validkeys = array ( 'library' , 'params' );
if ( isset ( $semantics -> extraAttributes )) {
$validkeys = array_merge ( $validkeys , $semantics -> extraAttributes );
2013-07-05 17:35:59 +02:00
}
2014-05-27 10:55:46 +02:00
$this -> filterParams ( $value , $validkeys );
2013-07-05 17:35:59 +02:00
}
2013-07-12 14:49:37 +02:00
2013-07-17 11:41:23 +02:00
/**
* Check params for a whitelist of allowed properties
*
* @ param array / object $params
* @ param array $whitelist
*/
public function filterParams ( & $params , $whitelist ) {
foreach ( $params as $key => $value ) {
if ( ! in_array ( $key , $whitelist )) {
unset ( $params -> { $key });
}
}
}
2013-07-12 14:49:37 +02:00
// XSS filters copied from drupal 7 common.inc. Some modifications done to
// replace Drupal one-liner functions with corresponding flat PHP.
/**
* Filters HTML to prevent cross - site - scripting ( XSS ) vulnerabilities .
*
* Based on kses by Ulf Harnhammar , see http :// sourceforge . net / projects / kses .
* For examples of various XSS attacks , see : http :// ha . ckers . org / xss . html .
*
* This code does four things :
* - Removes characters and constructs that can trick browsers .
* - Makes sure all HTML entities are well - formed .
* - Makes sure all HTML tags and attributes are well - formed .
* - Makes sure no HTML tags contain URLs with a disallowed protocol ( e . g .
* javascript : ) .
*
* @ param $string
* The string with raw HTML in it . It will be stripped of everything that can
* cause an XSS attack .
* @ param $allowed_tags
* An array of allowed tags .
*
* @ return
* An XSS safe version of $string , or an empty string if $string is not
* valid UTF - 8.
*
* @ ingroup sanitization
*/
private function filter_xss ( $string , $allowed_tags = array ( 'a' , 'em' , 'strong' , 'cite' , 'blockquote' , 'code' , 'ul' , 'ol' , 'li' , 'dl' , 'dt' , 'dd' )) {
if ( strlen ( $string ) == 0 ) {
return $string ;
}
// Only operate on valid UTF-8 strings. This is necessary to prevent cross
// site scripting issues on Internet Explorer 6. (Line copied from
// drupal_validate_utf8)
if ( preg_match ( '/^./us' , $string ) != 1 ) {
return '' ;
}
// Store the text format.
$this -> _filter_xss_split ( $allowed_tags , TRUE );
// Remove NULL characters (ignored by some browsers).
$string = str_replace ( chr ( 0 ), '' , $string );
// Remove Netscape 4 JS entities.
$string = preg_replace ( '%&\s*\{[^}]*(\}\s*;?|$)%' , '' , $string );
// Defuse all HTML entities.
$string = str_replace ( '&' , '&' , $string );
// Change back only well-formed entities in our whitelist:
// Decimal numeric entities.
$string = preg_replace ( '/&#([0-9]+;)/' , '&#\1' , $string );
// Hexadecimal numeric entities.
$string = preg_replace ( '/&#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/' , '&#x\1' , $string );
// Named entities.
$string = preg_replace ( '/&([A-Za-z][A-Za-z0-9]*;)/' , '&\1' , $string );
return preg_replace_callback ( ' %
(
< ( ? = [ ^ a - zA - Z !/ ]) # a lone <
| # or
<!--.* ? --> # a comment
| # or
< [ ^> ] * ( >| $ ) # a string that starts with a <, up until the > or the end of the string
| # or
> # just a >
) % x ', array($this, ' _filter_xss_split ' ), $string );
}
/**
* Processes an HTML tag .
*
* @ param $m
* An array with various meaning depending on the value of $store .
* If $store is TRUE then the array contains the allowed tags .
* If $store is FALSE then the array has one element , the HTML tag to process .
* @ param $store
* Whether to store $m .
*
* @ return
* If the element isn ' t allowed , an empty string . Otherwise , the cleaned up
* version of the HTML element .
*/
private function _filter_xss_split ( $m , $store = FALSE ) {
static $allowed_html ;
if ( $store ) {
$allowed_html = array_flip ( $m );
return ;
}
$string = $m [ 1 ];
if ( substr ( $string , 0 , 1 ) != '<' ) {
// We matched a lone ">" character.
return '>' ;
}
elseif ( strlen ( $string ) == 1 ) {
// We matched a lone "<" character.
return '<' ;
}
if ( ! preg_match ( '%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?|(<!--.*?-->)$%' , $string , $matches )) {
// Seriously malformed.
return '' ;
}
$slash = trim ( $matches [ 1 ]);
$elem = & $matches [ 2 ];
$attrlist = & $matches [ 3 ];
$comment = & $matches [ 4 ];
if ( $comment ) {
$elem = '!--' ;
}
if ( ! isset ( $allowed_html [ strtolower ( $elem )])) {
// Disallowed HTML element.
return '' ;
}
if ( $comment ) {
return $comment ;
}
if ( $slash != '' ) {
return " </ $elem > " ;
}
// Is there a closing XHTML slash at the end of the attributes?
$attrlist = preg_replace ( '%(\s?)/\s*$%' , '\1' , $attrlist , - 1 , $count );
$xhtml_slash = $count ? ' /' : '' ;
// Clean up attributes.
$attr2 = implode ( ' ' , $this -> _filter_xss_attributes ( $attrlist ));
$attr2 = preg_replace ( '/[<>]/' , '' , $attr2 );
$attr2 = strlen ( $attr2 ) ? ' ' . $attr2 : '' ;
return " < $elem $attr2 $xhtml_slash > " ;
}
/**
* Processes a string of HTML attributes .
*
* @ return
* Cleaned up version of the HTML attributes .
*/
private function _filter_xss_attributes ( $attr ) {
$attrarr = array ();
$mode = 0 ;
$attrname = '' ;
while ( strlen ( $attr ) != 0 ) {
// Was the last operation successful?
$working = 0 ;
switch ( $mode ) {
case 0 :
// Attribute name, href for instance.
if ( preg_match ( '/^([-a-zA-Z]+)/' , $attr , $match )) {
$attrname = strtolower ( $match [ 1 ]);
$skip = ( $attrname == 'style' || substr ( $attrname , 0 , 2 ) == 'on' );
$working = $mode = 1 ;
$attr = preg_replace ( '/^[-a-zA-Z]+/' , '' , $attr );
}
break ;
case 1 :
// Equals sign or valueless ("selected").
if ( preg_match ( '/^\s*=\s*/' , $attr )) {
$working = 1 ; $mode = 2 ;
$attr = preg_replace ( '/^\s*=\s*/' , '' , $attr );
break ;
}
if ( preg_match ( '/^\s+/' , $attr )) {
$working = 1 ; $mode = 0 ;
if ( ! $skip ) {
$attrarr [] = $attrname ;
}
$attr = preg_replace ( '/^\s+/' , '' , $attr );
}
break ;
case 2 :
// Attribute value, a URL after href= for instance.
if ( preg_match ( '/^"([^"]*)"(\s+|$)/' , $attr , $match )) {
$thisval = $this -> filter_xss_bad_protocol ( $match [ 1 ]);
if ( ! $skip ) {
$attrarr [] = " $attrname = \" $thisval\ " " ;
}
$working = 1 ;
$mode = 0 ;
$attr = preg_replace ( '/^"[^"]*"(\s+|$)/' , '' , $attr );
break ;
}
if ( preg_match ( " /^'([^']*)'( \ s+| $ )/ " , $attr , $match )) {
$thisval = $this -> filter_xss_bad_protocol ( $match [ 1 ]);
if ( ! $skip ) {
$attrarr [] = " $attrname =' $thisval ' " ;
}
$working = 1 ; $mode = 0 ;
$attr = preg_replace ( " /^'[^']*'( \ s+| $ )/ " , '' , $attr );
break ;
}
if ( preg_match ( " %^([^ \ s \" ']+)( \ s+| $ )% " , $attr , $match )) {
$thisval = $this -> filter_xss_bad_protocol ( $match [ 1 ]);
if ( ! $skip ) {
$attrarr [] = " $attrname = \" $thisval\ " " ;
}
$working = 1 ; $mode = 0 ;
$attr = preg_replace ( " %^[^ \ s \" ']+( \ s+| $ )% " , '' , $attr );
}
break ;
}
if ( $working == 0 ) {
// Not well formed; remove and try again.
$attr = preg_replace ( ' /
^
(
" [^ " ] * ( " | $ ) # - a string that starts with a double quote, up until the next double quote or the end of the string
| # or
\ ' [ ^ \ ' ] * ( \ ' | $ ) | # - a string that starts with a quote, up until the next quote or the end of the string
| # or
\S # - a non-whitespace character
) * # any number of the above three
\s * # any number of whitespaces
/ x ', ' ' , $attr );
$mode = 0 ;
}
}
// The attribute list ends with a valueless attribute like "selected".
if ( $mode == 1 && ! $skip ) {
$attrarr [] = $attrname ;
}
return $attrarr ;
}
2014-03-26 08:43:29 +01:00
// TODO: Remove Drupal related stuff in docs.
2013-07-12 14:49:37 +02:00
/**
* Processes an HTML attribute value and strips dangerous protocols from URLs .
2014-10-10 13:56:48 +02:00
*
2013-07-12 14:49:37 +02:00
* @ param $string
* The string with the attribute value .
* @ param $decode
* ( deprecated ) Whether to decode entities in the $string . Set to FALSE if the
* $string is in plain text , TRUE otherwise . Defaults to TRUE . This parameter
* is deprecated and will be removed in Drupal 8. To process a plain - text URI ,
* call _strip_dangerous_protocols () or check_url () instead .
*
* @ return
* Cleaned up and HTML - escaped version of $string .
*/
private function filter_xss_bad_protocol ( $string , $decode = TRUE ) {
// Get the plain text representation of the attribute value (i.e. its meaning).
// @todo Remove the $decode parameter in Drupal 8, and always assume an HTML
// string that needs decoding.
if ( $decode ) {
$string = html_entity_decode ( $string , ENT_QUOTES , 'UTF-8' );
}
2013-07-17 15:11:37 +02:00
return htmlspecialchars ( $this -> _strip_dangerous_protocols ( $string ), ENT_QUOTES , 'UTF-8' , FALSE );
2013-07-12 14:49:37 +02:00
}
/**
* Strips dangerous protocols ( e . g . 'javascript:' ) from a URI .
*
* This function must be called for all URIs within user - entered input prior
* to being output to an HTML attribute value . It is often called as part of
* check_url () or filter_xss (), but those functions return an HTML - encoded
* string , so this function can be called independently when the output needs to
* be a plain - text string for passing to t (), l (), drupal_attributes (), or
* another function that will call check_plain () separately .
*
* @ param $uri
* A plain - text URI that might contain dangerous protocols .
*
* @ return
* A plain - text URI stripped of dangerous protocols . As with all plain - text
* strings , this return value must not be output to an HTML page without
* check_plain () being called on it . However , it can be passed to functions
* expecting plain - text strings .
*
* @ see check_url ()
*/
private function _strip_dangerous_protocols ( $uri ) {
static $allowed_protocols ;
if ( ! isset ( $allowed_protocols )) {
$allowed_protocols = array_flip ( array ( 'ftp' , 'http' , 'https' , 'mailto' ));
}
// Iteratively remove any invalid protocol found.
do {
$before = $uri ;
$colonpos = strpos ( $uri , ':' );
if ( $colonpos > 0 ) {
// We found a colon, possibly a protocol. Verify.
$protocol = substr ( $uri , 0 , $colonpos );
// If a colon is preceded by a slash, question mark or hash, it cannot
// possibly be part of the URL scheme. This must be a relative URL, which
// inherits the (safe) protocol of the base document.
if ( preg_match ( '![/?#]!' , $protocol )) {
break ;
}
// Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
// (URI Comparison) scheme comparison must be case-insensitive.
if ( ! isset ( $allowed_protocols [ strtolower ( $protocol )])) {
$uri = substr ( $uri , $colonpos + 1 );
}
}
} while ( $before != $uri );
return $uri ;
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
public static function getCopyrightSemantics () {
static $semantics ;
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
if ( $semantics === NULL ) {
$semantics = ( object ) array (
'name' => 'copyright' ,
'type' => 'group' ,
'label' => 'Copyright information' ,
'fields' => array (
( object ) array (
'name' => 'title' ,
'type' => 'text' ,
'label' => 'Title' ,
'placeholder' => 'La Gioconda' ,
'optional' => TRUE
),
( object ) array (
'name' => 'author' ,
'type' => 'text' ,
'label' => 'Author' ,
'placeholder' => 'Leonardo da Vinci' ,
'optional' => TRUE
),
( object ) array (
'name' => 'year' ,
'type' => 'text' ,
'label' => 'Year(s)' ,
'placeholder' => '1503 - 1517' ,
'optional' => TRUE
),
( object ) array (
'name' => 'source' ,
'type' => 'text' ,
'label' => 'Source' ,
'placeholder' => 'http://en.wikipedia.org/wiki/Mona_Lisa' ,
'optional' => true ,
'regexp' => ( object ) array (
'pattern' => '^http[s]?://.+' ,
'modifiers' => 'i'
)
),
( object ) array (
'name' => 'license' ,
'type' => 'select' ,
'label' => 'License' ,
'default' => 'U' ,
'options' => array (
( object ) array (
'value' => 'U' ,
'label' => 'Undisclosed'
),
( object ) array (
'value' => 'CC BY' ,
'label' => 'Attribution'
),
( object ) array (
'value' => 'CC BY-SA' ,
'label' => 'Attribution-ShareAlike'
),
( object ) array (
'value' => 'CC BY-ND' ,
'label' => 'Attribution-NoDerivs'
),
( object ) array (
'value' => 'CC BY-NC' ,
'label' => 'Attribution-NonCommercial'
),
( object ) array (
'value' => 'CC BY-NC-SA' ,
'label' => 'Attribution-NonCommercial-ShareAlike'
),
( object ) array (
'value' => 'CC BY-NC-ND' ,
'label' => 'Attribution-NonCommercial-NoDerivs'
),
( object ) array (
'value' => 'GNU GPL' ,
'label' => 'General Public License'
),
( object ) array (
'value' => 'PD' ,
'label' => 'Public Domain'
),
( object ) array (
'value' => 'ODC PDDL' ,
'label' => 'Public Domain Dedication and Licence'
),
( object ) array (
'value' => 'CC PDM' ,
'label' => 'Public Domain Mark'
),
( object ) array (
'value' => 'C' ,
'label' => 'Copyright'
)
)
)
)
);
}
2014-10-10 13:56:48 +02:00
2014-03-26 08:43:29 +01:00
return $semantics ;
}
2013-07-05 17:35:59 +02:00
}