| [1c92d58] | 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 18 | MA 02110-1301 USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| [8c356c1] | 21 | require(__DIR__ ."/inc/envsetup.php");
|
|---|
| 22 |
|
|---|
| 23 | $settings = new SiWeather\Webservice\WebserviceConfig();
|
|---|
| [1c92d58] | 24 |
|
|---|
| [92c9c7c] | 25 | /**
|
|---|
| 26 | * @brief Handles the stations (list) REST requests
|
|---|
| 27 | */
|
|---|
| [1c92d58] | 28 | function stations(){
|
|---|
| 29 | $stations = Array();
|
|---|
| 30 | // $station = Array();
|
|---|
| 31 | // $station["id"] = "123456";
|
|---|
| 32 | // $station["name"] = "Station Name";
|
|---|
| 33 | // $station["longitude"] = 52.320;
|
|---|
| 34 | // $station["latitude"] = 48;
|
|---|
| 35 | // $station["city"] = "Krefeld";
|
|---|
| 36 | // $station["values"] = Array();
|
|---|
| 37 | // $station["values"][] = "temperature";
|
|---|
| 38 | // $station["values"][] = "humidity";
|
|---|
| 39 | // $station["url"] = "stations/". $station["id"];
|
|---|
| 40 | // $stations[] = $station;
|
|---|
| 41 | echo json_encode($stations);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| [b858e6f] | 44 | /**
|
|---|
| 45 | * @brief Registers a new weather station
|
|---|
| 46 | */
|
|---|
| 47 | function stationsRegister(){
|
|---|
| 48 | try{
|
|---|
| 49 | $app = \Slim\Slim::getInstance("siweather");
|
|---|
| 50 | $body = $app->request->getBody();
|
|---|
| [519df6e] | 51 | $json = json_decode($body, true);
|
|---|
| [b858e6f] | 52 |
|
|---|
| 53 | $app->response()->header("Content-Type", "application/json");
|
|---|
| [519df6e] | 54 |
|
|---|
| 55 | $expectedKeys = array("city", "longitude", "latitude");
|
|---|
| 56 | $missingKeys = array();
|
|---|
| 57 | foreach($expectedKeys as $key){
|
|---|
| 58 | if(!array_key_exists($key, $json))
|
|---|
| 59 | $missingKeys[] = $key;
|
|---|
| 60 | }
|
|---|
| 61 | if(count($missingKeys) > 0){
|
|---|
| 62 | echo json_encode
|
|---|
| 63 | (
|
|---|
| 64 | [
|
|---|
| 65 | "result" => "missingkeys",
|
|---|
| 66 | "message" => "Your request misses the following keys: ". implode(", ", $missingKeys),
|
|---|
| 67 | ]
|
|---|
| 68 | );
|
|---|
| 69 | return;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if(array_key_exists("locationchecked", $json) && $json["locationchecked"]){
|
|---|
| 73 | // user has checked the location already and we add it now
|
|---|
| 74 |
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | $errorMessage = "";
|
|---|
| 78 | $location = SiWeather\Webservice\IpGeoLocator::locate($app->request->getIp(), $errorMessage);
|
|---|
| 79 | if(is_null($location)){
|
|---|
| 80 | error_log($errorMessage);
|
|---|
| 81 | echo json_encode
|
|---|
| 82 | (
|
|---|
| 83 | [
|
|---|
| 84 | "result" => "unprovenlocation",
|
|---|
| 85 | "message" => "Could not check the city (". $json["city"] .") and location (". $json["longitude"] .", ". $json["latitude"] .") you send with help of your ip address. Please make sure, your data is correct. If your data is correct, please resend your request with the keyword \"locationchecked = true\".",
|
|---|
| 86 | ]
|
|---|
| 87 | );
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 | elseif($location->city() != $json["city"]){
|
|---|
| 91 | echo json_encode
|
|---|
| 92 | (
|
|---|
| 93 | [
|
|---|
| 94 | "result" => "unprovenlocation",
|
|---|
| 95 | "message" => "The city (". $json[city] .") and location (". $json["longitude"] .", ". $json["latitude"] .") you send, does not suit with your ip address associtated location (city: ". $location->city() ."; Location: ". $location->longitude() .", ". $location->latitude() ."). If your data is correct, please resend your request with the keyword \"locationchecked = true\".",
|
|---|
| 96 | ]
|
|---|
| 97 | );
|
|---|
| 98 | return;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| [b858e6f] | 101 | echo json_encode($json);
|
|---|
| 102 | }
|
|---|
| 103 | catch(Exception $exception){
|
|---|
| 104 | error_log("Caught exception handling request stationsRegister: ". $exception->getMessage());
|
|---|
| 105 | $app->response()->status(400);
|
|---|
| 106 | $app->response()->header("X-Status-Reason", $exception->getMessage());
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [1c92d58] | 110 | $app = new \Slim\Slim
|
|---|
| 111 | (
|
|---|
| 112 | array
|
|---|
| 113 | (
|
|---|
| [1e37648] | 114 | "mode" => $settings->getConfig("mode"),
|
|---|
| [1c92d58] | 115 | )
|
|---|
| 116 | );
|
|---|
| [b858e6f] | 117 | $app->setName("siweather");
|
|---|
| [1c92d58] | 118 |
|
|---|
| 119 | // Only invoked if mode is "production"
|
|---|
| 120 | $app->configureMode('production', function () use ($app) {
|
|---|
| 121 | $app->config(array(
|
|---|
| [1e37648] | 122 | "log.enable" => true,
|
|---|
| 123 | "log.level" => \Slim\Log::WARN,
|
|---|
| 124 | "debug" => false,
|
|---|
| [1c92d58] | 125 | ));
|
|---|
| 126 | });
|
|---|
| 127 | // Only invoked if mode is "development"
|
|---|
| 128 | $app->configureMode('development', function () use ($app) {
|
|---|
| 129 | $app->config(array(
|
|---|
| [1e37648] | 130 | "log.enable" => true,
|
|---|
| 131 | "log.level" => \Slim\Log::DEBUG,
|
|---|
| 132 | "debug" => true
|
|---|
| [1c92d58] | 133 | ));
|
|---|
| 134 | });
|
|---|
| 135 |
|
|---|
| [1e37648] | 136 | $app->notFound(function () use ($app) {
|
|---|
| 137 | $html = "";
|
|---|
| 138 | if($app->config("debug")){
|
|---|
| 139 | ob_start();
|
|---|
| 140 | phpinfo( INFO_VARIABLES ^ INFO_ENVIRONMENT );
|
|---|
| 141 | $html = ob_get_contents();
|
|---|
| 142 | ob_end_clean();
|
|---|
| 143 | }
|
|---|
| 144 | $app->render("404.php", array("dump" => $html));
|
|---|
| 145 | });
|
|---|
| 146 |
|
|---|
| 147 | $app->get("/setup", function () use ($app){
|
|---|
| 148 | $app->render("setup.php");
|
|---|
| 149 | });
|
|---|
| [1c92d58] | 150 |
|
|---|
| 151 | $app->get("/stations", "stations");
|
|---|
| [b858e6f] | 152 | $app->post("/stations/register", "stationsRegister");
|
|---|
| [1e37648] | 153 |
|
|---|
| [1c92d58] | 154 | $app->run();
|
|---|
| 155 |
|
|---|
| 156 | ?>
|
|---|