Issues (64)

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/Conversation.php (1 issue)

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
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot;
12
13
/**
14
 * Class Conversation
15
 *
16
 * Only one conversation can be active at any one time.
17
 * A conversation is directly linked to a user, chat and the command that is managing the conversation.
18
 */
19
class Conversation
20
{
21
    /**
22
     * All information fetched from the database
23
     *
24
     * @var array|null
25
     */
26
    protected $conversation;
27
28
    /**
29
     * Notes stored inside the conversation
30
     *
31
     * @var mixed
32
     */
33
    protected $protected_notes;
34
35
    /**
36
     * Notes to be stored
37
     *
38
     * @var mixed
39
     */
40
    public $notes;
41
42
    /**
43
     * Telegram user id
44
     *
45
     * @var int
46
     */
47
    protected $user_id;
48
49
    /**
50
     * Telegram chat id
51
     *
52
     * @var int
53
     */
54
    protected $chat_id;
55
56
    /**
57
     * Command to be executed if the conversation is active
58
     *
59
     * @var string
60
     */
61
    protected $command;
62
63
    /**
64
     * Conversation contructor to initialize a new conversation
65
     *
66
     * @param int    $user_id
67
     * @param int    $chat_id
68
     * @param string $command
69
     *
70
     * @throws \Longman\TelegramBot\Exception\TelegramException
71
     */
72 9
    public function __construct($user_id, $chat_id, $command = null)
73
    {
74 9
        $this->user_id = $user_id;
75 9
        $this->chat_id = $chat_id;
76 9
        $this->command = $command;
77
78
        //Try to load an existing conversation if possible
79 9
        if (!$this->load() && $command !== null) {
80
            //A new conversation start
81 6
            $this->start();
82
        }
83 8
    }
84
85
    /**
86
     * Clear all conversation variables.
87
     *
88
     * @return bool Always return true, to allow this method in an if statement.
89
     */
90 2
    protected function clear()
91
    {
92 2
        $this->conversation    = null;
93 2
        $this->protected_notes = null;
94 2
        $this->notes           = null;
95
96 2
        return true;
97
    }
98
99
    /**
100
     * Load the conversation from the database
101
     *
102
     * @return bool
103
     * @throws \Longman\TelegramBot\Exception\TelegramException
104
     */
105 9
    protected function load()
106
    {
107
        //Select an active conversation
108 9
        $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
0 ignored issues
show
1 is of type integer, but the function expects a boolean|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109 9
        if (isset($conversation[0])) {
110
            //Pick only the first element
111 5
            $this->conversation = $conversation[0];
112
113
            //Load the command from the conversation if it hasn't been passed
114 5
            $this->command = $this->command ?: $this->conversation['command'];
115
116 5
            if ($this->command !== $this->conversation['command']) {
117
                $this->cancel();
118
                return false;
119
            }
120
121
            //Load the conversation notes
122 5
            $this->protected_notes = json_decode($this->conversation['notes'], true);
123 5
            $this->notes           = $this->protected_notes;
124
        }
125
126 9
        return $this->exists();
127
    }
128
129
    /**
130
     * Check if the conversation already exists
131
     *
132
     * @return bool
133
     */
134 9
    public function exists()
135
    {
136 9
        return ($this->conversation !== null);
137
    }
138
139
    /**
140
     * Start a new conversation if the current command doesn't have one yet
141
     *
142
     * @return bool
143
     * @throws \Longman\TelegramBot\Exception\TelegramException
144
     */
145 6
    protected function start()
146
    {
147 6
        if ($this->command
148 6
            && !$this->exists()
149 6
            && ConversationDB::insertConversation(
150 6
                $this->user_id,
151 6
                $this->chat_id,
152 6
                $this->command
153
            )
154
        ) {
155 5
            return $this->load();
156
        }
157
158
        return false;
159
    }
160
161
    /**
162
     * Delete the current conversation
163
     *
164
     * Currently the Conversation is not deleted but just set to 'stopped'
165
     *
166
     * @return bool
167
     */
168 1
    public function stop()
169
    {
170 1
        return ($this->updateStatus('stopped') && $this->clear());
171
    }
172
173
    /**
174
     * Cancel the current conversation
175
     *
176
     * @return bool
177
     */
178 1
    public function cancel()
179
    {
180 1
        return ($this->updateStatus('cancelled') && $this->clear());
181
    }
182
183
    /**
184
     * Update the status of the current conversation
185
     *
186
     * @param string $status
187
     *
188
     * @return bool
189
     */
190 2
    protected function updateStatus($status)
191
    {
192 2
        if ($this->exists()) {
193 2
            $fields = ['status' => $status];
194
            $where  = [
195 2
                'id'      => $this->conversation['id'],
196 2
                'status'  => 'active',
197 2
                'user_id' => $this->user_id,
198 2
                'chat_id' => $this->chat_id,
199
            ];
200 2
            if (ConversationDB::updateConversation($fields, $where)) {
201 2
                return true;
202
            }
203
        }
204
205
        return false;
206
    }
207
208
    /**
209
     * Store the array/variable in the database with json_encode() function
210
     *
211
     * @return bool
212
     */
213 1
    public function update()
214
    {
215 1
        if ($this->exists()) {
216 1
            $fields = ['notes' => json_encode($this->notes)];
217
            //I can update a conversation whatever the state is
218 1
            $where = ['id' => $this->conversation['id']];
219 1
            if (ConversationDB::updateConversation($fields, $where)) {
220 1
                return true;
221
            }
222
        }
223
224
        return false;
225
    }
226
227
    /**
228
     * Retrieve the command to execute from the conversation
229
     *
230
     * @return string|null
231
     */
232 3
    public function getCommand()
233
    {
234 3
        return $this->command;
235
    }
236
}
237