diff options
32 files changed, 302 insertions, 988 deletions
diff --git a/appinfo/app.php b/appinfo/app.php index 1cc9994ae..ebd5f47a7 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -25,6 +25,8 @@ $config->registerHooks(); try { $config->testDependencies(); } catch(\OCA\News\Config\DependencyException $e) { - $container->getLogger()->log($e->getMessage()); + $logger = $container->getLogger(); + $params = $container->getLoggerParameters(); + $logger->warn($e->getMessage(), $params); } diff --git a/appinfo/application.php b/appinfo/application.php index fc2e00d1a..bf11936bd 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -18,15 +18,12 @@ use \OCP\AppFramework\App; use \OCA\News\Config\AppConfig; -use \OCA\News\Core\Logger; -use \OCA\News\Core\Db; - use \OCA\News\Controller\PageController; use \OCA\News\Controller\FolderController; use \OCA\News\Controller\FeedController; use \OCA\News\Controller\ItemController; use \OCA\News\Controller\ExportController; -use \OCA\News\Controller\ApiController; +use \OCA\News\Controller\UtilityApiController; use \OCA\News\Controller\FolderApiController; use \OCA\News\Controller\FeedApiController; use \OCA\News\Controller\ItemApiController; @@ -55,8 +52,6 @@ use \OCA\News\ArticleEnhancer\Enhancer; use \OCA\News\ArticleEnhancer\XPathArticleEnhancer; use \OCA\News\ArticleEnhancer\RegexArticleEnhancer; -use \OCA\News\Middleware\CORSMiddleware; - require_once __DIR__ . '/../3rdparty/htmlpurifier/library/HTMLPurifier.auto.php'; @@ -133,8 +128,8 @@ class Application extends App { ); }); - $container->registerService('ApiController', function($c) { - return new ApiController( + $container->registerService('UtilityApiController', function($c) { + return new UtilityApiController( $c->query('AppName'), $c->query('Request'), $c->query('Updater'), @@ -160,7 +155,8 @@ class Application extends App { $c->query('FeedBusinessLayer'), $c->query('ItemBusinessLayer'), $c->query('Logger'), - $c->query('UserId') + $c->query('UserId'), + $c->query('LoggerParameters') ); }); @@ -196,7 +192,8 @@ class Application extends App { $c->query('TimeFactory'), $c->query('Config'), $c->query('Enhancer'), - $c->query('HTMLPurifier') + $c->query('HTMLPurifier'), + $c->query('LoggerParameters') ); }); @@ -284,11 +281,15 @@ class Application extends App { }); $container->registerService('Logger', function($c) { - return new Logger($c->query('AppName')); + return $c->query('ServerContainer')->getLogger(); + }); + + $container->registerService('LoggerParameters', function($c) { + return array('app' => $c->query('AppName')); }); $container->registerService('Db', function() { - return new Db(); + return $c->query('ServerContainer')->getDb(); }); $container->registerService('CoreConfig', function($c) { @@ -313,7 +314,11 @@ class Application extends App { }); $container->registerService('Config', function($c) { - $config = new Config($c->query('ConfigView'), $c->query('Logger')); + $config = new Config( + $c->query('ConfigView'), + $c->query('Logger'), + $c->query('LoggerParameters') + ); $config->read('config.ini', true); return $config; }); @@ -426,17 +431,6 @@ class Application extends App { ); }); - /** - * Middleware - */ - $container->registerService('CORSMiddleware', function($c) { - return new CORSMiddleware( - $c->query('Request') - ); - }); - - $container->registerMiddleWare('CORSMiddleware'); - } public function getAppConfig() { @@ -447,5 +441,12 @@ class Application extends App { public function getLogger() { return $this->getContainer()->query('Logger'); } + + + public function getLoggerParameters() { + return $this->getContainer()->query('LoggerParameters'); + } + + } diff --git a/appinfo/routes.php b/appinfo/routes.php index 5ba7a2df7..4d91273b6 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -63,7 +63,7 @@ $application->registerRoutes($this, array('routes' => array( array('name' => 'api#version', 'url' => '/api/v1-2/version', 'verb' => 'GET'), array('name' => 'api#before_update', 'url' => '/api/v1-2/cleanup/before-update', 'verb' => 'GET'), array('name' => 'api#after_update', 'url' => '/api/v1-2/cleanup/after-update', 'verb' => 'GET'), - array('name' => 'api#cors', 'url' => '/api/v1-2/{path}', 'verb' => 'OPTIONS', 'requirements' => array('path' => '.+')), + array('name' => 'api#preflighted_cors', 'url' => '/api/v1-2/{path}', 'verb' => 'OPTIONS', 'requirements' => array('path' => '.+')), // folders array('name' => 'folder_api#index', 'url' => '/api/v1-2/folders', 'verb' => 'GET'), diff --git a/businesslayer/businesslayer.php b/businesslayer/businesslayer.php index ea03db1af..dc6fc1a68 100644 --- a/businesslayer/businesslayer.php +++ b/businesslayer/businesslayer.php @@ -13,8 +13,9 @@ namespace OCA\News\BusinessLayer; -use \OCA\News\Db\DoesNotExistException; -use \OCA\News\Db\MultipleObjectsReturnedException; +use \OCP\AppFramework\Db\DoesNotExistException; +use \OCP\AppFramework\Db\MultipleObjectsReturnedException; + use \OCA\News\Db\IMapper; diff --git a/businesslayer/feedbusinesslayer.php b/businesslayer/feedbusinesslayer.php index 024c7b8ea..acb999aa4 100644 --- a/businesslayer/feedbusinesslayer.php +++ b/businesslayer/feedbusinesslayer.php @@ -12,8 +12,11 @@ */ namespace OCA\News\BusinessLayer; -use \OCA\News\Core\Logger; -use \OCA\News\Db\DoesNotExistException; + +use \OCP\ILogger; +use \OCP\IL10N; +use \OCP\AppFramework\Db\DoesNotExistException; + use \OCA\News\Db\Feed; use \OCA\News\Db\Item; use \OCA\News\Db\FeedMapper; @@ -35,16 +38,18 @@ class FeedBusinessLayer extends BusinessLayer { private $autoPurgeMinimumInterval; private $enhancer; private $purifier; + private $loggerParams; public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher, ItemMapper $itemMapper, - Logger $logger, - $l10n, + ILogger $logger, + IL10N $l10n, $timeFactory, Config $config, Enhancer $enhancer, - $purifier){ + $purifier, + $loggerParams){ parent::__construct($feedMapper); $this->feedFetcher = $feedFetcher; $this->itemMapper = $itemMapper; @@ -55,6 +60,7 @@ class FeedBusinessLayer extends BusinessLayer { $this->enhancer = $enhancer; $this->purifier = $purifier; $this->feedMapper = $feedMapper; + $this->loggerParams = $loggerParams; } /** @@ -131,7 +137,7 @@ class FeedBusinessLayer extends BusinessLayer { return $feed; } catch(FetcherException $ex){ - $this->logger->log($ex->getMessage(), 'debug'); + $this->logger->debug($ex->getMessage(), $this->loggerParams); throw new BusinessLayerException( $this->l10n->t( 'Can not add feed: URL does not exist, SSL Certificate can not be validated ' . @@ -150,8 +156,8 @@ class FeedBusinessLayer extends BusinessLayer { try { $this->update($feed->getId(), $feed->getUserId()); } catch(BusinessLayerException $ex){ - $this->logger->log('Could not update feed ' . $ex->getMessage(), - 'debug'); + $this->logger->debug('Could not update feed ' . $ex->getMessage(), + $this->loggerParams); } } } @@ -200,9 +206,10 @@ class FeedBusinessLayer extends BusinessLayer { } catch(FetcherException $ex){ // failed updating is not really a problem, so only log it - $this->logger->log('Can not update feed with url ' . $existingFeed->getUrl() . - ': Not found or bad source', 'debug'); - $this->logger->log($ex->getMessage(), 'debug'); + + $this->logger->debug('Can not update feed with url ' . $existingFeed->getUrl() . + ': Not found or bad source', $this->loggerParams); + $this->logger->debug($ex->getMessage(), $this->loggerParams); } return $this->feedMapper->find($feedId, $userId); diff --git a/businesslayer/folderbusinesslayer.php b/businesslayer/folderbusinesslayer.php index 924e23c25..f58e937b2 100644 --- a/businesslayer/folderbusinesslayer.php +++ b/businesslayer/folderbusinesslayer.php @@ -13,6 +13,8 @@ namespace OCA\News\BusinessLayer; +use \OCP\IL10N; + use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; use \OCA\News\Utility\Config; @@ -26,7 +28,7 @@ class FolderBusinessLayer extends BusinessLayer { private $folderMapper; public function __construct(FolderMapper $folderMapper, - $l10n, + IL10N $l10n, $timeFactory, Config $config){ parent::__construct($folderMapper); diff --git a/businesslayer/itembusinesslayer.php b/businesslayer/itembusinesslayer.php index 85fa4d10f..23f6bcd2d 100644 --- a/businesslayer/itembusinesslayer.php +++ b/businesslayer/itembusinesslayer.php @@ -13,11 +13,12 @@ namespace OCA\News\BusinessLayer; +use \OCP\AppFramework\Db\DoesNotExistException; + use \OCA\News\Db\Item; use \OCA\News\Db\ItemMapper; use \OCA\News\Db\StatusFlag; use \OCA\News\Db\FeedType; -use \OCA\News\Db\DoesNotExistException; use \OCA\News\Utility\Config; @@ -28,8 +29,10 @@ class ItemBusinessLayer extends BusinessLayer { private $timeFactory; private $itemMapper; - public function __construct(ItemMapper $itemMapper, StatusFlag $statusFlag, - $timeFactory, Config $config){ + public function __construct(ItemMapper $itemMapper, + StatusFlag $statusFlag, + $timeFactory, + Config $config){ parent::__construct($itemMapper); $this->statusFlag = $statusFlag; $this->autoPurgeCount = $config->getAutoPurgeCount(); diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php index 05c50543e..883732c87 100644 --- a/controller/feedapicontroller.php +++ b/controller/feedapicontroller.php @@ -14,11 +14,11 @@ namespace OCA\News\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use \OCP\ILogger; +use \OCP\AppFramework\ApiController; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Core\Logger; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -26,34 +26,37 @@ use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerConflictException; -class FeedApiController extends Controller { +class FeedApiController extends ApiController { private $itemBusinessLayer; private $feedBusinessLayer; private $folderBusinessLayer; private $userId; private $logger; + private $loggerParams; public function __construct($appName, IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, ItemBusinessLayer $itemBusinessLayer, - Logger $logger, - $userId){ + ILogger $logger, + $userId, + $loggerParams){ parent::__construct($appName, $request); $this->folderBusinessLayer = $folderBusinessLayer; $this->feedBusinessLayer = $feedBusinessLayer; $this->itemBusinessLayer = $itemBusinessLayer; $this->userId = $userId; $this->logger = $logger; + $this->loggerParams = $loggerParams; } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function index() { @@ -62,56 +65,66 @@ class FeedApiController extends Controller { 'starredCount' => $this->itemBusinessLayer->starredCount($this->userId) ); - foreach ($this->feedBusinessLayer->findAll($this->userId) as $feed) { + $feeds = $this->feedBusinessLayer->findAll($this->userId); + foreach ($feeds as $feed) { array_push($result['feeds'], $feed->toAPI()); } // check case when there are no items try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); + $result['newestItemId'] = $this->itemBusinessLayer + ->getNewestItemId($this->userId); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId } catch(BusinessLayerException $ex) {} - return new JSONResponse($result); + return $result; } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param string $url + * @param int $folderId */ - public function create() { - $feedUrl = $this->params('url'); - $folderId = (int) $this->params('folderId', 0); - + public function create($url, $folderId=0) { try { $this->feedBusinessLayer->purgeDeleted($this->userId, false); - $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $this->userId); + $feed = $this->feedBusinessLayer->create($url, $folderId, + $this->userId); $result = array( 'feeds' => array($feed->toAPI()) ); try { - $result['newestItemId'] = - $this->itemBusinessLayer->getNewestItemId($this->userId); + $result['newestItemId'] = $this->itemBusinessLayer + ->getNewestItemId($this->userId); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId } catch(BusinessLayerException $ex) {} - return new JSONResponse($result); + return $result; } catch(BusinessLayerConflictException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_CONFLICT); + + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT + ); + } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND + ); + } } @@ -119,17 +132,18 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS + * + * @param int $feedId */ - public function delete() { - $feedId = (int) $this->params('feedId'); - + public function delete($feedId) { try { $this->feedBusinessLayer->delete($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { - return new JSONResponse(array('message' => $ex->getMessage()), - Http::STATUS_NOT_FOUND); + return new JSONResponse( + array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND + ); } } @@ -137,21 +151,20 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function read() { $feedId = (int) $this->params('feedId'); $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $this->userId); - return new JSONResponse(); } /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function move() { $feedId = (int) $this->params('feedId'); @@ -159,7 +172,6 @@ class FeedApiController extends Controller { try { $this->feedBusinessLayer->move($feedId, $folderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -170,7 +182,7 @@ class FeedApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function rename() { $feedId = (int) $this->params('feedId'); @@ -178,7 +190,6 @@ class FeedApiController extends Controller { try { $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -188,7 +199,7 @@ class FeedApiController extends Controller { /** * @NoCSRFRequired - * @API + * @CORS */ public function fromAllUsers() { $feeds = $this->feedBusinessLayer->findAllFromAllUsers(); @@ -216,11 +227,9 @@ class FeedApiController extends Controller { $this->feedBusinessLayer->update($feedId, $userId); // ignore update failure (feed could not be reachable etc, we dont care) } catch(\Exception $ex) { - $this->logger->log('Could not update feed ' . $ex->getMessage(), - 'debug'); + $this->logger->debug('Could not update feed ' . $ex->getMessage(), + $this->loggerParams); } - return new JSONResponse(); - } diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 56c7d5af8..02e523dff 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -167,7 +167,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->markDeleted($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -215,7 +214,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->move($feedId, $parentFolderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -232,7 +230,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->rename($feedId, $feedTitle, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() @@ -286,7 +283,6 @@ class FeedController extends Controller { try { $this->feedBusinessLayer->unmarkDeleted($feedId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array( 'msg' => $ex->getMessage() diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php index 4e566541c..40ffc1389 100644 --- a/controller/folderapicontroller.php +++ b/controller/folderapicontroller.php @@ -14,7 +14,7 @@ namespace OCA\News\Controller; use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use \OCP\AppFramework\ApiController; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; @@ -25,7 +25,7 @@ use \OCA\News\BusinessLayer\BusinessLayerConflictException; use \OCA\News\BusinessLayer\BusinessLayerValidationException; -class FolderApiController extends Controller { +class FolderApiController extends ApiController { private $folderBusinessLayer; private $itemBusinessLayer; @@ -46,7 +46,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function index() { $result = array( @@ -64,7 +64,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function create() { $folderName = $this->params('name'); @@ -93,14 +93,13 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function delete() { $folderId = (int) $this->params('folderId'); try { $this->folderBusinessLayer->delete($folderId, $this->userId); - return new JSONResponse(); } catch(BusinessLayerException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), Http::STATUS_NOT_FOUND); @@ -111,7 +110,7 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function update() { $folderId = (int) $this->params('folderId'); @@ -119,7 +118,6 @@ class FolderApiController extends Controller { try { $this->folderBusinessLayer->rename($folderId, $folderName, $this->userId); - return new JSONResponse(); } catch(BusinessLayerValidationException $ex) { return new JSONResponse(array('message' => $ex->getMessage()), @@ -139,14 +137,13 @@ class FolderApiController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * @API + * @CORS */ public function read() { $folderId = (int) $this->params('folderId'); $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $this->userId); - return new JSONResponse(); } diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index caa07f4d2..76476dd7a 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -27,12 +27,15 @@ use \OCA\News\BusinessLayer\BusinessLayerValidationException; class FolderController extends Controller { + use JSONHttpError; + private $folderBusinessLayer; private $feedBusinessLayer; private $itemBusinessLayer; private $userId; - public function __construct($appName, IRequest $request, + public function __construct($appName, + IRequest $request, FolderBusinessLayer $folderBusinessLayer, FeedBusinessLayer $feedBusinessLayer, ItemBusinessLayer $itemBusinessLayer, @@ -53,76 +56,64 @@ class FolderController extends Controller { $result = array( 'folders' => $folders ); - return new JSONResponse($result); + return $result; } - private function setOpened($isOpened){ - $folderId = (int) $this->params('folderId'); - + private function setOpened($isOpened, $folderId){ |