Issues (5)

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/Commands/LoadTranslationsFromFiles.php (2 issues)

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 MikeZange\LaravelDatabaseTranslation\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Translation\FileLoader;
8
use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository;
9
10
/**
11
 * Class LoadTranslationsFromFiles.
12
 */
13
class LoadTranslationsFromFiles extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'trans-db:load {locale}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Loads the translations from file into the DB for a given locale';
28
29
    /**
30
     * The Laravel File Loader.
31
     *
32
     * @var FileLoader
33
     */
34
    protected $laravelLoader;
35
36
    /**
37
     * Translation repository.
38
     *
39
     * @var TranslationRepository
40
     */
41
    protected $translationRepository;
42
43
    /**
44
     * Laravel Filesystem.
45
     *
46
     * @var Filesystem
47
     */
48
    protected $filesystem;
49
50
    /**
51
     * The locale.
52
     *
53
     * @var string
54
     */
55
    protected $locale;
56
57
    /**
58
     * Create a new command instance.
59
     *
60
     * @param Filesystem            $filesystem
61
     * @param TranslationRepository $translationRepository
62
     */
63
    public function __construct(Filesystem $filesystem, TranslationRepository $translationRepository)
64
    {
65
        parent::__construct();
66
67
        //this is so we can benefit from namespaces that are already loaded
68
        $this->laravelLoader = app()['translator']->getLoader()->getFileLoader();
69
        $this->translationRepository = $translationRepository;
70
        $this->filesystem = $filesystem;
71
    }
72
73
    /**
74
     * Execute the console command.
75
     *
76
     * @return mixed
77
     */
78
    public function handle()
79
    {
80
        $this->locale = $this->argument('locale');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('locale') can also be of type array. However, the property $locale is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
82
        $this->loadGroupLines();
83
84
        $this->loadNameSpacedLines();
85
86
        return $this->info('Complete');
87
    }
88
89
    protected function loadGroupLines()
90
    {
91
        $langFiles = $this->getLangFiles(resource_path().'/lang');
92
93
        $this->processFiles($langFiles);
94
    }
95
96
    protected function loadNameSpacedLines()
97
    {
98
        foreach ($this->laravelLoader->namespaces() as $namespace => $path) {
99
            $langFiles = $this->getLangFiles($path);
100
            $this->processFiles($langFiles, $namespace);
101
        }
102
    }
103
104
    /**
105
     * @param $files
106
     * @param null|string $namespace
107
     */
108
    protected function processFiles($files, $namespace = null)
109
    {
110
        foreach ($files as $file) {
111
            $group = $this->getGroup($file);
112
            $lines = $this->getLines($file);
113
            $this->loadLines($lines, $namespace, $group, $this->locale);
114
        }
115
    }
116
117
    /**
118
     * Load the lines into the database.
119
     *
120
     * @param $lines
121
     * @param $namespace
122
     * @param $group
123
     * @param $locale
124
     */
125
    protected function loadLines($lines, $namespace, $group, $locale)
126
    {
127
        foreach ($lines as $key => $value) {
128
            $line = $this->translationRepository->getItem($namespace, $group, $key);
129
            if ($line) {
130
                $this->translationRepository->updateTranslation($line, $this->locale, $value, false);
0 ignored issues
show
$line of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<MikeZange\Laravel...ion\Models\Translation>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
131
            } else {
132
                $attributes = [
133
                    'namespace' => $namespace,
134
                    'group'     => $group,
135
                    'key'       => $key,
136
                    'values'    => [
137
                        "{$locale}" => $value,
138
                    ],
139
                ];
140
                $this->translationRepository->create($attributes);
141
            }
142
        }
143
    }
144
145
    /**
146
     * @param $file
147
     *
148
     * @return mixed
149
     */
150
    protected function getGroup($file)
151
    {
152
        preg_match('/'.$this->locale.'\/([^\[]+).php/i', $file, $group);
153
154
        return $group[1];
155
    }
156
157
    /**
158
     * @param $file
159
     *
160
     * @return array
161
     */
162
    protected function getLines($file)
163
    {
164
        return array_dot($this->filesystem->getRequire($file));
165
    }
166
167
    /**
168
     * @param $path
169
     *
170
     * @return array
171
     */
172
    protected function getLangFiles($path)
173
    {
174
        $dir = $this->getLocaleDirPath($path);
175
176
        return $this->filesystem->files($dir);
177
    }
178
179
    /**
180
     * @param $path
181
     *
182
     * @return string
183
     */
184
    protected function getLocaleDirPath($path)
185
    {
186
        return $path.'/'.$this->locale;
187
    }
188
}
189