Issues (18)

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/Translation/Writer.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 Nikaia\TranslationSheet\Translation;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
use Nikaia\TranslationSheet\Commands\Output;
10
use Nikaia\TranslationSheet\Sheet\TranslationsSheet;
11
use Nikaia\TranslationSheet\Spreadsheet;
12
use Nikaia\TranslationSheet\Util;
13
14
class Writer
15
{
16
    use Output;
17
18
    /** @var Collection */
19
    protected $translations;
20
21
    /** @var Spreadsheet */
22
    protected $spreadsheet;
23
24
    /** @var Filesystem */
25
    protected $files;
26
27
    /** @var Application */
28
    protected $app;
29 2
30
    /** @var TranslationsSheet */
31 2
    protected $translationSheet;
32 2
33 2
    public function __construct(Spreadsheet $spreadsheet, Filesystem $files, Application $app)
34
    {
35 2
        $this->spreadsheet = $spreadsheet;
36 2
        $this->files = $files;
37
        $this->app = $app;
38 2
39
        $this->nullOutput();
40 2
    }
41
42 2
    /**
43
     * Set the translation sheets for reader.
44
     *
45 2
     * @param TranslationsSheet $translationSheet
46
     * @return Writer
47
     */
48 2
    public function setTranslationsSheet(TranslationsSheet $translationSheet)
49
    {
50 2
        $this->translationSheet = $translationSheet;
51 2
52
        return $this;
53
    }
54 2
55 2
56
    public function setTranslations($translations)
57 2
    {
58
        $this->translations = $translations;
59 2
60
        return $this;
61 2
    }
62
63 2
    public function write()
64 1
    {
65
        $this
66
            ->groupTranslationsByFile()
67 2
            ->each(function ($items, $sourceFile) {
68 2
69
                if ($this->files->extension($sourceFile) === 'json') {
70 2
                    $this->writeJsonFile(
71
                        $this->translationSheet->getPath() . '/' . $sourceFile,
72
                        $items
73 2
                    );
74 2
                    return;
75
                }
76 2
77 2
                $this->writeFile(
78
                    $this->translationSheet->getPath() . '/' . $sourceFile,
79
                    $items
80 2
                );
81 2
            });
82 2
    }
83
84
    protected function writeFile($file, $items)
85 2
    {
86
        $this->output->writeln('    <comment>php</comment> ' . $file);
87
88 2
        $content = sprintf('<?php%s%sreturn %s;%s', PHP_EOL, PHP_EOL, Util::varExport($items), PHP_EOL);
89
90 2 View Code Duplication
        if (!$this->files->isDirectory($dir = dirname($file))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91 2
            $this->files->makeDirectory($dir, 0755, true);
92
        }
93 2
94 2
        $this->files->put($file, $content);
95
    }
96
97
    protected function writeJsonFile($file, $items)
98
    {
99
        $this->output->writeln('    <comment>json</comment> ' . $file);
100 2
101 View Code Duplication
        if (!$this->files->isDirectory($dir = dirname($file))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            $this->files->makeDirectory($dir, 0755, true);
103
        }
104 2
        $this->files->put($file, json_encode($items, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL);
105 2
    }
106 2
107
    protected function groupTranslationsByFile()
108
    {
109 2
        $items = $this
110
            ->translations
111
            ->groupBy('sourceFile')
112
            ->map(function ($fileTranslations) {
113 2
                return $this->buildTranslationsForFile($fileTranslations);
114
            });
115
116
        // flatten does not seem to work for every case. !!! refactor !!!
117
        $result = [];
118
        foreach ($items as $subitems) {
119
            $result = array_merge($result, $subitems);
120
        }
121
122
        return new Collection($result);
123
    }
124
125
    protected function buildTranslationsForFile($fileTranslations)
126
    {
127
        $files = [];
128
        $locales = $this->spreadsheet->getLocales();
129
130
        foreach ($locales as $locale) {
131
            foreach ($fileTranslations as $translation) {
132
133
                // We will only write non empty translations
134
                // For instance, we have `app.title` that is the same for each locale,
135
                // We dont want to translate it to every locale, and prefer letting
136
                // Laravel default back to the default locale.
137
                if ($this->skipToDefault($translation, $locale)) {
138
                    continue;
139
                }
140
141
                $localeFile = $this->fileIsJson($translation['sourceFile']) ?
142
                    $locale . '.json' :
143
                    str_replace('{locale}/', $locale . '/', $translation['sourceFile']);
144
145
                if (empty($files[$localeFile])) {
146
                    $files[$localeFile] = [];
147
                }
148
149
                if ($this->fileIsJson($translation['sourceFile'])) {
150
                    $files[$localeFile][$translation['key']] = $translation[$locale];
151
                } else {
152
                    Arr::set($files[$localeFile], $translation['key'], $translation[$locale]);
153
                }
154
            }
155
        }
156
157
        return $files;
158
    }
159
160
    protected function skipToDefault($translation, $locale)
161
    {
162
        if (!isset($translation[$locale])) {
163
            return true;
164
        }
165
166
        return empty($translation[$locale]) && $this->fileIsJson($translation['sourceFile']);
167
    }
168
169
    protected function fileIsJson($sourceFile)
170
    {
171
        return $sourceFile === '{locale}.json';
172
    }
173
174
}
175