From 0ce798d40bd16e7fb3b9fcd7b2b2012e6226dd24 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Fri, 26 Feb 2021 12:42:10 -0500 Subject: Add support for extension user files (#3433) Extension user files can be stored easily in the user folder instead of the static folder. --- lib/Minz/Extension.php | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 6807e0b76..c97e56355 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -148,19 +148,22 @@ abstract class Minz_Extension { * * @param $filename name of the file to serve. * @param $type the type (js or css) of the file to serve. + * @param $isStatic indicates if the file is a static file or a user file. Default is static. * @return the url corresponding to the file. */ - public function getFileUrl($filename, $type) { - $dir = substr(strrchr($this->path, '/'), 1); - $file_name_url = urlencode($dir . '/static/' . $filename); - - $absolute_path = $this->path . '/static/' . $filename; - $mtime = @filemtime($absolute_path); + public function getFileUrl($filename, $type, $isStatic = true) { + if ($isStatic) { + $dir = basename($this->path); + $file_name_url = urlencode("{$dir}/static/{$filename}"); + $mtime = @filemtime("{$this->path}/static/{$filename}"); + } else { + $username = Minz_Session::param('currentUser'); + $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; + $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}"); + $mtime = @filemtime($path); + } - $url = '/ext.php?f=' . $file_name_url . - '&t=' . $type . - '&' . $mtime; - return Minz_Url::display($url, 'php'); + return Minz_Url::display("/ext.php?f={$file_name_url}&t={$type}&{$mtime}", 'php'); } /** @@ -269,7 +272,7 @@ abstract class Minz_Extension { $this->user_configuration = $configuration; } - public function removeUserConfiguration(){ + public function removeUserConfiguration() { if (!$this->isUserConfigurationEnabled()) { return; } @@ -288,4 +291,24 @@ abstract class Minz_Extension { $this->user_configuration = null; } + + public function saveFile(string $filename, string $content) { + $username = Minz_Session::param('currentUser'); + $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}"; + + if (!file_exists($path)) { + mkdir($path, 0777, true); + } + + file_put_contents("{$path}/{$filename}", $content); + } + + public function removeFile(string $filename) { + $username = Minz_Session::param('currentUser'); + $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; + + if (file_exists($path)) { + unlink($path); + } + } } -- cgit v1.2.3