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/Database/LongPolling.php (10 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\Database;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \class LongPolling
25
 * \brief Use getUpdates saving and getting offset in Redis and database.
26
 */
27
trait LongPolling
28
{
29
    abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60);
30
31
    abstract protected function initCommands();
32
33
    /**
34
     * \addtogroup Database
35
     * @{
36
     */
37
38
    /**
39
     * @internal
40
     * \brief Get first update offset in Redis.
41
     * \details Called by getUpdatesRedis in order to get the saved offset in Redis or retrieve it from Telegram and save it.
42
     * @param string $offset_key Name of the variable where the offset is saved on Redis.
43
     * @return int Id of the first update to process.
44
     */
45
    protected function getUpdateOffsetRedis(string $offset_key) : int
46
    {
47
        $redis = $this->getRedis();
0 ignored issues
show
It seems like getRedis() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
        if ($redis->exists($offset_key)) {
49
            return $redis->get($offset_key);
50
        } else {
51
            // Get offset by first update.
52
            do {
53
                $update = $this->getUpdates(0, 1);
54
            } while (empty($update));
55
56
            $offset = $update[0]['update_id'];
57
58
            $redis->set($offset_key, $offset);
59
            return $offset;
60
        }
61
    }
62
63
    /**
64
     * \brief Get updates received by the bot, and use Redis to save and get the last offset.
65
     * \details It check if an offset exists on Redis: then get it or call getUpdates to set it.
66
     * Then it start a loop where it process updates and update the offset on Redis.
67
     * Each update is surrounded by a try/catch.
68
     * @see getUpdates
69
     * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
70
     * @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling.
71
     * @param string $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis.
72
     */
73
    public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset')
74
    {
75
        $redis = $this->getRedis();
0 ignored issues
show
It seems like getRedis() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
$redis 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...
76
77
        $offset = $this->getUpdateOffsetRedis($offset_key);
78
        $this->initCommands();
79
80
         // Process all updates received
81 View Code Duplication
        while (true) {
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...
82
            $updates = $this->getUpdates($offset, $limit, $timeout);
83
84
            foreach ($updates as $key => $update) {
85
                try {
86
                    $this->processUpdate($update);
0 ignored issues
show
It seems like processUpdate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
                } catch (BotException $e) {
88
                    echo $e->getMessage();
89
                }
90
            }
91
92
            $offset = $offset + count($updates);
93
            // Update the offset in redis
94
            $this->redis->set($offset_key, $offset);
0 ignored issues
show
The property redis 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...
95
        }
96
    }
97
98
    /**
99
     * @internal
100
     * \brief Get first update offset in database.
101
     * \details Called by getUpdatesDatabase to get the offset saved in database.
102
     * If the offset is not saved: it retrieve the offset from Telegram and save it on the database.
103
     * @param string $table_name Name of the table where offset is saved in the database.
104
     * @param string $column_name Name of the column where the offset is saved in the database.
105
     * @return int Id of the first update to process.
106
     */
107
    protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int
108
    {
109
        $sth = $this->pdo->prepare('SELECT ' . $column_name . ' FROM ' . $table_name);
0 ignored issues
show
The property pdo 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...
110
111
        try {
112
            $sth->execute();
113
        } catch (\PDOException $e) {
114
            echo $e->getMessage();
115
        }
116
117
        $offset = $sth->fetchColumn();
118
119
        $sth = null;
0 ignored issues
show
$sth 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...
120
121
        // Get the offset from the first update to update.
122
        if ($offset === false) {
123
            do {
124
                $update = $this->getUpdates(0, 1);
125
            } while (empty($update));
126
127
            $offset = $update[0]['update_id'];
128
        }
129
130
        return $offset;
131
    }
132
133
    /**
134
     * \brief Get updates received by the bot, using the SQL database to store and get the last offset.
135
     * \details It check if an offset exists on redis, then get it, or call getUpdates to set it.
136
     * Then it start a loop where it process updates and update the offset on Redis.
137
     * Each update is surrounded by a try/catch.
138
     * @see getUpdates
139
     * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
140
     * @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling.
141
     * @param string $table_name <i>Optional</i>. Name of the table where offset is saved in the database.
142
     * @param string $column_name <i>Optional</i>. Name of the column where the offset is saved in the database.
143
     */
144
    public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset')
145
    {
146
147
        if (!isset($this->_database)) {
148
            throw new BotException("Database connection is not set");
149
        }
150
151
        $offset = $this->getUpdateOffsetDatabase($table_name, $column_name);
152
        $this->initCommands();
153
154
        // Prepare the query for updating the offset in the database
155
        $sth = $this->pdo->prepare('UPDATE ' . $table_name . ' SET ' . $column_name . ' = :new_offset');
156
157 View Code Duplication
        while (true) {
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...
158
            $updates = $this->getUpdates($offset, $limit, $timeout);
159
160
            foreach ($updates as $key => $update) {
161
                try {
162
                    $this->processUpdate($update);
0 ignored issues
show
It seems like processUpdate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
163
                } catch (BotException $e) {
164
                    echo $e->getMessage();
165
                }
166
            }
167
168
            $offset = $offset + count($updates);
169
            // Update the offset on the database
170
            $sth->bindParam(':new_offset', $offset);
171
            $sth->execute();
172
        }
173
    }
174
175
    /** @} */
176
}
177