diff options
-rw-r--r-- | controllers/controller.php | 76 | ||||
-rw-r--r-- | controllers/news.controller.php | 7 | ||||
-rw-r--r-- | index.php | 4 | ||||
-rw-r--r-- | lib/serve.php | 96 | ||||
-rw-r--r-- | templates/main.php | 1 |
5 files changed, 145 insertions, 39 deletions
diff --git a/controllers/controller.php b/controllers/controller.php index e1cf3e866..43487f0ad 100644 --- a/controllers/controller.php +++ b/controllers/controller.php @@ -11,37 +11,16 @@ */ -/* - -Usage - -MyController extends Controller { - - public function __construct($request=null, $userLoggedInCheck=true, $csrfCheck=true){ - super($request, $userLoggedInCheck, $csrfCheck); - } - - public function myRoute(){ - - } - -} - - -*/ - - namespace OCA\News; class Controller { protected $trans; + private $safeParams; public function __construct(){ $this->trans = \OC_L10N::get('news'); $this->safeParams = array(); - - } @@ -89,10 +68,7 @@ class Controller { * @param Renderer $renderer: the render which should be used to render the page */ protected function render(Renderer $renderer){ - $renderer->bind('userId', $this->request->userId); $renderer->render(); - $this->csrfCheck = true; - $this->userLoggedInCheck = true; } @@ -107,6 +83,7 @@ class Controller { protected function renderTemplate($templateName, $arguments=array(), $safeParams=array(), $fullPage=true){ $renderer = new TemplateRenderer($templateName, $fullPage); + $renderer->bind($arguments); $renderer->bindSafe($safeParams); $this->render($renderer); } @@ -119,6 +96,7 @@ class Controller { */ protected function renderJSON($arguments=array(), $error=""){ $renderer = new JSONRenderer($error); + $renderer->bind($arguments); $this->render($renderer); } @@ -126,34 +104,50 @@ class Controller { } - - - - +/** + * Renderers + */ interface Renderer { public function render(); public function bind($params); } - +/** + * Used to render a normal template + */ class TemplateRenderer implements Renderer { private $safeParams = array(); + private $template; + /** + * @param string $name: the template which we want to render + * @param $fullPage: if the page should be included into the standard page + */ public function __construct($name, $fullPage=true){ if($fullPage){ - $this->template = new \OCP\Template('news', $template, 'user'); + $this->template = new \OCP\Template('news', $name, 'user'); } else { - $this->template = new \OCP\Template('news', $template); + $this->template = new \OCP\Template('news', $name); } } + + /** + * @brief binds parameters to the renderer which shouldnt be escaped + * @param array $params: an array of the form $doNotEscape => true + */ public function bindSafe($params){ $this->safeParams = $params; } + /** + * Bind parameters to the template + * @param array $params: an array of the form $key => value which will be used + * for access in templates + */ public function bind($params){ foreach($params as $key => $value){ if(array_key_exists($key, $this->safeParams)) { @@ -165,6 +159,9 @@ class TemplateRenderer implements Renderer { } + /** + * Print the page + */ public function render(){ $this->template->printPage(); } @@ -173,20 +170,35 @@ class TemplateRenderer implements Renderer { } + +/** + * Use this to render JSON calls + */ class JSONRenderer implements Renderer { private $params; + /** + * @param string $error: if empty a success is sent, otherwise an error message + * will be logged + */ public function __construct($error){ $this->error = $error; } + /** + * Bind parameters to the template + * @param array $params: an array which will be converted to JSON + */ public function bind($params){ $this->params = $params; } + /** + * Print the json array + */ public function render(){ if($this->error === ""){ OCP\JSON::success($this->params); diff --git a/controllers/news.controller.php b/controllers/news.controller.php index 3a77d5f7e..bf31d08f2 100644 --- a/controllers/news.controller.php +++ b/controllers/news.controller.php @@ -21,7 +21,6 @@ class NewsController extends Controller { * Decides wether to show the feedpage or the firstrun page */ public function index($request){ - echo "hi"; $feedMapper = new FeedMapper($this->userId); if($feedMapper->feedCount() > 0){ @@ -36,7 +35,7 @@ class NewsController extends Controller { $this->addScript('news'); $this->addScript('firstrun'); $this->addStyle('firstrun'); - $this->render('firstrun'); + $this->renderTemplate('firstrun'); } @@ -102,7 +101,7 @@ class NewsController extends Controller { * @param $showAll if true, it will also include unread items * @return an array with all items */ - private function getItems($feedType, $feedId, $showAll){ + public function getItems($feedType, $feedId, $showAll){ $items = array(); $itemMapper = new ItemMapper($this->userId); @@ -152,7 +151,7 @@ class NewsController extends Controller { * @param $feedId the id of the feed or folder * @return the unread count */ - private function getItemUnreadCount($feedType, $feedId){ + public function getItemUnreadCount($feedType, $feedId){ $unreadCount = 0; $itemMapper = new ItemMapper($this->userId); @@ -13,8 +13,8 @@ namespace OCA\News; -require_once \OC_App::getAppPath('news') . '/lib/url.php'; +require_once \OC_App::getAppPath('news') . '/lib/serve.php'; require_once \OC_App::getAppPath('news') . '/controllers/news.controller.php'; // routes -url(new NewsController(), 'index');
\ No newline at end of file +serve(new NewsController(), 'index', false); diff --git a/lib/serve.php b/lib/serve.php new file mode 100644 index 000000000..687ea9c26 --- /dev/null +++ b/lib/serve.php @@ -0,0 +1,96 @@ +<?php +/** +* ownCloud - News app +* +* @author Bernhard Posselt +* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +* +* This file is licensed under the Affero General Public License version 3 or later. +* See the COPYING-README file +* +*/ + +namespace OCA\News; + +/** + * Used for mapping controllers and doing security checks + * @param Controller $controller: a new instance of the controller + * @param string $method: the name of the controller method that should be called + * @param bool $csrfCheck: if false, there wont be a csrf check. enable this on + * sites that are called with ajax + * @param bool $userLoggedIn: if false, there wont be a logged in check + */ +function serve($controller, $method, $csrfCheck=true, $userLoggedInCheck=true){ + + \OCP\App::setActiveNavigationEntry('news'); + + if(!\OC_App::isEnabled('news')){ + \OCP\Util::writeLog('news', 'App news is not enabled!', \OCP\Util::ERROR); + exit(); + } + + if($userLoggedInCheck){ + if(!\OC_User::isLoggedIn()){ + \OCP\Util::writeLog('news', 'User is not logged in!', \OCP\Util::ERROR); + exit(); + } + } + + if($csrfCheck){ + if(!\OC_Util::isCallRegistered()){ + \OCP\Util::writeLog('news', 'CSRF check failed', \OCP\Util::ERROR); + exit(); + } + } + + $controller->$method(new Request()); +} + + + +/** + * This class is used to wrap $_GET and $_POST to improve testability of apps + */ +class Request { + public $get; + public $post; + public $user = null; + + private $userId; + + /** + * All parameters default to the built in $_GET, $_POST and \OCP\USER::getUser() + * @param array $get: an array with all get variables + * @param array $post: an array with all post variables + * @param string $userId: the id fo the user + */ + public function __construct($get=null, $post=null, $userId=null){ + if($get === null){ + $get = $_GET; + } + + if($post === null){ + $post = $_POST; + } + + if($userId === null){ + $userId = \OCP\USER::getUser(); + } + + $this->get = $get; + $this->post = $post; + $this->userId = $userId; + } + + + /** + * This is used to do lazy fetching for user data + */ + public function __get($name){ + if($name === 'user' && $this->user === null){ + // FIXME: get a new user instance + } + return $this->$name; + } + +}
\ No newline at end of file diff --git a/templates/main.php b/templates/main.php index 364ad9846..ccc8c5261 100644 --- a/templates/main.php +++ b/templates/main.php @@ -4,7 +4,6 @@ <?php -$l = $_['trans']; $lastViewedFeedId = $_['lastViewedFeedId']; $lastViewedFeedType = $_['lastViewedFeedType']; |