minimal mail template editor for administrators, refs #7177
parent
c88c0b9a13
commit
37afab87b5
@ -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 |
||||
@ -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 |
||||
@ -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 |
||||
@ -0,0 +1,5 @@ |
||||
<?php |
||||
|
||||
if(!\OCP\App::isEnabled('files_sharing')){ |
||||
\OCP\Response::setStatus(410); // GONE |
||||
} |
||||
@ -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; |
||||
} |
||||
@ -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); |
||||
});
|
||||
} |
||||
); |
||||
}); |
||||
@ -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, |
||||
); |
||||
} |
||||
} |
||||
@ -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(); |
||||
@ -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> |
||||
@ -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"> </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"> </td></tr> |
||||
<tr> |
||||
<td bgcolor="#f8f8f8" width="20px"> </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"> </td></tr> |
||||
<tr> |
||||
<td bgcolor="#f8f8f8" width="20px"> </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"> </td> |
||||
</tr> |
||||
</table> |
||||
</td></tr> |
||||
</table> |
||||
Loading…
Reference in new issue