mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
Add php-cs-fixer
This commit is contained in:
parent
e00e6ece5f
commit
309ae17036
54 changed files with 4900 additions and 4106 deletions
|
|
@ -6,69 +6,69 @@ use Demostf\API\Providers\UserProvider;
|
|||
use flight\Engine;
|
||||
|
||||
class AuthController extends BaseController {
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
|
||||
/**
|
||||
* @var AuthProvider
|
||||
*/
|
||||
private $authProvider;
|
||||
/**
|
||||
* @var AuthProvider
|
||||
*/
|
||||
private $authProvider;
|
||||
|
||||
/** @var string */
|
||||
private $host;
|
||||
/** @var string */
|
||||
private $host;
|
||||
|
||||
private $apiRoot;
|
||||
private $apiRoot;
|
||||
|
||||
public function __construct(UserProvider $userProvider, AuthProvider $authProvider, string $host, string $apiRoot) {
|
||||
$this->userProvider = $userProvider;
|
||||
$this->authProvider = $authProvider;
|
||||
$this->host = $host;
|
||||
$this->apiRoot = $apiRoot;
|
||||
}
|
||||
public function __construct(UserProvider $userProvider, AuthProvider $authProvider, string $host, string $apiRoot) {
|
||||
$this->userProvider = $userProvider;
|
||||
$this->authProvider = $authProvider;
|
||||
$this->host = $host;
|
||||
$this->apiRoot = $apiRoot;
|
||||
}
|
||||
|
||||
public function token() {
|
||||
echo $this->authProvider->generateToken();
|
||||
}
|
||||
public function token() {
|
||||
echo $this->authProvider->generateToken();
|
||||
}
|
||||
|
||||
public function get($token) {
|
||||
$userData = $this->authProvider->getUser($token);
|
||||
\Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => $userData['steamid'],
|
||||
'name' => $userData['name'],
|
||||
'key' => $userData['key']
|
||||
]);
|
||||
}
|
||||
public function get($token) {
|
||||
$userData = $this->authProvider->getUser($token);
|
||||
\Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => $userData['steamid'],
|
||||
'name' => $userData['name'],
|
||||
'key' => $userData['key']
|
||||
]);
|
||||
}
|
||||
|
||||
public function login($token) {
|
||||
$_SESSION['return'] = $this->query('return', 'https://' . $this->host);
|
||||
$steam = new SteamLogin();
|
||||
$url = $steam->url($this->apiRoot . '/auth/handle/' . urlencode($token), $this->apiRoot);
|
||||
\Flight::redirect(str_replace('&', '&', $url)); // headers make no sense
|
||||
}
|
||||
public function login($token) {
|
||||
$_SESSION['return'] = $this->query('return', 'https://' . $this->host);
|
||||
$steam = new SteamLogin();
|
||||
$url = $steam->url($this->apiRoot . '/auth/handle/' . urlencode($token), $this->apiRoot);
|
||||
\Flight::redirect(str_replace('&', '&', $url)); // headers make no sense
|
||||
}
|
||||
|
||||
public function logout($token) {
|
||||
$this->authProvider->logout($token);
|
||||
\Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => null,
|
||||
'name' => null,
|
||||
'key' => null
|
||||
]);
|
||||
}
|
||||
public function logout($token) {
|
||||
$this->authProvider->logout($token);
|
||||
\Flight::json([
|
||||
'token' => $token,
|
||||
'steamid' => null,
|
||||
'name' => null,
|
||||
'key' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function handle($token) {
|
||||
$return = $_SESSION['return'] ?? 'https://' . $this->host;
|
||||
unset($_SESSION['return']);
|
||||
$steam = new SteamLogin();
|
||||
$steamId = $steam->validate();
|
||||
if ($steamId) {
|
||||
$steamIdObject = new \SteamId($steamId);
|
||||
$key = $this->userProvider->store($steamIdObject);
|
||||
$this->authProvider->setUser($token, $steamIdObject, $key);
|
||||
}
|
||||
\Flight::redirect($return);
|
||||
}
|
||||
public function handle($token) {
|
||||
$return = $_SESSION['return'] ?? 'https://' . $this->host;
|
||||
unset($_SESSION['return']);
|
||||
$steam = new SteamLogin();
|
||||
$steamId = $steam->validate();
|
||||
if ($steamId) {
|
||||
$steamIdObject = new \SteamId($steamId);
|
||||
$key = $this->userProvider->store($steamIdObject);
|
||||
$this->authProvider->setUser($token, $steamIdObject, $key);
|
||||
}
|
||||
\Flight::redirect($return);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
<?php namespace Demostf\API\Controllers;
|
||||
|
||||
|
||||
class BaseController {
|
||||
protected function query($name, $default) {
|
||||
$request = \Flight::request();
|
||||
return isset($request->query[$name]) ? $request->query[$name] : $default;
|
||||
}
|
||||
|
||||
protected function query($name, $default) {
|
||||
$request = \Flight::request();
|
||||
return isset($request->query[$name]) ? $request->query[$name] : $default;
|
||||
}
|
||||
protected function file($name) {
|
||||
$request = \Flight::request();
|
||||
return $request->files[$name];
|
||||
}
|
||||
|
||||
protected function file($name) {
|
||||
$request = \Flight::request();
|
||||
return $request->files[$name];
|
||||
}
|
||||
|
||||
protected function post($name, $default = null) {
|
||||
$request = \Flight::request();
|
||||
return isset($request->data[$name]) ? $request->data[$name] : $default;
|
||||
}
|
||||
protected function post($name, $default = null) {
|
||||
$request = \Flight::request();
|
||||
return isset($request->data[$name]) ? $request->data[$name] : $default;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,97 +6,97 @@ use Demostf\API\Providers\DemoProvider;
|
|||
use flight\Engine;
|
||||
|
||||
class DemoController extends BaseController {
|
||||
/** @var DemoProvider */
|
||||
private $demoProvider;
|
||||
/** @var DemoProvider */
|
||||
private $demoProvider;
|
||||
|
||||
/** @var ChatProvider */
|
||||
private $chatProvider;
|
||||
/** @var ChatProvider */
|
||||
private $chatProvider;
|
||||
|
||||
private $demoListProvider;
|
||||
private $demoListProvider;
|
||||
|
||||
private $editKey;
|
||||
private $editKey;
|
||||
|
||||
public function __construct(DemoProvider $demoProvider, ChatProvider $chatProvider, DemoListProvider $demoListProvider, string $editKey) {
|
||||
$this->demoProvider = $demoProvider;
|
||||
$this->chatProvider = $chatProvider;
|
||||
$this->demoListProvider = $demoListProvider;
|
||||
$this->editKey = $editKey;
|
||||
}
|
||||
public function __construct(DemoProvider $demoProvider, ChatProvider $chatProvider, DemoListProvider $demoListProvider, string $editKey) {
|
||||
$this->demoProvider = $demoProvider;
|
||||
$this->chatProvider = $chatProvider;
|
||||
$this->demoListProvider = $demoListProvider;
|
||||
$this->editKey = $editKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function get($id) {
|
||||
\Flight::json($this->demoProvider->get($id));
|
||||
}
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function get($id) {
|
||||
\Flight::json($this->demoProvider->get($id));
|
||||
}
|
||||
|
||||
protected function getFilter() {
|
||||
$map = $this->query('map', '');
|
||||
$players = $this->query('players', '');
|
||||
$type = $this->query('type', '');
|
||||
$filter = [];
|
||||
if ($map) {
|
||||
$filter['map'] = $map;
|
||||
}
|
||||
if ($players) {
|
||||
if (!is_array($players)) {
|
||||
$players = explode(',', $players);
|
||||
}
|
||||
$players = array_filter($players);
|
||||
$filter['players'] = $players;
|
||||
}
|
||||
switch ($type) {
|
||||
case 'hl':
|
||||
$filter['playerCount'] = [17, 18, 19];
|
||||
break;
|
||||
case '6v6':
|
||||
$filter['playerCount'] = [11, 12, 13];
|
||||
break;
|
||||
case '4v4':
|
||||
$filter['playerCount'] = [7, 8, 9];
|
||||
break;
|
||||
}
|
||||
return $filter;
|
||||
}
|
||||
protected function getFilter() {
|
||||
$map = $this->query('map', '');
|
||||
$players = $this->query('players', '');
|
||||
$type = $this->query('type', '');
|
||||
$filter = [];
|
||||
if ($map) {
|
||||
$filter['map'] = $map;
|
||||
}
|
||||
if ($players) {
|
||||
if (!is_array($players)) {
|
||||
$players = explode(',', $players);
|
||||
}
|
||||
$players = array_filter($players);
|
||||
$filter['players'] = $players;
|
||||
}
|
||||
switch ($type) {
|
||||
case 'hl':
|
||||
$filter['playerCount'] = [17, 18, 19];
|
||||
break;
|
||||
case '6v6':
|
||||
$filter['playerCount'] = [11, 12, 13];
|
||||
break;
|
||||
case '4v4':
|
||||
$filter['playerCount'] = [7, 8, 9];
|
||||
break;
|
||||
}
|
||||
return $filter;
|
||||
}
|
||||
|
||||
public function listDemos() {
|
||||
$page = $this->query('page', 1);
|
||||
$order = $this->query('order', 'DESC') === 'ASC' ? 'ASC' : 'DESC';
|
||||
\Flight::json($this->demoListProvider->listDemos($page, $this->getFilter(), $order));
|
||||
}
|
||||
public function listDemos() {
|
||||
$page = $this->query('page', 1);
|
||||
$order = $this->query('order', 'DESC') === 'ASC' ? 'ASC' : 'DESC';
|
||||
\Flight::json($this->demoListProvider->listDemos($page, $this->getFilter(), $order));
|
||||
}
|
||||
|
||||
public function listProfile($steamid) {
|
||||
$page = $this->query('page', 1);
|
||||
$where = $this->getFilter();
|
||||
$where['players'][] = $steamid;
|
||||
\Flight::json($this->demoListProvider->listProfile($page, $where));
|
||||
}
|
||||
public function listProfile($steamid) {
|
||||
$page = $this->query('page', 1);
|
||||
$where = $this->getFilter();
|
||||
$where['players'][] = $steamid;
|
||||
\Flight::json($this->demoListProvider->listProfile($page, $where));
|
||||
}
|
||||
|
||||
public function listUploads($steamid) {
|
||||
$page = $this->query('page', 1);
|
||||
\Flight::json($this->demoListProvider->listUploads($steamid, $page, $this->getFilter()));
|
||||
}
|
||||
public function listUploads($steamid) {
|
||||
$page = $this->query('page', 1);
|
||||
\Flight::json($this->demoListProvider->listUploads($steamid, $page, $this->getFilter()));
|
||||
}
|
||||
|
||||
public function chat($demoId) {
|
||||
\Flight::json($this->chatProvider->getChat($demoId));
|
||||
}
|
||||
public function chat($demoId) {
|
||||
\Flight::json($this->chatProvider->getChat($demoId));
|
||||
}
|
||||
|
||||
public function setDemoUrl($id) {
|
||||
$hash = $this->post('hash', '');
|
||||
$backend = $this->post('backend', '');
|
||||
$path = $this->post('path', '');
|
||||
$url = $this->post('url', '');
|
||||
$editKey = $this->post('key', '');
|
||||
if ($editKey !== $this->editKey || $editKey === '') {
|
||||
throw new \InvalidArgumentException('Invalid key');
|
||||
}
|
||||
public function setDemoUrl($id) {
|
||||
$hash = $this->post('hash', '');
|
||||
$backend = $this->post('backend', '');
|
||||
$path = $this->post('path', '');
|
||||
$url = $this->post('url', '');
|
||||
$editKey = $this->post('key', '');
|
||||
if ($editKey !== $this->editKey || $editKey === '') {
|
||||
throw new \InvalidArgumentException('Invalid key');
|
||||
}
|
||||
|
||||
$demo = $this->demoProvider->get((int)$id);
|
||||
$existingHash = $demo->getHash();
|
||||
if ($existingHash === '' || $existingHash === $hash) {
|
||||
$this->demoProvider->setDemoUrl((int)$id, $backend, $url, $path);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid demo hash');
|
||||
}
|
||||
}
|
||||
$demo = $this->demoProvider->get((int)$id);
|
||||
$existingHash = $demo->getHash();
|
||||
if ($existingHash === '' || $existingHash === $hash) {
|
||||
$this->demoProvider->setDemoUrl((int)$id, $backend, $url, $path);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid demo hash');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,18 +4,18 @@ use Demostf\API\Providers\InfoProvider;
|
|||
use flight\Engine;
|
||||
|
||||
class InfoController extends BaseController {
|
||||
/** @var InfoProvider */
|
||||
private $infoProvider;
|
||||
/** @var InfoProvider */
|
||||
private $infoProvider;
|
||||
|
||||
public function __construct(InfoProvider $infoProvider) {
|
||||
$this->infoProvider = $infoProvider;
|
||||
}
|
||||
public function __construct(InfoProvider $infoProvider) {
|
||||
$this->infoProvider = $infoProvider;
|
||||
}
|
||||
|
||||
public function listMaps() {
|
||||
\Flight::json($this->infoProvider->listMaps());
|
||||
}
|
||||
public function listMaps() {
|
||||
\Flight::json($this->infoProvider->listMaps());
|
||||
}
|
||||
|
||||
public function stats() {
|
||||
\Flight::json($this->infoProvider->getStats());
|
||||
}
|
||||
public function stats() {
|
||||
\Flight::json($this->infoProvider->getStats());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,36 +4,36 @@ use Demostf\API\Providers\UploadProvider;
|
|||
use flight\Engine;
|
||||
|
||||
class UploadController extends BaseController {
|
||||
private $uploadProvider;
|
||||
private $uploadProvider;
|
||||
|
||||
public function __construct(UploadProvider $uploadProvider) {
|
||||
$this->uploadProvider = $uploadProvider;
|
||||
}
|
||||
public function __construct(UploadProvider $uploadProvider) {
|
||||
$this->uploadProvider = $uploadProvider;
|
||||
}
|
||||
|
||||
public function upload() {
|
||||
$key = $this->post('key', '');
|
||||
$red = $this->post('red', 'RED');
|
||||
$blu = $this->post('blu', 'BLU');
|
||||
$name = $this->post('name', 'Unnamed');
|
||||
$demo = $this->file('demo');
|
||||
if (is_null($demo)) {
|
||||
echo 'No demo uploaded';
|
||||
return;
|
||||
}
|
||||
$demoFile = $demo['tmp_name'];
|
||||
public function upload() {
|
||||
$key = $this->post('key', '');
|
||||
$red = $this->post('red', 'RED');
|
||||
$blu = $this->post('blu', 'BLU');
|
||||
$name = $this->post('name', 'Unnamed');
|
||||
$demo = $this->file('demo');
|
||||
if (is_null($demo)) {
|
||||
echo 'No demo uploaded';
|
||||
return;
|
||||
}
|
||||
$demoFile = $demo['tmp_name'];
|
||||
|
||||
try {
|
||||
$result = $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
|
||||
if ($result === 'Invalid key') {
|
||||
\Flight::response()->status(401)->write($result)->send();
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Flight::response()
|
||||
->status(500)
|
||||
->write($e->getMessage())
|
||||
->send();
|
||||
}
|
||||
}
|
||||
try {
|
||||
$result = $this->uploadProvider->upload($key, $red, $blu, $name, $demoFile);
|
||||
if ($result === 'Invalid key') {
|
||||
\Flight::response()->status(401)->write($result)->send();
|
||||
} else {
|
||||
echo $result;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Flight::response()
|
||||
->status(500)
|
||||
->write($e->getMessage())
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@ use Demostf\API\Providers\UserProvider;
|
|||
use flight\Engine;
|
||||
|
||||
class UserController extends BaseController {
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
|
||||
public function __construct(UserProvider $userProvider) {
|
||||
$this->userProvider = $userProvider;
|
||||
}
|
||||
public function __construct(UserProvider $userProvider) {
|
||||
$this->userProvider = $userProvider;
|
||||
}
|
||||
|
||||
public function get($steamid) {
|
||||
\Flight::json($this->userProvider->get($steamid));
|
||||
}
|
||||
public function get($steamid) {
|
||||
\Flight::json($this->userProvider->get($steamid));
|
||||
}
|
||||
|
||||
public function search() {
|
||||
$query = $this->query('query', '');
|
||||
\Flight::json($this->userProvider->search($query));
|
||||
}
|
||||
public function search() {
|
||||
$query = $this->query('query', '');
|
||||
\Flight::json($this->userProvider->search($query));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue