diff --git a/appinfo/application.php b/appinfo/application.php index bee4292..6d666cb 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -36,6 +36,10 @@ class Application extends App { return \OCP\User::getUser(); }); + $container->registerService('Config', function($c) { + return $c->query('ServerContainer')->getConfig(); + }); + /** * Database Layer */ @@ -53,6 +57,7 @@ class Application extends App { $container->registerService('CityController', function(IContainer $c) { return new CityController( $c->query('AppName'), + $c->query('Config'), $c->query('Request'), $c->query('UserId'), $c->query('CityMapper'), @@ -63,6 +68,7 @@ class Application extends App { $container->registerService('SettingsController', function(IContainer $c) { return new SettingsController( $c->query('AppName'), + $c->query('Config'), $c->query('Request'), $c->query('UserId'), $c->query('SettingsMapper'), @@ -73,6 +79,7 @@ class Application extends App { $container->registerService('WeatherController', function(IContainer $c) { return new WeatherController( $c->query('AppName'), + $c->query('Config'), $c->query('Request'), $c->query('UserId'), $c->query('CityMapper'), diff --git a/controller/citycontroller.php b/controller/citycontroller.php index b664e15..3d81f26 100644 --- a/controller/citycontroller.php +++ b/controller/citycontroller.php @@ -11,6 +11,7 @@ namespace OCA\Weather\Controller; +use \OCP\IConfig; use \OCP\IRequest; use \OCP\AppFramework\Http\TemplateResponse; use \OCP\AppFramework\Controller; @@ -27,14 +28,14 @@ class CityController extends IntermediateController { private $userId; private $mapper; private $settingsMapper; - private $apiKey; + private $config; - public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { + public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { parent::__construct($appName, $request); $this->userId = $userId; $this->mapper = $mapper; $this->settingsMapper = $settingsMapper; - $this->apiKey = \OC::$server->getConfig()->getAppValue('weather', 'openweathermap_api_key', ''); + $this->config = $config; } /** @@ -120,15 +121,16 @@ class CityController extends IntermediateController { } private function getCityInformations ($name) { + $apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key'); $cityDatas = json_decode($this->curlGET( - "http://api.openweathermap.org/data/2.5/forecast?q=".urlencode($name)."&mode=json&APPID=".urlencode($this->apiKey))[1], + "http://api.openweathermap.org/data/2.5/forecast?q=".urlencode($name)."&mode=json&APPID=".urlencode($apiKey))[1], true); if (in_array('cod', $cityDatas)) { return array("code" => 502, "response" => null); } if ($cityDatas['cod'] != '200') { - return array("code" => $cityDatas['cod'], "response" => null); + return array("code" => $cityDatas['cod'], "response" => null, "apikey" => $apiKey); } return array("code" => 200, "response" => $cityDatas); diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php index a2f9b61..f7844dc 100644 --- a/controller/settingscontroller.php +++ b/controller/settingscontroller.php @@ -11,6 +11,7 @@ namespace OCA\Weather\Controller; +use \OCP\IConfig; use \OCP\IRequest; use \OCP\AppFramework\Http\TemplateResponse; use \OCP\AppFramework\Controller; @@ -25,12 +26,14 @@ class SettingsController extends Controller { private $userId; private $mapper; private $cityMapper; + private $config; - public function __construct ($appName, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) { + public function __construct ($appName, IConfig $config, IRequest $request, $userId, SettingsMapper $mapper, CityMapper $cityMapper) { parent::__construct($appName, $request); $this->userId = $userId; $this->mapper = $mapper; $this->cityMapper = $cityMapper; + $this->config = $config; } /** @@ -50,12 +53,11 @@ class SettingsController extends Controller { return new JSONResponse(array("set" => true)); } - /** - * @NoCSRFRequired - */ public function apiKeySet ($apikey) { - \OC::$server->getConfig()->setAppValue('weather', 'openweathermap_api_key', $apiKey); - return new JSONResponse(array("set" => true)); + $this->config->setAppValue($this->appName, 'openweathermap_api_key', $apikey); + return new JSONResponse(array( + "apikey" => $this->config->getAppValue($this->appName, 'openweathermap_api_key', ''), + )); } /** diff --git a/controller/weathercontroller.php b/controller/weathercontroller.php index fe73d54..91a329f 100644 --- a/controller/weathercontroller.php +++ b/controller/weathercontroller.php @@ -11,6 +11,7 @@ namespace OCA\Weather\Controller; +use \OCP\IConfig; use \OCP\IRequest; use \OCP\AppFramework\Http\TemplateResponse; use \OCP\AppFramework\Controller; @@ -27,17 +28,15 @@ class WeatherController extends IntermediateController { private $userId; private $mapper; private $settingsMapper; - private $apiKey; private $metric; private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q="; private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q="; - public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { + public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { parent::__construct($appName, $request); $this->userId = $userId; $this->mapper = $mapper; $this->settingsMapper = $settingsMapper; - $this->apiKey = \OC::$server->getConfig()->getAppValue('weather', 'openweathermap_api_key', ''); $this->metric = $settingsMapper->getMetric($this->userId); } @@ -54,8 +53,9 @@ class WeatherController extends IntermediateController { } private function getCityInformations ($name) { + $apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key'); $name = preg_replace("[ ]",'%20',$name); - $reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric); + $reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric); if ($reqContent[0] != Http::STATUS_OK) { $this->errorCode = $reqContent[0]; return null; @@ -63,7 +63,7 @@ class WeatherController extends IntermediateController { $cityDatas = json_decode($reqContent[1], true); $cityDatas["forecast"] = array(); - $forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true); + $forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric), true); if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) { // Show only 8 values max // @TODO: setting ? diff --git a/js/admin.js b/js/admin.js index e911176..975cbf9 100644 --- a/js/admin.js +++ b/js/admin.js @@ -32,12 +32,12 @@ url: OC.generateUrl('/apps/weather/settings/apikey'), type: 'POST', data: { - apiKey: $('#openweathermap-api-key').val() + apikey: $('#openweathermap-api-key').val() } }); request.done(function (data) { - $('#openweathermap-api-key').val(data.apiKey); + $('#openweathermap-api-key').val(data.apikey); OC.msg.finishedSuccess('#OWMApiKeySettingsMsg', 'Saved'); });