<commit_before><?php

namespace Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as Response;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormFactoryInterface as FormFactoryInterface;
use Symfony\Component\HttpFoundation\Session\Session;

class BaseStalkerController {

    protected $app;
    protected $request;
    protected $baseDir;
    protected $baseHost;
    protected $workHost;
    protected $workURL;
    protected $refferer;
    protected $Uri;
    protected $method;
    protected $isAjax;
    protected $data;
    protected $postData;
    protected $db;
    protected $admin;
    protected $session;
    protected $access_level = 0;
    protected $access_levels = array(
        0 => 'denied',
        1 => 'view',
        2 => 'edit',
        3 => 'edit',
        4 => 'action',
        5 => 'all',
        6 => 'all',
        7 => 'all',
        8 => 'all',
    );

    public function __construct(Application $app, $modelName = '') {
        $this->app = $app;
        $this->request = $app['request'];
        
        if (session_id()) {
            session_write_close();
            $this->app['request']->getSession()->save();
        }
        $this->app['request']->getSession()->start();
        $this->admin = \Admin::getInstance();
        
        $this->app['userlogin'] = $this->admin->getLogin();
                
        $this->baseDir = rtrim(str_replace(array("src", "Controller"), '', __DIR__), '//');
        $this->getPathInfo();
        $this->setRequestMethod();
        $this->setAjaxFlag();
        $this->getData();
        $this->setDataTablePluginSettings();
        
        $modelName = "Model\\" . (empty($modelName) ? 'BaseStalker' : str_replace(array("\\", "Controller"), '', $modelName)) . 'Model';
        $this->db = FALSE;
        $modelName = (class_exists($modelName)? $modelName: 'Model\BaseStalkerModel');
        if (class_exists($modelName)) {
            $this->db = new $modelName;
            if (!($this->db instanceof $modelName)) {
                $this->db = FALSE;
            }
        }
        $uid = $this->admin->getId(); 
        if ($this->db !== FALSE && !empty($uid)) {
            $this->app['userTaskMsgs'] = $this->db->getCountUnreadedMsgsByUid($uid);
        }
        $this->app['reseller'] = $this->admin->getResellerID();
        $this->db->setReseller($this->app['reseller']);

        $this->saveFiles = $app['saveFiles'];
        $this->setSideBarMenu();
        if($this->app['userlogin'] == 'admin'){
            $this->access_level = 8;
        } else {
            $this->setAccessLevel();
        }
        if (isset($this->data['set-dropdown-attribute'])) {            
            $this->set_dropdown_attribute();
            exit;
        }
    }

    protected function getTemplateName($metod_name) {
        return str_replace(array(__NAMESPACE__, '\\', '::'), array('', '', '_'), $metod_name) . ".twig";
    }

    private function getPathInfo() {
        $tmp = explode('/', trim($this->request->getPathInfo(), '/'));
        $this->app['controller_alias'] = $tmp[0];
        $this->app['action_alias'] = (count($tmp) == 2) ? $tmp[1] : '';
        $this->baseHost = $this->request->getSchemeAndHttpHost();
        $this->workHost = $this->baseHost . \Config::getSafe('portal_url', '/stalker_portal/');
        $this->app['workHost'] = $this->workHost;
        $this->Uri = $this->app['request']->getUri();
        $controller = (!empty($this->app['controller_alias']) ? "/" . $this->app['controller_alias'] : '');
        $action = (!empty($this->app['action_alias']) ? "/" . $this->app['action_alias'] : '');
        $workUrl = explode("?", str_replace(array($action, $controller), '', $this->Uri));
        $this->workURL = $workUrl[0];
        $this->app['breadcrumbs']->addItem('Stalker', $this->workURL);
        $this->refferer = $this->request->server->get('HTTP_REFERER');
    }

    private function setSideBarMenu() {
        $side_bar  = json_decode(str_replace(array("_(", ")"), '', file_get_contents($this->baseDir . '/json_menu/menu.json')), TRUE);
        if (!empty($this->app['userlogin'])) {
            $side_bar[1]['add_params'] = '<span class="hidden-xs">"'. $this->app['userlogin'] .'"</span>';
            if (!empty($this->app['userTaskMsgs'])) {
                $side_bar[1]['action'][2]['add_params'] = '<span class="hidden-xs badge">'. $this->app['userTaskMsgs'] .'</span>';
            }
        }
        
        $this->setControllerAccessMap();
        $this->cleanSideBar($side_bar);
        $this->app['side_bar'] = $side_bar;
    }

    private function setRequestMethod() {
        $this->method = $this->request->getMethod();
    }

    private function setAjaxFlag() {
        $this->isAjax = $this->request->isXmlHttpRequest();
    }

    private function getData() {
        $this->data = $this->request->query->all();
        $this->postData = $this->request->request->all();
    }

    public function setLocalization($source = array(), $fieldname = '', $number = FALSE, $params = array()) {
        if (!empty($source)) {
            if (!is_array($source)) {
                $translate = '';
                if ($number === FALSE) {
                    $translate =  $this->app['translator']->trans($source, $params);
                } else {
                    $translate =  $this->app['translator']->transChoice($source, $number, $params);
                }
                return (!empty($translate) ? $translate: $source);
            } elseif (array_key_exists($fieldname, $source) && is_string($source[$fieldname])) {
                $source[$fieldname] = $this->setLocalization($source[$fieldname], $fieldname, $number, $params);
            } else {
                while (list($key, $row) = each($source)) {
                    $source[$key] = $this->setLocalization($row, $fieldname, $number, $params);
                }
            }
            return $source;
        }
        return FALSE;
    }

    public function getFieldFromArray($array, $field) {
        $return_array = array();
        $tmp = array_values($array);
        if (!empty($tmp) && is_array($tmp[0]) && array_key_exists($field, $tmp[0])) {
            foreach ($array as $key => $value) {
                $return_array[] = $value[$field];
            }
        }
        return $return_array;
    }

    public function generateAjaxResponse($data = array(), $error = '') {
        $response = array();

        if (empty($error) && !empty($data)) {
            $response['success'] = TRUE;
            $response['error'] = FALSE;
        } else {
            $response['success'] = FALSE;
            $response['error'] = $error;
        }

        return array_merge($response, $data);
    }

    protected function checkAuth() {
        if (empty($this->app['controller_alias']) || ($this->app['action_alias'] != 'register' && $this->app['action_alias'] != 'login')) {
            if (!$this->admin->isAuthorized()) {
                if ($this->isAjax) {
                    $response = $this->generateAjaxResponse(array(), 'Need authorization');
                    return new Response(json_encode($response), 401);
                } else {
                    return $this->app->redirect($this->workURL . '/login', 302);
                }
            }

            $parent_access = $this->getParentActionAccess();

            if(
                $this->access_level < 1 ||
                (!empty($this->postData) && !$this->isAjax && $this->access_level < 2) ||
                (!empty($this->postData) && $this->isAjax && $this->access_level < 4 && $parent_access === FALSE) ||
                ($parent_access !== FALSE && !$parent_access)
            ) {
                if ($this->isAjax) {
                    $response = $this->generateAjaxResponse(array(), 'Access denied');
                    return new Response(json_encode($response), 403);
                } else {<commit_msg>The main screen for the new admin panel - small fix 3(issue #5544)<commit_after><?php

namespace Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as Response;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormFactoryInterface as FormFactoryInterface;
use Symfony\Component\HttpFoundation\Session\Session;

class BaseStalkerController {

    protected $app;
    protected $request;
    protected $baseDir;
    protected $baseHost;
    protected $workHost;
    protected $workURL;
    protected $refferer;
    protected $Uri;
    protected $method;
    protected $isAjax;
    protected $data;
    protected $postData;
    protected $db;
    protected $admin;
    protected $session;
    protected $access_level = 0;
    protected $access_levels = array(
        0 => 'denied',
        1 => 'view',
        2 => 'edit',
        3 => 'edit',
        4 => 'action',
        5 => 'all',
        6 => 'all',
        7 => 'all',
        8 => 'all',
    );

    public function __construct(Application $app, $modelName = '') {
        $this->app = $app;
        $this->request = $app['request'];
        
        if (session_id()) {
            session_write_close();
            $this->app['request']->getSession()->save();
        }
        $this->app['request']->getSession()->start();
        $this->admin = \Admin::getInstance();
        
        $this->app['userlogin'] = $this->admin->getLogin();
                
        $this->baseDir = rtrim(str_replace(array("src", "Controller"), '', __DIR__), '//');
        $this->getPathInfo();
        $this->setRequestMethod();
        $this->setAjaxFlag();
        $this->getData();
        $this->setDataTablePluginSettings();
        
        $modelName = "Model\\" . (empty($modelName) ? 'BaseStalker' : str_replace(array("\\", "Controller"), '', $modelName)) . 'Model';
        $this->db = FALSE;
        $modelName = (class_exists($modelName)? $modelName: 'Model\BaseStalkerModel');
        if (class_exists($modelName)) {
            $this->db = new $modelName;
            if (!($this->db instanceof $modelName)) {
                $this->db = FALSE;
            }
        }
        $uid = $this->admin->getId(); 
        if ($this->db !== FALSE && !empty($uid)) {
            $this->app['userTaskMsgs'] = $this->db->getCountUnreadedMsgsByUid($uid);
        }
        $this->app['reseller'] = $this->admin->getResellerID();
        $this->db->setReseller($this->app['reseller']);

        $this->saveFiles = $app['saveFiles'];
        $this->setSideBarMenu();
        if($this->app['userlogin'] == 'admin'){
            $this->access_level = 8;
        } else {
            $this->setAccessLevel();
        }
        if (isset($this->data['set-dropdown-attribute'])) {            
            $this->set_dropdown_attribute();
            exit;
        }
    }

    protected function getTemplateName($metod_name) {
        return str_replace(array(__NAMESPACE__, '\\', '::'), array('', '', '_'), $metod_name) . ".twig";
    }

    private function getPathInfo() {
        $tmp = explode('/', trim($this->request->getPathInfo(), '/'));
        $this->app['controller_alias'] = $tmp[0];
        $this->app['action_alias'] = (count($tmp) == 2) ? $tmp[1] : '';
        $this->baseHost = $this->request->getSchemeAndHttpHost();
        $this->workHost = $this->baseHost . \Config::getSafe('portal_url', '/stalker_portal/');
        $this->app['workHost'] = $this->workHost;
        $this->Uri = $this->app['request']->getUri();
        $controller = (!empty($this->app['controller_alias']) ? "/" . $this->app['controller_alias'] : '');
        $action = (!empty($this->app['action_alias']) ? "/" . $this->app['action_alias'] : '');
        $workUrl = explode("?", str_replace(array($action, $controller), '', $this->Uri));
        $this->workURL = $workUrl[0];
        $this->app['breadcrumbs']->addItem('Stalker', $this->workURL);
        $this->refferer = $this->request->server->get('HTTP_REFERER');
    }

    private function setSideBarMenu() {
        $side_bar  = json_decode(str_replace(array("_(", ")"), '', file_get_contents($this->baseDir . '/json_menu/menu.json')), TRUE);
        if (!empty($this->app['userlogin'])) {
            $side_bar[1]['add_params'] = '<span class="hidden-xs">"'. $this->app['userlogin'] .'"</span>';
            if (!empty($this->app['userTaskMsgs'])) {
                $side_bar[1]['action'][2]['add_params'] = '<span class="hidden-xs badge">'. $this->app['userTaskMsgs'] .'</span>';
            }
        }
        
        $this->setControllerAccessMap();
        $this->cleanSideBar($side_bar);
        $this->app['side_bar'] = $side_bar;
    }

    private function setRequestMethod() {
        $this->method = $this->request->getMethod();
    }

    private function setAjaxFlag() {
        $this->isAjax = $this->request->isXmlHttpRequest();
    }

    private function getData() {
        $this->data = $this->request->query->all();
        $this->postData = $this->request->request->all();
    }

    public function setLocalization($source = array(), $fieldname = '', $number = FALSE, $params = array()) {
        if (!empty($source)) {
            if (!is_array($source)) {
                $translate = '';
                if ($number === FALSE) {
                    $translate =  $this->app['translator']->trans($source, $params);
                } else {
                    $translate =  $this->app['translator']->transChoice($source, $number, $params);
                }
                return (!empty($translate) ? $translate: $source);
            } elseif (array_key_exists($fieldname, $source) && is_string($source[$fieldname])) {
                $source[$fieldname] = $this->setLocalization($source[$fieldname], $fieldname, $number, $params);
            } else {
                while (list($key, $row) = each($source)) {
                    $source[$key] = $this->setLocalization($row, $fieldname, $number, $params);
                }
            }
            return $source;
        }
        return FALSE;
    }

    public function getFieldFromArray($array, $field) {
        $return_array = array();
        $tmp = array_values($array);
        if (!empty($tmp) && is_array($tmp[0]) && array_key_exists($field, $tmp[0])) {
            foreach ($array as $key => $value) {
                $return_array[] = $value[$field];
            }
        }
        return $return_array;
    }

    public function generateAjaxResponse($data = array(), $error = '') {
        $response = array();

        if (empty($error) && !empty($data)) {
            $response['success'] = TRUE;
            $response['error'] = FALSE;
        } else {
            $response['success'] = FALSE;
            $response['error'] = $error;
        }

        return array_merge($response, $data);
    }

    protected function checkAuth() {
        if (empty($this->app['controller_alias']) || ($this->app['action_alias'] != 'register' && $this->app['action_alias'] != 'login')) {
            if (!$this->admin->isAuthorized()) {
                if ($this->isAjax) {
                    $response = $this->generateAjaxResponse(array(), 'Need authorization');
                    return new Response(json_encode($response), 401);
                } else {
                    return $this->app->redirect(trim($this->workURL, '/') . '/login', 302);
                }
            }

            $parent_access = $this->getParentActionAccess();

            if(
                $this->access_level < 1 ||
                (!empty($this->postData) && !$this->isAjax && $this->access_level < 2) ||
                (!empty($this->postData) && $this->isAjax && $this->access_level < 4 && $parent_access === FALSE) ||
                ($parent_access !== FALSE && !$parent_access)
            ) {
                if ($this->isAjax) {
                    $response = $this->generateAjaxResponse(array(), 'Access denied');
                    return new Response(json_encode($response), 403);
                } else {
{'fraction_non_alphanumeric': 0.18614450365596727, 'fraction_numerical': 0.0042756227537489155, 'mean_word_length': 2.263040841083704, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 263, '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': '125456', 'n_tokens_mistral': 4764, 'n_tokens_neox': 4641, 'n_words': 801}