1: <?php
2:
3: namespace api;
4:
5: include_once "../../global.inc.php";
6: include_once PATH_DATABASE . "/SpsSqlManager.php";
7: require_once PATH_API . "/Api.php";
8:
9: 10: 11: 12: 13:
14: class SpsApi extends Api {
15:
16: 17: 18: 19:
20: public static $routeParameterAlias = "/alias/:alias";
21:
22: 23: 24: 25:
26: public static $routeParameterDomain = "(/domain/:domain)";
27: 28: 29: 30:
31: public static $routeParameterRange = "(/range/:range)";
32:
33: 34: 35: 36:
37: public static $routeParameterLongitude = "/longitude/:longitude";
38:
39: 40: 41: 42:
43: public static $routeParameterLatitude = "/latitude/:latitude";
44:
45: 46: 47: 48:
49: public static $keyAlias = "alias";
50:
51: 52: 53: 54:
55: public static $keyDomain = "domain";
56:
57: 58: 59: 60:
61: public static $keyPoly = "poly";
62:
63: 64: 65: 66:
67: public static $keyLong = "longitude";
68:
69: 70: 71: 72:
73: public static $keyLat = "latitude";
74:
75: 76: 77: 78:
79: public static $keyRange = "range";
80:
81: 82: 83: 84:
85: private static $range = 0.001;
86:
87: 88: 89: 90:
91: private static $polyStartStr = "GeomFromText('Polygon((";
92:
93: 94: 95: 96:
97: private static $polyEndStr = "))'";
98:
99: 100: 101: 102:
103: private static $maxLat = 180;
104:
105: 106: 107: 108:
109: private static $minLat = -180;
110:
111: 112: 113: 114:
115: private static $maxLong = 180;
116:
117: 118: 119: 120:
121: private static $minLong = -180;
122:
123: 124: 125: 126:
127: public function __construct($headers = array()) {
128: $this->sqlManager = new \database\SpsSqlManager();
129: parent::__construct($headers);
130: }
131:
132: 133: 134:
135: public function __destruct() {
136: parent::__destruct();
137: }
138:
139: 140: 141: 142: 143:
144: public function sendSpsAliasQuery($queryArgs) {
145: $result = $this->sqlManager->sendSpsAliasQuery($queryArgs);
146: return $this->serialManager->arrayToSps($result);
147: }
148:
149: 150: 151: 152: 153:
154: public function sendSpsCoordinateQuery($queryArgs) {
155:
156:
157: if (!array_key_exists(self::$keyLong, $queryArgs) || !array_key_exists(self::$keyLat, $queryArgs)) {
158: return null;
159: }
160: $latitude = $queryArgs[self::$keyLat];
161: $longitude = $queryArgs[self::$keyLong];
162:
163: if (!$this->validLatitude($latitude) || !$this->validLongitude($longitude)) {
164: return null;
165: }
166:
167:
168: if (array_key_exists(self::$keyRange, $queryArgs)) {
169: $newRange = $queryArgs[self::$keyRange];
170: if ($this->validRange($newRange)) {
171: self::$range = $newRange;
172: } else {
173: return;
174: }
175: }
176: $queryArgs[self::$keyPoly] = $this->createPolygon($latitude, $longitude, self::$range);
177:
178:
179: $result = $this->sqlManager->sendSpsCoordinateQuery($queryArgs);
180: return $this->serialManager->arrayToSps($result);
181: }
182:
183: 184: 185: 186: 187:
188: private function validRange($string) {
189: $digit = str_replace(".", "", $string);
190: if (ctype_digit($digit)) {
191: return TRUE;
192: };
193: return FALSE;
194: }
195:
196: 197: 198: 199: 200:
201: private function validLongitude($string) {
202: $digitLessPoint = str_replace(".", "", $string);
203: $digit = str_replace("-", "", $digitLessPoint);
204: if (ctype_digit($digit)) {
205: if ($string <= self::$maxLong && $string >= self::$minLong) {
206: return TRUE;
207: }
208: };
209: return FALSE;
210: }
211:
212: 213: 214: 215: 216:
217: private function validLatitude($string) {
218: $digitLessPoint = str_replace(".", "", $string);
219: $digit = str_replace("-", "", $digitLessPoint);
220: if (ctype_digit($digit)) {
221: if ($string <= self::$maxLat && $string >= self::$minLat) {
222: return TRUE;
223: }
224: };
225: return FALSE;
226: }
227:
228: 229: 230: 231: 232: 233: 234:
235: private function createPolygon($latitude, $longitude, $range) {
236: $minLat = $latitude - $range;
237: $minLong = $longitude - $range;
238: $maxLat = $latitude + $range;
239: $maxLong = $longitude + $range;
240: return self::$polyStartStr . "$minLong $minLat,$minLong $maxLat,$maxLong $maxLat,$maxLong $minLat,$minLong $minLat" . self::$polyEndStr;
241: }
242:
243: }
244:
245: ?>
246: