Issues (94)

Security Analysis    no request data  

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/Localization/File.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
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Localization;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \class File Localization Files handler
25
 * \brief Handle localizations files, load them from disk.
26
 */
27
trait File
28
{
29
    /**
30
     * \addtogroup Localization Localization
31
     * \brief Methods to create a localized bot.
32
     * \details Localization files must have this syntax:
33
     * file <code>./localization/en.json</code>:
34
     *
35
     *     {"Hello_Msg": "Hello"}
36
     *
37
     * File <code>./localization/it.json</code>:
38
     *
39
     *     {"Hello_Msg": "Ciao"}
40
     *
41
     * Usage in <code>processMessage()</code>:
42
     *
43
     *     $sendMessage($this->local[$this->language]["Hello_Msg"]);
44
     *
45
     * @{
46
     */
47
48
    /** @internal
49
      * \brief Store the localized strings. */
50
    protected $local;
51
52
    /** \brief Source for localization files. */
53
    protected $localization_dir = './localization';
54
55
    /**
56
     * @internal
57
     * \brief Load a localization file into the localized strings array.
58
     * @param string $lang Language to load.
59
     * @param string $dir Directory in which there are the JSON files.
60
     * @return bool True if loaded.
61
     */
62
    protected function loadSingleLanguage(string $lang = 'en', string $dir = './localization') : bool
63
    {
64
        // Name of the file
65
        $filename = "$dir/$lang";
66
67
        // If this language isn't already set
68
        if (!isset($this->local[$lang])) {
69
            // and the file exists
70
            if (file_exists($filename)) {
71
                // Load localization in memory
72
                $this->local[$lang] = json_decode(file_get_contents($filename), true);
73
74
                // We loaded it
75
                return true;
76
            }
77
            // The file doens't exists
78
            return false;
79
        }
80
81
        // The language is already set
82
        return true;
83
    }
84
85
    /**
86
     * \brief Load all localization files (like JSON) from a folder and set them in <code>$local</code> variable.
87
     * \details Save all localization files, using JSON format, from a directory and put
88
     * the contents in <code>$local</code> variable.
89
     *
90
     * Each file will be saved into <code>$local</code> with the first two letters of the filename as the index.
91
     * @param string $dir Source directory for localization files.
92
     * @return bool True if the directory could be opened without errors.
93
     */
94
    public function loadAllLanguages(string $dir = './localization') : bool
95
    {
96
        if ($handle = opendir($dir)) {
97
            // Iterate over all files
98
            while (false !== ($file = readdir($handle))) {
99
                // If the file is a JSON data file
100
                if (strlen($file) > 6 && substr($file, -5) === '.json') {
101
                    try {
102
                        // Add the contents of the file to the $local variable, after converting it to a PHP object.
103
                        // The contents will be added with the 2 letter of the file as the index
104
                        $this->local[substr($file, 0, 2)] = json_decode(file_get_contents("$dir/$file"), true);
105
                    } catch (BotException $e) {
106
                        echo $e->getMessage();
107
                    }
108
                }
109
            }
110
            return true;
111
        }
112
        return false;
113
    }
114
115
    /**
116
     * @internal
117
     * \brief Load localization for current language and mode.
118
     * \details This method will load only the current user/group localization file if the bot is using webhook, all files otherwise.
119
     * @param string $dir Directory where the localization file is stored. If no directory is given (by default) it will load it from $localization_dir (which is ./localization if it is not set).
120
     * @return bool True if the localization has been loaded or it is already loaded.
121
     */
122
    public function loadCurrentLocalization(string $dir = '') : bool
123
    {
124
        // If no dir has been given
125
        if (!isset($dir) || $dir === '') {
126
            // The dir is the default one
127
            $dir = $this->localization_dir;
0 ignored issues
show
$dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
        }
129
130
        // If the language of the user is already set in the array containing localizated strings
131
        if (!isset($this->local[$this->bot->language])) {
132
            // Is the bot using webhook?
133
            if (isset($this->bot->_is_webhook)) {
0 ignored issues
show
The property bot does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
134
                return $this->loadSingleLanguage($this->localization_dir);
135
            } else {
136
                return $this->loadAllLanguages($this->localization_dir);
137
            }
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * \brief Change source directory for localization files.
145
     * @param string $dir Source directory.
146
     */
147
    public function setLocalizationDir(string $dir)
148
    {
149
        $this->localization_dir = $dir;
150
    }
151
152
    /** @} */
153
}
154