Issues (86)

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.

app/Interfaces/Slack/Client.php (5 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 GitterBot package.
5
 *
6
 * @author Serafim <[email protected]>
7
 * @author butschster <[email protected]>
8
 * @date 24.09.2015 00:00
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Interfaces\Slack;
15
16
use Domains\Bot\ClientInterface;
17
use Domains\Message;
18
use Domains\Middleware\Storage;
19
use Domains\Room\RoomInterface;
20
use Domains\User;
21
22
/**
23
 * Class Client
24
 */
25
class Client implements ClientInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $token;
31
32
    /**
33
     * @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop
34
     */
35
    protected $loop;
36
37
    /**
38
     * @var \Slack\RealTimeClient
39
     */
40
    protected $client;
41
42
    /**
43
     * @var TextParser
44
     */
45
    protected $parser;
46
47
    /**
48
     * Client constructor.
49
     *
50
     * @param string $token
51
     */
52
    public function __construct($token)
53
    {
54
        $this->token = $token;
55
        $this->loop = \React\EventLoop\Factory::create();
56
        $this->client = new \Slack\RealTimeClient($this->loop);
57
58
        $this->client->setToken($token);
59
        $this->parser = new TextParser('');
0 ignored issues
show
The call to TextParser::__construct() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
    }
61
62
    /**
63
     * @param RoomInterface $room
64
     * @param string        $message
65
     *
66
     * @return void
67
     */
68
    public function sendMessage(RoomInterface $room, $message)
69
    {
70
        $this->client->getChannelById($room->id())->then(function (\Slack\Channel $channel) use($message) {
71
            $this->client->apiCall('chat.postMessage', [
72
                'text' => (string) $this->parser->parse($message),
73
                'channel' => $channel->getId(),
74
                'as_user' => true,
75
            ]);
76
        });
77
    }
78
79
    /**
80
     * @param RoomInterface $room
81
     *
82
     * @return void
83
     */
84
    public function listen(RoomInterface $room)
85
    {
86
        $this->client->on('message', function (\Slack\Payload $msg) use($room) {
87
            if ($msg->getData()['channel'] != $room->id()) {
88
                return;
89
            }
90
91
            $this->onMessage(
92
                $room->middleware(),
93
                Message::unguarded(function() use($room, $msg) {
94
                    return new Message(
0 ignored issues
show
Deprecated Code introduced by
The class Domains\Message has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
95
                        (new MessageMapper($room, $msg->getData()))->toArray(),
96
                        $room
97
                    );
98
                })
99
            );
100
        });
101
102
        $this->client->on('reaction_added', function (\Slack\Payload $payload) use($room) {
103
            if ($payload->getData()['item']['type'] != 'message' and $payload->getData()['item']['channel'] != $room->id()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
104
                return;
105
            }
106
107
            $this->onMessage(
108
                $room->middleware(),
109
                Message::unguarded(function() use($room, $payload) {
110
                    $message = $payload->getData();
111
                    $message['mentions'] = [$message['item_user']];
112
                    $message['text'] = 'Спасибо';
113
114
                    return new Message(
0 ignored issues
show
Deprecated Code introduced by
The class Domains\Message has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
115
                        (new MessageMapper($room, $message))->toArray(),
116
                        $room
117
                    );
118
                })
119
            );
120
        });
121
122
        $this->client->connect();
123
    }
124
125
126
    /**
127
     * @param Storage $middleware
128
     * @param Message $message
129
     */
130
    public function onMessage(Storage $middleware, Message $message)
131
    {
132
        try {
133
            $middleware->handle($message);
134
        } catch (\Exception $e) {
135
            $this->logException($e);
136
        }
137
    }
138
139
    protected function onClose()
140
    {
141
        $this->client->disconnect();
142
    }
143
144
    /**
145
     * @param \Exception $e
146
     */
147
    protected function onError(\Exception $e)
148
    {
149
        $this->logException($e);
150
    }
151
    /**
152
     * @return string
153
     */
154
    public function version()
155
    {
156
        return 'SlackBot v0.1b';
157
    }
158
159
    /**
160
     * @return ClientInterface
161
     */
162
    public function run(): ClientInterface
163
    {
164
        $this->loop->run();
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param \Exception $e
171
     */
172 View Code Duplication
    protected function logException(\Exception $e)
0 ignored issues
show
This method seems to be duplicated in 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...
173
    {
174
        \Log::error(
175
            $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() . "\n" .
176
            $e->getTraceAsString() . "\n" .
177
            str_repeat('=', 80) . "\n"
178
        );
179
    }
180
181
    /**
182
     * @param string $id
183
     *
184
     * @return User
185
     */
186
    public function getUserById($id)
187
    {
188
        $user = null;
189
190
        $this->client->getUserById($id)->then(function(\Slack\User $slackUser) use(&$user) {
191
            $user = UserMapper::fromSlackObject(
192
                $slackUser
193
            );
194
        });
195
196
        return $user;
197
    }
198
}