From 37afab87b56fa7b2a1b0e751df72e9624663f94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 21 Feb 2014 10:01:02 +0100 Subject: [PATCH 1/3] minimal mail template editor for administrators, refs #7177 --- apps/files_sharing/ajax/getmailtemplate.php | 21 ++++ apps/files_sharing/ajax/resetmailtemplate.php | 24 ++++ apps/files_sharing/ajax/setmailtemplate.php | 24 ++++ apps/files_sharing/ajax/settings.php | 5 + apps/files_sharing/appinfo/app.php | 3 + apps/files_sharing/css/settings-admin.css | 33 ++++++ apps/files_sharing/js/settings-admin.js | 75 +++++++++++++ apps/files_sharing/lib/mailtemplate.php | 106 ++++++++++++++++++ apps/files_sharing/settings-admin.php | 53 +++++++++ .../templates/settings-admin.php | 39 +++++++ core/templates/mail.html | 38 +++++++ 11 files changed, 421 insertions(+) create mode 100644 apps/files_sharing/ajax/getmailtemplate.php create mode 100644 apps/files_sharing/ajax/resetmailtemplate.php create mode 100644 apps/files_sharing/ajax/setmailtemplate.php create mode 100644 apps/files_sharing/ajax/settings.php create mode 100644 apps/files_sharing/css/settings-admin.css create mode 100644 apps/files_sharing/js/settings-admin.js create mode 100644 apps/files_sharing/lib/mailtemplate.php create mode 100644 apps/files_sharing/settings-admin.php create mode 100644 apps/files_sharing/templates/settings-admin.php create mode 100644 core/templates/mail.html diff --git a/apps/files_sharing/ajax/getmailtemplate.php b/apps/files_sharing/ajax/getmailtemplate.php new file mode 100644 index 0000000000..8caf0ebcec --- /dev/null +++ b/apps/files_sharing/ajax/getmailtemplate.php @@ -0,0 +1,21 @@ +renderContent(); + } catch (\OCP\Files\NotPermittedException $ex) { + \OC_Response::setStatus(403); // forbidden + } + exit(); +} +\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/resetmailtemplate.php b/apps/files_sharing/ajax/resetmailtemplate.php new file mode 100644 index 0000000000..4050235bc5 --- /dev/null +++ b/apps/files_sharing/ajax/resetmailtemplate.php @@ -0,0 +1,24 @@ +reset(); + \OC_Response::setStatus(200); // ok + } catch (\OCP\Files\NotPermittedException $ex) { + \OC_Response::setStatus(403); // forbidden + } + exit(); +} +\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/setmailtemplate.php b/apps/files_sharing/ajax/setmailtemplate.php new file mode 100644 index 0000000000..d8bb587627 --- /dev/null +++ b/apps/files_sharing/ajax/setmailtemplate.php @@ -0,0 +1,24 @@ +setContent($_POST['content']); + \OC_Response::setStatus(200); // ok + } catch (\OCP\Files\NotPermittedException $ex) { + \OC_Response::setStatus(403); // forbidden + } + exit(); +} +\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/settings.php b/apps/files_sharing/ajax/settings.php new file mode 100644 index 0000000000..da244f8de4 --- /dev/null +++ b/apps/files_sharing/ajax/settings.php @@ -0,0 +1,5 @@ +add( diff --git a/apps/files_sharing/css/settings-admin.css b/apps/files_sharing/css/settings-admin.css new file mode 100644 index 0000000000..c807fc83c7 --- /dev/null +++ b/apps/files_sharing/css/settings-admin.css @@ -0,0 +1,33 @@ +#mailTemplateSettings .actions div { + display: inline-block; +} + +#mailTemplateSettings div label { + display: block +} + +#mailTemplateSettings textarea { + box-sizing: border-box; + width: 100%; + height: 100px; +} + +#mailTemplateSettings .templateEditor + .actions { + height:28px; +} + + +#mailTemplateSettings .actions .reset { + margin: 0; +} + +#mailTemplateSettings .actions .save { + float: right; + margin: 0; +} + +#mailTemplateSettings #mts-msg { + float: right; + margin: 1px 5px; + padding:3px; +} diff --git a/apps/files_sharing/js/settings-admin.js b/apps/files_sharing/js/settings-admin.js new file mode 100644 index 0000000000..25950b9f4f --- /dev/null +++ b/apps/files_sharing/js/settings-admin.js @@ -0,0 +1,75 @@ +$(document).ready(function() { + + var loadTemplate = function (theme, template) { + $.get( + OC.filePath( 'files_sharing', 'ajax', 'getmailtemplate.php' ) + , { theme: theme, template: template } + ).done(function( result ) { + $( '#mailTemplateSettings textarea' ).val(result); + }).fail(function( result ) { + alert(result); + }); + + } + + // load default template + var theme = $( '#mts-theme' ).val(); + var template = $( '#mts-template' ).val(); + loadTemplate(theme, template); + + $( '#mts-template' ).change( + function() { + var theme = $( '#mts-theme' ).val(); + var template = $( this ).val(); + loadTemplate(theme, template); + } + ); + $( '#mts-theme' ).change( + function() { + var theme = $( this ).val(); + var template = $( '#mts-template' ).val(); + loadTemplate(theme, template); + } + ); + $( '#mailTemplateSettings .actions' ).on('click', '.save', + function() { + var theme = $( '#mts-theme' ).val(); + var template = $( '#mts-template' ).val(); + var content = $( '#mailTemplateSettings textarea' ).val(); + OC.msg.startSaving('#mts-msg'); + $.post( + OC.filePath( 'files_sharing', 'ajax', 'setmailtemplate.php' ) + , { theme: theme, template: template, content: content } + ).done(function( result ) { + var data = { status:'success', data:{message:t('files_sharing', 'Saved')} }; + OC.msg.finishedSaving('#mts-msg', data); + }).fail(function( result ) { + var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; + OC.msg.finishedSaving('#mts-msg', data); + }); + } + ); + $( '#mailTemplateSettings .actions' ).on('click', '.reset', + function() { + var theme = $( '#mts-theme' ).val(); + var template = $( '#mts-template' ).val(); + var content = $( '#mailTemplateSettings textarea' ).val(); + OC.msg.startSaving('#mts-msg'); + $.post( + OC.filePath( 'files_sharing', 'ajax', 'resetmailtemplate.php' ) + , { theme: theme, template: template } + ).done(function( result ) { + var data = { status:'success', data:{message:t('files_sharing', 'Reset')} }; + OC.msg.finishedSaving('#mts-msg', data); + + // load default template + var theme = $( '#mts-theme' ).val(); + var template = $( '#mts-template' ).val(); + loadTemplate(theme, template); + }).fail(function( result ) { + var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; + OC.msg.finishedSaving('#mts-msg', data); + }); + } + ); +}); diff --git a/apps/files_sharing/lib/mailtemplate.php b/apps/files_sharing/lib/mailtemplate.php new file mode 100644 index 0000000000..0ea8b6ea34 --- /dev/null +++ b/apps/files_sharing/lib/mailtemplate.php @@ -0,0 +1,106 @@ +theme = $theme; + $this->path = $path; + + //determine valid theme names + $this->editableThemes = self::getEditableThemes(); + //for now hardcode the valid mail template paths + $this->editableTemplates = self::getEditableTemplates(); + } + + public function renderContent() { + if($this->isEditable()) { + list($app, $filename) = explode("/templates/", $this->path, 2); + $name = substr($filename, 0, -4); + list($path, $template) = $this->findTemplate($this->theme, $app, $name, ''); + \OC_Response::sendFile($template); + } else { + throw new NotPermittedException('Template not editable.'); + } + } + + public function isEditable() { + if ($this->editableThemes[$this->theme] + && $this->editableTemplates[$this->path] + ) { + return true; + } + return false; + } + public function setContent($data) { + if($this->isEditable()) { + //save default templates in default folder to overwrite core template + $absolutePath = \OC::$SERVERROOT.'/themes/'.$this->theme.'/'.$this->path; + $parent = dirname($absolutePath); + if ( ! is_dir($parent) ) { + if ( ! mkdir(dirname($absolutePath), 0777, true) ){ + throw new NotPermittedException('Could not create directory.'); + } + } + if ( $this->theme !== 'default' && is_file($absolutePath) ) { + if ( ! copy($absolutePath, $absolutePath.'.bak') ){ + throw new NotPermittedException('Could not create directory.'); + } + } + //overwrite theme templates? versioning? + return file_put_contents($absolutePath, $data); + } + throw new NotPermittedException('Template not editable.'); + } + public function reset(){ + if($this->isEditable()) { + $absolutePath = \OC::$SERVERROOT.'/themes/'.$this->theme.'/'.$this->path; + if ($this->theme === 'default') { + //templates can simply be deleted in the themes folder + if (unlink($absolutePath)) { + return true; + } + } else { + //if a bak file exists overwrite the template with it + if (is_file($absolutePath.'.bak')) { + if (rename($absolutePath.'.bak', $absolutePath)) { + return true; + } + } + } + return false; + } + throw new NotPermittedException('Template not editable.'); + } + public static function getEditableThemes() { + $themes = array( + 'default' => true + ); + if ($handle = opendir(\OC::$SERVERROOT.'/themes')) { + while (false !== ($entry = readdir($handle))) { + if ($entry != '.' && $entry != '..') { + if (is_dir(\OC::$SERVERROOT.'/themes/'.$entry)) { + $themes[$entry] = true; + } + } + } + closedir($handle); + } + return $themes; + } + public static function getEditableTemplates() { + return array( + 'core/templates/mail.php' => true, + 'core/templates/altmail.php' => true, + 'core/lostpassword/templates/email.php' => true, + ); + } +} \ No newline at end of file diff --git a/apps/files_sharing/settings-admin.php b/apps/files_sharing/settings-admin.php new file mode 100644 index 0000000000..cbc500c218 --- /dev/null +++ b/apps/files_sharing/settings-admin.php @@ -0,0 +1,53 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +\OC_Util::checkAdminUser(); + +if (\OC_Util::getTheme()) { + $mailTemplatePath = \OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/core/templates/mail.php'; +} + +if (!isset($mailTemplatePath) || !file_exists($mailTemplatePath) ) { + $mailTemplatePath = \OC::$SERVERROOT . '/core/templates/mail.php'; +} + +if (file_exists($mailTemplatePath)) { + $mailTemplate = file_get_contents($mailTemplatePath); +} else { + //log no mail template found +} + + +\OCP\Util::addStyle('files_sharing', 'settings-admin'); +\OCP\Util::addScript('files_sharing', 'settings-admin'); +//\OCP\Util::addScript('settings', 'personal'); + +$themes = array('default'); + +if ($handle = opendir(\OC::$SERVERROOT.'/themes')) { + while (false !== ($entry = readdir($handle))) { + if ($entry != '.' && $entry != '..') { + if (is_dir(\OC::$SERVERROOT.'/themes/'.$entry)) { + $themes[] = $entry; + } + } + } + closedir($handle); +} + +$editableTemplates = \OCA\Files_Sharing\MailTemplate::getEditableTemplates(); + +$tmpl = new OCP\Template('files_sharing', 'settings-admin'); +$tmpl->assign('themes', $themes); +$tmpl->assign('editableTemplates', $editableTemplates); + + +//\OCP\Util::addscript('files_settings', 'settings'); +//\OCP\Util::addscript('core', 'multiselect'); + +return $tmpl->fetchPage(); diff --git a/apps/files_sharing/templates/settings-admin.php b/apps/files_sharing/templates/settings-admin.php new file mode 100644 index 0000000000..44864c2471 --- /dev/null +++ b/apps/files_sharing/templates/settings-admin.php @@ -0,0 +1,39 @@ +
+

t('Mail templates'));?>

+ +
+ +
+ + +
+ +
+ + +
+ +
+ +
+ +
+ +
+ + + + + + + +
+
diff --git a/core/templates/mail.html b/core/templates/mail.html new file mode 100644 index 0000000000..3382dbf81d --- /dev/null +++ b/core/templates/mail.html @@ -0,0 +1,38 @@ + + +
+ + + + + + + + + + + + + + + + + + +
  +{{theme.name}} +
 
  +t('Hey there,

just letting you know that %s shared %s with you.
View it!

', array($_['user_displayname'], $_['filename'], $_['link']))); +if ( isset($_['expiration']) ) { + p($l->t("The share will expire on %s.", array($_['expiration']))); + print_unescaped('

'); +} +p($l->t('Cheers!')); +?> +
 
 --
+{{theme.name}} - +{{theme.slogan}} +
{{theme.baseurl}} +
 
+
From b5a145b297ee933dc4c9d7e016a57feba91bf8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Tue, 3 Jun 2014 16:50:05 +0200 Subject: [PATCH 2/3] convert mail template editor to app framework for a restful api --- apps/files_sharing/ajax/getmailtemplate.php | 21 ------- apps/files_sharing/ajax/resetmailtemplate.php | 24 -------- apps/files_sharing/ajax/setmailtemplate.php | 24 -------- apps/files_sharing/ajax/settings.php | 5 -- apps/files_sharing/app/sharing.php | 25 +++++++++ apps/files_sharing/appinfo/routes.php | 15 +++++ .../controller/adminsettingscontroller.php | 49 +++++++++++++++++ apps/files_sharing/css/settings-admin.css | 2 +- .../http/mailtemplateresponse.php | 55 +++++++++++++++++++ apps/files_sharing/js/settings-admin.js | 42 +++++++------- apps/files_sharing/lib/mailtemplate.php | 41 ++++++++++---- apps/files_sharing/settings-admin.php | 34 +----------- .../templates/settings-admin.php | 8 ++- 13 files changed, 204 insertions(+), 141 deletions(-) delete mode 100644 apps/files_sharing/ajax/getmailtemplate.php delete mode 100644 apps/files_sharing/ajax/resetmailtemplate.php delete mode 100644 apps/files_sharing/ajax/setmailtemplate.php delete mode 100644 apps/files_sharing/ajax/settings.php create mode 100644 apps/files_sharing/app/sharing.php create mode 100644 apps/files_sharing/controller/adminsettingscontroller.php create mode 100644 apps/files_sharing/http/mailtemplateresponse.php diff --git a/apps/files_sharing/ajax/getmailtemplate.php b/apps/files_sharing/ajax/getmailtemplate.php deleted file mode 100644 index 8caf0ebcec..0000000000 --- a/apps/files_sharing/ajax/getmailtemplate.php +++ /dev/null @@ -1,21 +0,0 @@ -renderContent(); - } catch (\OCP\Files\NotPermittedException $ex) { - \OC_Response::setStatus(403); // forbidden - } - exit(); -} -\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/resetmailtemplate.php b/apps/files_sharing/ajax/resetmailtemplate.php deleted file mode 100644 index 4050235bc5..0000000000 --- a/apps/files_sharing/ajax/resetmailtemplate.php +++ /dev/null @@ -1,24 +0,0 @@ -reset(); - \OC_Response::setStatus(200); // ok - } catch (\OCP\Files\NotPermittedException $ex) { - \OC_Response::setStatus(403); // forbidden - } - exit(); -} -\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/setmailtemplate.php b/apps/files_sharing/ajax/setmailtemplate.php deleted file mode 100644 index d8bb587627..0000000000 --- a/apps/files_sharing/ajax/setmailtemplate.php +++ /dev/null @@ -1,24 +0,0 @@ -setContent($_POST['content']); - \OC_Response::setStatus(200); // ok - } catch (\OCP\Files\NotPermittedException $ex) { - \OC_Response::setStatus(403); // forbidden - } - exit(); -} -\OC_Response::setStatus(404); // not found diff --git a/apps/files_sharing/ajax/settings.php b/apps/files_sharing/ajax/settings.php deleted file mode 100644 index da244f8de4..0000000000 --- a/apps/files_sharing/ajax/settings.php +++ /dev/null @@ -1,5 +0,0 @@ -getContainer(); + + /** + * Controllers + */ + $container->registerService('AdminSettingsController', function($c) { + return new AdminSettingsController( + $c->query('AppName'), + $c->query('Request') + ); + }); + } +} diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 7c2834dc9c..5b6286e2bf 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -5,6 +5,21 @@ $this->create('core_ajax_public_preview', '/publicpreview')->action( require_once __DIR__ . '/../ajax/publicpreview.php'; }); +use \OCA\Files_Sharing\App\Sharing; + +$app = new Sharing(); + +$app->registerRoutes($this, array('routes' => array( + + // mailTemplate settings + array('name' => 'admin_settings#render', 'url' => '/settings/mailtemplate', 'verb' => 'GET'), + + array('name' => 'admin_settings#update', 'url' => '/settings/mailtemplate', 'verb' => 'POST'), + + array('name' => 'admin_settings#reset', 'url' => '/settings/mailtemplate', 'verb' => 'DELETE') + +))); + // OCS API //TODO: SET: mail notification, waiting for PR #4689 to be accepted diff --git a/apps/files_sharing/controller/adminsettingscontroller.php b/apps/files_sharing/controller/adminsettingscontroller.php new file mode 100644 index 0000000000..7125641617 --- /dev/null +++ b/apps/files_sharing/controller/adminsettingscontroller.php @@ -0,0 +1,49 @@ +getResponse(); + } + + /** + * @param string $theme + * @param string $template + * @param string $content + * @return array + */ + public function update( $theme, $template, $content ) { + $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); + $template->setContent( $content ); + return new JSONResponse(); + } + + /** + * @param string $theme + * @param string $template + * @return array + */ + public function reset( $theme, $template ) { + $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); + $template->reset(); + return new JSONResponse(); + } + +} diff --git a/apps/files_sharing/css/settings-admin.css b/apps/files_sharing/css/settings-admin.css index c807fc83c7..7ee7196343 100644 --- a/apps/files_sharing/css/settings-admin.css +++ b/apps/files_sharing/css/settings-admin.css @@ -9,7 +9,7 @@ #mailTemplateSettings textarea { box-sizing: border-box; width: 100%; - height: 100px; + height: 150px; } #mailTemplateSettings .templateEditor + .actions { diff --git a/apps/files_sharing/http/mailtemplateresponse.php b/apps/files_sharing/http/mailtemplateresponse.php new file mode 100644 index 0000000000..98a2dfcc94 --- /dev/null +++ b/apps/files_sharing/http/mailtemplateresponse.php @@ -0,0 +1,55 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ + +namespace OCA\Files_Sharing\Http; + +/** + * Prompts the user to download the a file + */ +class MailTemplateResponse extends \OCP\AppFramework\Http\Response { + + private $filename; + private $contentType; + + /** + * Creates a response that prompts the user to download the file + * @param string $filename the name that the downloaded file should have + * @param string $contentType the mimetype that the downloaded file should have + */ + public function __construct($filename, $contentType = 'text/php') { + $this->filename = $filename; + $this->contentType = $contentType; + + $this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); + $this->addHeader('Content-Type', $contentType); + } + + /** + * Returns the raw template content + * @return string the file + */ + public function render(){ + return file_get_contents($this->filename); + } + +} diff --git a/apps/files_sharing/js/settings-admin.js b/apps/files_sharing/js/settings-admin.js index 25950b9f4f..0362af0232 100644 --- a/apps/files_sharing/js/settings-admin.js +++ b/apps/files_sharing/js/settings-admin.js @@ -1,17 +1,16 @@ $(document).ready(function() { - + var loadTemplate = function (theme, template) { - $.get( - OC.filePath( 'files_sharing', 'ajax', 'getmailtemplate.php' ) - , { theme: theme, template: template } - ).done(function( result ) { - $( '#mailTemplateSettings textarea' ).val(result); - }).fail(function( result ) { - alert(result); - }); - + $.get( + OC.generateUrl('apps/files_sharing/settings/mailtemplate'), + { theme: theme, template: template } + ).done(function( result ) { + $( '#mailTemplateSettings textarea' ).val(result); + }).fail(function( result ) { + alert(result); + }); } - + // load default template var theme = $( '#mts-theme' ).val(); var template = $( '#mts-template' ).val(); @@ -24,6 +23,7 @@ $(document).ready(function() { loadTemplate(theme, template); } ); + $( '#mts-theme' ).change( function() { var theme = $( this ).val(); @@ -31,6 +31,7 @@ $(document).ready(function() { loadTemplate(theme, template); } ); + $( '#mailTemplateSettings .actions' ).on('click', '.save', function() { var theme = $( '#mts-theme' ).val(); @@ -38,27 +39,29 @@ $(document).ready(function() { var content = $( '#mailTemplateSettings textarea' ).val(); OC.msg.startSaving('#mts-msg'); $.post( - OC.filePath( 'files_sharing', 'ajax', 'setmailtemplate.php' ) - , { theme: theme, template: template, content: content } + OC.generateUrl('apps/files_sharing/settings/mailtemplate'), + { theme: theme, template: template, content: content } ).done(function( result ) { var data = { status:'success', data:{message:t('files_sharing', 'Saved')} }; OC.msg.finishedSaving('#mts-msg', data); }).fail(function( result ) { var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; OC.msg.finishedSaving('#mts-msg', data); - }); + }); } ); + $( '#mailTemplateSettings .actions' ).on('click', '.reset', function() { var theme = $( '#mts-theme' ).val(); var template = $( '#mts-template' ).val(); var content = $( '#mailTemplateSettings textarea' ).val(); OC.msg.startSaving('#mts-msg'); - $.post( - OC.filePath( 'files_sharing', 'ajax', 'resetmailtemplate.php' ) - , { theme: theme, template: template } - ).done(function( result ) { + $.ajax({ + type: "DELETE", + url: OC.generateUrl('apps/files_sharing/settings/mailtemplate'), + data: { theme: theme, template: template } + }).done(function( result ) { var data = { status:'success', data:{message:t('files_sharing', 'Reset')} }; OC.msg.finishedSaving('#mts-msg', data); @@ -69,7 +72,8 @@ $(document).ready(function() { }).fail(function( result ) { var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; OC.msg.finishedSaving('#mts-msg', data); - }); + }); } ); + }); diff --git a/apps/files_sharing/lib/mailtemplate.php b/apps/files_sharing/lib/mailtemplate.php index 0ea8b6ea34..cb08b534d6 100644 --- a/apps/files_sharing/lib/mailtemplate.php +++ b/apps/files_sharing/lib/mailtemplate.php @@ -3,9 +3,11 @@ namespace OCA\Files_Sharing; use \OCP\Files\NotPermittedException; +use \OC\AppFramework\Middleware\Security\SecurityException; +use OCA\Files_Sharing\Http\MailTemplateResponse; class MailTemplate extends \OC_Template { - + private $path; private $theme; private $editableThemes; @@ -14,24 +16,37 @@ class MailTemplate extends \OC_Template { public function __construct($theme, $path) { $this->theme = $theme; $this->path = $path; - + //determine valid theme names $this->editableThemes = self::getEditableThemes(); //for now hardcode the valid mail template paths $this->editableTemplates = self::getEditableTemplates(); } + /** + * + * @return \OCA\Files_Sharing\Http\MailTemplateResponse + */ + public function getResponse() { + if($this->isEditable()) { + list($app, $filename) = explode('/templates/', $this->path, 2); + $name = substr($filename, 0, -4); + list($path, $template) = $this->findTemplate($this->theme, $app, $name, ''); + return new MailTemplateResponse($template); + } + } + public function renderContent() { if($this->isEditable()) { - list($app, $filename) = explode("/templates/", $this->path, 2); + list($app, $filename) = explode('/templates/', $this->path, 2); $name = substr($filename, 0, -4); list($path, $template) = $this->findTemplate($this->theme, $app, $name, ''); \OC_Response::sendFile($template); } else { - throw new NotPermittedException('Template not editable.'); + throw new SecurityException('Template not editable.', 403); } } - + public function isEditable() { if ($this->editableThemes[$this->theme] && $this->editableTemplates[$this->path] @@ -40,6 +55,7 @@ class MailTemplate extends \OC_Template { } return false; } + public function setContent($data) { if($this->isEditable()) { //save default templates in default folder to overwrite core template @@ -47,19 +63,20 @@ class MailTemplate extends \OC_Template { $parent = dirname($absolutePath); if ( ! is_dir($parent) ) { if ( ! mkdir(dirname($absolutePath), 0777, true) ){ - throw new NotPermittedException('Could not create directory.'); + throw new \Exception('Could not create directory.', 500); } } if ( $this->theme !== 'default' && is_file($absolutePath) ) { if ( ! copy($absolutePath, $absolutePath.'.bak') ){ - throw new NotPermittedException('Could not create directory.'); + throw new \Exception('Could not overwrite template.', 500); } } //overwrite theme templates? versioning? return file_put_contents($absolutePath, $data); } - throw new NotPermittedException('Template not editable.'); + throw new SecurityException('Template not editable.', 403); } + public function reset(){ if($this->isEditable()) { $absolutePath = \OC::$SERVERROOT.'/themes/'.$this->theme.'/'.$this->path; @@ -78,15 +95,16 @@ class MailTemplate extends \OC_Template { } return false; } - throw new NotPermittedException('Template not editable.'); + throw new NotPermittedException('Template not editable.', 403); } + public static function getEditableThemes() { $themes = array( 'default' => true ); if ($handle = opendir(\OC::$SERVERROOT.'/themes')) { while (false !== ($entry = readdir($handle))) { - if ($entry != '.' && $entry != '..') { + if ($entry != '.' && $entry != '..' && $entry != 'default') { if (is_dir(\OC::$SERVERROOT.'/themes/'.$entry)) { $themes[$entry] = true; } @@ -96,6 +114,7 @@ class MailTemplate extends \OC_Template { } return $themes; } + public static function getEditableTemplates() { return array( 'core/templates/mail.php' => true, @@ -103,4 +122,4 @@ class MailTemplate extends \OC_Template { 'core/lostpassword/templates/email.php' => true, ); } -} \ No newline at end of file +} diff --git a/apps/files_sharing/settings-admin.php b/apps/files_sharing/settings-admin.php index cbc500c218..8f15e27231 100644 --- a/apps/files_sharing/settings-admin.php +++ b/apps/files_sharing/settings-admin.php @@ -8,46 +8,14 @@ \OC_Util::checkAdminUser(); -if (\OC_Util::getTheme()) { - $mailTemplatePath = \OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/core/templates/mail.php'; -} - -if (!isset($mailTemplatePath) || !file_exists($mailTemplatePath) ) { - $mailTemplatePath = \OC::$SERVERROOT . '/core/templates/mail.php'; -} - -if (file_exists($mailTemplatePath)) { - $mailTemplate = file_get_contents($mailTemplatePath); -} else { - //log no mail template found -} - - \OCP\Util::addStyle('files_sharing', 'settings-admin'); \OCP\Util::addScript('files_sharing', 'settings-admin'); -//\OCP\Util::addScript('settings', 'personal'); - -$themes = array('default'); - -if ($handle = opendir(\OC::$SERVERROOT.'/themes')) { - while (false !== ($entry = readdir($handle))) { - if ($entry != '.' && $entry != '..') { - if (is_dir(\OC::$SERVERROOT.'/themes/'.$entry)) { - $themes[] = $entry; - } - } - } - closedir($handle); -} +$themes = \OCA\Files_Sharing\MailTemplate::getEditableThemes(); $editableTemplates = \OCA\Files_Sharing\MailTemplate::getEditableTemplates(); $tmpl = new OCP\Template('files_sharing', 'settings-admin'); $tmpl->assign('themes', $themes); $tmpl->assign('editableTemplates', $editableTemplates); - -//\OCP\Util::addscript('files_settings', 'settings'); -//\OCP\Util::addscript('core', 'multiselect'); - return $tmpl->fetchPage(); diff --git a/apps/files_sharing/templates/settings-admin.php b/apps/files_sharing/templates/settings-admin.php index 44864c2471..4021be871c 100644 --- a/apps/files_sharing/templates/settings-admin.php +++ b/apps/files_sharing/templates/settings-admin.php @@ -1,4 +1,5 @@
+

t('Mail templates'));?>

@@ -6,7 +7,7 @@
@@ -22,11 +23,11 @@
- +
- +
@@ -36,4 +37,5 @@
+
From fc1d6f4c3cd2882c22d71c060c8ffef8c4a44b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 4 Jun 2014 15:01:36 +0200 Subject: [PATCH 3/3] fixes during test --- .../controller/adminsettingscontroller.php | 33 ++++++++++------ apps/files_sharing/js/settings-admin.js | 17 ++++----- apps/files_sharing/lib/mailtemplate.php | 3 +- core/templates/mail.html | 38 ------------------- 4 files changed, 32 insertions(+), 59 deletions(-) delete mode 100644 core/templates/mail.html diff --git a/apps/files_sharing/controller/adminsettingscontroller.php b/apps/files_sharing/controller/adminsettingscontroller.php index 7125641617..fed3147a99 100644 --- a/apps/files_sharing/controller/adminsettingscontroller.php +++ b/apps/files_sharing/controller/adminsettingscontroller.php @@ -15,35 +15,46 @@ class AdminSettingsController extends ApiController { /** * @param string $theme * @param string $template - * @return type Description * @return \OCA\Files_Sharing\Http\MailTemplateResponse */ public function render( $theme, $template ) { - $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); - return $template->getResponse(); + try { + $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); + return $template->getResponse(); + } catch (\Exception $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), $ex->getCode()); + } } /** * @param string $theme * @param string $template * @param string $content - * @return array + * @return JSONResponse */ public function update( $theme, $template, $content ) { - $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); - $template->setContent( $content ); - return new JSONResponse(); + try { + $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); + $template->setContent( $content ); + return new JSONResponse(); + } catch (\Exception $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), $ex->getCode()); + } } /** * @param string $theme * @param string $template - * @return array + * @return JSONResponse */ public function reset( $theme, $template ) { - $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); - $template->reset(); - return new JSONResponse(); + try { + $template = new \OCA\Files_Sharing\MailTemplate( $theme, $template ); + $template->reset(); + return new JSONResponse(); + } catch (\Exception $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), $ex->getCode()); + } } } diff --git a/apps/files_sharing/js/settings-admin.js b/apps/files_sharing/js/settings-admin.js index 0362af0232..fa9b236ea9 100644 --- a/apps/files_sharing/js/settings-admin.js +++ b/apps/files_sharing/js/settings-admin.js @@ -7,9 +7,9 @@ $(document).ready(function() { ).done(function( result ) { $( '#mailTemplateSettings textarea' ).val(result); }).fail(function( result ) { - alert(result); + OC.dialogs.alert(result.message, t('files_sharing', 'Could not load template')); }); - } + }; // load default template var theme = $( '#mts-theme' ).val(); @@ -41,11 +41,11 @@ $(document).ready(function() { $.post( OC.generateUrl('apps/files_sharing/settings/mailtemplate'), { theme: theme, template: template, content: content } - ).done(function( result ) { + ).done(function() { var data = { status:'success', data:{message:t('files_sharing', 'Saved')} }; OC.msg.finishedSaving('#mts-msg', data); - }).fail(function( result ) { - var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; + }).fail(function(result) { + var data = { status: 'error', data:{message:result.responseJSON.message} }; OC.msg.finishedSaving('#mts-msg', data); }); } @@ -55,13 +55,12 @@ $(document).ready(function() { function() { var theme = $( '#mts-theme' ).val(); var template = $( '#mts-template' ).val(); - var content = $( '#mailTemplateSettings textarea' ).val(); OC.msg.startSaving('#mts-msg'); $.ajax({ type: "DELETE", url: OC.generateUrl('apps/files_sharing/settings/mailtemplate'), data: { theme: theme, template: template } - }).done(function( result ) { + }).done(function() { var data = { status:'success', data:{message:t('files_sharing', 'Reset')} }; OC.msg.finishedSaving('#mts-msg', data); @@ -69,8 +68,8 @@ $(document).ready(function() { var theme = $( '#mts-theme' ).val(); var template = $( '#mts-template' ).val(); loadTemplate(theme, template); - }).fail(function( result ) { - var data = { status:'error', data:{message:t('files_sharing', 'Error')} }; + }).fail(function(result) { + var data = { status: 'error', data:{message:result.responseJSON.message} }; OC.msg.finishedSaving('#mts-msg', data); }); } diff --git a/apps/files_sharing/lib/mailtemplate.php b/apps/files_sharing/lib/mailtemplate.php index cb08b534d6..ca1b0234cc 100644 --- a/apps/files_sharing/lib/mailtemplate.php +++ b/apps/files_sharing/lib/mailtemplate.php @@ -19,7 +19,7 @@ class MailTemplate extends \OC_Template { //determine valid theme names $this->editableThemes = self::getEditableThemes(); - //for now hardcode the valid mail template paths + //for now hard code the valid mail template paths $this->editableTemplates = self::getEditableTemplates(); } @@ -34,6 +34,7 @@ class MailTemplate extends \OC_Template { list($path, $template) = $this->findTemplate($this->theme, $app, $name, ''); return new MailTemplateResponse($template); } + throw new SecurityException('Template not editable.', 403); } public function renderContent() { diff --git a/core/templates/mail.html b/core/templates/mail.html deleted file mode 100644 index 3382dbf81d..0000000000 --- a/core/templates/mail.html +++ /dev/null @@ -1,38 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - -
  -{{theme.name}} -
 
  -t('Hey there,

just letting you know that %s shared %s with you.
View it!

', array($_['user_displayname'], $_['filename'], $_['link']))); -if ( isset($_['expiration']) ) { - p($l->t("The share will expire on %s.", array($_['expiration']))); - print_unescaped('

'); -} -p($l->t('Cheers!')); -?> -
 
 --
-{{theme.name}} - -{{theme.slogan}} -
{{theme.baseurl}} -
 
-