First commit, working framework and example API endpoints (/ping and /network/whois)
commit
8fafdc80a6
@ -0,0 +1,2 @@
|
||||
/env.php
|
||||
/vendor/
|
@ -0,0 +1,4 @@
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine on
|
||||
RewriteRule ([a-zA-Z0-9]+) index.php?action=$1 [PT]
|
||||
</IfModule>
|
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
$APIS = [
|
||||
"ping" => [
|
||||
"load" => "ping.php",
|
||||
"vars" => [
|
||||
]
|
||||
],
|
||||
"network/whois" => [
|
||||
"load" => "network.whois.php",
|
||||
"vars" => [
|
||||
"domain" => "/\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/",
|
||||
"nocache (optional)" => ""
|
||||
]
|
||||
]
|
||||
];
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This function is called before processing an API request, to determine if the request
|
||||
* should be processed. It should check API keys to make sure the request is allowed.
|
||||
* @return bool true to let the request in, false otherwise
|
||||
*/
|
||||
function authenticaterequest(): bool {
|
||||
return true;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"require": {
|
||||
"catfan/medoo": "^2.0",
|
||||
"io-developer/php-whois": "^4.0"
|
||||
}
|
||||
}
|
@ -0,0 +1,283 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "917e5c74de9bfe78539a7be4bb0588bc",
|
||||
"packages": [
|
||||
{
|
||||
"name": "catfan/medoo",
|
||||
"version": "v2.0.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/catfan/Medoo.git",
|
||||
"reference": "2d0345be643e67f8d43acb282bf9a882e8e9971a"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/catfan/Medoo/zipball/2d0345be643e67f8d43acb282bf9a882e8e9971a",
|
||||
"reference": "2d0345be643e67f8d43acb282bf9a882e8e9971a",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-pdo": "*",
|
||||
"php": ">=7.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-pdo_dblib": "For MSSQL or Sybase database on Linux/UNIX platform",
|
||||
"ext-pdo_mysql": "For MySQL or MariaDB database",
|
||||
"ext-pdo_oci": "For Oracle database",
|
||||
"ext-pdo_pqsql": "For PostgreSQL database",
|
||||
"ext-pdo_sqlite": "For SQLite database",
|
||||
"ext-pdo_sqlsrv": "For MSSQL database on both Window/Liunx platform"
|
||||
},
|
||||
"type": "framework",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Medoo\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Angel Lai",
|
||||
"email": "angel@medoo.in"
|
||||
}
|
||||
],
|
||||
"description": "The lightweight PHP database framework to accelerate development",
|
||||
"homepage": "https://medoo.in",
|
||||
"keywords": [
|
||||
"database",
|
||||
"database library",
|
||||
"lightweight",
|
||||
"mariadb",
|
||||
"mssql",
|
||||
"mysql",
|
||||
"oracle",
|
||||
"php framework",
|
||||
"postgresql",
|
||||
"sql",
|
||||
"sqlite"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/catfan/Medoo/issues",
|
||||
"source": "https://github.com/catfan/Medoo"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://opencollective.com/medoo",
|
||||
"type": "open_collective"
|
||||
}
|
||||
],
|
||||
"time": "2021-05-12T17:55:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "io-developer/php-whois",
|
||||
"version": "4.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/io-developer/php-whois.git",
|
||||
"reference": "21a17d83fef83768c06fd7e1884f6898b0047b5d"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/io-developer/php-whois/zipball/21a17d83fef83768c06fd7e1884f6898b0047b5d",
|
||||
"reference": "21a17d83fef83768c06fd7e1884f6898b0047b5d",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-curl": "*",
|
||||
"ext-json": "*",
|
||||
"ext-mbstring": "*",
|
||||
"php": ">=7.2",
|
||||
"true/punycode": "^2.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Iodev\\": "src/Iodev/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sergey Sedyshev",
|
||||
"email": "i.o.developer@gmail.com",
|
||||
"homepage": "https://github.com/io-developer"
|
||||
}
|
||||
],
|
||||
"description": "PHP WHOIS provides parsed and raw whois lookup of domains and ASN routes. PHP 5.4+ and 7+ compatible ",
|
||||
"homepage": "https://github.com/io-developer/php-whois",
|
||||
"keywords": [
|
||||
"asn",
|
||||
"domain",
|
||||
"info",
|
||||
"lookup",
|
||||
"parser",
|
||||
"php",
|
||||
"query",
|
||||
"routes",
|
||||
"tld",
|
||||
"whois",
|
||||
"црщшы"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/io-developer/php-whois/issues",
|
||||
"source": "https://github.com/io-developer/php-whois/tree/4.0.0"
|
||||
},
|
||||
"time": "2020-03-22T21:36:05+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/polyfill-mbstring",
|
||||
"version": "v1.22.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/polyfill-mbstring.git",
|
||||
"reference": "5232de97ee3b75b0360528dae24e73db49566ab1"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/5232de97ee3b75b0360528dae24e73db49566ab1",
|
||||
"reference": "5232de97ee3b75b0360528dae24e73db49566ab1",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-mbstring": "For best performance"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "1.22-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/polyfill",
|
||||
"url": "https://github.com/symfony/polyfill"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Symfony\\Polyfill\\Mbstring\\": ""
|
||||
},
|
||||
"files": [
|
||||
"bootstrap.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony polyfill for the Mbstring extension",
|
||||
"homepage": "https://symfony.com",
|
||||
"keywords": [
|
||||
"compatibility",
|
||||
"mbstring",
|
||||
"polyfill",
|
||||
"portable",
|
||||
"shim"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.22.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2021-01-22T09:19:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "true/punycode",
|
||||
"version": "v2.1.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/true/php-punycode.git",
|
||||
"reference": "a4d0c11a36dd7f4e7cd7096076cab6d3378a071e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/true/php-punycode/zipball/a4d0c11a36dd7f4e7cd7096076cab6d3378a071e",
|
||||
"reference": "a4d0c11a36dd7f4e7cd7096076cab6d3378a071e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"symfony/polyfill-mbstring": "^1.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.7",
|
||||
"squizlabs/php_codesniffer": "~2.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"TrueBV\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Renan Gonçalves",
|
||||
"email": "renan.saddam@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "A Bootstring encoding of Unicode for Internationalized Domain Names in Applications (IDNA)",
|
||||
"homepage": "https://github.com/true/php-punycode",
|
||||
"keywords": [
|
||||
"idna",
|
||||
"punycode"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/true/php-punycode/issues",
|
||||
"source": "https://github.com/true/php-punycode/tree/master"
|
||||
},
|
||||
"time": "2016-11-16T10:37:54+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.0.0"
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
use Iodev\Whois\Factory;
|
||||
use Iodev\Whois\Exceptions\ConnectionException;
|
||||
use Iodev\Whois\Exceptions\ServerMismatchException;
|
||||
use Iodev\Whois\Exceptions\WhoisException;
|
||||
use Iodev\Whois\Loaders\SocketLoader;
|
||||
use Iodev\Whois\Loaders\MemcachedLoader;
|
||||
|
||||
$json = [];
|
||||
$domain = $VARS["domain"];
|
||||
if (empty($VARS["nocache"])) {
|
||||
$cacheresp = $memcache->get("network.whois.$domain");
|
||||
if ($cacheresp !== false) {
|
||||
exitWithJson(json_decode($cacheresp, true));
|
||||
}
|
||||
}
|
||||
try {
|
||||
if ($memcache->enabled() && empty($VARS["nocache"])) {
|
||||
$loader = new MemcachedLoader(new SocketLoader(), $memcache->getMemcached(), $memcache->getPrefix());
|
||||
$whois = Factory::get()->createWhois($loader);
|
||||
} else {
|
||||
$whois = Factory::get()->createWhois();
|
||||
}
|
||||
$info = $whois->loadDomainInfo($domain);
|
||||
$response = $whois->lookupDomain($domain);
|
||||
if (!$info) {
|
||||
$json = [
|
||||
"domain" => $domain,
|
||||
"registered" => false
|
||||
];
|
||||
} else {
|
||||
$json = [
|
||||
"domain" => $info->domainName,
|
||||
"registered" => true,
|
||||
"created" => $info->creationDate,
|
||||
"expires" => $info->expirationDate,
|
||||
"updated" => $info->updatedDate,
|
||||
"registrar" => $info->registrar,
|
||||
"owner" => $info->owner,
|
||||
"nameservers" => $info->nameServers,
|
||||
"raw" => $response->text
|
||||
];
|
||||
}
|
||||
$json["status"] = "OK";
|
||||
$memcache->set("network.whois.$domain", json_encode($json), 60 * 60 * 4);
|
||||
exitWithJson($json);
|
||||
} catch (ConnectionException $e) {
|
||||
sendJsonResp("Disconnect or connection timeout", "ERROR");
|
||||
} catch (ServerMismatchException $e) {
|
||||
sendJsonResp("TLD server not found in current server hosts", "ERROR");
|
||||
} catch (WhoisException $e) {
|
||||
sendJsonResp("Whois server responded with error '{$e->getMessage()}'", "ERROR");
|
||||
} catch (Exception $e) {
|
||||
Logger::log("network.whois error: " . $e->getMessage());
|
||||
sendJsonResp("An unknown error occurred.", "ERROR");
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
sendJsonResp();
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
$SETTINGS = [
|
||||
"debugmode" => true,
|
||||
"database" => [
|
||||
"database_type" => "mysql",
|
||||
"database_name" => "mysql",
|
||||
"server" => "localhost",
|
||||
"username" => "root",
|
||||
"password" => "",
|
||||
"charset" => "utf8"
|
||||
],
|
||||
"memcached" => [
|
||||
"enable" => true,
|
||||
"server" => "127.0.0.1",
|
||||
"port" => 11211,
|
||||
"prefix" => "apiserver"
|
||||
],
|
||||
// If this is running in a subfolder, set it accordingly or things will be strange
|
||||
// Include trailing slash
|
||||
"urlbase" => "/",
|
||||
];
|
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build and send a simple JSON response.
|
||||
* @param string $msg A message
|
||||
* @param string $status "OK" or "ERROR"
|
||||
* @param array $data More JSON data
|
||||
*/
|
||||
function sendJsonResp(string $msg = null, string $status = "OK", array $data = null) {
|
||||
$resp = [];
|
||||
if (!is_null($data)) {
|
||||
$resp = $data;
|
||||
}
|
||||
if (!is_null($msg)) {
|
||||
$resp["msg"] = $msg;
|
||||
}
|
||||
$resp["status"] = $status;
|
||||
header("Content-Type: application/json");
|
||||
exit(json_encode($resp));
|
||||
}
|
||||
|
||||
function exitWithJson(array $json) {
|
||||
header("Content-Type: application/json");
|
||||
exit(json_encode($json));
|
||||
}
|
||||
|
||||
function checkVars($vars, $or = false) {
|
||||
global $VARS;
|
||||
$ok = [];
|
||||
foreach ($vars as $key => $val) {
|
||||
if (strpos($key, "OR") === 0) {
|
||||
checkVars($vars[$key], true);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only check type of optional variables if they're set, and don't
|
||||
// mark them as bad if they're not set
|
||||
if (strpos($key, " (optional)") !== false) {
|
||||
$key = str_replace(" (optional)", "", $key);
|
||||
if (empty($VARS[$key])) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (empty($VARS[$key])) {
|
||||
$ok[$key] = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If there's no pattern at all
|
||||
if (empty($val)) {
|
||||
$ok[$key] = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strpos($val, "/") === 0) {
|
||||
// regex
|
||||
$ok[$key] = preg_match($val, $VARS[$key]) === 1;
|
||||
} else {
|
||||
$checkmethod = "is_$val";
|
||||
$ok[$key] = !($checkmethod($VARS[$key]) !== true);
|
||||
}
|
||||
}
|
||||
if ($or) {
|
||||
$success = false;
|
||||
$bad = "";
|
||||
foreach ($ok as $k => $v) {
|
||||
if ($v) {
|
||||
$success = true;
|
||||
break;
|
||||
} else {
|
||||
$bad = $k;
|
||||
}
|
||||
}
|
||||
if (!$success) {
|
||||
http_response_code(400);
|
||||
die("400 Bad request: variable $bad is missing or invalid");
|
||||
}
|
||||
} else {
|
||||
foreach ($ok as $key => $bool) {
|
||||
if (!$bool) {
|
||||
http_response_code(400);
|
||||
die("400 Bad request: variable $key is missing or invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
require __DIR__ . '/required.php';
|
||||
require __DIR__ . '/apiconfig.php';
|
||||
require __DIR__ . '/authenticator.php';
|
||||
require __DIR__ . '/functions.php';
|
||||
|
||||
/*
|
||||
* Gather request data in a variety of ways for maximum client flexibility
|
||||
*/
|
||||
|
||||
// URL parameters and POST fields
|
||||
$VARS = $_GET;
|
||||
if ($_SERVER['REQUEST_METHOD'] != "GET") {
|
||||
$VARS = array_merge($VARS, $_POST);
|
||||
}
|
||||
|
||||
// Parse JSON request body
|
||||
$requestbody = file_get_contents('php://input');
|
||||
$requestjson = json_decode($requestbody, TRUE);
|
||||
if (json_last_error() == JSON_ERROR_NONE) {
|
||||
$VARS = array_merge($VARS, $requestjson);
|
||||
}
|
||||
|
||||
// Get requested API endpoint
|
||||
// Strip out urlbase
|
||||
$pos = strpos($_SERVER['REQUEST_URI'], env("urlbase", "/"));
|
||||
if ($pos !== false) {
|
||||
$routestr = substr_replace($_SERVER['REQUEST_URI'], "", $pos, strlen(env("urlbase", "/")));
|
||||
}
|
||||
$route = explode("/", $routestr);
|
||||
if ($route[0] == "") {
|
||||
// The urlbase didn't have a trailing slash, so there's an empty zeroth element
|
||||
array_shift($route);
|
||||
}
|
||||
|
||||
$ENDPOINT = $VARS["endpoint"] ?? "";
|
||||
if (count($route) >= 1) {
|
||||
$ep = $route[0];
|
||||
// Ignore URL parameters if present,
|
||||
// otherwise requests like /endpoint?abc=123 will have the endpoint "endpoint?abc=123"
|
||||
$ep = explode("?", $ep, 2)[0];
|
||||
// Don't allow = in endpoint ID because it's confusing
|
||||
if (strpos($ep, "=") === FALSE) {
|
||||
$ENDPOINT = $ep;
|
||||
}
|
||||
}
|
||||
$urlkeyvaluepairs = [];
|
||||
if (count($route) >= 2 && strpos($route[1], "?") !== 0) {
|
||||
for ($i = 1; $i < count($route); $i++) {
|
||||
if (empty($route[$i])) {
|
||||
continue;
|
||||
}
|
||||
if (strpos($route[$i], "=") === false) {
|
||||
// Allow slashes in endpoint as long as no key/value pairs have
|
||||
// been found yet
|
||||
if (empty($urlkeyvaluepairs)) {
|
||||
$ENDPOINT .= "/" . $route[$i];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$key = explode("=", $route[$i], 2)[0];
|
||||
$val = explode("=", $route[$i], 2)[1];
|
||||
$urlkeyvaluepairs[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
$VARS += $urlkeyvaluepairs;
|
||||
|
||||
// Deny access if authenticator returns false (add your logic to authenticator.php)
|
||||
if (!authenticaterequest()) {
|
||||
http_response_code(401);
|
||||
die("401 Unauthorized: You need to supply valid credentials.");
|
||||
}
|
||||
|
||||
if (empty($ENDPOINT)) {
|
||||
http_response_code(404);
|
||||
die("404 No endpoint specified.");
|
||||
}
|
||||
|
||||
if (!isset($APIS[$ENDPOINT])) {
|
||||
http_response_code(404);
|
||||
die("404 Requested endpoint not known.");
|
||||
}
|
||||
|
||||
$APIENDPOINTCONFIG = $APIS[$ENDPOINT];
|
||||
|
||||
if (!file_exists(__DIR__ . "/endpoints/" . $APIENDPOINTCONFIG["load"])) {
|
||||
http_response_code(404);
|
||||
die("404 Requested endpoint known but not found.");
|
||||
}
|
||||
|
||||
// Check and validate arguments/data passed from client based on configured patterns
|
||||
// This makes sure that all required variables are present and not malformed, reducing
|
||||
// the amount of validation code in each endpoint
|
||||
if (!empty($APIENDPOINTCONFIG["vars"])) {
|
||||
checkVars($APIENDPOINTCONFIG["vars"]);
|
||||
}
|
||||
|
||||
// cleanup variables that won't be needed in endpoints
|
||||
unset($route, $ep, $key, $val, $urlkeyvaluepairs, $pos, $requestjson, $requestbody, $i, $routestr);
|
||||
|
||||
require_once __DIR__ . "/endpoints/" . $APIENDPOINTCONFIG["load"];
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
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]);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
class Logger {
|
||||
|
||||
public static function log(string $message) {
|
||||
error_log($message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
class Memcache {
|
||||
|
||||
public $memcache;
|
||||
private bool $enabled;
|
||||
private string $server;
|
||||
private string $prefix;
|
||||
private int $port;
|
||||
private $dummycache;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param bool $enabled true to enable memcached, false to use non-persistent dummy cache
|
||||
* @param string $server
|
||||
* @param int $port
|
||||
* @param string $prefix Prefix to add to keys to avoid conflicts with other apps on same server
|
||||
*/
|
||||
function __construct(bool $enabled = false, string $server = "127.0.0.1", int $port = 11211, string $prefix = "") {
|
||||
$this->enabled = $enabled;
|
||||
$this->server = $server;
|
||||
$this->port = $port;
|
||||
$this->prefix = $prefix;
|
||||
if ($enabled) {
|
||||
$this->memcache = new Memcached();
|
||||
$this->memcache->addServer($this->server, $this->port);
|
||||
}
|
||||
$this->dummycache = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the memcache is actually setup or if it's a dummy shim.
|
||||
* @return bool true if memcache is enabled, false if only dummy cache enabled
|
||||
*/
|
||||
function enabled(): bool {
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for key.
|
||||
* @param string $key
|
||||
* @return value or false if not found
|
||||
*/
|
||||
function get(string $key) {
|
||||
if ($this->enabled) {
|
||||
$key = $this->prefix . $key;
|
||||
return $this->memcache->get($key);
|
||||
} else {
|
||||
if (!empty($this->dummycache[$key])) {
|
||||
return $this->dummycache[$key];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of $key to $val
|
||||
* @param string $key
|
||||
* @param type $val
|
||||
* @param int $expires number of seconds (max 60*60*24*30) or UNIX timestamp for cache expiry
|
||||
* Default expiration of 0 means no expiration
|
||||
*/
|
||||
function set(string $key, $val, int $expires = 0) {
|
||||
if ($expires < 0) {
|
||||
$expires = 0;
|
||||
}
|
||||
if ($this->enabled) {
|
||||
$key = $this->prefix . $key;
|
||||
$this->memcache->set($key, $val, $expires);
|
||||
} else {
|
||||
$this->dummycache[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cache key prefix string
|
||||
* @return string
|
||||
*/
|
||||
function getPrefix(): string {
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
function getMemcached() {
|
||||
return $this->memcache;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
include.path=${php.global.include.path}
|
||||
php.version=PHP_74
|
||||
source.encoding=UTF-8
|
||||
src.dir=.
|
||||
tags.asp=false
|
||||
tags.short=false
|
||||
web.root=.
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.php.project</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
||||
<name>apis.netsyms.net</name>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This file sets up important stuff like loading settings, database, libraries, and dependencies.
|
||||
* It's intended to be the first thing a script runs before responding to a request.
|
||||
*/
|
||||
ob_start(); // allow sending headers after content
|
||||
// Unicode, solves almost all stupid encoding problems
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
|
||||
// Strip PHP version
|
||||
header('X-Powered-By: PHP');
|
||||
|
||||
// Security
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-XSS-Protection: 1; mode=block');
|
||||
header('X-Frame-Options: "DENY"');
|
||||
header('Referrer-Policy: "no-referrer, strict-origin-when-cross-origin"');
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
|
||||
//
|
||||
// Composer
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
// Settings
|
||||
require __DIR__ . '/env.php';
|
||||
|
||||
// Load in other code
|
||||
$libs = glob(__DIR__ . "/lib/*.lib.php");
|
||||
foreach ($libs as $lib) {
|
||||
require_once $lib;
|
||||
}
|
||||
unset($libs, $lib);
|
||||
|
||||
// Set timezone
|
||||
if (envhas("timezone")) {
|
||||
date_default_timezone_set(env("timezone", "UTC"));
|
||||
}
|
||||
|
||||
// Initialize database driver
|
||||
use Medoo\Medoo;
|
||||
$database;
|
||||
try {
|
||||
$database = new Medoo(env("database", []));
|
||||
} catch (Exception $ex) {
|
||||
http_response_code(500);
|
||||
Logger::log("Database error: $ex");
|
||||
}
|
||||
|
||||
$memcacheconfig = env("memcached", [
|
||||
"enable" => false,
|
||||
"server" => "127.0.0.1",
|
||||
"port" => 11211,
|
||||
"prefix" => "apiserver"
|
||||
]);
|
||||
$memcache = new Memcache($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');
|
||||
}
|
Loading…
Reference in New Issue