139 lines
3.8 KiB
PHP
139 lines
3.8 KiB
PHP
|
<?php
|
||
|
|
||
|
/*
|
||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
|
||
|
*/
|
||
|
|
||
|
$SETTINGS = [
|
||
|
"debugmode" => true,
|
||
|
"require_database" => true,
|
||
|
"result_limit" => 20,
|
||
|
"database" => [
|
||
|
"database_type" => "mysql",
|
||
|
"database_name" => "address_autocomplete",
|
||
|
"server" => "localhost",
|
||
|
"username" => "root",
|
||
|
"password" => "",
|
||
|
"charset" => "utf8"
|
||
|
],
|
||
|
"memcached" => [
|
||
|
"enable" => false,
|
||
|
"server" => "127.0.0.1",
|
||
|
"port" => 11211,
|
||
|
"prefix" => "aa"
|
||
|
]
|
||
|
];
|
||
|
|
||
|
function env(string $key, $defaultvalue = null) {
|
||
|
global $SETTINGS;
|
||
|
if (!empty($SETTINGS[$key])) {
|
||
|
return $SETTINGS[$key];
|
||
|
}
|
||
|
return $defaultvalue;
|
||
|
}
|
||
|
|
||
|
function envhas(string $key): bool {
|
||
|
global $SETTINGS;
|
||
|
return !empty($SETTINGS[$key]);
|
||
|
}
|
||
|
|
||
|
ob_start(); // allow sending headers after content
|
||
|
// Unicode, solves almost all stupid encoding problems
|
||
|
header('Content-Type: application/json; charset=utf-8');
|
||
|
header('X-Powered-By: PHP');
|
||
|
header("Access-Control-Allow-Origin: *");
|
||
|
|
||
|
//
|
||
|
// Composer
|
||
|
require __DIR__ . '/vendor/autoload.php';
|
||
|
|
||
|
use Medoo\Medoo;
|
||
|
|
||
|
$database;
|
||
|
try {
|
||
|
$database = new Medoo(env("database", []));
|
||
|
} catch (Exception $ex) {
|
||
|
if (env("require_database")) {
|
||
|
http_response_code(500);
|
||
|
exit('{"status": "ERROR", "msg": "Database error."}');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$memcacheconfig = env("memcached", [
|
||
|
"enable" => false,
|
||
|
"server" => "127.0.0.1",
|
||
|
"port" => 11211,
|
||
|
"prefix" => ""
|
||
|
]);
|
||
|
$memcache = new MemcacheDriver($memcacheconfig["enable"], $memcacheconfig["server"], $memcacheconfig["port"], $memcacheconfig["prefix"]);
|
||
|
unset($memcacheconfig);
|
||
|
|
||
|
if (env("debugmode", false)) {
|
||
|
error_reporting(E_ALL);
|
||
|
ini_set('display_errors', 'On');
|
||
|
} else {
|
||
|
error_reporting(0);
|
||
|
ini_set('display_errors', 'Off');
|
||
|
}
|
||
|
|
||
|
if (empty($_REQUEST["address"])) {
|
||
|
exit(json_encode(["status" => "ERROR", "message" => "Empty address"]));
|
||
|
}
|
||
|
|
||
|
if (empty($_REQUEST["zip"])) {
|
||
|
exit(json_encode(["status" => "ERROR", "message" => "Empty ZIP"]));
|
||
|
}
|
||
|
|
||
|
if (preg_match("/[0-9]{5}-?[0-9]{4}", $_REQUEST["zip"])) {
|
||
|
$_REQUEST["zip"] = substr($_REQUEST["zip"], 0, 5);
|
||
|
}
|
||
|
|
||
|
$addressparts = explode(" ", $_REQUEST["address"], 2);
|
||
|
|
||
|
if (count($addressparts) < 1 || !is_numeric($addressparts[0])) {
|
||
|
exit(json_encode(["status" => "OK", "results" => []]));
|
||
|
}
|
||
|
|
||
|
if (count($addressparts) == 1) {
|
||
|
$results = $database->select("addresses",
|
||
|
["number", "street", "city", "state", "zipcode (zip)", "plus4"],
|
||
|
[
|
||
|
"AND" => ["zipcode[~]" => $_REQUEST["zip"] . '%', "number[~]" => $addressparts[0] . "%"],
|
||
|
"LIMIT" => env("result_limit", 20),
|
||
|
"ORDER" => ["updated"]
|
||
|
]
|
||
|
);
|
||
|
} else {
|
||
|
$results = $database->select("addresses",
|
||
|
["number", "street", "city", "state", "zipcode (zip)", "plus4"],
|
||
|
[
|
||
|
"AND" => ["zipcode[~]" => $_REQUEST["zip"] . '%', "number" => $addressparts[0], "street[~]" => $addressparts[1] . "%"],
|
||
|
"LIMIT" => env("result_limit", 20),
|
||
|
"ORDER" => ["updated"]
|
||
|
]
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
for ($i = 0; $i < count($results); $i++) {
|
||
|
$results[$i]["address"] = $results[$i]["number"] . " " . $results[$i]["street"];
|
||
|
}
|
||
|
|
||
|
// Remove duplicates
|
||
|
$finalresults = [];
|
||
|
for ($i = 0; $i < count($results); $i++) {
|
||
|
$infinal = false;
|
||
|
for ($j = 0; $j < count($finalresults); $j++) {
|
||
|
if ($results[$i]["address"] == $finalresults[$j]["address"]) {
|
||
|
$infinal = true;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
if (!$infinal) {
|
||
|
$finalresults[] = $results[$i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
exit(json_encode(["status" => "OK", "results" => $finalresults]));
|