aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Template
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2021-01-19 11:20:50 +0100
committerJulius Härtl <jus@bitgrid.net>2021-01-28 12:00:20 +0100
commit7e6d69d166cbc92fb457fc72efc9abe850a0bbe4 (patch)
tree54328a78dbbd8b4570d7e6913ce4c8291a451a36 /lib/private/Files/Template
parent66fa3fca8323fe763e4177cc31d3f535802e976a (diff)
downloadnextcloud-server-7e6d69d166cbc92fb457fc72efc9abe850a0bbe4.tar.gz
nextcloud-server-7e6d69d166cbc92fb457fc72efc9abe850a0bbe4.zip
Add templatedirectory config value to let admins have their custom templates by default
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private/Files/Template')
-rw-r--r--lib/private/Files/Template/TemplateManager.php73
1 files changed, 64 insertions, 9 deletions
diff --git a/lib/private/Files/Template/TemplateManager.php b/lib/private/Files/Template/TemplateManager.php
index 3b033a44048..614440327e8 100644
--- a/lib/private/Files/Template/TemplateManager.php
+++ b/lib/private/Files/Template/TemplateManager.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace OC\Files\Template;
+use OC\Files\Cache\Scanner;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\File;
@@ -59,6 +60,7 @@ class TemplateManager implements ITemplateManager {
private $l10n;
private $logger;
private $userId;
+ private $l10nFactory;
public function __construct(
IServerContainer $serverContainer,
@@ -67,7 +69,7 @@ class TemplateManager implements ITemplateManager {
IUserSession $userSession,
IPreview $previewManager,
IConfig $config,
- IFactory $l10n,
+ IFactory $l10nFactory,
LoggerInterface $logger
) {
$this->serverContainer = $serverContainer;
@@ -75,7 +77,8 @@ class TemplateManager implements ITemplateManager {
$this->rootFolder = $rootFolder;
$this->previewManager = $previewManager;
$this->config = $config;
- $this->l10n = $l10n->get('lib');
+ $this->l10nFactory = $l10nFactory;
+ $this->l10n = $l10nFactory->get('lib');
$this->logger = $logger;
$user = $userSession->getUser();
$this->userId = $user ? $user->getUID() : null;
@@ -224,14 +227,66 @@ class TemplateManager implements ITemplateManager {
if ($userId !== null) {
$this->userId = $userId;
}
- $userFolder = $this->rootFolder->getUserFolder($this->userId);
- $templateDirectoryPath = $path ?? $this->l10n->t('Templates') . '/';
+
+ $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton';
+ $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates';
+ $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory);
+ $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory);
+ $userLang = $this->l10nFactory->getUserLanguage();
+
try {
- $userFolder->get($templateDirectoryPath);
- } catch (NotFoundException $e) {
- $folder = $userFolder->newFolder($templateDirectoryPath);
- $folder->newFile('Testtemplate.txt');
+ $l10n = $this->l10nFactory->get('lib', $userLang);
+ $userFolder = $this->rootFolder->getUserFolder($this->userId);
+ $userTemplatePath = $path ?? $l10n->t('Templates') . '/';
+
+ // All locations are default so we just need to rename the directory to the users language
+ if ($skeletonPath === $defaultSkeletonDirectory && $skeletonTemplatePath === $defaultTemplateDirectory && $userFolder->nodeExists('Templates')) {
+ $newPath = $userFolder->getPath() . '/' . $userTemplatePath;
+ if ($newPath !== $userFolder->get('Templates')->getPath()) {
+ $userFolder->get('Templates')->move($newPath);
+ }
+ $this->setTemplatePath($userTemplatePath);
+ return;
+ }
+
+ // A custom template directory is specified
+ if (!empty($skeletonTemplatePath) && $skeletonTemplatePath !== $defaultTemplateDirectory) {
+ // In case the shipped template files are in place we remove them
+ if ($skeletonPath === $defaultSkeletonDirectory && $userFolder->nodeExists('Templates')) {
+ $shippedSkeletonTemplates = $userFolder->get('Templates');
+ $shippedSkeletonTemplates->delete();
+ }
+ try {
+ $userFolder->get($userTemplatePath);
+ } catch (NotFoundException $e) {
+ $folder = $userFolder->newFolder($userTemplatePath);
+
+ $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang);
+ if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) {
+ \OC_Util::copyr($localizedSkeletonTemplatePath, $folder);
+ $userFolder->getStorage()->getScanner()->scan($userTemplatePath, Scanner::SCAN_RECURSIVE);
+ }
+ }
+ $this->setTemplatePath($userTemplatePath);
+ }
+ } catch (\Throwable $e) {
+ $this->logger->error('Failed to rename templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates']);
}
- $this->setTemplatePath($templateDirectoryPath);
+ }
+
+ private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) {
+ $localizedSkeletonTemplatePath = str_replace('{lang}', $userLang, $skeletonTemplatePath);
+
+ if (!file_exists($localizedSkeletonTemplatePath)) {
+ $dialectStart = strpos($userLang, '_');
+ if ($dialectStart !== false) {
+ $localizedSkeletonTemplatePath = str_replace('{lang}', substr($userLang, 0, $dialectStart), $skeletonTemplatePath);
+ }
+ if ($dialectStart === false || !file_exists($localizedSkeletonTemplatePath)) {
+ $localizedSkeletonTemplatePath = str_replace('{lang}', 'default', $skeletonTemplatePath);
+ }
+ }
+
+ return $localizedSkeletonTemplatePath;
}
}