diff --git a/h5p-default-storage.class.php b/h5p-default-storage.class.php index 9055266..6e39670 100644 --- a/h5p-default-storage.class.php +++ b/h5p-default-storage.class.php @@ -143,11 +143,18 @@ class H5PDefaultStorage implements \H5PFileStorage { * Path on file system to temporary export file. * @param string $filename * Name of export file. + * @throws Exception Unable to save the file */ public function saveExport($source, $filename) { $this->deleteExport($filename); - self::dirReady("{$this->path}/exports"); - copy($source, "{$this->path}/exports/{$filename}"); + + if (!self::dirReady("{$this->path}/exports")) { + throw new Exception("Unable to create directory for H5P export file."); + } + + if (!copy($source, "{$this->path}/exports/{$filename}")) { + throw new Exception("Unable to save H5P export file."); + } } /** @@ -162,6 +169,17 @@ class H5PDefaultStorage implements \H5PFileStorage { } } + /** + * Check if the given export file exists + * + * @param string $filename + * @return boolean + */ + public function hasExport($filename) { + $target = "{$this->path}/exports/{$filename}"; + return file_exists($target); + } + /** * Will concatenate all JavaScrips and Stylesheets into two files in order * to improve page performance. diff --git a/h5p-file-storage.interface.php b/h5p-file-storage.interface.php index abee27a..3c621f7 100644 --- a/h5p-file-storage.interface.php +++ b/h5p-file-storage.interface.php @@ -90,6 +90,14 @@ interface H5PFileStorage { */ public function deleteExport($filename); + /** + * Check if the given export file exists + * + * @param string $filename + * @return boolean + */ + public function hasExport($filename); + /** * Will concatenate all JavaScrips and Stylesheets into two files in order * to improve page performance. diff --git a/h5p.classes.php b/h5p.classes.php index ffe6f09..e043966 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -553,6 +553,11 @@ interface H5PFrameworkInterface { * return int */ public function getLibraryContentCount(); + + /** + * Will trigger after the export file is created. + */ + public function afterExportCreated(); } /** @@ -1574,11 +1579,15 @@ Class H5PExport { $zip = new ZipArchive(); $zip->open($tmpFile, ZipArchive::CREATE | ZipArchive::OVERWRITE); + // Some system needs the root prefix for ZipArchive's addFile() + $rootPrefix = (empty($_SERVER['DOCUMENT_ROOT']) ? '' : $_SERVER['DOCUMENT_ROOT'] . '/'); + // Add all the files from the tmp dir. foreach ($files as $file) { // Please note that the zip format has no concept of folders, we must // use forward slashes to separate our directories. $zip->addFile($file->absolutePath, $file->relativePath); + $zip->addFile($rootPrefix . $file->absolutePath, $file->relativePath); } // Close zip and remove tmp dir @@ -1591,9 +1600,11 @@ Class H5PExport { } catch (Exception $e) { $this->h5pF->setErrorMessage($this->h5pF->t($e->getMessage())); + return false; } unlink($tmpFile); + $this->h5pF->afterExportCreated(); return true; } @@ -1808,7 +1819,10 @@ class H5PCore { * @return Object NULL on failure. */ public function filterParameters(&$content) { - if (isset($content['filtered']) && $content['filtered'] !== '') { + if (!empty($content['filtered']) && + (!$this->exportEnabled || + ($content['slug'] && + $this->fs->hasExport($content['slug'] . '-' . $content['id'] . '.h5p')))) { return $content['filtered']; }