]> source.dussan.org Git - nextcloud-server.git/commitdiff
convert mail template editor to app framework for a restful api
authorJörn Friedrich Dreyer <jfd@butonic.de>
Tue, 3 Jun 2014 14:50:05 +0000 (16:50 +0200)
committerJörn Friedrich Dreyer <jfd@butonic.de>
Tue, 3 Jun 2014 14:53:03 +0000 (16:53 +0200)
13 files changed:
apps/files_sharing/ajax/getmailtemplate.php [deleted file]
apps/files_sharing/ajax/resetmailtemplate.php [deleted file]
apps/files_sharing/ajax/setmailtemplate.php [deleted file]
apps/files_sharing/ajax/settings.php [deleted file]
apps/files_sharing/app/sharing.php [new file with mode: 0644]
apps/files_sharing/appinfo/routes.php
apps/files_sharing/controller/adminsettingscontroller.php [new file with mode: 0644]
apps/files_sharing/css/settings-admin.css
apps/files_sharing/http/mailtemplateresponse.php [new file with mode: 0644]
apps/files_sharing/js/settings-admin.js
apps/files_sharing/lib/mailtemplate.php
apps/files_sharing/settings-admin.php
apps/files_sharing/templates/settings-admin.php

diff --git a/apps/files_sharing/ajax/getmailtemplate.php b/apps/files_sharing/ajax/getmailtemplate.php
deleted file mode 100644 (file)
index 8caf0eb..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-<?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
deleted file mode 100644 (file)
index 4050235..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?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
deleted file mode 100644 (file)
index d8bb587..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?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
deleted file mode 100644 (file)
index da244f8..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-<?php
-
-if(!\OCP\App::isEnabled('files_sharing')){
-       \OCP\Response::setStatus(410); // GONE
-}
diff --git a/apps/files_sharing/app/sharing.php b/apps/files_sharing/app/sharing.php
new file mode 100644 (file)
index 0000000..4272697
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace OCA\Files_Sharing\App;
+
+use \OCP\AppFramework\App;
+use \OCA\Files_Sharing\Controller\AdminSettingsController;
+
+class Sharing extends App {
+
+       public function __construct(array $urlParams=array()){
+               parent::__construct('files_sharing', $urlParams);
+
+               $container = $this->getContainer();
+
+               /**
+                * Controllers
+                */
+               $container->registerService('AdminSettingsController', function($c) {
+                       return new AdminSettingsController(
+                               $c->query('AppName'),
+                               $c->query('Request')
+                       );
+               });
+       }
+}
index 7c2834dc9c2b008f02fef4ea6c2e4876699fbac7..5b6286e2bfb8e5e4189dc5a1ca31b57debdd3751 100644 (file)
@@ -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 (file)
index 0000000..7125641
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace OCA\Files_Sharing\Controller;
+
+use \OCP\AppFramework\ApiController;
+use \OCP\IRequest;
+use \OCP\AppFramework\Http\JSONResponse;
+
+class AdminSettingsController extends ApiController {
+
+       public function __construct($appName, IRequest $request) {
+               parent::__construct($appName, $request);
+       }
+
+       /**
+        * @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();
+       }
+
+       /**
+        * @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();
+       }
+
+}
index c807fc83c760c9346247a4e05dac80c02439eb3e..7ee719634361d084dcb44061a50c74b0f58ea129 100644 (file)
@@ -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 (file)
index 0000000..98a2dfc
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Jörn Dreyer
+ * @copyright 2014 Jörn Dreyer <jfd@owncloud.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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);
+       }
+
+}
index 25950b9f4f3862143a89fb0bdfd59a3a5346a268..0362af0232b6e827b2277aa0407604c8bf2898c9 100644 (file)
@@ -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);
-                       });     
+                       });
                }
        );
+
 });
index 0ea8b6ea344ebce0984444dcec3a0fc01c30bb7c..cb08b534d6f5e2b05a0f10ff42b8f47aa075568c 100644 (file)
@@ -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
+}
index cbc500c21852f3b74240d0c5fb8919b355a55295..8f15e272312a58fdd6ca60c8d37dcf60e27a6d57 100644 (file)
@@ -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();
index 44864c24717176a5f9efeb9b253b69dbeea17746..4021be871cd26ffe8c8a4b9114f0bba5360ae92e 100644 (file)
@@ -1,4 +1,5 @@
 <div class="section" id="mailTemplateSettings" >
+
        <h2><?php p($l->t('Mail templates'));?></h2>
 
        <div class="actions">
@@ -6,7 +7,7 @@
                <div>
                        <label for="mts-theme"><?php p($l->t('Theme'));?></label>
                        <select id="mts-theme">
-                               <?php foreach($_['themes'] as $theme): ?>
+                               <?php foreach($_['themes'] as $theme => $editable): ?>
                                <option><?php p($theme); ?></option>
                                <?php endforeach; ?>
                        </select>
                </div>
 
        </div>
-       
+
        <div class="templateEditor">
                <textarea></textarea>
        </div>
-       
+
        <div class="actions">
 
                <button class="reset"><?php p($l->t('Reset'));?></button>
@@ -36,4 +37,5 @@
                <span id="mts-msg" class="msg"></span>
 
        </div>
+
 </div>