<commit_before><?php
namespace Areanet\PIM\Classes\Controller\Provider;

use Areanet\PIM\Classes\Config\Adapter;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class BaseControllerProvider implements ControllerProviderInterface
{

    const LOGIN_PATH           = '/login';
    const TOKEN_HEADER_KEY_ALT = 'X-XSRF-TOKEN';
    const TOKEN_HEADER_KEY     = 'APPMS-TOKEN';
    const TOKEN_REQUEST_KEY    = '_token';
    protected $basePath = '';

    public function __construct($basePath)
    {
        $this->basePath = $basePath;
    }


    protected function setUpMiddleware(Application $app)
    {
        $app->before(function (Request $request)use ($app) {

            $controller = $request->get('_controller');
            if (is_string($controller) && substr($controller, 0, 7) != 'install' && Adapter::getConfig()->DB_HOST == '$SET_DB_HOST'){
                return $app->redirect('install');
            }

            if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
                $data = null;
                if($request->getContent()) {
                    $data = json_decode($request->getContent(), true);
                    if ($data === null) {
                        throw new \Exception("Inavlid JSON-Data", 500);
                    }
                }
                $request->request->replace(is_array($data) ? $data : array());
            }else{

            }

            if(!is_object($request->get('_controller'))) {
                $event = new \Areanet\PIM\Classes\Event();
                $event->setParam('request', $request);
                $event->setParam('app', $app);
                $controllerAction = str_replace(':', '.', strtolower($request->get('_controller')));
                if (empty($controllerAction)) {
                    return;
                }

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.before.' . $controllerParts[0] . '.' . $controllerParts[2], $event);

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.before.' . $controllerParts[0], $event);
                $app['dispatcher']->dispatch('pim.controller.before', $event);
            }
        });

        $app->after(function (Request $request, Response $response) use ($app) {

            if(!is_object($request->get('_controller'))) {
                $event = new \Areanet\PIM\Classes\Event();
                $event->setParam('request', $request);
                $event->setParam('response', $response);
                $event->setParam('app', $app);

                $controllerAction = str_replace(':', '.', strtolower($request->get('_controller')));
                if (empty($controllerAction)) {
                    return;
                }
                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.after.' . $controllerParts[0] . '.' . $controllerParts[2], $event);

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.after.' . $controllerParts[0], $event);
                $app['dispatcher']->dispatch('pim.controller.after', $event);
            }
        });
    }

    protected function isAuthRequiredForPath($path)
    {
        return !in_array($path, [$this->basePath . self::LOGIN_PATH]);
    }

    protected function checkToken(Request $request, Application $app){
        $tokenString = $request->headers->get(self::TOKEN_HEADER_KEY, $request->headers->get(self::TOKEN_HEADER_KEY_ALT, $request->get(self::TOKEN_REQUEST_KEY)));
       
        if(!$tokenString){
            return false;
        }

        $token = $app['orm.em']->getRepository('Areanet\PIM\Entity\Token')->findOneBy(array('token' => $tokenString));

        if(!$token){
            return false;
        }

        if(!$token->getUser() || !$token->getUser()->getIsActive()){
            return false;
        }

        $tokenTimeout = Adapter::getConfig()->APP_TOKEN_TIMEOUT;

        if(($group = $token->getUser()->getGroup())){
            $tokenTimeout =  $group->getTokenTimeout() * 60;
        }

        if(Adapter::getConfig()->APP_CHECK_TOKEN_TIMEOUT && !$token->getReferrer() && $tokenTimeout) {
            
            $modified   = $token->getModified()->getTimestamp();
            $now        = new \DateTime();
            $diff       = $now->getTimestamp() - $modified;

            if ($diff > $tokenTimeout) {
                $app['orm.em']->remove($token);
                $app['orm.em']->flush();<commit_msg>fixed #33<commit_after><?php
namespace Areanet\PIM\Classes\Controller\Provider;

use Areanet\PIM\Classes\Config\Adapter;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class BaseControllerProvider implements ControllerProviderInterface
{

    const LOGIN_PATH           = '/login';
    const TOKEN_HEADER_KEY_ALT = 'X-XSRF-TOKEN';
    const TOKEN_HEADER_KEY     = 'appmcs-token';
    const TOKEN_REQUEST_KEY    = '_token';
    protected $basePath = '';

    public function __construct($basePath)
    {
        $this->basePath = $basePath;
    }


    protected function setUpMiddleware(Application $app)
    {
        $app->before(function (Request $request)use ($app) {

            $controller = $request->get('_controller');
            if (is_string($controller) && substr($controller, 0, 7) != 'install' && Adapter::getConfig()->DB_HOST == '$SET_DB_HOST'){
                return $app->redirect('install');
            }

            if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
                $data = null;
                if($request->getContent()) {
                    $data = json_decode($request->getContent(), true);
                    if ($data === null) {
                        throw new \Exception("Inavlid JSON-Data", 500);
                    }
                }
                $request->request->replace(is_array($data) ? $data : array());
            }else{

            }

            if(!is_object($request->get('_controller'))) {
                $event = new \Areanet\PIM\Classes\Event();
                $event->setParam('request', $request);
                $event->setParam('app', $app);
                $controllerAction = str_replace(':', '.', strtolower($request->get('_controller')));
                if (empty($controllerAction)) {
                    return;
                }

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.before.' . $controllerParts[0] . '.' . $controllerParts[2], $event);

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.before.' . $controllerParts[0], $event);
                $app['dispatcher']->dispatch('pim.controller.before', $event);
            }
        });

        $app->after(function (Request $request, Response $response) use ($app) {

            if(!is_object($request->get('_controller'))) {
                $event = new \Areanet\PIM\Classes\Event();
                $event->setParam('request', $request);
                $event->setParam('response', $response);
                $event->setParam('app', $app);

                $controllerAction = str_replace(':', '.', strtolower($request->get('_controller')));
                if (empty($controllerAction)) {
                    return;
                }
                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.after.' . $controllerParts[0] . '.' . $controllerParts[2], $event);

                $controllerParts = explode('.', $controllerAction);
                $app['dispatcher']->dispatch('pim.controller.after.' . $controllerParts[0], $event);
                $app['dispatcher']->dispatch('pim.controller.after', $event);
            }
        });
    }

    protected function isAuthRequiredForPath($path)
    {
        return !in_array($path, [$this->basePath . self::LOGIN_PATH]);
    }

    protected function checkToken(Request $request, Application $app){
        $tokenString = $request->headers->get(self::TOKEN_HEADER_KEY, $request->headers->get(self::TOKEN_HEADER_KEY_ALT, $request->get(self::TOKEN_REQUEST_KEY)));
        if(!$tokenString){
            return false;
        }

        $token = $app['orm.em']->getRepository('Areanet\PIM\Entity\Token')->findOneBy(array('token' => $tokenString));

        if(!$token){
            return false;
        }

        if(!$token->getUser() || !$token->getUser()->getIsActive()){
            return false;
        }

        $tokenTimeout = Adapter::getConfig()->APP_TOKEN_TIMEOUT;

        if(($group = $token->getUser()->getGroup())){
            $tokenTimeout =  $group->getTokenTimeout() * 60;
        }

        if(Adapter::getConfig()->APP_CHECK_TOKEN_TIMEOUT && !$token->getReferrer() && $tokenTimeout) {
            
            $modified   = $token->getModified()->getTimestamp();
            $now        = new \DateTime();
            $diff       = $now->getTimestamp() - $modified;

            if ($diff > $tokenTimeout) {
                $app['orm.em']->remove($token);
                $app['orm.em']->flush();
{'fraction_non_alphanumeric': 0.16429091665799606, 'fraction_numerical': 0.0031214233690562898, 'mean_word_length': 2.191235059760956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565174', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2576, 'n_words': 398}