<?php
namespace App\Controller\API;
use App\Controller\BaseController;
use Empire\Access\Access;
use Empire\Core\Approval;
use Empire\Core\Branch;
use Empire\Core\Codename;
use Empire\Core\Core;
use Empire\Core\Notification;
use Empire\Core\Oversector;
use Empire\Core\Sector;
use Empire\Core\System;
use Empire\Core\Rank;
use Empire\Core\Region;
use Empire\Core\Page;
use Empire\Core\Position;
use Empire\Core\Unit;
use Empire\Core\User;
use Empire\Legacy\cgt;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
/**
* Class ApiController
* @package App\Controller\API
* @Route("/api")
*/
class ApiController extends BaseController
{
/**
* @param $id
* @param $offset
* @return RedirectResponse
* @Route("/position/{id}/{offset}.jpg")
*/
public function positionAvatar($id, $offset) {
try {
$obj = Position::getAvatarByPosition($id, false, $offset);
return $this->redirect($obj->AVATAR_SRC);
} catch(Exception $e) {
//var_dump($e->getMessage());
//return "";
return $this->redirect('https://art.swc-empire.com/avatar/error.jpg');
}
}
/**
* @param $id
* @return string
* @Route("/branch/{id}/units")
*/
public function branchUnits($id) {
$branch = Branch::load([$id]);
$branch->initialize();
$branchKey = $branch->getKey();
$user = Core::user();
$units = Unit::loadEditable($user, $branch->getID());
return $this->api(["units" => $units]);
}
/**
* @param $id
* @return string
* @Route("/unit/{id}")
*/
public function unitDetails($id) {
try {
$unit = Unit::load([$id]);
if($unit->isEditable($this->getUser())) return $this->api(["unit" => $unit]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Unit not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/unit/{id}/positions")
*/
public function unitPositions($id) {
try {
$unit = Unit::load([$id]);
if(!$unit->isEditable($this->getUser())) {
return $this->api([], 403, [], 'error');
}
return $this->api(['positions' => $unit->getPositions()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Unit not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/unit/{id}/members")
*/
public function unitMembers($id) {
try {
$unit = Unit::load([$id]);
if(!$unit->isEditable($this->getUser(), true)) {
return $this->api([], 403, [], 'error');
}
return $this->api(['members' => $unit->getMembersDisplay()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Unit not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/unit/{id}/codenames")
*/
public function unitCodenames($id) {
try {
$unit = Unit::load([$id]);
if(!$unit->isEditable($this->getUser())) {
return $this->api([], 403, [], 'error');
}
return $this->api(['codenames' => $unit->getCodenames()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Unit not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return JsonResponse
* @Route("/position/{id}")
*/
public function positionDetails($id) {
try {
$position = Position::load([$id]);
if($position->isEditable($this->getUser())) {
return $this->api(['position' => $position]);
} else {
return $this->api(['error' => 'User not authorized to view Position with id'], 403, [], 'error');
}
} catch (Exception $e) {
return $this->api(['error' => 'Position not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return JsonResponse
* @Route("/position/{id}/access")
*/
public function positionAccess($id) {
try {
$position = Position::load([$id]);
if($position->isEditable($this->getUser())) { //TODO: Filter for access keys user can see?
return $this->api(['access' => $position->getKeys()]);
} else {
return $this->api(['error' => 'User not authorized to view Position with id'], 403, [], 'error');
}
} catch (Exception $e) {
return $this->api(['error' => 'Position not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @param Request $request
* @return JsonResponse
* @Route("/page/{id}/set-keys")
*/
public function setPageAccessKeys($id, Request $request) {
$keyString = $request->request->get('keys');
$keyArr = json_decode($keyString);
if(json_last_error() !== JSON_ERROR_NONE) {
return $this->api(['error' => json_last_error_msg()], 400);
}
try {
$page = Page::load([$id]);
$page->updateFields(['SEPA_KEYS' => $keyString]);
return $this->api(["status" => "success"]);
} catch (Exception $e) {
return $this->api(['error' => 'Page not found or other error.'], 500, [], 'error');
}
}
/**
* @return JsonResponse
* @Route("/cgt")
*/
public function getTime() {
return $this->api(["cgt" => cgt::evaluateString("now")]);
}
/**
* @Route("/menu")
*/
public function getSidemenu(){
$ing = \Empire\Core\PageCategory::loadAll(\Empire\Core\Page::PAGE_TYPE_PUBLIC);
$cat = $ing[0];
$return = "";
foreach($cat->subCategories() as $scat) {
$return .= "<div class='menu'>";
$return .= "<h2>" . $scat->getName() . "</h2>\n";
$return .= "<ul>\n";
foreach ($scat->pages() as $page) {
if($page->visible) {
$active = $page->url == ltrim(Core::get("fullPage"), '/') ? 'class="menu-active-link"' : '';
if (strpos($page->url, "http://") !== 0 && strpos($page->url, "https://") !== 0 ) {
$return .= "\t<li><a href='" . Core::get("base_url") . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
} else {
$return .= "\t<li><a href='" . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
}
}
}
$return .= "</ul>\n</div>";
}
return $this->api($return);
}
/**
* @Route("/cgt/{args}", requirements={"args"="\w+"}, )
*/
public function getCgt($args = "now"){
return $this->api(["time" => \Empire\Legacy\cgt::evaluateString($args)], 200, ["Access-Control-Allow-Origin" => '*']);
}
/**
* @Route("/ranks")
*/
public function getBranchRanks(Request $request){
$ranks = \Empire\Core\Branch::load([$request->request->get("branch")])->getRanksOrdered();
return $this->api(["ranks" => $ranks]);
}
/**
* @Route("/rank-update", methods={"POST"})
*/
public function updateRank(Request $request){
$id = $request->request->get('id');
$name = $request->request->get('name');
$image = $request->request->get('image');
$uniform = $request->request->get('uniform');
$pay = $request->request->get('pay');
$success = Rank::updateRank($id, $name, $image, $uniform, $pay);
if($success) {
return $this->api(["success"]);
} else {
return $this->api(['error' => 'Rank could not be updated.'], 400);
}
}
/**
* @Route("/notification-read", methods={"POST"})
*/
public function readNotification(Request $request){
$id = $request->request->get('id');
$success = Notification::markRead($id);
if($success) {
return $this->api(["success"]);
} else {
return $this->api(['error' => 'Unknown error.'], 400);
}
}
/**
* @Route("/approval-respond", methods={"POST"})
*/
public function approvalResponse(Request $request){
$id = $request->request->get('id');
$state = $request->request->get('state');
$success = Approval::respond($id, $state);
if($success) {
return $this->api(["success"]);
} else {
return $this->api(['error' => 'Unknown error.'], 400);
}
}
/**
* @Route("/auth/user")
*/
public function apiUser(Request $request){
$c = $request->getContent();
$h = $request->headers->all();
$user = $this->getUser();
if(is_null($user)){
return $this->api(["error" => "no user!", 'headers' => $h, 'content' => $c, ], 200);
} else {
return $this->api(['headers' => $h, 'content' => $c, 'username' => $user->getName(), 'avatar_url' => $user->getAvatar(), 'id' => $user->getID()]);
}
}
/**
* @param $id
* @return string
* @Route("/region/{id}", name="region")
*/
public function regionDetails($id) {
try {
$region = Region::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["region" => $region]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Region not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/region/{id}/access", name="region_nie_access")
*/
public function regionNieAccess($id) {
try {
$region = Region::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["region" => $region->getAccess()]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Region not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/region/{id}/oversectors", name="region_oversectors")
*/
public function regionOversectors($id) {
try {
$region = Region::load([$id]);
if(!Core::user()->hasAccess('nie_management')) {
return $this->api([], 403, [], 'error');
}
return $this->api(['oversectors' => $region->getOversectors()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Region not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/oversector/{id}", name="oversector")
*/
public function oversectorDetails($id) {
try {
$oversector = Oversector::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["oversector" => $oversector]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Oversector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/oversector/{id}/sectors", name="oversector_sectors")
*/
public function osSectors($id) {
try {
$oversector = Oversector::load([$id]);
if(!Core::user()->hasAccess('nie_management')) {
return $this->api([], 403, [], 'error');
}
return $this->api(['sectors' => $oversector->getSectors()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Oversector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/sector/{id}", name="sector")
*/
public function sectorDetails($id) {
try {
$sector = Sector::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["sector" => $sector]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Sector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/sector/{id}/access", name="sector_nie_access")
*/
public function sectorNieAccess($id) {
try {
$sector = Sector::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["sector" => $sector->getAccess()]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Sector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string|JsonResponse
* @Route("/sector/{id}/systems", name="sector_systems")
*/
public function sectorSystems($id) {
try {
$sector = Sector::load([$id]);
if(!Core::user()->hasAccess('nie_management')) {
return $this->api([], 403, [], 'error');
}
return $this->api(['systems' => $sector->getSystems()]);
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Sector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/system/{id}", name="system")
*/
public function systemDetails($id) {
try {
$system = System::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["system" => $system]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'Sector not found or other error.'], 500, [], 'error');
}
}
/**
* @param $id
* @return string
* @Route("/system/{id}/access", name="system_nie_access")
*/
public function systemNieAccess($id) {
try {
$system = System::load([$id]);
if(Core::user()->hasAccess('nie_management')) return $this->api(["system" => $system->getAccess()]);
else return $this->api([], 403, [], 'error');
} catch (Exception $e) {
var_dump($e->getMessage());
return $this->api(['error' => 'System not found or other error.'], 500, [], 'error');
}
}
}