summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorJörn Friedrich Dreyer <jfd@butonic.de>2014-02-21 10:01:02 +0100
committerJörn Friedrich Dreyer <jfd@butonic.de>2014-06-02 19:22:58 +0200
commit37afab87b56fa7b2a1b0e751df72e9624663f94f (patch)
tree11252d05738f7d7ee4bdad333ee762a8a63dfad6 /apps
parentc88c0b9a13231478c626296d78aac7c1f66d87d9 (diff)
downloadnextcloud-server-37afab87b56fa7b2a1b0e751df72e9624663f94f.tar.gz
nextcloud-server-37afab87b56fa7b2a1b0e751df72e9624663f94f.zip
minimal mail template editor for administrators, refs #7177
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/ajax/getmailtemplate.php21
-rw-r--r--apps/files_sharing/ajax/resetmailtemplate.php24
-rw-r--r--apps/files_sharing/ajax/setmailtemplate.php24
-rw-r--r--apps/files_sharing/ajax/settings.php5
-rw-r--r--apps/files_sharing/appinfo/app.php3
-rw-r--r--apps/files_sharing/css/settings-admin.css33
-rw-r--r--apps/files_sharing/js/settings-admin.js75
-rw-r--r--apps/files_sharing/lib/mailtemplate.php106
-rw-r--r--apps/files_sharing/settings-admin.php53
-rw-r--r--apps/files_sharing/templates/settings-admin.php39
10 files changed, 383 insertions, 0 deletions
diff --git a/apps/files_sharing/ajax/getmailtemplate.php b/apps/files_sharing/ajax/getmailtemplate.php
new file mode 100644
index 00000000000..8caf0ebcec8
--- /dev/null
+++ b/apps/files_sharing/ajax/getmailtemplate.php
@@ -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
index 00000000000..4050235bc5d
--- /dev/null
+++ b/apps/files_sharing/ajax/resetmailtemplate.php
@@ -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
index 00000000000..d8bb587627c
--- /dev/null
+++ b/apps/files_sharing/ajax/setmailtemplate.php
@@ -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
index 00000000000..da244f8de47
--- /dev/null
+++ b/apps/files_sharing/ajax/settings.php
@@ -0,0 +1,5 @@
+<?php
+
+if(!\OCP\App::isEnabled('files_sharing')){
+ \OCP\Response::setStatus(410); // GONE
+}
diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php
index 21b2646c5ea..81f91b60d30 100644
--- a/apps/files_sharing/appinfo/app.php
+++ b/apps/files_sharing/appinfo/app.php
@@ -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
index 00000000000..c807fc83c76
--- /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 00000000000..25950b9f4f3
--- /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 00000000000..0ea8b6ea344
--- /dev/null
+++ b/apps/files_sharing/lib/mailtemplate.php
@@ -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
index 00000000000..cbc500c2185
--- /dev/null
+++ b/apps/files_sharing/settings-admin.php
@@ -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
index 00000000000..44864c24717
--- /dev/null
+++ b/apps/files_sharing/templates/settings-admin.php
@@ -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>