2016-01-11 18:31:04 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-12 12:55:03 +01:00
|
|
|
|
* File info?
|
2016-01-11 18:31:04 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-12 12:55:03 +01:00
|
|
|
|
* The default file storage class for H5P. Will carry out the requested file
|
|
|
|
|
* operations using PHP's standard file operation functions.
|
|
|
|
|
*
|
|
|
|
|
* Some implementations of H5P that doesn't use the standard file system will
|
|
|
|
|
* want to create their own implementation of the \H5P\FileStorage interface.
|
2016-01-11 18:31:04 +01:00
|
|
|
|
*
|
2016-01-12 12:55:03 +01:00
|
|
|
|
* @package H5P
|
|
|
|
|
* @copyright 2016 Joubel AS
|
|
|
|
|
* @license MIT
|
2016-01-11 18:31:04 +01:00
|
|
|
|
*/
|
2016-01-25 14:46:06 +01:00
|
|
|
|
class H5PDefaultStorage implements \H5PFileStorage {
|
2016-01-11 18:31:04 +01:00
|
|
|
|
private $path;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The great Constructor!
|
2016-01-12 12:55:03 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* The base location of H5P files
|
2016-01-11 18:31:04 +01:00
|
|
|
|
*/
|
|
|
|
|
function __construct($path) {
|
|
|
|
|
// Set H5P storage path
|
|
|
|
|
$this->path = $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store the library folder.
|
|
|
|
|
*
|
|
|
|
|
* @param array $library
|
|
|
|
|
* Library properties
|
|
|
|
|
*/
|
|
|
|
|
public function saveLibrary($library) {
|
|
|
|
|
$dest = $this->path . '/libraries/' . \H5PCore::libraryToString($library, TRUE);
|
|
|
|
|
|
|
|
|
|
// Make sure destination dir doesn't exist
|
|
|
|
|
\H5PCore::deleteFileTree($dest);
|
|
|
|
|
|
|
|
|
|
// Move library folder
|
|
|
|
|
self::copyFileTree($library['uploadDirectory'], $dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store the content folder.
|
|
|
|
|
*
|
|
|
|
|
* @param string $source
|
|
|
|
|
* Path on file system to content directory.
|
|
|
|
|
* @param int $id
|
|
|
|
|
* What makes this content unique.
|
|
|
|
|
*/
|
|
|
|
|
public function saveContent($source, $id) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
$dest = "{$this->path}/content/{$id}";
|
|
|
|
|
|
|
|
|
|
// Remove any old content
|
|
|
|
|
\H5PCore::deleteFileTree($dest);
|
|
|
|
|
|
|
|
|
|
self::copyFileTree($source, $dest);
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove content folder.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* Content identifier
|
|
|
|
|
*/
|
|
|
|
|
public function deleteContent($id) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
\H5PCore::deleteFileTree("{$this->path}/content/{$id}");
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a stored copy of the content folder.
|
|
|
|
|
*
|
|
|
|
|
* @param string $id
|
|
|
|
|
* Identifier of content to clone.
|
|
|
|
|
* @param int $newId
|
|
|
|
|
* The cloned content's identifier
|
|
|
|
|
*/
|
|
|
|
|
public function cloneContent($id, $newId) {
|
|
|
|
|
$path = $this->path . '/content/';
|
|
|
|
|
self::copyFileTree($path . $id, $path . $newId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get path to a new unique tmp folder.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* Path
|
|
|
|
|
*/
|
|
|
|
|
public function getTmpPath() {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
return $this->path . '/temp/' . uniqid('h5p-');
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch content folder and save in target directory.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* Content identifier
|
|
|
|
|
* @param string $target
|
|
|
|
|
* Where the content folder will be saved
|
|
|
|
|
*/
|
|
|
|
|
public function exportContent($id, $target) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
self::copyFileTree("{$this->path}/content/{$id}", $target);
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch library folder and save in target directory.
|
|
|
|
|
*
|
|
|
|
|
* @param array $library
|
|
|
|
|
* Library properties
|
|
|
|
|
* @param string $target
|
|
|
|
|
* Where the library folder will be saved
|
|
|
|
|
*/
|
|
|
|
|
public function exportLibrary($library, $target) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
$folder = \H5PCore::libraryToString($library, TRUE);
|
2016-01-25 14:46:55 +01:00
|
|
|
|
self::copyFileTree("{$this->path}/libraries/{$folder}", "{$target}/{$folder}");
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save export in file system
|
|
|
|
|
*
|
|
|
|
|
* @param string $source
|
|
|
|
|
* Path on file system to temporary export file.
|
|
|
|
|
* @param string $filename
|
|
|
|
|
* Name of export file.
|
|
|
|
|
*/
|
|
|
|
|
public function saveExport($source, $filename) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
$this->deleteExport($filename);
|
|
|
|
|
self::dirReady("{$this->path}/exports");
|
|
|
|
|
copy($source, "{$this->path}/exports/{$filename}");
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes given export file
|
|
|
|
|
*
|
|
|
|
|
* @param string $filename
|
|
|
|
|
*/
|
2016-01-12 10:31:36 +01:00
|
|
|
|
public function deleteExport($filename) {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
$target = "{$this->path}/exports/{$filename}";
|
|
|
|
|
if (file_exists($target)) {
|
|
|
|
|
unlink($target);
|
|
|
|
|
}
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 14:23:34 +01:00
|
|
|
|
/**
|
|
|
|
|
* Will concatenate all JavaScrips and Stylesheets into two files in order
|
|
|
|
|
* to improve page performance.
|
|
|
|
|
*
|
|
|
|
|
* @param array $files
|
|
|
|
|
* A set of all the assets required for content to display
|
|
|
|
|
* @param string $key
|
|
|
|
|
* Hashed key for cached asset
|
|
|
|
|
*/
|
|
|
|
|
public function cacheAssets(&$files, $key) {
|
|
|
|
|
foreach ($files as $type => $assets) {
|
2016-01-25 14:47:48 +01:00
|
|
|
|
if (empty($assets)) {
|
|
|
|
|
continue; // Skip no assets
|
|
|
|
|
}
|
2016-01-13 14:23:34 +01:00
|
|
|
|
|
2016-01-25 14:47:48 +01:00
|
|
|
|
$content = '';
|
2016-01-13 14:23:34 +01:00
|
|
|
|
foreach ($assets as $asset) {
|
|
|
|
|
// Get content from asset file
|
|
|
|
|
$assetContent = file_get_contents($this->path . $asset->path);
|
|
|
|
|
$cssRelPath = preg_replace('/[^\/]+$/', '', $asset->path);
|
|
|
|
|
|
|
|
|
|
// Get file content and concatenate
|
|
|
|
|
if ($type === 'scripts') {
|
|
|
|
|
$content .= $assetContent . ";\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Rewrite relative URLs used inside stylesheets
|
|
|
|
|
$content .= preg_replace_callback(
|
|
|
|
|
'/url\([\'"]?([^"\')]+)[\'"]?\)/i',
|
|
|
|
|
function ($matches) use ($cssRelPath) {
|
2016-01-25 14:49:29 +01:00
|
|
|
|
if (preg_match("/^([a-z0-9]+:)?\//i", $matches[1]) === 1) {
|
|
|
|
|
return $matches[0]; // Not relative, skip
|
|
|
|
|
}
|
|
|
|
|
return 'url("../' . $cssRelPath . $matches[1] . '")';
|
2016-01-13 14:23:34 +01:00
|
|
|
|
},
|
2016-01-25 14:49:29 +01:00
|
|
|
|
$assetContent) . "\n";
|
2016-01-13 14:23:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::dirReady("{$this->path}/cachedassets");
|
|
|
|
|
$ext = ($type === 'scripts' ? 'js' : 'css');
|
|
|
|
|
file_put_contents("{$this->path}/cachedassets/{$key}.{$ext}", $content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the newly created cache
|
|
|
|
|
$files = self::formatCachedAssets($key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will check if there are cache assets available for content.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* Hashed key for cached asset
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getCachedAssets($key) {
|
|
|
|
|
if (!file_exists("{$this->path}/cachedassets/{$key}.js") ||
|
|
|
|
|
!file_exists("{$this->path}/cachedassets/{$key}.css") {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return self::formatCachedAssets($key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the aggregated cache files.
|
|
|
|
|
*
|
|
|
|
|
* @param array $keys
|
|
|
|
|
* The hash keys of removed files
|
|
|
|
|
*/
|
|
|
|
|
public function deleteCachedAssets($keys) {
|
|
|
|
|
$context = \context_system::instance();
|
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
|
|
|
|
|
foreach ($keys as $hash) {
|
|
|
|
|
foreach (array('js', 'css') as $ext) {
|
|
|
|
|
$path = "{$this->path}/cachedassets/{$key}.{$ext}";
|
|
|
|
|
if (file_exists($path)) {
|
|
|
|
|
unlink($path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format the cached assets data the way it's supposed to be.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* Hashed key for cached asset
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private static function formatCachedAssets($key) {
|
|
|
|
|
return array(
|
|
|
|
|
'scripts' => array((object) array(
|
|
|
|
|
'path' => "/cachedassets/{$key}.js",
|
|
|
|
|
'version' => ''
|
|
|
|
|
)),
|
|
|
|
|
'styles' => array((object) array(
|
|
|
|
|
'path' => "/cachedassets/{$key}.css",
|
|
|
|
|
'version' => ''
|
|
|
|
|
))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 18:31:04 +01:00
|
|
|
|
/**
|
|
|
|
|
* Recursive function for copying directories.
|
|
|
|
|
*
|
|
|
|
|
* @param string $source
|
|
|
|
|
* From path
|
|
|
|
|
* @param string $destination
|
|
|
|
|
* To path
|
|
|
|
|
* @return boolean
|
|
|
|
|
* Indicates if the directory existed.
|
|
|
|
|
*/
|
|
|
|
|
private static function copyFileTree($source, $destination) {
|
|
|
|
|
if (!self::dirReady($destination)) {
|
|
|
|
|
throw new \Exception('unabletocopy');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dir = opendir($source);
|
|
|
|
|
if ($dir === FALSE) {
|
|
|
|
|
trigger_error('Unable to open directory ' . $source, E_USER_WARNING);
|
|
|
|
|
throw new \Exception('unabletocopy');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (false !== ($file = readdir($dir))) {
|
|
|
|
|
if (($file != '.') && ($file != '..') && $file != '.git' && $file != '.gitignore') {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
if (is_dir("{$source}/{$file}")) {
|
|
|
|
|
self::copyFileTree("{$source}/{$file}", "{$destination}/{$file}");
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2016-01-12 12:55:03 +01:00
|
|
|
|
copy("{$source}/{$file}", "{$destination}/{$file}");
|
2016-01-11 18:31:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir($dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursive function that makes sure the specified directory exists and
|
|
|
|
|
* is writable.
|
|
|
|
|
*
|
2016-01-25 14:48:14 +01:00
|
|
|
|
* TODO: Will be made private when the editor file handling is done by this
|
|
|
|
|
* class!
|
|
|
|
|
*
|
2016-01-11 18:31:04 +01:00
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-01-25 14:48:14 +01:00
|
|
|
|
public static function dirReady($path) {
|
2016-01-11 18:31:04 +01:00
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
|
$parent = preg_replace("/\/[^\/]+\/?$/", '', $path);
|
|
|
|
|
if (!self::dirReady($parent)) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mkdir($path, 0777, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
|
trigger_error('Path is not a directory ' . $path, E_USER_WARNING);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_writable($path)) {
|
|
|
|
|
trigger_error('Unable to write to ' . $path . ' – check directory permissions –', E_USER_WARNING);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|