aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2021-02-26 12:42:10 -0500
committerGravatar GitHub <noreply@github.com> 2021-02-26 18:42:10 +0100
commit0ce798d40bd16e7fb3b9fcd7b2b2012e6226dd24 (patch)
tree01c2169b7084c63d0c543d479fa87b886e0d8a7c /lib/Minz
parent9ea3e7710ad54e76ff1f84bafb208da865816f93 (diff)
Add support for extension user files (#3433)
Extension user files can be stored easily in the user folder instead of the static folder.
Diffstat (limited to 'lib/Minz')
-rw-r--r--lib/Minz/Extension.php45
1 files changed, 34 insertions, 11 deletions
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 .
- '&amp;t=' . $type .
- '&amp;' . $mtime;
- return Minz_Url::display($url, 'php');
+ return Minz_Url::display("/ext.php?f={$file_name_url}&amp;t={$type}&amp;{$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);
+ }
+ }
}