Issues (2134)

public/main/inc/global.inc.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Controller\ErrorController;
6
use Chamilo\CoreBundle\EventListener\ExceptionListener;
7
use Chamilo\CoreBundle\Exception\NotAllowedException;
8
use Chamilo\CoreBundle\Framework\Container;
9
use Symfony\Component\Dotenv\Dotenv;
10
use Symfony\Component\ErrorHandler\Debug;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
13
use Symfony\Component\HttpKernel\HttpKernelInterface;
14
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
15
use Symfony\Component\HttpFoundation\Response;
16
17
// Use when running PHPUnit tests.
18
if (isset($fileToLoad)) {
19
    return;
20
}
21
22
/**
23
 * All legacy Chamilo scripts should include this important file.
24
 */
25
require_once __DIR__.'/../../../vendor/autoload.php';
26
27
// Get settings from the created .env file.
28
$envFile = __DIR__.'/../../../.env';
29
if (file_exists($envFile)) {
30
    (new Dotenv())->load($envFile);
31
} else {
32
    throw new \RuntimeException('APP_ENV environment variable is not defined.
33
        You need to define environment variables for configuration to load variables from a .env file.');
34
}
35
36
$env = $_SERVER['APP_ENV'] ?? 'dev';
37
$debug = 'dev' === $env;
38
if ($debug) {
39
    Debug::enable();
40
}
41
42
$isCli = php_sapi_name() === 'cli';
43
if ($isCli) {
44
45
    $kernel = new Chamilo\Kernel($env, $debug);
46
    $kernel->boot();
47
48
    if (!$kernel->isInstalled()) {
49
        throw new Exception('Chamilo is not installed');
50
    }
51
52
    $container = $kernel->getContainer();
53
    Container::setContainer($container);
54
    $session = Container::getLegacyHelper()->getSession();
55
    $request = Request::create('/');
56
    $request->setSession($session);
57
    $container->get('request_stack')->push($request);
58
    Container::setLegacyServices($container);
59
    $router = $container->get('router');
60
    $context = $router->getContext();
61
    $router->setContext($context);
62
63
    $cliOptions = getopt('', ['url:']);
64
    if (!empty($cliOptions['url'])) {
65
        $baseUrl = $cliOptions['url'];
66
        $context->setBaseUrl($baseUrl);
67
    }
68
} else {
69
    $kernel = new Chamilo\Kernel($env, $debug);
70
    // Loading Request from Sonata. In order to use Sonata Pages Bundle.
71
    $request = Request::createFromGlobals();
72
    if (!empty($_SERVER['TRUSTED_PROXIES'])) {
73
        $request->setTrustedProxies(
74
            preg_split('#,#', $_SERVER['TRUSTED_PROXIES']),
75
            Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT
76
        );
77
        // TRUSTED_PROXIES must be defined in .env. For non-legacy code, check config/packages/framework.yaml
78
    }
79
    // This 'load_legacy' variable is needed to know that symfony is loaded using old style legacy mode,
80
    // and not called from a symfony controller from public/
81
    $request->request->set('load_legacy', true);
82
    $currentBaseUrl = $request->getBaseUrl();
83
84
    if (empty($currentBaseUrl)) {
85
        $currentBaseUrl = $request->getSchemeAndHttpHost() . $request->getBasePath();
86
    }
87
88
    // Catch Symfony kernel exceptions (e.g. CidReqListener) in prod.
89
    // Needed because set_exception_handler() won't catch them here.
90
    try {
91
        $response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false);
92
    } catch (\Throwable $exception) {
93
        if (\in_array($kernel->getEnvironment(), ['dev', 'test'], true)) {
94
            throw $exception;
95
        }
96
97
        $event = new ExceptionEvent(
98
            $kernel,
99
            $request,
100
            HttpKernelInterface::MAIN_REQUEST,
101
            $exception
102
        );
103
104
        $listener = $kernel->getContainer()->get(ExceptionListener::class);
105
        if (is_callable($listener)) {
106
            $listener($event);
107
        }
108
109
        $response = $event->getResponse();
110
        if (!$response) {
111
            $response = new Response('An error occurred.', 500);
112
        }
113
114
        $response->send();
115
        exit;
116
    }
117
118
    $container = $kernel->getContainer();
119
    $router = $container->get('router');
120
    $context = $router->getContext();
121
    $router->setContext($context);
122
123
    // Catch legacy exceptions after kernel execution.
124
    // Complements the try/catch above for full coverage.
125
    set_exception_handler(function ($exception) use ($kernel, $container, $request) {
126
        if (
127
            in_array($kernel->getEnvironment(), ['dev', 'test'], true) &&
128
            !($exception instanceof NotAllowedException)
129
        ) {
130
            throw $exception;
131
        }
132
133
        $event = new ExceptionEvent(
134
            $kernel,
135
            $request,
136
            HttpKernelInterface::MAIN_REQUEST,
137
            $exception
138
        );
139
140
        /** @var callable $listener */
141
        $listener = $container->get(ExceptionListener::class);
142
        if (is_callable($listener)) {
143
            $listener($event);
144
        }
145
        $response = $event->getResponse();
146
        if (!$response) {
147
            $controller = Container::$container->get(ErrorController::class);
0 ignored issues
show
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
            /** @scrutinizer ignore-call */ 
148
            $controller = Container::$container->get(ErrorController::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            $response = $controller->show($request, $exception, null);
149
        }
150
        $response->send();
151
    });
152
153
    $context = Container::getRouter()->getContext();
154
155
    $currentUri = $request->getRequestUri();
156
157
    $fullUrl = $currentBaseUrl . $currentUri;
158
    $posMain = strpos($fullUrl, '/main');
159
    $posPlugin = strpos($fullUrl, '/plugin');
160
    $posCourse = strpos($fullUrl, '/course');
161
    $posCertificate = strpos($fullUrl, '/certificate');
162
163
    if (false === $posMain && false === $posPlugin && false === $posCourse && false === $posCertificate) {
164
        echo 'Cannot load current URL';
165
        exit;
166
    }
167
168
    if (false !== $posMain) {
169
        $newBaseUrl = substr($fullUrl, 0, $posMain);
170
    } elseif (false !== $posPlugin) {
171
        $newBaseUrl = substr($fullUrl, 0, $posPlugin);
172
    } elseif (false !== $posCourse) {
173
        $newBaseUrl = substr($fullUrl, 0, $posCourse);
174
    } elseif (false !== $posCertificate) {
175
        $newBaseUrl = substr($fullUrl, 0, $posCertificate);
176
    }
177
178
    $context->setBaseUrl($newBaseUrl);
179
180
    // Do not over-use this variable. It is only for this script's local use.
181
    $libraryPath = __DIR__.'/lib/';
182
    $container = $kernel->getContainer();
183
184
    // Symfony uses request_stack now
185
    $container->get('request_stack')->push($request);
186
    $container->get('translator')->setLocale($request->getLocale());
187
188
    $container->get('stof_doctrine_extensions.tool.locale_synchronizer')->setLocale($request->getLocale());
189
190
    /** @var FlashBag $flashBag */
191
    $flashBag = $request->getSession()->getFlashBag();
192
    $saveFlashBag = !empty($flashBag->keys()) ? $flashBag->all() : null;
193
194
    if (!empty($saveFlashBag)) {
195
        foreach ($saveFlashBag as $typeMessage => $messageList) {
196
            foreach ($messageList as $message) {
197
                Container::getSession()->getFlashBag()->add($typeMessage, $message);
198
            }
199
        }
200
    }
201
202
    $charset = 'UTF-8';
203
    ini_set('log_errors', '1');
204
    $this_section = SECTION_GLOBAL;
205
    define('DEFAULT_DOCUMENT_QUOTA', 100000000);
206
}
207