1: <?php
2: 3: 4: 5: 6: 7:
8: namespace Core\BaseClasses;
9:
10: use Core\System\System;
11: use Twig_Environment;
12: use Twig_Function;
13: use Twig_Loader_Filesystem;
14: use Core\Middleware\CSRF\CSRF;
15:
16: class BaseController
17: {
18: public $requestUri;
19: public $requestMethod;
20: public $requestData;
21: public $rawData;
22:
23: 24: 25: 26:
27: public function __construct($request)
28: {
29: $this->requestUri = $request[0];
30: $this->requestMethod = $request[1];
31: $this->requestData = $request[2];
32: $this->rawData = $request[3];
33: if($this->requestMethod == "POST")
34: {
35: CSRF::verifyCSRFToken($this->requestUri);
36: }
37: }
38:
39:
40: 41: 42: 43: 44: 45:
46: public static function view($view, $arrayParams = [], $exit = false)
47: {
48: $loader = new Twig_Loader_Filesystem(__DIR__.'/../../Views');
49: $cache = __DIR__.'/../../Storage/Cache/Views';
50: if(!$_ENV['cache_template'])
51: {
52: $cache = false;
53: }
54: $twig = new Twig_Environment($loader, array(
55: 'cache' => $cache,
56: 'auto_reload' => $_ENV['debug'],
57: ));
58: $csrfFunction = new Twig_Function('csrf_token', function () {
59: $csrf = new BaseCSRF();
60: return $csrf->generateToken();
61: });
62: $twig->addFunction($csrfFunction);
63: $template = $twig->load($view.'.vu.php');
64: if($exit === true)
65: {
66: echo $template->display($arrayParams);
67: exit(1);
68: }
69: return $template->display($arrayParams);
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89:
90:
91: 92: 93: 94:
95: public static function redirect($path)
96: {
97: $path="location: ".$path;
98: header($path);
99: }
100:
101: 102: 103: 104:
105: public static function json($inArray)
106: {
107: System::GiveJSON($inArray);
108: }
109:
110: 111: 112:
113: public static function back()
114: {
115: $ref = "location: ".$_SERVER['HTTP_REFERER'];
116: header($ref);
117: }
118: }