Index: src/webservice/htdocs/index.php
===================================================================
--- src/webservice/htdocs/index.php	(revision 519df6e4d06375cf69d39c97a1268e926c826060)
+++ src/webservice/htdocs/index.php	(revision 02fbeb816a6fd00276fc6f4dfc1dd966ba3dc6d5)
@@ -28,15 +28,46 @@
 function stations(){
 	$stations = Array();
-// 	$station = Array();
-// 	$station["id"] = "123456";
-// 	$station["name"] = "Station Name";
-// 	$station["longitude"] = 52.320;
-// 	$station["latitude"] = 48;
-// 	$station["city"] = "Krefeld";
-// 	$station["values"] = Array();
-// 	$station["values"][] = "temperature";
-// 	$station["values"][] = "humidity";
-// 	$station["url"] = "stations/". $station["id"];
-// 	$stations[] = $station;
+
+	$db = new SiWeather\Webservice\DatabaseHandler();
+	if(!$db->connect()){
+		echo json_encode
+		(
+			[
+				"result" => "failed",
+				"message" => "Could not connect to the database",
+			]
+		);
+		return;
+	}
+	$db->exec("BEGIN");
+
+	$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");
+	if(!$sqlStatement){
+		$db->exec("ROLLBACK");
+		echo json_encode
+		(
+			[
+				"result" => "failed",
+				"message" => "Could not fetch stations from database",
+			]
+		);
+		return;
+	}
+	$result = $sqlStatement->execute();
+	while($row = $result->fetchArray()){
+		$station = Array();
+		$station["uuid"] = $row["uuid"];
+		$station["name"] = $row["name"];
+		$station["longitude"] = $row["longitude"];
+		$station["latitude"] = $row["latitude"];
+		$station["city"] = $row["city"];
+// 		$station["values"] = Array();
+// 		$station["values"][] = "temperature";
+// 		$station["values"][] = "humidity";
+		$station["url"] = "stations/". $station["uuid"];
+		$stations[] = $station;
+	}
+	$db->exec("COMMIT");
+
 	echo json_encode($stations);
 }
@@ -44,6 +75,12 @@
 /**
   * @brief Registers a new weather station
+  *
+  * @note Call this function with a big timeout > 2seconds, because we use another webservice
+  *       to fetch location data. This fetch runs already 2seconds.
+	* @note Requires php-pecl-uuid
   */
 function stationsRegister(){
+	$geoLocationRadius = 5;
+
 	try{
 		$app = \Slim\Slim::getInstance("siweather");
@@ -53,5 +90,5 @@
 		$app->response()->header("Content-Type", "application/json");
 
-		$expectedKeys = array("city", "longitude", "latitude");
+		$expectedKeys = array("name", "city", "longitude", "latitude", "email");
 		$missingKeys = array();
 		foreach($expectedKeys as $key){
@@ -70,12 +107,38 @@
 		}
 
-		if(array_key_exists("locationchecked", $json) && $json["locationchecked"]){
-			// user has checked the location already and we add it now
-			
+		if(!SiWeather\Webservice\Location::ValidLatitude($json["latitude"])){
+			echo json_encode
+			(
+				[
+					"result" => "invalid latitude",
+					"message" => "Your specified an invalid latitude: ". $json["latitude"],
+				]
+			);
+			return;
+		}
+		if(!SiWeather\Webservice\Location::ValidLongitude($json["longitude"])){
+			echo json_encode
+			(
+				[
+					"result" => "invalid longitude",
+					"message" => "Your specified an invalid longitude: ". $json["longitude"],
+				]
+			);
+			return;
+		}
+		if(empty($json["email"]) || !filter_var($json["email"], FILTER_VALIDATE_EMAIL)){
+			echo json_encode
+			(
+				[
+					"result" => "invalid email",
+					"message" => "Your specified an invalid email address: ". $json["email"],
+				]
+			);
+			return;
 		}
 
 		$errorMessage = "";
-		$location = SiWeather\Webservice\IpGeoLocator::locate($app->request->getIp(), $errorMessage);
-		if(is_null($location)){
+		$locations = SiWeather\Webservice\GeoLocator::locations($json["latitude"], $json["longitude"], $geoLocationRadius, $errorMessage);
+		if(!$locations || count($locations) < 1){
 			error_log($errorMessage);
 			echo json_encode
@@ -83,21 +146,156 @@
 				[
 					"result" => "unprovenlocation",
-					"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\".",
-				]
-			);
-			return;
-		}
-		elseif($location->city() != $json["city"]){
-			echo json_encode
-			(
-				[
-					"result" => "unprovenlocation",
-					"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\".",
-				]
-			);
-			return;
-		}
-
-		echo json_encode($json);
+					"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.",
+				]
+			);
+			return;
+		}
+		else{
+			$validatedLocation = false;
+			foreach($locations as $location){
+				if($location->city() == $json["city"]){
+					$validatedLocation = $location;
+				}
+			}
+			if(!$validatedLocation){
+				$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";
+				foreach($locations as $location){
+					$message .= "    ". $location->city() .": ". $location->latitude() .", ". $location->longitude() ."\n";
+				}
+				$message .= " Please make sure, your data is correct.";
+
+				echo json_encode
+				(
+					[
+						"result" => "unprovenlocation",
+						"message" => $message,
+					]
+				);
+				return;
+			}
+		}
+
+		$db = new SiWeather\Webservice\DatabaseHandler();
+		if(!$db->connect()){
+			echo json_encode
+			(
+				[
+					"result" => "failed",
+					"message" => "Could not connect to the database",
+				]
+			);
+			return;
+		}
+		$db->exec("BEGIN");
+
+		// find out, if the city is already there
+		$cityId = -1;
+		$sqlStatement = $db->prepare("SELECT id FROM cities WHERE name=:name AND location=:location");
+		if(!$sqlStatement){
+			$db->exec("ROLLBACK");
+			echo json_encode
+			(
+				[
+					"result" => "failed",
+					"message" => "Could not fetch city from database",
+				]
+			);
+			return;
+		}
+		$sqlStatement->bindValue(":name", $validatedLocation->city());
+		$sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
+		$result = $sqlStatement->execute();
+		if($result->numColumns() && $result->columnType(0) != SQLITE3_NULL){
+			error_log("not cool");
+			$row = $result->fetchArray();
+			$cityId = $row[0];
+			error_log($row[0]);
+		}
+		else{
+			$sqlStatement = $db->prepare("INSERT INTO cities (name, location) VALUES(:name, GeomFromText(:location, 4326))");
+			if(!$sqlStatement){
+				$db->exec("ROLLBACK");
+				echo json_encode
+				(
+					[
+						"result" => "failed",
+						"message" => "Could insert city into the database",
+					]
+				);
+				return;
+			}
+			$sqlStatement->bindValue(":name", $validatedLocation->city());
+			$sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
+			$sqlResult = $sqlStatement->execute();
+			if(!$sqlResult){
+				error_log("Failed to add city to the database: ". $db->lastDatabaseErrorMessage());
+				$db->exec("ROLLBACK");
+				echo json_encode
+				(
+					[
+						"result" => "failed",
+						"message" => "Could not add city to the database",
+					]
+				);
+				return;
+			}
+			$cityId = $db->lastInsertRowId();
+		}
+		if($cityId < 0){
+			error_log("Failed to select city from the database: ". $db->lastDatabaseErrorMessage());
+			$db->exec("ROLLBACK");
+			echo json_encode
+			(
+				[
+					"result" => "failed",
+					"message" => "Could not select city from the database",
+				]
+			);
+			return;
+		}
+
+		// insert station
+		$uuid = uuid_create(4);
+		$sqlStatement = $db->prepare("INSERT INTO stations (name, city, uuid, ownerMail, location) VALUES(:name, :city, :uuid, :ownerMail, GeomFromText(:location, 4326))");
+		if(!$sqlStatement){
+			$db->exec("ROLLBACK");
+			echo json_encode
+			(
+				[
+					"result" => "failed",
+					"message" => "Could insert station into the database",
+				]
+			);
+			return;
+		}
+		$sqlStatement->bindValue(":name", $json["name"]);
+		$sqlStatement->bindValue(":city", $cityId);
+		$sqlStatement->bindValue(":uuid", $uuid);
+		$sqlStatement->bindValue(":ownerMail",$json["email"]);
+		$sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
+		$sqlResult = $sqlStatement->execute();
+		if(!$sqlResult){
+			error_log("Failed to add station to the database: ". $db->lastDatabaseErrorMessage());
+			$db->query("ROLLBACK");
+			echo json_encode
+			(
+				[
+					"result" => "failed",
+					"message" => "Could not add station to the database",
+				]
+			);
+			return;
+		}
+
+		// finish transaction
+		$db->exec("COMMIT");
+
+		echo json_encode
+		(
+			[
+				"result" => "success",
+				"uuid" => $uuid,
+			]
+		);
 	}
 	catch(Exception $exception){
