]> source.dussan.org Git - nextcloud-server.git/commitdiff
minimal mail template editor for administrators, refs #7177
authorJörn Friedrich Dreyer <jfd@butonic.de>
Fri, 21 Feb 2014 09:01:02 +0000 (10:01 +0100)
committerJörn Friedrich Dreyer <jfd@butonic.de>
Mon, 2 Jun 2014 17:22:58 +0000 (19:22 +0200)
apps/files_sharing/ajax/getmailtemplate.php [new file with mode: 0644]
apps/files_sharing/ajax/resetmailtemplate.php [new file with mode: 0644]
apps/files_sharing/ajax/setmailtemplate.php [new file with mode: 0644]
apps/files_sharing/ajax/settings.php [new file with mode: 0644]
apps/files_sharing/appinfo/app.php
apps/files_sharing/css/settings-admin.css [new file with mode: 0644]
apps/files_sharing/js/settings-admin.js [new file with mode: 0644]
apps/files_sharing/lib/mailtemplate.php [new file with mode: 0644]
apps/files_sharing/settings-admin.php [new file with mode: 0644]
apps/files_sharing/templates/settings-admin.php [new file with mode: 0644]
core/templates/mail.html [new file with mode: 0644]

diff --git a/apps/files_sharing/ajax/getmailtemplate.php b/apps/files_sharing/ajax/getmailtemplate.php
new file mode 100644 (file)
index 0000000..8caf0eb
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+OC_JSON::checkAdminUser();
+OCP\JSON::callCheck();
+
+if(!\OCP\App::isEnabled('files_sharing')){
+       \OC_Response::setStatus(410); // GONE
+}
+
+// Get data
+if ( isset( $_GET['theme'] ) && isset( $_GET['template'] ) ) {
+
+       $template = new \OCA\Files_Sharing\MailTemplate( $_GET['theme'], $_GET['template'] );
+       try {
+               $template->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 (file)
index 0000000..4050235
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+OC_JSON::checkAdminUser();
+OCP\JSON::callCheck();
+
+if(!\OCP\App::isEnabled('files_sharing')){
+       \OCP\Response::setStatus(410); // GONE
+}
+
+$l=OC_L10N::get('core');
+
+// post data
+if ( isset( $_POST['theme'] ) && isset( $_POST['template'] ) ) {
+
+       $template = new \OCA\Files_Sharing\MailTemplate( $_POST['theme'], $_POST['template'] );
+       try {
+               $template->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 (file)
index 0000000..d8bb587
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+OC_JSON::checkAdminUser();
+OCP\JSON::callCheck();
+
+if(!\OCP\App::isEnabled('files_sharing')){
+       \OCP\Response::setStatus(410); // GONE
+}
+
+$l=OC_L10N::get('core');
+
+// post data
+if ( isset( $_POST['theme'] ) && isset( $_POST['template'] ) && isset( $_POST['content'] ) ) {
+       
+       $template = new \OCA\Files_Sharing\MailTemplate( $_POST['theme'], $_POST['template'] );
+       try {
+               $template->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 (file)
index 0000000..da244f8
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+if(!\OCP\App::isEnabled('files_sharing')){
+       \OCP\Response::setStatus(410); // GONE
+}
index 21b2646c5eade7a57cde704acf06e6cc346c08a9..81f91b60d30e4065ee84aa3f9ce47693af250c27 100644 (file)
@@ -21,6 +21,9 @@ OCP\Util::addScript('files_sharing', 'share');
 \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Shared_Updater', 'renameHook');
 \OC_Hook::connect('OC_Appconfig', 'post_set_value', '\OCA\Files\Share\Maintainer', 'configChangeHook');
 
+// Register settings scripts for mail template editing
+OCP\App::registerAdmin('files_sharing', 'settings-admin');
+
 OC_FileProxy::register(new OCA\Files\Share\Proxy());
 
 \OCA\Files\App::getNavigationManager()->add(
diff --git a/apps/files_sharing/css/settings-admin.css b/apps/files_sharing/css/settings-admin.css
new file mode 100644 (file)
index 0000000..c807fc8
--- /dev/null
@@ -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 (file)
index 0000000..25950b9
--- /dev/null
@@ -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 (file)
index 0000000..0ea8b6e
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+
+namespace OCA\Files_Sharing;
+
+use \OCP\Files\NotPermittedException;
+
+class MailTemplate extends \OC_Template {
+       
+       private $path;
+       private $theme;
+       private $editableThemes;
+       private $editableTemplates;
+
+       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();
+       }
+       
+       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 (file)
index 0000000..cbc500c
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Copyright (c) 2011 Robin Appelman <icewind@owncloud.com>
+ * 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 (file)
index 0000000..44864c2
--- /dev/null
@@ -0,0 +1,39 @@
+<div class="section" id="mailTemplateSettings" >
+       <h2><?php p($l->t('Mail templates'));?></h2>
+
+       <div class="actions">
+
+               <div>
+                       <label for="mts-theme"><?php p($l->t('Theme'));?></label>
+                       <select id="mts-theme">
+                               <?php foreach($_['themes'] as $theme): ?>
+                               <option><?php p($theme); ?></option>
+                               <?php endforeach; ?>
+                       </select>
+               </div>
+
+               <div>
+                       <label for="mts-template"><?php p($l->t('Template'));?></label>
+                       <select id="mts-template">
+                               <?php foreach($_['editableTemplates'] as $template => $editable): ?>
+                               <option><?php p($template); ?></option>
+                               <?php endforeach; ?>
+                       </select>
+               </div>
+
+       </div>
+       
+       <div class="templateEditor">
+               <textarea></textarea>
+       </div>
+       
+       <div class="actions">
+
+               <button class="reset"><?php p($l->t('Reset'));?></button>
+
+               <button class="save"><?php p($l->t('Save'));?></button>
+
+               <span id="mts-msg" class="msg"></span>
+
+       </div>
+</div>
diff --git a/core/templates/mail.html b/core/templates/mail.html
new file mode 100644 (file)
index 0000000..3382dbf
--- /dev/null
@@ -0,0 +1,38 @@
+<table cellspacing="0" cellpadding="0" border="0" width="100%">
+<tr><td>
+<table cellspacing="0" cellpadding="0" border="0" width="600px">
+<tr>
+<td bgcolor="#1d2d44" width="20px">&nbsp;</td>
+<td bgcolor="#1d2d44">
+<img src="<?php print_unescaped(OC_Helper::makeURLAbsolute(image_path('', 'logo-mail.gif'))); ?>" alt="{{theme.name}}"/>
+</td>
+</tr>
+<tr><td bgcolor="#f8f8f8" colspan="2">&nbsp;</td></tr>
+<tr>
+<td bgcolor="#f8f8f8" width="20px">&nbsp;</td>
+<td bgcolor="#f8f8f8" style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
+<?php
+print_unescaped($l->t('Hey there,<br><br>just letting you know that %s shared <strong>%s</strong> with you.<br><a href="%s">View it!</a><br><br>', array($_['user_displayname'], $_['filename'], $_['link'])));
+if ( isset($_['expiration']) ) {
+       p($l->t("The share will expire on %s.", array($_['expiration'])));
+       print_unescaped('<br><br>');
+}
+p($l->t('Cheers!'));
+?>
+</td>
+</tr>
+<tr><td bgcolor="#f8f8f8" colspan="2">&nbsp;</td></tr>
+<tr>
+<td bgcolor="#f8f8f8" width="20px">&nbsp;</td>
+<td bgcolor="#f8f8f8" style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">--<br>
+{{theme.name}} -
+{{theme.slogan}}
+<br><a href="{{theme.baseurl}}">{{theme.baseurl}}</a>
+</td>
+</tr>
+<tr>
+<td bgcolor="#f8f8f8" colspan="2">&nbsp;</td>
+</tr>
+</table>
+</td></tr>
+</table>