| 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 |
|
|---|
| 21 | namespace SiWeather\Webservice;
|
|---|
| 22 |
|
|---|
| 23 | require(__DIR__ ."/../../inc/envsetup.php");
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @brief Implementation of the Webservice API
|
|---|
| 27 | *
|
|---|
| 28 | * @module WebserviceAPI
|
|---|
| 29 | */
|
|---|
| 30 | class WebserviceApi {
|
|---|
| 31 | /**
|
|---|
| 32 | * @brief Handles the stations (list) REST requests
|
|---|
| 33 | */
|
|---|
| 34 | public static function stations(){
|
|---|
| 35 | $stations = Array();
|
|---|
| 36 |
|
|---|
| 37 | $db = new DatabaseHandler();
|
|---|
| 38 | if(!$db->connect()){
|
|---|
| 39 | echo json_encode
|
|---|
| 40 | (
|
|---|
| 41 | [
|
|---|
| 42 | "result" => "failed",
|
|---|
| 43 | "message" => "Could not connect to the database",
|
|---|
| 44 | ]
|
|---|
| 45 | );
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 | $db->exec("BEGIN");
|
|---|
| 49 |
|
|---|
| 50 | $sqlStatement = $db->prepare("SELECT stations.uuid as uuid, stations.name as name, X(stations.location) as longitude, Y(stations.location) as latitude, cities.name as city FROM stations, cities WHERE stations.city=cities.id");
|
|---|
| 51 | if(!$sqlStatement){
|
|---|
| 52 | $db->exec("ROLLBACK");
|
|---|
| 53 | echo json_encode
|
|---|
| 54 | (
|
|---|
| 55 | [
|
|---|
| 56 | "result" => "failed",
|
|---|
| 57 | "message" => "Could not fetch stations from database",
|
|---|
| 58 | ]
|
|---|
| 59 | );
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 | $result = $sqlStatement->execute();
|
|---|
| 63 | while($row = $result->fetchArray()){
|
|---|
| 64 | $station = Array();
|
|---|
| 65 | $station["uuid"] = $row["uuid"];
|
|---|
| 66 | $station["name"] = $row["name"];
|
|---|
| 67 | $station["longitude"] = $row["longitude"];
|
|---|
| 68 | $station["latitude"] = $row["latitude"];
|
|---|
| 69 | $station["city"] = $row["city"];
|
|---|
| 70 | // $station["values"] = Array();
|
|---|
| 71 | // $station["values"][] = "temperature";
|
|---|
| 72 | // $station["values"][] = "humidity";
|
|---|
| 73 | $station["url"] = "stations/". $station["uuid"];
|
|---|
| 74 | $stations[] = $station;
|
|---|
| 75 | }
|
|---|
| 76 | $db->exec("COMMIT");
|
|---|
| 77 |
|
|---|
| 78 | echo json_encode($stations);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * @brief Registers a new weather station
|
|---|
| 83 | *
|
|---|
| 84 | * @note Call this function with a big timeout > 2seconds, because we use another webservice
|
|---|
| 85 | * to fetch location data. This fetch runs already 2seconds.
|
|---|
| 86 | * @note Requires php-pecl-uuid
|
|---|
| 87 | */
|
|---|
| 88 | public static function stationsRegister(){
|
|---|
| 89 | $geoLocationRadius = 5;
|
|---|
| 90 |
|
|---|
| 91 | try{
|
|---|
| 92 | $app = \Slim\Slim::getInstance("siweather");
|
|---|
| 93 | $body = $app->request->getBody();
|
|---|
| 94 | $json = json_decode($body, true);
|
|---|
| 95 |
|
|---|
| 96 | $app->response()->header("Content-Type", "application/json");
|
|---|
| 97 |
|
|---|
| 98 | $expectedKeys = array("name", "city", "longitude", "latitude", "email");
|
|---|
| 99 | $missingKeys = array();
|
|---|
| 100 | foreach($expectedKeys as $key){
|
|---|
| 101 | if(!array_key_exists($key, $json))
|
|---|
| 102 | $missingKeys[] = $key;
|
|---|
| 103 | }
|
|---|
| 104 | if(count($missingKeys) > 0){
|
|---|
| 105 | echo json_encode
|
|---|
| 106 | (
|
|---|
| 107 | [
|
|---|
| 108 | "result" => "missingkeys",
|
|---|
| 109 | "message" => "Your request misses the following keys: ". implode(", ", $missingKeys),
|
|---|
| 110 | ]
|
|---|
| 111 | );
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if(!Location::ValidLatitude($json["latitude"])){
|
|---|
| 116 | echo json_encode
|
|---|
| 117 | (
|
|---|
| 118 | [
|
|---|
| 119 | "result" => "invalid latitude",
|
|---|
| 120 | "message" => "Your specified an invalid latitude: ". $json["latitude"],
|
|---|
| 121 | ]
|
|---|
| 122 | );
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 | if(!Location::ValidLongitude($json["longitude"])){
|
|---|
| 126 | echo json_encode
|
|---|
| 127 | (
|
|---|
| 128 | [
|
|---|
| 129 | "result" => "invalid longitude",
|
|---|
| 130 | "message" => "Your specified an invalid longitude: ". $json["longitude"],
|
|---|
| 131 | ]
|
|---|
| 132 | );
|
|---|
| 133 | return;
|
|---|
| 134 | }
|
|---|
| 135 | if(empty($json["email"]) || !filter_var($json["email"], FILTER_VALIDATE_EMAIL)){
|
|---|
| 136 | echo json_encode
|
|---|
| 137 | (
|
|---|
| 138 | [
|
|---|
| 139 | "result" => "invalid email",
|
|---|
| 140 | "message" => "Your specified an invalid email address: ". $json["email"],
|
|---|
| 141 | ]
|
|---|
| 142 | );
|
|---|
| 143 | return;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | $errorMessage = "";
|
|---|
| 147 | $locations = GeoLocator::locations($json["latitude"], $json["longitude"], $geoLocationRadius, $errorMessage);
|
|---|
| 148 | if(!$locations || count($locations) < 1){
|
|---|
| 149 | error_log($errorMessage);
|
|---|
| 150 | echo json_encode
|
|---|
| 151 | (
|
|---|
| 152 | [
|
|---|
| 153 | "result" => "unprovenlocation",
|
|---|
| 154 | "message" => "Could not check the city (". $json["city"] .") and location (". $json["latitude"] .", ". $json["longitude"] .") you send with help of your city and geo coordinates. Please make sure, your data is correct.",
|
|---|
| 155 | ]
|
|---|
| 156 | );
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 | else{
|
|---|
| 160 | $validatedLocation = false;
|
|---|
| 161 | foreach($locations as $location){
|
|---|
| 162 | if($location->city() == $json["city"]){
|
|---|
| 163 | $validatedLocation = $location;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | if(!$validatedLocation){
|
|---|
| 167 | $message = "The city (". $json[city] .") and location (". $json["latitude"] .", ". $json["longitude"] .") you send, does not suite with locations we found at your given location (city, latitude, longitude):\n";
|
|---|
| 168 | foreach($locations as $location){
|
|---|
| 169 | $message .= " ". $location->city() .": ". $location->latitude() .", ". $location->longitude() ."\n";
|
|---|
| 170 | }
|
|---|
| 171 | $message .= " Please make sure, your data is correct.";
|
|---|
| 172 |
|
|---|
| 173 | echo json_encode
|
|---|
| 174 | (
|
|---|
| 175 | [
|
|---|
| 176 | "result" => "unprovenlocation",
|
|---|
| 177 | "message" => $message,
|
|---|
| 178 | ]
|
|---|
| 179 | );
|
|---|
| 180 | return;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | $db = new DatabaseHandler();
|
|---|
| 185 | if(!$db->connect()){
|
|---|
| 186 | echo json_encode
|
|---|
| 187 | (
|
|---|
| 188 | [
|
|---|
| 189 | "result" => "failed",
|
|---|
| 190 | "message" => "Could not connect to the database",
|
|---|
| 191 | ]
|
|---|
| 192 | );
|
|---|
| 193 | return;
|
|---|
| 194 | }
|
|---|
| 195 | $db->exec("BEGIN");
|
|---|
| 196 |
|
|---|
| 197 | // find out, if the city is already there
|
|---|
| 198 | $cityId = -1;
|
|---|
| 199 | $sqlStatement = $db->prepare("SELECT id FROM cities WHERE name=:name AND location=:location");
|
|---|
| 200 | if(!$sqlStatement){
|
|---|
| 201 | $db->exec("ROLLBACK");
|
|---|
| 202 | echo json_encode
|
|---|
| 203 | (
|
|---|
| 204 | [
|
|---|
| 205 | "result" => "failed",
|
|---|
| 206 | "message" => "Could not fetch city from database",
|
|---|
| 207 | ]
|
|---|
| 208 | );
|
|---|
| 209 | return;
|
|---|
| 210 | }
|
|---|
| 211 | $sqlStatement->bindValue(":name", $validatedLocation->city());
|
|---|
| 212 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 213 | $result = $sqlStatement->execute();
|
|---|
| 214 | if($result->numColumns() && $result->columnType(0) != SQLITE3_NULL){
|
|---|
| 215 | error_log("not cool");
|
|---|
| 216 | $row = $result->fetchArray();
|
|---|
| 217 | $cityId = $row[0];
|
|---|
| 218 | error_log($row[0]);
|
|---|
| 219 | }
|
|---|
| 220 | else{
|
|---|
| 221 | $sqlStatement = $db->prepare("INSERT INTO cities (name, location) VALUES(:name, GeomFromText(:location, 4326))");
|
|---|
| 222 | if(!$sqlStatement){
|
|---|
| 223 | $db->exec("ROLLBACK");
|
|---|
| 224 | echo json_encode
|
|---|
| 225 | (
|
|---|
| 226 | [
|
|---|
| 227 | "result" => "failed",
|
|---|
| 228 | "message" => "Could insert city into the database",
|
|---|
| 229 | ]
|
|---|
| 230 | );
|
|---|
| 231 | return;
|
|---|
| 232 | }
|
|---|
| 233 | $sqlStatement->bindValue(":name", $validatedLocation->city());
|
|---|
| 234 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 235 | $sqlResult = $sqlStatement->execute();
|
|---|
| 236 | if(!$sqlResult){
|
|---|
| 237 | error_log("Failed to add city to the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 238 | $db->exec("ROLLBACK");
|
|---|
| 239 | echo json_encode
|
|---|
| 240 | (
|
|---|
| 241 | [
|
|---|
| 242 | "result" => "failed",
|
|---|
| 243 | "message" => "Could not add city to the database",
|
|---|
| 244 | ]
|
|---|
| 245 | );
|
|---|
| 246 | return;
|
|---|
| 247 | }
|
|---|
| 248 | $cityId = $db->lastInsertRowId();
|
|---|
| 249 | }
|
|---|
| 250 | if($cityId < 0){
|
|---|
| 251 | error_log("Failed to select city from the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 252 | $db->exec("ROLLBACK");
|
|---|
| 253 | echo json_encode
|
|---|
| 254 | (
|
|---|
| 255 | [
|
|---|
| 256 | "result" => "failed",
|
|---|
| 257 | "message" => "Could not select city from the database",
|
|---|
| 258 | ]
|
|---|
| 259 | );
|
|---|
| 260 | return;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | // insert station
|
|---|
| 264 | $uuid = uuid_create(4);
|
|---|
| 265 | $sqlStatement = $db->prepare("INSERT INTO stations (name, city, uuid, ownerMail, location) VALUES(:name, :city, :uuid, :ownerMail, GeomFromText(:location, 4326))");
|
|---|
| 266 | if(!$sqlStatement){
|
|---|
| 267 | $db->exec("ROLLBACK");
|
|---|
| 268 | echo json_encode
|
|---|
| 269 | (
|
|---|
| 270 | [
|
|---|
| 271 | "result" => "failed",
|
|---|
| 272 | "message" => "Could insert station into the database",
|
|---|
| 273 | ]
|
|---|
| 274 | );
|
|---|
| 275 | return;
|
|---|
| 276 | }
|
|---|
| 277 | $sqlStatement->bindValue(":name", $json["name"]);
|
|---|
| 278 | $sqlStatement->bindValue(":city", $cityId);
|
|---|
| 279 | $sqlStatement->bindValue(":uuid", $uuid);
|
|---|
| 280 | $sqlStatement->bindValue(":ownerMail",$json["email"]);
|
|---|
| 281 | $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
|
|---|
| 282 | $sqlResult = $sqlStatement->execute();
|
|---|
| 283 | if(!$sqlResult){
|
|---|
| 284 | error_log("Failed to add station to the database: ". $db->lastDatabaseErrorMessage());
|
|---|
| 285 | $db->query("ROLLBACK");
|
|---|
| 286 | echo json_encode
|
|---|
| 287 | (
|
|---|
| 288 | [
|
|---|
| 289 | "result" => "failed",
|
|---|
| 290 | "message" => "Could not add station to the database",
|
|---|
| 291 | ]
|
|---|
| 292 | );
|
|---|
| 293 | return;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | // finish transaction
|
|---|
| 297 | $db->exec("COMMIT");
|
|---|
| 298 |
|
|---|
| 299 | echo json_encode
|
|---|
| 300 | (
|
|---|
| 301 | [
|
|---|
| 302 | "result" => "success",
|
|---|
| 303 | "uuid" => $uuid,
|
|---|
| 304 | ]
|
|---|
| 305 | );
|
|---|
| 306 | }
|
|---|
| 307 | catch(Exception $exception){
|
|---|
| 308 | error_log("Caught exception handling request stationsRegister: ". $exception->getMessage());
|
|---|
| 309 | $app->response()->status(400);
|
|---|
| 310 | $app->response()->header("X-Status-Reason", $exception->getMessage());
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | ?>
|
|---|