Issues (2091)

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

Labels
Severity
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Controller\ExceptionController;
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
            $statusCode = $exception instanceof NotAllowedException
148
                ? Response::HTTP_OK
149
                : Response::HTTP_INTERNAL_SERVER_ERROR;
150
151
            $response = new Response(
152
                $exception->getMessage(),
153
                $statusCode
154
            );
155
        }
156
        $response->send();
157
    });
158
159
    $context = Container::getRouter()->getContext();
160
161
    $currentUri = $request->getRequestUri();
162
163
    $fullUrl = $currentBaseUrl . $currentUri;
164
    $posMain = strpos($fullUrl, '/main');
165
    $posPlugin = strpos($fullUrl, '/plugin');
166
    $posCourse = strpos($fullUrl, '/course');
167
    $posCertificate = strpos($fullUrl, '/certificate');
168
169
    if (false === $posMain && false === $posPlugin && false === $posCourse && false === $posCertificate) {
170
        echo 'Cannot load current URL';
171
        exit;
172
    }
173
174
    if (false !== $posMain) {
175
        $newBaseUrl = substr($fullUrl, 0, $posMain);
176
    } elseif (false !== $posPlugin) {
177
        $newBaseUrl = substr($fullUrl, 0, $posPlugin);
178
    } elseif (false !== $posCourse) {
179
        $newBaseUrl = substr($fullUrl, 0, $posCourse);
180
    } elseif (false !== $posCertificate) {
181
        $newBaseUrl = substr($fullUrl, 0, $posCertificate);
182
    }
183
184
    $context->setBaseUrl($newBaseUrl);
185
186
    try {
187
        // Do not over-use this variable. It is only for this script's local use.
188
        $libraryPath = __DIR__.'/lib/';
189
        $container = $kernel->getContainer();
190
191
        // Symfony uses request_stack now
192
        $container->get('request_stack')->push($request);
193
        $container->get('translator')->setLocale($request->getLocale());
194
195
        $container->get('stof_doctrine_extensions.tool.locale_synchronizer')->setLocale($request->getLocale());
196
197
        /** @var FlashBag $flashBag */
198
        $flashBag = $request->getSession()->getFlashBag();
199
        $saveFlashBag = !empty($flashBag->keys()) ? $flashBag->all() : null;
200
201
        if (!empty($saveFlashBag)) {
202
            foreach ($saveFlashBag as $typeMessage => $messageList) {
203
                foreach ($messageList as $message) {
204
                    Container::getSession()->getFlashBag()->add($typeMessage, $message);
205
                }
206
            }
207
        }
208
209
        $charset = 'UTF-8';
210
        ini_set('log_errors', '1');
211
        $this_section = SECTION_GLOBAL;
212
        define('DEFAULT_DOCUMENT_QUOTA', 100000000);
213
    } catch (Exception $e) {
214
        $controller = new ExceptionController();
0 ignored issues
show
The call to Chamilo\CoreBundle\Contr...ntroller::__construct() has too few arguments starting with urlGenerator. ( Ignorable by Annotation )

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

214
        $controller = /** @scrutinizer ignore-call */ new ExceptionController();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
215
        $controller->show($e);
216
    }
217
}
218