Issues (20)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Detect.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Locale\Detectors\FallbackDetector;
7
use Thinktomorrow\Locale\Detectors\HiddenSegmentDetector;
8
use Thinktomorrow\Locale\Detectors\QueryDetector;
9
use Thinktomorrow\Locale\Detectors\SegmentDetector;
10
use Thinktomorrow\Locale\Values\ApplicationLocale;
11
use Thinktomorrow\Locale\Values\Config;
12
use Thinktomorrow\Locale\Values\Locale;
13
14
final class Detect
15
{
16
    /**
17
     * @var Request
18
     */
19
    private $request;
20
21
    /**
22
     * @var Config
23
     */
24
    private $config;
25
26
    /**
27
     * @var Locale
28
     */
29
    private $locale;
30
31
    /**
32
     * Current scope of locales.
33
     *
34
     * @var Scope
35
     */
36
    private $scope;
37
38 75
    public function __construct(Request $request, Config $config)
0 ignored issues
show
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
39
    {
40 75
        $this->request = $request;
41 75
        $this->config = $config;
42 75
    }
43
44
    /**
45
     * Detect the locale from current request url.
46
     * Once the locale has been determined, it will be set as the application locale.
47
     * A locale is only validated if it is present within the current locale scope.
48
     *
49
     * Detection honours following priority:
50
     * 1) If locale is found in request as query parameter e.g. ?locale=fr,
51
     * 2) If locale is found in request url, either from host or segment eg. nl.example.com, example.nl or example.com/nl
52
     * 3) Otherwise set locale to our fallback language (app.locale)
53
     */
54 72
    public function detectLocale(): self
55
    {
56 72
        $locale = null;
57
58
        $detectors = [
59 72
            FallbackDetector::class,
60
            HiddenSegmentDetector::class,
61
            SegmentDetector::class,
62
            QueryDetector::class,
63
        ];
64
65 72
        foreach ($detectors as $detector) {
66 72
            $locale = app($detector)->get($this->getScope(), $this->config) ?? $locale;
67
        }
68
69 72
        $this->locale = $locale;
70
71 72
        Scope::setActiveLocale($locale);
72
73 72
        $this->setApplicationLocale();
74
75 72
        return $this;
76
    }
77
78 72
    public function getLocale(): Locale
79
    {
80 72
        if (!$this->locale) {
81 72
            $this->detectLocale();
82
        }
83
84 72
        return $this->locale;
85
    }
86
87 75
    public function getScope(): Scope
88
    {
89 75
        if (!$this->scope) {
90 74
            $this->detectScope();
91
        }
92
93 75
        return $this->scope;
94
    }
95
96
    public function getConfig(): Config
97
    {
98
        return $this->config;
99
    }
100
101
    /**
102
     * This is handy for setting allowed scope via other source than config file.
103
     * this can be configurable from a cms.
104
     *
105
     * @param Scope|null $scope
106
     *
107
     * @return $this
108
     */
109 2
    public function setScope(Scope $scope = null)
110
    {
111 2
        if ($scope) {
112 2
            $this->scope = $scope;
113
        }
114
115 2
        return $this;
116
    }
117
118 74
    private function detectScope()
119
    {
120 74
        $this->scope = ScopeCollection::fromConfig($this->config)->findByRoot($this->request->root());
121 74
    }
122
123 72
    private function setApplicationLocale()
124
    {
125 72
        $applicationLocale = ApplicationLocale::from($this->locale);
126
127 72
        app()->setLocale($applicationLocale->__toString());
128 72
    }
129
}
130