<?php
namespace App\Controller;
use App\Cache\BlogArticleCache;
use App\Entity\User;
use App\Entity\BlogArticle;
use App\EntityManager\BlogArticleManager;
use App\EntityManager\ForumManager;
use App\EntityManager\UserManager;
use App\Service\CounterService;
use App\Service\GitlabConnector;
use Psr\SimpleCache\CacheInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class HomeController extends AbstractController
{
protected $userManager;
protected $gitlabConnector;
protected $cache;
public function __construct(
UserManager $userManager,
GitlabConnector $gitlabConnector,
CacheInterface $cache
) {
$this->userManager = $userManager;
$this->gitlabConnector = $gitlabConnector;
$this->cache = $cache;
}
/**
* @Route("/", name="_home")
* @Template()
*/
public function index(BlogArticleManager $articleManager, CounterService $counterService)
{
$articlesData = $this->cache->get(BlogArticleCache::HOME_ARTICLES_KEY);
if ($articlesData == null) {
$articles = $this->getDoctrine()->getRepository(BlogArticle::class)->getList(1, 10);
$articlesData = array_map(
function (BlogArticle $article) use ($articleManager) {
return [
'article' => $article,
'bannerUri' => $articleManager->getBannerImage($article),
];
},
$articles
);
$this->cache->set(BlogArticleCache::HOME_ARTICLES_KEY, $articlesData);
}
return [
'articlesData' => $articlesData,
'counters' => $counterService->getCounters(),
];
}
/**
* @Route("/lang/{locale}", name="_change_locale")
* @Template()
*/
public function changeLocale(
Request $request,
$locale
) {
$this->userManager->updateLocale($locale);
return $this->redirect($request->headers->get('referer', $this->generateUrl('_home')));
}
/**
* @Route("/report_bug", name="_report_a_bug")
* @Method({"POST"})
*/
public
function reportBug(
Request $request
): JsonResponse {
if (!$request->isXmlHttpRequest()) {
throw new AccessDeniedException();
}
$comment = $request->request->get('comment');
$screenshot = $request->request->get('screenshot');
if (is_null($comment)) {
throw new \Exception("The bug report has no comment.");
}
$extras = [
'date' => $request->request->get('timestamp'),
'uri' => $request->request->get('uri'),
'_route' => $request->request->get('route'),
'viewport' => $request->request->get('viewport'),
];
if (!is_null($screenshot)) {
$filename = sprintf('%s_%s.png', date('Y-m-d'), md5(uniqid(rand(), true)));
$filepath = sprintf(
'%s/data/bug_report/%s',
$this->getParameter('kernel.project_dir'),
$filename
);
$screenshot = explode(',', $screenshot);
if (is_array($screenshot) &&
isset($screenshot[1]) &&
false !== file_put_contents($filepath, base64_decode($screenshot[1]))) {
$extras['screenshot'] = $this->generateUrl(
'_serve_bug_screenshot',
['filename' => $filename],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
}
$response = $this->gitlabConnector->reportBug($comment, $extras);
if (in_array($response->getStatusCode(), [200, 201])) {
return new JsonResponse('success');
}
return new JsonResponse('error', 400);
}
/**
* @Route("/report_bug/screenshot/{filename}", name="_serve_bug_screenshot")
* @IsGranted(User::ROLE_SUPER_ADMIN)
*/
public
function serveBugScreenshot(
string $filename
) {
$filepath = sprintf(
'%s/data/bug_report/%s',
$this->getParameter('kernel.project_dir'),
$filename
);
if (file_exists($filepath)) {
return $this->file($filepath, null, ResponseHeaderBag::DISPOSITION_INLINE);
}
throw new FileNotFoundException();
}
/**
* @Route("/under_construction", name="_under_construction")
* @Template()
*/
public
function underConstruction()
{
return [];
}
}