This commit is contained in:
2021-06-28 23:15:13 -04:00
parent 0061a3d920
commit f4a8db56ff
93 changed files with 2422 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
class API {
use JSON;
public function __invoke(Request $request, Response $response): Response {
$output = [
'version' => '1.0.0',
'routes' => [
'/coins' => [
'/' => 'List all coins',
'/add' => 'Add coin'
],
'/coin/{coin_id}' => [
'/' => 'Show coin information',
'/edit' => 'Edit coin',
'/delete' => 'Delete coin'
],
'/locations' => [
'/' => 'List all locations',
'/add' => 'Add location'
],
'/location/{location_id}' => [
'/' => 'Show location information',
'/edit' => 'Edit location',
'/delete' => 'Delete location'
],
'/wallets' => [
'/' => 'List all wallets',
'/add' => 'Add wallet'
],
'/wallet/{wallet_id}' => [
'/' => 'Show wallet information',
'/edit' => 'Edit wallet',
'/delete' => 'Delete wallet'
]
]
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Crypto\Coin;
class Coins {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$coins = $factory->find(Coin::class)->array();
usort($coins, function($a, $b) {
return strcmp($a['code'], $b['code']);
});
$output = compact('coins');
return $this->withJson($response, $output);
}
public function show(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
$coin = $factory->find(Coin::class)->one($coin_id);
if (!$coin) {
return $this->withJson($response, ['coin' => null]);
}
$output = ['coin' => $coin->toArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = $request->getBody()->getContents();
$post = json_decode($post);
$coin = Coin::add($factory, $post);
$status = false;
if ($coin->isNew()) {
$status = $coin->save();
}
$output = [
'input' => $post,
'coin' => $coin->toArray(),
'created' => $status
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
$coin = $factory->find(Coin::class)->one($coin_id);
if (!$coin) {
return $this->withJson($response, ['coin' => null]);
}
$post = json_decode($request->getBody()->getContents());
$edited = $coin->edit($post);
$output = ['input' => $post, 'coin' => $coin->toArray(), 'edited' => $edited];
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $coin_id): Response {
$coin = $factory->find(Coin::class)->one($coin_id);
if (!$coin) {
return $this->withJson($response, ['coin' => null, 'deleted' => false]);
}
$output = [
'coin' => $coin->toArray()
];
$status = $coin->delete();
$output['deleted'] = $status;
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Crypto\Location;
class Locations {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$locations = $factory->find(Location::class)->array();
$output = compact('locations');
return $this->withJson($response, $output);
}
public function show(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null]);
}
$output = ['location' => $location->asArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = json_decode($request->getBody()->getContents());
$fields = [
'name' => 'name',
'description' => 'description'
];
$data = array_combine($fields, array_merge(array_intersect_key((array) $post, $fields), array_fill_keys(array_keys(array_diff_key($fields, (array) $post)), null)));
$location = $factory->find(Location::class)->where([
['name', $data['name']]
])->one();
$status = true;
if (!$location) {
$location = $factory->create(Location::class, $data);
$status = $location->save();
}
$output = [
'information_provided' => $post,
'used_data' => $data,
'location' => $location->asArray(),
'saved' => $status
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null]);
}
$post = json_decode($request->getBody()->getContents());
$output = compact('location');
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $location_id): Response {
$location = $factory->find(Location::class)->one($location_id);
if (!$location) {
return $this->withJson($response, ['location' => null, 'deleted' => false]);
}
$output = [
'location' => $location->asArray()
];
$status = $location->delete();
$output['deleted'] = $status;
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
use ProVM\Crypto\Common\Service\Update as Updater;
class Update {
use JSON;
public function __invoke(Request $request, Response $response, Updater $updater) {
$result = $updater->run();
$output = [
'result' => $result
];
return $this->withJson($response, $output);
}
public function register(Request $request, Response $response, Updater $updater, $coin_id, $type) {
$result = $updater->register($coin_id, $type);
$output = [
'input' => ['coin_id' => $coin_id, 'type' => $type],
'result' => $result
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace ProVM\Crypto\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Define\Controller\JSON;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Crypto\Wallet;
class Wallets {
use JSON;
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
$wallets = $factory->find(Wallet::class)->array();
$output = compact('wallets');
return $this->withJson($response, $output);
}
public function show(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null]);
}
$output = ['wallet' => $wallet->asArray()];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, ModelFactory $factory): Response {
$post = json_decode($request->getBody()->getContents());
$fields = [
'name' => 'name',
'location' => 'location_id',
'address' => 'public_address'
];
$data = array_combine($fields, array_merge(array_intersect_key((array) $post, $fields), array_fill_keys(array_keys(array_diff_key($fields, (array) $post)), null)));
$wallet = $factory->find(Wallet::class)->where([
['name', $data['name']]
])->one();
$status = true;
if (!$wallet) {
$wallet = $factory->create(Wallet::class, $data);
$status = $wallet->save();
}
$output = [
'information_provided' => $post,
'used_data' => $data,
'wallet' => $wallet->asArray(),
'saved' => $status
];
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null]);
}
$post = json_decode($request->getBody()->getContents());
$output = compact('wallet');
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, ModelFactory $factory, $wallet_id): Response {
$wallet = $factory->find(Wallet::class)->one($wallet_id);
if (!$wallet) {
return $this->withJson($response, ['wallet' => null, 'deleted' => false]);
}
$output = [
'wallet' => $wallet->asArray()
];
$status = $wallet->delete();
$output['deleted'] = $status;
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace ProVM\Crypto\Common\Factory;
use ProVM\Common\Factory\Model as BaseFactory;
class Model extends BaseFactory {
}

View File

@ -0,0 +1,38 @@
<?php
namespace ProVM\Crypto\Common\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Slim\Routing\RouteContext;
final class CORS implements MiddlewareInterface
{
/**
* Invoke middleware.
*
* @param ServerRequestInterface $request The request
* @param RequestHandlerInterface $handler The handler
*
* @return ResponseInterface The response
*/
public function process(Request $request, Handler $handler): Response {
$routeContext = RouteContext::fromRequest($request);
$routingResults = $routeContext->getRoutingResults();
$methods = $routingResults->getAllowedMethods();
$requestHeaders = $request->getHeaderLine('Access-Control-Request-Headers');
$response = $handler->handle($request);
/*$response = $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Methods', implode(', ', $methods))
->withHeader('Access-Control-Allow-Headers', $requestHeaders ?: '*');*/
// Optional: Allow Ajax CORS requests with Authorization header
$response = $response->withHeader('Access-Control-Allow-Credentials', 'true');
return $response;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace ProVM\Crypto\Common\Service;
use Carbon\Carbon;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Crypt\User;
use ProVM\Crypt\Login;
class Auth {
protected $login_time;
public function __construct(int $login_time) {
$this->login_time = $login_time;
}
protected $factory;
public function setFactory(ModelFactory $factory) {
$this->factory = $factory;
}
protected function createToken() {
return password_hash(random_bytes(100), \PASSWORD_BCRYPT);
}
public function login(string $username, string $password) {
$user = $this->factory->find(User::class)->where([['name', $username]])->one();
if (!$user) {
return false;
}
if (!password_verify($password, $user->password)) {
return false;
}
$now = Carbon::now();
$login = $this->factory->find(Login::class)->where([['user_id', $user->id], ['date_time', $now->copy()->subSeconds($this->login_time), '>=']])->one();
if (!$login) {
$token = $this->createToken();
$data = [
'user_id' => $user->id,
'token' => $token,
'date_time' => $now->format('Y-m-d H:i:s')
];
$login = $this->factory->create(Login::class, $data);
} else {
$login->date($now);
}
$login->save();
return $token;
}
public function isLoggedIn($token) {
$login = $this->factory->find(Login::class)->where([['token', $token]])->one();
return $login->user();
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace ProVM\Crypto\Common\Service;
use Carbon\Carbon;
use ProVM\Common\Factory\Model as Factory;
use ProVM\Crypto\Coin;
class Update {
protected $factory;
protected $execs;
public function __construct(Factory $factory, array $executables) {
$this->factory = $factory;
$this->execs = $executables;
$this->load();
}
public function load() {
$this->coins = [[], []];
$results = \ORM::for_table('coin_registers')->find_many();
foreach ($results as $result) {
$this->coins[$result->type] []= $result->coin_id;
}
}
protected $coins;
public function register(int $coin_id, int $type = 0) {
/*if (array_search($coin_id, $this->coins[$type]) !== false) {
return;
}*/
$this->coins[$type] []= $coin_id;
$this->getHistorical($coin_id, $type);
$check = \ORM::for_table('coin_registers')->where('coin_id', $coin_id)->find_one();
if (!$check) {
\ORM::raw_execute("INSERT INTO coin_registers (coin_id, type) VALUES (?, ?)", [$coin_id, $type]);
}
return true;
}
protected function getHistorical(int $coin_id, int $type) {
$coin = $this->factory->find(Coin::class)->one($coin_id);
$f = Carbon::now();
$exe = [$this->execs[$type]];
switch ($type) {
case 0:
$exe []= '-i ' . $coin->identifier;
$exe []= '-c usd,clp';
$exe []= 'hist -hi';
$exe []= '-f ' . $f->copy()->subYears(10)->timestamp;
$exe []= '-t ' . $f->timestamp();
break;
case 1:
$exe []= '-i ' . $coin->identifier;
$exe []= 'hist -hi';
$exe []= '-s ' . $f->copy()->subYears(10)->year;
break;
}
!d(implode(' ', $exe));
$output = shell_exec(implode(' ', $exe));
!d($output);
}
public function run() {
}
}