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/Commands/CommandHandler.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\Commands;
20
21
/*
22
 * \class CommandHandler
23
 * \brief Handle all bot commands.
24
 * \details Add commands, check if commands trigger a user update.
25
 */
26
trait CommandHandler
27
{
28
    /**
29
     * \addtogroup Commands
30
     * \brief Bots command and usage.
31
     * @{
32
     */
33
34
    /** @internal
35
     * \brief Contains all command used by the bot. */
36
    private $_commands = [];
37
38
    /**
39
     * @internal
40
     * \brief Initialize commands to speed up processing.
41
     * \details Get all command that the bot handle, and put them in priority.
42
     */
43 3
    protected function initCommands()
44
    {
45 3
        $commands_temp = $this->_commands;
46 3
        $this->_commands = [];
47
48
        // Iterate over each
49 3
        foreach ($commands_temp as $command) {
50 1
            $this->_commands[$command::$type][] = $command;
51
        }
52
53 3
        foreach ($this->_commands as $index => $array) {
54
        // Sort them by priority
55 1
            uasort($this->_commands[$index], 'PhpBotFramework\Commands\CommandHandler::sortingPrior');
56
        }
57 3
    }
58
59
    /**
60
     * @internal
61
     * \brief Process updates prioritizing bot's commands over the general methods (e.g. BaseBot::processMessage())
62
     * @param array $update Update to process.
63
     * @return bool True if this update trigger any command.
64
     */
65 5
    protected function processCommands(array $update) : bool
66
    {
67
        // For each command active (checked by initCommands())
68 5
        foreach ($this->_commands as $entity => $commands) {
69 3
            foreach ($commands as $command) {
70
                // If the update type is right and the update triggered a command
71 3
                if (isset($update[$entity]) && $command->checkCommand($update[$entity])) {
72 3
                    $entity = new $command::$object_class($update[$entity]);
73 3
                    $this->_chat_id = $entity->getChatID();
0 ignored issues
show
The property _chat_id 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...
74 3
                    $command->getScript()($this, $entity);
75
                    // Return the id as we already processed this update
76 3
                    return true;
77
                }
78
            }
79
        }
80
81
        // Return -1 because this update didn't trigger any command.
82 2
        return false;
83
    }
84
85
    /**
86
     * \brief Add a command to the bot.
87
     * @param BasicCommand $command Command to add. Must be an object that inherits BasicCommand class.
88
     */
89 2
    public function addCommand(BasicCommand $command)
90
    {
91 2
        $this->_commands[] = $command;
92 2
    }
93
94
    /**
95
     * \brief Add various commands at once.
96
     * @param ...BasicCommand $commands The commands to add.
0 ignored issues
show
The doc-type ...BasicCommand could not be parsed: Unknown type name "...BasicCommand" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
97
     */
98 1
    public function addCommands(BasicCommand ...$commands)
99
    {
100 1
      foreach ($commands as $command) {
101 1
            $this->addCommand($command);
102
        }
103 1
    }
104
105
    /**
106
     * \brief Add a message command to the bot.
107
     * @param string $command The command that will trigger the function (e.g. start).
108
     * @param callable $script The function that will be triggered by a command.
109
     */
110 1
    public function addMessageCommand(string $command, callable $script)
111
    {
112 1
      $this->_commands[] = new MessageCommand($command, $script);
113 1
    }
114
115
    /**
116
     * \brief Add a callback command to the bot.
117
     * @param string $data The data that will trigger the function.
118
     * @param callable $script The function that will be triggered by the data.
119
     */
120
    public function addCallbackCommand(string $data, callable $script)
121
    {
122
      $this->_commands[] = new CallbackCommand($data, $script);
123
    }
124
125
    /**
126
     * @internal
127
     * \brief Sort an array based on <code>prior</code> index value.
128
     * @param array $a First array.
129
     * @param array $b Second array.
130
     * @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise.
131
     */
132
    public static function sortingPrior($a, $b)
133
    {
134
        $prior_a = $a::$priority;
135
        $prior_b = $b::$priority;
136
        if ($prior_a > $prior_b) {
137
            return 1;
138
        }
139
140
        if ($prior_a < $prior_b) {
141
            return -1;
142
        }
143
144
        if ($prior_a == $prior_b) {
145
            return 0;
146
        }
147
    }
148
149
    /** @} */
150
}
151