removed unforseen overwrite of controller classes

This commit is contained in:
Bernhard Posselt 2012-11-02 22:17:06 +01:00
Родитель 9112586eec
Коммит ded22aaf4b
5 изменённых файлов: 39 добавлений и 248 удалений

Просмотреть файл

@ -10,15 +10,16 @@
*
*/
namespace OCA\News;
class Controller {
protected $userId;
protected $trans;
private $safeParams;
public function __construct(){
$this->userId = \OCP\USER::getUser();
$this->trans = \OC_L10N::get('news');
$this->safeParams = array();
}
@ -63,155 +64,37 @@ class Controller {
}
/**
* Renders a renderer and sets the csrf check and logged in check to true
* @param Renderer $renderer: the render which should be used to render the page
*/
protected function render(Renderer $renderer){
$renderer->render();
}
/**
* Binds variables to the template and prints it
* @param $templateName the name of the template
* The following values are always assigned: userId, trans
* @param $arguments an array with arguments in $templateVar => $content
* @param $template the name of the template
* @param $safeParams template parameters which should not be escaped
* @param $fullPage if true, it will render a full page, otherwise only a part
* defaults to true
*/
protected function renderTemplate($templateName, $arguments=array(),
$safeParams=array(), $fullPage=true){
$renderer = new TemplateRenderer($templateName, $fullPage);
$renderer->bind($arguments);
$renderer->bindSafe($safeParams);
$this->render($renderer);
}
protected function render($template, $arguments=array(), $safeParams=array(),
$fullPage=true){
/**
* Binds variables to a JSON array and prints it
* @param $arguments an array with arguments in $key => $value
* @param $error: Empty by default. If set, a log message written and the
* $error will be sent to the client
*/
protected function renderJSON($arguments=array(), $error=""){
$renderer = new JSONRenderer($error);
$renderer->bind($arguments);
$this->render($renderer);
}
}
/**
* 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', $name, 'user');
$template = new \OCP\Template('news', $template, 'user');
} else {
$this->template = new \OCP\Template('news', $name);
$template = new \OCP\Template('news', $template);
}
}
/**
* @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)) {
$this->template->assign($key, $value, false);
foreach($arguments as $key => $value){
if(array_key_exists($key, $safeParams)) {
$template->assign($key, $value, false);
} else {
$this->template->assign($key, $value);
$template->assign($key, $value);
}
}
}
/**
* Print the page
*/
public function render(){
$this->template->printPage();
$template->assign('userId', $this->userId);
$template->assign('trans', $this->trans);
$template->printPage();
}
}
/**
* 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);
} else {
OCP\JSON::error(array(
'data' => array('message' => $l->t('An error occured: ') . $error)
)
);
OCP\Util::writeLog('news',$_SERVER['REQUEST_URI'] . 'Error: '. $error, OCP\Util::ERROR);
exit();
}
}
}

Просмотреть файл

@ -12,34 +12,31 @@
namespace OCA\News;
require_once \OC_App::getAppPath('news') . '/controllers/controller.php';
class NewsController extends Controller {
/**
* Decides wether to show the feedpage or the firstrun page
*/
public function index($request){
public function index(){
$feedMapper = new FeedMapper($this->userId);
if($feedMapper->feedCount() > 0){
$this->feedPage($request);
$this->feedPage();
} else {
$this->firstRun($request);
$this->firstRun();
}
}
public function firstRun($request){
public function firstRun(){
$this->addScript('news');
$this->addScript('firstrun');
$this->addStyle('firstrun');
$this->renderTemplate('firstrun');
$this->render('firstrun');
}
public function feedPage($request){
public function feedPage(){
$this->addScript('main');
$this->addScript('news');
$this->addScript('menu');
@ -54,10 +51,10 @@ class NewsController extends Controller {
$itemMapper = new ItemMapper($this->userId);
// if no feed id is passed as parameter, then show the last viewed feed on reload
$lastViewedFeedId = isset( $request->get['feedid'] ) ? $request->get['feedid'] : (int)$this->getUserValue('lastViewedFeed');
$lastViewedFeedType = isset( $request->get['feedid'] ) ? FeedType::FEED : (int)$this->getUserValue('lastViewedFeedType');
$lastViewedFeedId = isset( $_GET['feedid'] ) ? $_GET['feedid'] : (int)$this->getUserValue('lastViewedFeed');
$lastViewedFeedType = isset( $_GET['feedid'] ) ? FeedType::FEED : (int)$this->getUserValue('lastViewedFeedType');
$showAll = $this->getUserValue('showAll');
$showAll = $this->getUserValue('showAll');
if( $lastViewedFeedId === null || $lastViewedFeedType === null) {
$lastViewedFeedId = $feedMapper->mostRecent();
@ -90,7 +87,7 @@ class NewsController extends Controller {
'items' => $items
);
$this->renderTemplate('main', $params, array('items' => true));
$this->render('main', $params, array('items' => true));
}

Просмотреть файл

@ -11,10 +11,18 @@
*
*/
namespace OCA\News;
require_once OC_App::getAppPath('news') . '/controllers/controller.php';
require_once OC_App::getAppPath('news') . '/controllers/news.controller.php';
require_once \OC_App::getAppPath('news') . '/lib/serve.php';
require_once \OC_App::getAppPath('news') . '/controllers/news.controller.php';
OCP\User::checkLoggedIn();
OCP\App::checkAppEnabled('news');
OCP\App::setActiveNavigationEntry('news');
$controller = new OCA\News\NewsController();
// routes
serve(new NewsController(), 'index', false);
if(isset($_GET['jstest'])){
$controller->javascriptTests();
} else {
$controller->index();
}

Просмотреть файл

@ -1,96 +0,0 @@
<?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;
}
}

1
news
Просмотреть файл

@ -1 +0,0 @@
/srv/http/apps/news/