Repository navigation
Add optional setting to use system temp directory for H5P temporary files #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
08eb899
fc3adf5
225f219
2b42aa9
1254094
a868b4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,16 +94,67 @@ public function getLibraryFileUrl($libraryFolderName, $fileName) { | |
| return $upload_dir['baseurl'] . '/h5p/libraries/' . $libraryFolderName . '/' . $fileName; | ||
| } | ||
|
|
||
| /** | ||
| * Get path to the folder H5P stages uploads in inside the system temp | ||
| * directory. Derived from the WordPress installation path, so installations | ||
| * sharing a server do not share a staging folder. Note that all sites in a | ||
| * network do share it. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function getSystemTmpParentPath() { | ||
| return rtrim(sys_get_temp_dir(), '/\\') . '/h5p-' . substr(md5(ABSPATH), 0, 8); | ||
| } | ||
|
|
||
| /** | ||
| * Get path to a new unique tmp folder inside the system temp directory. | ||
| * | ||
| * Staging happens in a dedicated, non-listable subfolder rather than | ||
| * directly in the system temp directory, so other local users can not | ||
| * predict and pre-create the paths we are about to write to. | ||
| * | ||
| * @return string|bool | ||
| * Unique path without extension, or FALSE if the system temp directory | ||
| * can not be used (missing, not writable or outside open_basedir). | ||
| */ | ||
| private function getSystemTmpPath() { | ||
| $parent = self::getSystemTmpParentPath(); | ||
|
|
||
| // Suppress warnings, an unusable temp dir is handled by the return value. | ||
| if (!@wp_mkdir_p($parent) || !@is_writable($parent)) { | ||
| return FALSE; | ||
| } | ||
| @chmod($parent, 0700); | ||
|
|
||
| // Using wp_generate_password() to generate string to use as part of the | ||
| // parent directory name | ||
| return $parent . '/h5p-' . wp_generate_password(16, FALSE); | ||
| } | ||
|
|
||
| /** | ||
| * Helper, get path to the H5P tmp folder in the plugin directory. | ||
| */ | ||
| private function getPluginTmpPath() { | ||
| $plugin = H5P_Plugin::get_instance(); | ||
| $core = $plugin->get_h5p_instance('core'); | ||
| return $core->fs->getTmpPath(); | ||
| } | ||
|
|
||
| /** | ||
| * Implements getUploadedH5PFolderPath | ||
| */ | ||
| public function getUploadedH5pFolderPath() { | ||
| static $dir; | ||
|
|
||
| if (is_null($dir)) { | ||
| $plugin = H5P_Plugin::get_instance(); | ||
| $core = $plugin->get_h5p_instance('core'); | ||
| $dir = $core->fs->getTmpPath(); | ||
| if (get_option('h5p_use_system_temp_dir', FALSE)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said, having the option to have this in other H5P integrations, too, would be benefitial. I am pinging @icc here (currently on vacation AFAIK) as I cannot tell whether that's in the interest of H5P Group. Then
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my latest comments below. |
||
| $dir = $this->getSystemTmpPath(); | ||
| } | ||
|
|
||
| if (empty($dir)) { | ||
| // Not using or unable to use the system temp dir, fall back. | ||
| $dir = $this->getPluginTmpPath(); | ||
| } | ||
| } | ||
|
|
||
| return $dir; | ||
|
|
@@ -116,9 +167,16 @@ public function getUploadedH5pPath() { | |
| static $path; | ||
|
|
||
| if (is_null($path)) { | ||
| $plugin = H5P_Plugin::get_instance(); | ||
| $core = $plugin->get_h5p_instance('core'); | ||
| $path = $core->fs->getTmpPath() . '.h5p'; | ||
| if (get_option('h5p_use_system_temp_dir', FALSE)) { | ||
|
Lin87 marked this conversation as resolved.
|
||
| $path = $this->getSystemTmpPath(); | ||
| } | ||
|
|
||
| if (empty($path)) { | ||
| // Not using or unable to use the system temp dir, fall back. | ||
| $path = $this->getPluginTmpPath(); | ||
| } | ||
|
|
||
| $path .= '.h5p'; | ||
| } | ||
|
|
||
| return $path; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.