diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2012-11-19 04:03:02 +0400 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2012-11-19 04:19:11 +0400 |
commit | d6599a0db9c7bdcd3a10e98970ea458d9d5e4053 (patch) | |
tree | 328b2550ebf0ada94bbdb4a6c0272bbd9351fbbf /apptemplate | |
parent | 0bbfb7d0aff2965e3b164520a255921cbef1c2f3 (diff) |
added dependency injection, routes and controllers to the apptemplate
Diffstat (limited to 'apptemplate')
-rw-r--r-- | apptemplate/3rdparty/Pimple/Pimple.php | 202 | ||||
-rw-r--r-- | apptemplate/admin/settings.php (renamed from apptemplate/index.php) | 25 | ||||
-rw-r--r-- | apptemplate/ajax/seturl.php | 14 | ||||
-rw-r--r-- | apptemplate/appinfo/app.php | 14 | ||||
-rw-r--r-- | apptemplate/appinfo/bootstrap.php | 85 | ||||
-rw-r--r-- | apptemplate/appinfo/routes.php | 85 | ||||
-rw-r--r-- | apptemplate/controllers/ajax.controller.php | 49 | ||||
-rw-r--r-- | apptemplate/controllers/index.controller.php | 56 | ||||
-rw-r--r-- | apptemplate/controllers/settings.controller.php | 52 | ||||
-rw-r--r-- | apptemplate/js/admin.js | 16 | ||||
-rw-r--r-- | apptemplate/lib/api.php | 99 | ||||
-rw-r--r-- | apptemplate/lib/controller.php | 74 | ||||
-rw-r--r-- | apptemplate/lib/request.php | 74 | ||||
-rw-r--r-- | apptemplate/lib/security.php | 95 | ||||
-rw-r--r-- | apptemplate/settings.php | 11 | ||||
-rw-r--r-- | apptemplate/templates/main.php | 2 |
16 files changed, 904 insertions, 49 deletions
diff --git a/apptemplate/3rdparty/Pimple/Pimple.php b/apptemplate/3rdparty/Pimple/Pimple.php new file mode 100644 index 000000000..cb1acd5e0 --- /dev/null +++ b/apptemplate/3rdparty/Pimple/Pimple.php @@ -0,0 +1,202 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Pimple main class. + * + * @package pimple + * @author Fabien Potencier + */ +class Pimple implements ArrayAccess +{ + private $values; + + /** + * Instantiate the container. + * + * Objects and parameters can be passed as argument to the constructor. + * + * @param array $values The parameters or objects. + */ + public function __construct (array $values = array()) + { + $this->values = $values; + } + + /** + * Sets a parameter or an object. + * + * Objects must be defined as Closures. + * + * Allowing any PHP callable leads to difficult to debug problems + * as function names (strings) are callable (creating a function with + * the same a name as an existing parameter would break your container). + * + * @param string $id The unique identifier for the parameter or object + * @param mixed $value The value of the parameter or a closure to defined an object + */ + public function offsetSet($id, $value) + { + $this->values[$id] = $value; + } + + /** + * Gets a parameter or an object. + * + * @param string $id The unique identifier for the parameter or object + * + * @return mixed The value of the parameter or an object + * + * @throws InvalidArgumentException if the identifier is not defined + */ + public function offsetGet($id) + { + if (!array_key_exists($id, $this->values)) { + throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + } + + $isFactory = is_object($this->values[$id]) && method_exists($this->values[$id], '__invoke'); + + return $isFactory ? $this->values[$id]($this) : $this->values[$id]; + } + + /** + * Checks if a parameter or an object is set. + * + * @param string $id The unique identifier for the parameter or object + * + * @return Boolean + */ + public function offsetExists($id) + { + return array_key_exists($id, $this->values); + } + + /** + * Unsets a parameter or an object. + * + * @param string $id The unique identifier for the parameter or object + */ + public function offsetUnset($id) + { + unset($this->values[$id]); + } + + /** + * Returns a closure that stores the result of the given closure for + * uniqueness in the scope of this instance of Pimple. + * + * @param Closure $callable A closure to wrap for uniqueness + * + * @return Closure The wrapped closure + */ + public function share(Closure $callable) + { + return function ($c) use ($callable) { + static $object; + + if (null === $object) { + $object = $callable($c); + } + + return $object; + }; + } + + /** + * Protects a callable from being interpreted as a service. + * + * This is useful when you want to store a callable as a parameter. + * + * @param Closure $callable A closure to protect from being evaluated + * + * @return Closure The protected closure + */ + public function protect(Closure $callable) + { + return function ($c) use ($callable) { + return $callable; + }; + } + + /** + * Gets a parameter or the closure defining an object. + * + * @param string $id The unique identifier for the parameter or object + * + * @return mixed The value of the parameter or the closure defining an object + * + * @throws InvalidArgumentException if the identifier is not defined + */ + public function raw($id) + { + if (!array_key_exists($id, $this->values)) { + throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + } + + return $this->values[$id]; + } + + /** + * Extends an object definition. + * + * Useful when you want to extend an existing object definition, + * without necessarily loading that object. + * + * @param string $id The unique identifier for the object + * @param Closure $callable A closure to extend the original + * + * @return Closure The wrapped closure + * + * @throws InvalidArgumentException if the identifier is not defined + */ + public function extend($id, Closure $callable) + { + if (!array_key_exists($id, $this->values)) { + throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + } + + $factory = $this->values[$id]; + + if (!($factory instanceof Closure)) { + throw new InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id)); + } + + return $this->values[$id] = function ($c) use ($callable, $factory) { + return $callable($factory($c), $c); + }; + } + + /** + * Returns all defined value names. + * + * @return array An array of value names + */ + public function keys() + { + return array_keys($this->values); + } +} diff --git a/apptemplate/index.php b/apptemplate/admin/settings.php index 5d0262a6e..eb22ad03e 100644 --- a/apptemplate/index.php +++ b/apptemplate/admin/settings.php @@ -3,10 +3,8 @@ /** * ownCloud - App Template Example * -* @author Frank Karlitschek -* @author Florian Hülsmann -* @copyright 2011 Frank Karlitschek karlitschek@kde.org -* @copyright 2012 Florian Hülsmann fh@cbix.de +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -23,11 +21,16 @@ * */ -// Check if we are a user -OCP\User::checkLoggedIn(); +namespace OCA\AppTemplate; -$somesetting = OCP\Config::getSystemValue( "somesetting", '' ); -OCP\App::setActiveNavigationEntry( 'apptemplate' ); -$tmpl = new OCP\Template( 'apptemplate', 'main', 'user' ); -$tmpl->assign( 'somesetting', $somesetting ); -$tmpl->printPage(); + +require_once \OC_App::getAppPath('apptemplate') . '/appinfo/bootstrap.php'; + +$container = createDIContainer(); + +$security = $container['Security']; +$security->setCSRFCheck(false); +$security->runChecks(); + +$controller = $container['SettingsController']; +return $controller->index()->fetchPage(); diff --git a/apptemplate/ajax/seturl.php b/apptemplate/ajax/seturl.php deleted file mode 100644 index fa6d513b4..000000000 --- a/apptemplate/ajax/seturl.php +++ /dev/null @@ -1,14 +0,0 @@ -<?php -/** - * Copyright (c) 2011, Frank Karlitschek <karlitschek@kde.org> - * Copyright (c) 2012, Florian Hülsmann <fh@cbix.de> - * This file is licensed under the Affero General Public License version 3 or later. - * See the COPYING-README file. - */ - -OCP\User::checkAdminUser(); -OCP\JSON::callCheck(); - -OCP\Config::setSystemValue( 'somesetting', $_POST['somesetting'] ); - -echo 'true'; diff --git a/apptemplate/appinfo/app.php b/apptemplate/appinfo/app.php index 9bb567c4e..3e37d49fc 100644 --- a/apptemplate/appinfo/app.php +++ b/apptemplate/appinfo/app.php @@ -23,12 +23,16 @@ * */ -OCP\App::registerAdmin( 'apptemplate', 'settings' ); + +require_once \OC_App::getAppPath('apptemplate') . '/appinfo/bootstrap.php'; + + +OCP\App::registerAdmin(APP_NAME, 'admin/settings'); OCP\App::addNavigationEntry( array( - 'id' => 'apptemplate', + 'id' => APP_NAME, 'order' => 74, - 'href' => OCP\Util::linkTo( 'apptemplate', 'index.php' ), - 'icon' => OCP\Util::imagePath( 'apptemplate', 'example.png' ), - 'name' => 'App Template' + 'href' => \OC_Helper::linkToRoute(APP_NAME . '_index'), + 'icon' => OCP\Util::imagePath(APP_NAME, 'example.png' ), + 'name' => \OC_L10N::get(APP_NAME)->t('App Template') )); diff --git a/apptemplate/appinfo/bootstrap.php b/apptemplate/appinfo/bootstrap.php new file mode 100644 index 000000000..fb9305932 --- /dev/null +++ b/apptemplate/appinfo/bootstrap.php @@ -0,0 +1,85 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + +/* Config */ +DEFINE('APP_NAME', 'apptemplate'); + + +/** + * Declare your classes and their include path so that they'll be automatically + * loaded once you instantiate them + */ +\OC::$CLASSPATH['Pimple'] = 'apps/apptemplate/3rdparty/Pimple/Pimple.php'; + +\OC::$CLASSPATH['OCA\AppTemplate\API'] = 'apps/apptemplate/lib/api.php'; +\OC::$CLASSPATH['OCA\AppTemplate\Request'] = 'apps/apptemplate/lib/request.php'; +\OC::$CLASSPATH['OCA\AppTemplate\Security'] = 'apps/apptemplate/lib/security.php'; +\OC::$CLASSPATH['OCA\AppTemplate\Controller'] = 'apps/apptemplate/lib/controller.php'; +\OC::$CLASSPATH['OCA\AppTemplate\IndexController'] = 'apps/apptemplate/controllers/index.controller.php'; +\OC::$CLASSPATH['OCA\AppTemplate\SettingsController'] = 'apps/apptemplate/controllers/settings.controller.php'; +\OC::$CLASSPATH['OCA\AppTemplate\AjaxController'] = 'apps/apptemplate/controllers/ajax.controller.php'; + + +/** + * @return a new DI container with prefilled values for the news app + */ +function createDIContainer(){ + $container = new \Pimple(); + + /** + * BASE + */ + $container['API'] = $container->share(function($c){ + return new API(APP_NAME); + }); + + $container['Security'] = $container->share(function($c){ + return new Security($c['API']->getAppName()); + }); + + $container['Request'] = $container->share(function($c){ + return new Request($c['API']->getUserId(), $_GET, $_POST); + }); + + + /** + * CONTROLLERS + */ + $container['IndexController'] = function($c){ + return new IndexController($c['API'], $c['Request']); + }; + + $container['SettingsController'] = function($c){ + return new SettingsController($c['API'], $c['Request']); + }; + + + $container['AjaxController'] = function($c){ + return new AjaxController($c['API'], $c['Request']); + }; + + + return $container; +}
\ No newline at end of file diff --git a/apptemplate/appinfo/routes.php b/apptemplate/appinfo/routes.php new file mode 100644 index 000000000..5da6dacbe --- /dev/null +++ b/apptemplate/appinfo/routes.php @@ -0,0 +1,85 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +require_once \OC_App::getAppPath('apptemplate') . '/appinfo/bootstrap.php'; + +/** + * Shortcut for calling a controller method and printing the result + * @param string $controllerName: the name of the controller under which it is + * stored in the DI container + * @param string $methodName: the method that you want to call + * @param array $urlParams: an array with variables extracted from the routes + * @param bool $disableCSRF: disables the csrf check, defaults to false + */ +function callController($controllerName, $methodName, $urlParams, + $disableCSRF=false, $disableAdminCheck=true){ + $container = createDIContainer(); + + // run security checks + $security = $container['Security']; + if($disableCSRF){ + $security->setCSRFCheck(false); + } + if($disableAdminCheck){ + $security->setIsAdminCheck(false); + } + + $security->runChecks(); + + // call the controller and render the page + $controller = $container[$controllerName]; + $page = $controller->$methodName($urlParams); + $page->printPage(); +} + + +/************************* + * Define your routes here + ************************/ + +/** + * Normal Routes + */ +$this->create(APP_NAME. '_index', '/')->action( + function($params){ + callController('IndexController', 'index', $params, true); + } +); + +/** + * Ajax Routes + */ +$this->create(APP_NAME . '_ajax_setsystemvalue', '/setsystemvalue')->post()->action( + function($params){ + $container = createDIContainer(); + + $security = $container['Security']; + $security->runChecks(); + + $controller = $container[$controllerName]; + $container->setsystemvalue($params); + } +);
\ No newline at end of file diff --git a/apptemplate/controllers/ajax.controller.php b/apptemplate/controllers/ajax.controller.php new file mode 100644 index 000000000..ab6ff54dd --- /dev/null +++ b/apptemplate/controllers/ajax.controller.php @@ -0,0 +1,49 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +class AjaxController extends Controller { + + + /** + * @param Request $request: an instance of the request + * @param API $api: an api wrapper instance + */ + public function __construct($api, $request){ + parent::__construct($api, $request); + } + + + /** + * @brief sets a global system value + * @param array $urlParams: an array with the values, which were matched in + * the routes file + */ + public function setSystemValue($urlParams=array()){ + $value = $this->params['somesetting']; + $this->api->setSystemValue('somesetting', $value); + } + +}
\ No newline at end of file diff --git a/apptemplate/controllers/index.controller.php b/apptemplate/controllers/index.controller.php new file mode 100644 index 000000000..a7b5912aa --- /dev/null +++ b/apptemplate/controllers/index.controller.php @@ -0,0 +1,56 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +class IndexController extends Controller { + + + /** + * @param Request $request: an instance of the request + * @param API $api: an api wrapper instance + */ + public function __construct($api, $request){ + parent::__construct($api, $request); + + // this will set the current navigation entry of the app, use this only + // for normal HTML requests and not for AJAX requests + $this->api->activateNavigationEntry(); + } + + + /** + * @brief renders the index page + * @param array $urlParams: an array with the values, which were matched in + * the routes file + */ + public function index($urlParams=array()){ + $template = $this->getTemplate('main'); + $templateVariable = $this->api->getSystemValue('somesetting'); + $template->assign('somesetting', $templateVariable, false); + + return $template; + } + +}
\ No newline at end of file diff --git a/apptemplate/controllers/settings.controller.php b/apptemplate/controllers/settings.controller.php new file mode 100644 index 000000000..61bc718d6 --- /dev/null +++ b/apptemplate/controllers/settings.controller.php @@ -0,0 +1,52 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +class SettingsController extends Controller { + + + /** + * @param Request $request: an instance of the request + * @param API $api: an api wrapper instance + */ + public function __construct($api, $request){ + parent::__construct($api, $request); + } + + + /** + * @brief renders the settings page + * @param array $urlParams: an array with the values, which were matched in + * the routes file + */ + public function index($urlParams=array()){ + $this->api->addScript('admin'); + $template = $this->getTemplate('settings', 'admin'); + $template->assign('url', $this->api->getSystemValue('somesetting')); + + return $template; + } + +}
\ No newline at end of file diff --git a/apptemplate/js/admin.js b/apptemplate/js/admin.js index 4b0bef0b5..fa9b7dc8c 100644 --- a/apptemplate/js/admin.js +++ b/apptemplate/js/admin.js @@ -1,15 +1,17 @@ $(document).ready(function(){ + OC.Router.registerLoadedCallback(function(){ + $('#somesetting').blur(function(event){ + + event.preventDefault(); + var post = $( "#somesetting" ).serialize(); + var url = OC.Router.generate('apptemplate_ajax_setsystemvalue'); + $.post(url , post, function(data){ + $('#apptemplate .msg').text('Finished saving: ' + data); + }); - $('#somesetting').blur(function(event){ - event.preventDefault(); - var post = $( "#somesetting" ).serialize(); - $.post( OC.filePath('apptemplate', 'ajax', 'seturl.php') , post, function(data){ - $('#apptemplate .msg').text('Finished saving: ' + data); }); }); - - }); diff --git a/apptemplate/lib/api.php b/apptemplate/lib/api.php new file mode 100644 index 000000000..380247c5a --- /dev/null +++ b/apptemplate/lib/api.php @@ -0,0 +1,99 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + +/** + * This is used to wrap the owncloud static api calls into an object to make the + * code better abstractable for use in the dependency injection container + * + * Extend this to your needs + */ +class API { + + private $appName; + + /** + * @param string $appName: the name of your application + */ + public function __construct($appName){ + $this->appName = $appName; + } + + + /** + * @return the name of your application + */ + public function getAppName(){ + return $this->appName; + } + + + /** + * @return: the user id of the current user + */ + public function getUserId(){ + return \OCP\USER::getUser(); + } + + + /** + * Sets the current navigation entry to the currently running app + */ + public function activateNavigationEntry(){ + \OCP\App::setActiveNavigationEntry($this->appName); + } + + + /** + * Adds a new javascript file + * @param string $scriptName: the name of the javascript in js/ + * without the suffix + */ + public function addScript($scriptName){ + \OCP\Util::addScript($this->appName, $scriptName); + } + + + /** + * Looks up a systemwide defined value + * @param string $key: the key of the value, under which it was saved + * @return the saved value + */ + public function getSystemValue($key){ + return \OCP\Config::getSystemValue($key, ''); + } + + + /** + * Sets a new systemwide value + * @param string $key: the key of the value, under which will be saved + * @param $value: the value that should be stored + */ + public function setSystemValue($key, $value){ + return \OCP\Config::setSystemValue($key, $value); + } + + +}
\ No newline at end of file diff --git a/apptemplate/lib/controller.php b/apptemplate/lib/controller.php new file mode 100644 index 000000000..512286734 --- /dev/null +++ b/apptemplate/lib/controller.php @@ -0,0 +1,74 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +class Controller { + + protected $api; + protected $request; + + /** + * @param API $api: an api wrapper instance + * @param Request $request: an instance of the request + */ + public function __construct($api, $request){ + $this->api = $api; + $this->request = $request; + } + + + /** + * @brief lets you access post and get parameters by the index + * @param string $key: the key which you want to access in the $_POST or + * $_GET array. If both arrays store things under the same + * key, return the value in $_POST + * @return: the content of the array + */ + protected function param($key){ + $postValue = $this->request->getPOST($key); + $getValue = $this->request->getGET($key); + + if($postValue !== null){ + return $postValue; + } + + if($getValue !== null){ + return $getValue; + } + } + + + /** + * @brief Shortcut for creating a template for the current app# + * @param string $templateName: the name of the template in the templates + * directory without the .php suffix + * @param bool $userPage: 'user' renders are normal page, 'admin' an admin page + * @return a new Template instance + */ + protected function getTemplate($templateName, $page='user'){ + return new \OCP\Template($this->api->getAppName(), $templateName, $page); + } + +}
\ No newline at end of file diff --git a/apptemplate/lib/request.php b/apptemplate/lib/request.php new file mode 100644 index 000000000..82f6fbaa7 --- /dev/null +++ b/apptemplate/lib/request.php @@ -0,0 +1,74 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + +/** + * Encapsulates $_GET and $_POST arrays for better testability + */ +class Request { + + private $get; + private $post; + + /** + * @param array $get: the $_GET array + * @param array $post: the $_POST array + */ + public function __construct($get=array(), $post=array()) { + $this->get = $get; + $this->post = $post; + } + + + /** + * Returns the get value or the default if not found + * @param string $key: the array key that should be looked up + * @param string $default: if the key is not found, return this value + * @return the value of the stored array + */ + public function getGET($key, $default=null){ + if(isset($this->get[$key])){ + return $this->get[$key]; + } else { + return $default; + } + } + + + /** + * Returns the get value or the default if not found + * @param string $key: the array key that should be looked up + * @param string $default: if the key is not found, return this value + * @return the value of the stored array + */ + public function getPOST($key, $default=null){ + if(isset($this->post[$key])){ + return $this->post[$key]; + } else { + return $default; + } + } + +} diff --git a/apptemplate/lib/security.php b/apptemplate/lib/security.php new file mode 100644 index 000000000..c7b5363f3 --- /dev/null +++ b/apptemplate/lib/security.php @@ -0,0 +1,95 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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\AppTemplate; + + +/** + * This class is a simple object with getters and setters and allows + * finegrained controll over security checks + * All security checks are enabled by default + */ +class Security { + + private $csrfCheck; + private $loggedInCheck; + private $appEnabledCheck; + private $isAdminCheck; + private $appName; + + /** + * @param string $appName: the name of the app + */ + public function __construct($appName){ + $this->appName = $appName; + + // enable all checks by default + $this->csrfCheck = true; + $this->loggedInCheck = true; + $this->appEnabledCheck = true; + $this->isAdminCheck = true; + } + + + public function setCSRFCheck($csrfCheck){ + $this->csrfCheck = $csrfCheck; + } + + public function setLoggedInCheck($loggedInCheck){ + $this->loggedInCheck = $loggedInCheck; + } + + public function setAppEnabledCheck($appEnabledCheck){ + $this->appEnabledCheck = $appEnabledCheck; + } + + public function setIsAdminCheck($isAdminCheck){ + $this->isAdminCheck = $isAdminCheck; + } + + + /** + * Runs all security checks + */ + public function runChecks() { + + if($this->csrfCheck){ + \OCP\JSON::callCheck(); + } + + if($this->loggedInCheck){ + \OCP\JSON::checkLoggedIn(); + } + + if($this->appEnabledCheck){ + \OCP\JSON::checkAppEnabled($this->appName); + } + + if($this->isAdminCheck){ + \OCP\User::checkAdminUser(); + } + + } + + +}
\ No newline at end of file diff --git a/apptemplate/settings.php b/apptemplate/settings.php deleted file mode 100644 index 04aaca651..000000000 --- a/apptemplate/settings.php +++ /dev/null @@ -1,11 +0,0 @@ -<?php - -OCP\User::checkAdminUser(); - -OCP\Util::addScript( "apptemplate", "admin" ); - -$tmpl = new OCP\Template( 'apptemplate', 'settings'); - -$tmpl->assign('url', OCP\Config::getSystemValue( "somesetting", '' )); - -return $tmpl->fetchPage(); diff --git a/apptemplate/templates/main.php b/apptemplate/templates/main.php index 8052fbe83..d084cd91f 100644 --- a/apptemplate/templates/main.php +++ b/apptemplate/templates/main.php @@ -1,3 +1,3 @@ <h1>This is an example app template</h1> -<?php echo $l->t('Some Setting');?>: "<?php echo $_['somesetting']; ?>" +<?php p($l->t('Some Setting')); ?>: "<?php p($_['somesetting']); ?>" |