diff --git a/geoapi/api/Api.php b/geoapi/api/Api.php index efcceed..0bb32b1 100644 --- a/geoapi/api/Api.php +++ b/geoapi/api/Api.php @@ -24,9 +24,6 @@ abstract class Api extends \Slim\Slim { * Default-Constructor */ public function __construct() { - if (!$this->sqlManager) { - $this->sqlManager = new \database\SQLManager(); - } $this->connect(); parent::__construct(); } diff --git a/geoapi/api/SpsApi.php b/geoapi/api/SpsApi.php index 70890f8..0a8fb94 100644 --- a/geoapi/api/SpsApi.php +++ b/geoapi/api/SpsApi.php @@ -4,6 +4,7 @@ namespace api; include_once "../../global.inc.php"; include_once PATH_DATABASE . "/SpsSqlManager.php"; +include_once PATH_UTILITTY . "/XmlManager.php"; require_once PATH_API . "/Api.php"; /** @@ -13,12 +14,85 @@ require_once PATH_API . "/Api.php"; */ class SpsApi extends Api { + /** + * Keyword for alias arguments + * @var string + */ + public static $keyAlias = "alias"; + + /** + * Keyword for domain arguments + * @var string + */ + public static $keyDomain = "domain"; + + /** + * Keyword for polygon arguments + * @var string + */ + public static $keyPoly = "poly"; + + /** + * Keyword for longitude arguments + * @var string + */ + public static $keyLong = "longitude"; + + /** + * Keyword for latitude arguments + * @var string + */ + public static $keyLat = "latitude"; + + /* + * Varible for the range of the searchpolygon + * @var float + */ + private $range = 1; + + /* + * Varible for the fist chars of the string for a Polygon + * @var string + */ + private $polyStartStr = "GeomFromText('Polygon(("; + + /* + * Varible for the last chars of the string for a Polygon + * @var string + */ + private $polyEndStr = "))'"; + + /** + * maximum value of latitude + * @var float + */ + private $maxLat = 180; + + /** + * minimum value of latitude + * @var float + */ + private $minLat = -180; + + /** + * maximum value of longitude + * @var float + */ + private $maxLong = 180; + + /** + * minimum value of longitude + * @var float + */ + private $minLong = -180; + /** * Default-Constructor */ public function __construct() { $this->sqlManager = new \database\SpsSqlManager(); parent::__construct(); + $this->contentType("Content-type: application/xml;charset=utf-8"); } /** @@ -29,12 +103,81 @@ class SpsApi extends Api { } /** - * Method stert a sps-query + * Method start a sps-query(alias) * @param array $queryArgs - * @return array[num] [assoc] + * @return querry result as xml */ - public function sendSpsQuery($queryArgs = array()) { - return $this->sqlManager->sendSpsQuery($queryArgs); + public function sendSpsAliasQuery($queryArgs = array()) { + $result = $this->sqlManager->sendSpsAliasQuery($queryArgs); + return \utiliy\XmlManager::arrayToSpsXml($result); + } + + /** + * Method start a sps-query(Coordinates) + * @param array $queryArgs + * @return querry result as xml + */ + public function sendSpsCoordinateQuery($queryArgs = array()) { + if (!array_key_exists(SpsApi::$keyLong, $queryArgs) || !array_key_exists(SpsApi::$keyLat, $queryArgs)) { + return null; + } + $latitude = $queryArgs[SpsApi::$keyLat]; + $longitude = $queryArgs[SpsApi::$keyLong]; + + if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) { + return null; + } + + $queryArgs[SpsApi::$keyPoly] = $this->createPolygon($latitude, $longitude, $this->range); + $result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs); + return \utiliy\XmlManager::arrayToSpsXml($result); + } + + /** + * Method check if a string is a valid Longitude + * @param string $string + * @return bool + */ + private function validLongitude($string) { + $digitLessPoint = str_replace(".", "", $string); + $digit = str_replace("-", "", $digitLessPoint); + if (ctype_digit($digit)) { + if ($string <= $this->maxLong && $string >= $this->minLong) { + return TRUE; + } + }; + return FALSE; + } + + /** + * Method check if a string is a valid Latitude + * @param strinf $string + * @return bool + */ + private function validLatitude($string) { + $digitLessPoint = str_replace(".", "", $string); + $digit = str_replace("-", "", $digitLessPoint); + if (ctype_digit($digit)) { + if ($string <= $this->maxLat && $string >= $this->minLat) { + return TRUE; + } + }; + return FALSE; + } + + /** + * Method create a Polygon + * @param float $latitude + * @param float $longitude + * @param float $range + * @return string + */ + private function createPolygon($latitude, $longitude, $range) { + $minLat = $latitude - $range; + $minLong = $longitude - $range; + $maxLat = $latitude + $range; + $maxLong = $longitude + $range; + return $this->polyStartStr . "$minLat $minLong,$minLat $maxLong,$maxLat $maxLong,$maxLat $minLong,$minLat $minLong" . $this->polyEndStr; } } diff --git a/geoapi/config/config.db.php b/geoapi/config/config.db.php index 5da580f..6dc498f 100644 --- a/geoapi/config/config.db.php +++ b/geoapi/config/config.db.php @@ -9,21 +9,25 @@ class DBConfig { /** * Address of the sqlserver + * @var string */ public static $sqlServer = "localhost"; /** * name of the database + * @var string */ public static $sqlDBName = "geoDB"; /** * user of the database + * @var string */ public static $sqlDBUser = "geoDB"; /** * password of the database + * @var string */ public static $sqlDBUserPW = "VZrSAYvmcrntQ97b"; diff --git a/geoapi/database/SpsSqlManager.php b/geoapi/database/SpsSqlManager.php index f7dc7d2..1019aa3 100644 --- a/geoapi/database/SpsSqlManager.php +++ b/geoapi/database/SpsSqlManager.php @@ -3,8 +3,8 @@ namespace database; include_once "../../global.inc.php"; -include_once PATH_DATABASE . "/SpsSqlManager.php"; -include_once PATH_DATABASE . "/SqlManager.php"; +include_once PATH_UTILITTY . "/StringManager.php"; +require_once PATH_DATABASE . "/SqlManager.php"; /** * Description of SpsSqlManager @@ -29,14 +29,199 @@ class SpsSqlManager extends SQLManager { } /** - * Methods send an query for the sps-service + * Fieldname of the placeID + * @var string + */ + public static $placeId = "id"; + + /** + * Fieldname of the parendId + * @var string + */ + public static $parentId = "parent"; + + /** + * String for the select part of the query + * @var string + */ + private $selectTerm = "SELECT DISTINCT id, parent FROM sps WHERE "; + + /** + * String for the alias part of the query + * @var string + */ + private $aliasTerm = "alias = "; + + /** + * String for the did part of the query + * @var string + */ + private $domainTerm = "did = "; + + /** + * String for the dNamet part of the query + * @var string + */ + private $domainNameTerm = "dName = "; + + /** + * first part of intersect-function + * @var string + */ + private $interSectTermStart = "Intersects("; + + /** + * last part of intersect-function + * @var string + */ + private $interSectTermEnd = "),plan)"; + + /** + * first part of GeomFromText('Polygon-function + * @var string + */ + private $polyStartStr = "GeomFromText('Polygon(("; + + /** + * last part of GeomFromText('Polygon-function + * @var string + */ + private $polyEndStr = "))'"; + + /** + * maximium length of the value-string for an aliasname + * @var int + */ + private $aliasMaxLenght = 32; + + /** + * maximium length of the value-string for a domainname + * @var int + */ + private $domainMaxLenght = 32; + + /** + * Methods send an query for the sps-service depends of alias * @param array $queryArgs * @return array [num][assoc] */ - public function sendSpsQuery($queryArgs = array()) { - // TODO Input validitaion - $alias = $queryArgs["alias"]; - return $this->query("SELECT * FROM sps Where alias = \"$alias\""); + public function sendSpsAliasQuery($queryArgs = array()) { + + // check arguments of the query + if (array_key_exists(\api\SpsApi::$keyAlias, $queryArgs)) { + $alias = $queryArgs[\api\SpsApi::$keyAlias]; + } else { + return null; + } + + $domain = null; + if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) { + $domain = $queryArgs[\api\SpsApi::$keyDomain]; + } + + // build query string + $query = $this->selectTerm; + if ($this->validAliasString($alias)) { + $query .= $this->aliasTerm . $this->quoteTerm . $alias . $this->quoteTerm . $this->addDomainTerm($domain); + } else { + return null; + } + + // send query + return $this->query($query); + } + + /** + * Methods send an query for the sps-service depends of coordinates + * @param array $queryArgs + * @return array [num][assoc] + */ + public function sendSpsCoordinateQuery($queryArgs = array()) { + + // check arguments of the query + if (array_key_exists(\api\SpsApi::$keyPoly, $queryArgs)) { + $poly = $queryArgs[\api\SpsApi::$keyPoly]; + } else { + return null; + } + + $domain = null; + if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) { + $domain = $queryArgs[\api\SpsApi::$keyDomain]; + } + + // build query string + if ($this->validPolyString($poly)) { + $query = $this->selectTerm . $this->interSectTermStart . $poly . $this->interSectTermEnd . $this->addDomainTerm($domain); + } else { + return null; + } + + // send query + return $this->query($query); + } + + /** + * Method create the correct domain part depends of $domain. If it is a number => did + * otherwise => dName + * @param string $domain + * @return string + */ + private function addDomainTerm($domain) { + $result = null; + if ($domain != null && $this->validDomainString($domain)) { + if ($this->isDid($domain)) { + $result .= $this->andTerm . $this->domainTerm . $this->quoteTerm . $domain . $this->quoteTerm; + } else { + $result .= $this->andTerm . $this->domainNameTerm . $this->quoteTerm . $domain . $this->quoteTerm; + } + } + return $result; + } + + /** + * Method check if the input value for the alias is valid + * @param string $alias + * @return boolean + */ + private function validAliasString($alias) { + if ($this->validString($alias) && ctype_alnum($alias) && strlen($alias) <= $this->aliasMaxLenght) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the input value for the alias is valid + * @param string $domain + * @return boolean + */ + private function validDomainString($domain) { + if ($this->validString($domain) && ctype_alnum($domain) && strlen($domain) <= $this->domainMaxLenght) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the input value for the polygon is valid + * @param string $poly + * @return boolean + */ + private function validPolyString($poly) { + if ($this->validString($poly) && \utiliy\StringManager::startsWith($poly, $this->polyStartStr) && \utiliy\StringManager::endsWith($poly, $this->polyEndStr)) { + return TRUE; + } + return FALSE; + } + + /** + * Method check if the ntring is a did + * @param string $string + * @return boolean + */ + private function isDid($string) { + return ctype_digit($string); } } diff --git a/geoapi/database/SqlManager.php b/geoapi/database/SqlManager.php index 03f84be..a1716b0 100644 --- a/geoapi/database/SqlManager.php +++ b/geoapi/database/SqlManager.php @@ -42,11 +42,22 @@ abstract class SqlManager { */ private $link; + /** + * String for a and operrator + * @var string + */ + protected $andTerm = " and "; + + /** + * String for quotes in a query + * @var string + */ + protected $quoteTerm = "\""; + /** * Default-Constructor */ public function __construct() { - $this->serverAddress = \config\DBConfig::$sqlServer; $this->dbName = \config\DBConfig::$sqlDBName; $this->userName = \config\DBConfig::$sqlDBUser; @@ -108,6 +119,18 @@ abstract class SqlManager { return $result; } + /** + * Method if the string is not a empty String (not only spaces and controlls) + * @param string $string + * @return boolean + */ + protected function validString($string) { + if (!ctype_space($string) && !ctype_cntrl($string)) { + return TRUE; + } + return FALSE; + } + } ?> diff --git a/geoapi/global.inc.php b/geoapi/global.inc.php index 38d4a6a..4fc0ddc 100644 --- a/geoapi/global.inc.php +++ b/geoapi/global.inc.php @@ -29,4 +29,9 @@ define("PATH_DATABASE", PATH_ROOT . "/database"); * Path to the config */ define("PATH_CONFIG", PATH_ROOT . "/config"); + +/** + * Path to the utility + */ +define("PATH_UTILITTY", PATH_ROOT . "/utility"); ?> diff --git a/geoapi/service/sps/index.php b/geoapi/service/sps/index.php index 6f18ae0..21c139f 100644 --- a/geoapi/service/sps/index.php +++ b/geoapi/service/sps/index.php @@ -1,15 +1,39 @@ get('/alias/:alias', function ($alias) use ($app) { - $args =array(); - $args["alias"] = $alias; - print_r($app->sendSpsQuery($args)); -}); + $args = array(); + $args[\api\SpsApi::$keyAlias] = $alias; + echo $app->sendSpsAliasQuery($args); + }); + +$app->get('/alias/:alias/domain/:domain', function ($alias, $domain) use ($app) { + $args = array(); + $args[\api\SpsApi::$keyAlias] = $alias; + $args[\api\SpsApi::$keyDomain] = $domain; + echo $app->sendSpsAliasQuery($args); + }); + +$app->get('/longitude/:longitude/latitude/:latitude', function ($longitude, $latitude) use ($app) { + $args = array(); + $args[\api\SpsApi::$keyLong] = $longitude; + $args[\api\SpsApi::$keyLat] = $latitude; + echo ($app->sendSpsCoordinateQuery($args)); + }); + +$app->get('/longitude/:longitude/latitude/:latitude/domain/:domain', function ($longitude, $latitude, $domain) use ($app) { + $args = array(); + $args[\api\SpsApi::$keyLong] = $longitude; + $args[\api\SpsApi::$keyLat] = $latitude; + $args[\api\SpsApi::$keyDomain] = $domain; + echo ($app->sendSpsCoordinateQuery($args)); + }); $app->run(); ?> diff --git a/geoapi/utility/StringManager.php b/geoapi/utility/StringManager.php new file mode 100644 index 0000000..d83cdfe --- /dev/null +++ b/geoapi/utility/StringManager.php @@ -0,0 +1,35 @@ + diff --git a/geoapi/utility/XmlManager.php b/geoapi/utility/XmlManager.php new file mode 100644 index 0000000..eca7472 --- /dev/null +++ b/geoapi/utility/XmlManager.php @@ -0,0 +1,56 @@ +"; + + /** + * Name for the place element + * @var string + */ + public static $placeElementName = "place"; + + /** + * Name for the placeid attribute + * @var string + */ + public static $placeIdAttrName = "id"; + + /** + * Name for the parent attribute + * @var string + */ + public static $parentIdAttrName = "parentId"; + + /** + * Method convert an array to a response xml for the sps service + * @param array[num][assoc] $result + * @return xml-string + */ + public static function arrayToSpsXml($result = array()) { + $xml = new \SimpleXMLElement(XmlManager::$defaultXmlDoc); + + foreach ($result as $row) { + $place = $xml->addChild(XmlManager::$placeElementName); + $place->addAttribute(XmlManager::$placeIdAttrName, $row[\database\SpsSqlManager::$placeId]); + $place->addAttribute(XmlManager::$parentIdAttrName, $row[\database\SpsSqlManager::$parentId]); + } + return $xml->asXML(); + } + +} + +?>