Issues (36)

src/Commands/AdminCommands/WhoisCommand.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Written by Jack'lul <[email protected]>
12
 */
13
14
namespace Longman\TelegramBot\Commands\AdminCommands;
15
16
use Longman\TelegramBot\Commands\AdminCommand;
17
use Longman\TelegramBot\DB;
18
use Longman\TelegramBot\Entities\Chat;
19
use Longman\TelegramBot\Entities\PhotoSize;
20
use Longman\TelegramBot\Entities\ServerResponse;
21
use Longman\TelegramBot\Entities\UserProfilePhotos;
22
use Longman\TelegramBot\Exception\TelegramException;
23
use Longman\TelegramBot\Request;
24
25
/**
26
 * Admin "/whois" command
27
 */
28
class WhoisCommand extends AdminCommand
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $name = 'whois';
34
35
    /**
36
     * @var string
37
     */
38
    protected $description = 'Lookup user or group info';
39
40
    /**
41
     * @var string
42
     */
43
    protected $usage = '/whois <id> or /whois <search string>';
44
45
    /**
46
     * @var string
47
     */
48
    protected $version = '1.3.0';
49
50
    /**
51
     * @var bool
52
     */
53
    protected $need_mysql = true;
54
55
    /**
56
     * Command execute method
57
     *
58
     * @return ServerResponse
59
     * @throws TelegramException
60
     */
61
    public function execute(): ServerResponse
62
    {
63
        $message = $this->getMessage();
64
65
        $chat_id = $message->getChat()->getId();
66
        $command = $message->getCommand();
67
        $text    = trim($message->getText(true));
0 ignored issues
show
It seems like $message->getText(true) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $text    = trim(/** @scrutinizer ignore-type */ $message->getText(true));
Loading history...
68
69
        $data = ['chat_id' => $chat_id];
70
71
        //No point in replying to messages in private chats
72
        if (!$message->getChat()->isPrivateChat()) {
73
            $data['reply_to_message_id'] = $message->getMessageId();
74
        }
75
76
        if ($command !== 'whois') {
77
            $text = substr($command, 5);
0 ignored issues
show
It seems like $command can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $text = substr(/** @scrutinizer ignore-type */ $command, 5);
Loading history...
78
79
            //We need that '-' now, bring it back
80
            if (strpos($text, 'g') === 0) {
81
                $text = str_replace('g', '-', $text);
82
            }
83
        }
84
85
        if ($text === '') {
86
            $text = 'Provide the id to lookup: /whois <id>';
87
        } else {
88
            $user_id    = $text;
89
            $chat       = null;
90
            $created_at = null;
91
            $updated_at = null;
92
            $result     = null;
93
94
            if (is_numeric($text)) {
95
                $results = DB::selectChats([
96
                    'groups'      => true,
97
                    'supergroups' => true,
98
                    'channels'    => true,
99
                    'users'       => true,
100
                    'chat_id'     => $user_id, //Specific chat_id to select
101
                ]);
102
103
                if (!empty($results)) {
104
                    $result = reset($results);
105
                }
106
            } else {
107
                $results = DB::selectChats([
108
                    'groups'      => true,
109
                    'supergroups' => true,
110
                    'channels'    => true,
111
                    'users'       => true,
112
                    'text'        => $text //Text to search in user/group name
113
                ]);
114
115
                if (is_array($results) && count($results) === 1) {
116
                    $result = reset($results);
117
                }
118
            }
119
120
            if (is_array($result)) {
121
                $result['id']       = $result['chat_id'];
122
                $result['username'] = $result['chat_username'];
123
                $chat               = new Chat($result);
124
125
                $user_id    = $result['id'];
126
                $created_at = $result['chat_created_at'];
127
                $updated_at = $result['chat_updated_at'];
128
                $old_id     = $result['old_id'];
129
            }
130
131
            if ($chat !== null) {
132
                if ($chat->isPrivateChat()) {
133
                    $text = 'User ID: ' . $user_id . PHP_EOL;
134
                    $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
135
136
                    $username = $chat->getUsername();
137
                    if ($username !== null && $username !== '') {
138
                        $text .= 'Username: @' . $username . PHP_EOL;
139
                    }
140
141
                    $text .= 'First time seen: ' . $created_at . PHP_EOL;
142
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
143
144
                    //Code from Whoami command
145
                    $limit    = 10;
146
                    $offset   = null;
147
                    $response = Request::getUserProfilePhotos(
148
                        [
149
                            'user_id' => $user_id,
150
                            'limit'   => $limit,
151
                            'offset'  => $offset,
152
                        ]
153
                    );
154
155
                    if ($response->isOk()) {
156
                        /** @var UserProfilePhotos $user_profile_photos */
157
                        $user_profile_photos = $response->getResult();
158
159
                        if ($user_profile_photos->getTotalCount() > 0) {
160
                            $photos = $user_profile_photos->getPhotos();
161
162
                            /** @var PhotoSize $photo */
163
                            $photo   = $photos[0][2];
164
                            $file_id = $photo->getFileId();
165
166
                            $data['photo']   = $file_id;
167
                            $data['caption'] = $text;
168
169
                            return Request::sendPhoto($data);
170
                        }
171
                    }
172
                } elseif ($chat->isGroupChat()) {
173
                    $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
174
                    $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
175
                    $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
176
                    $text .= 'First time added to group: ' . $created_at . PHP_EOL;
177
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
178
                }
179
            } elseif (is_array($results) && count($results) > 1) {
180
                $text = 'Multiple chats matched!';
181
            } else {
182
                $text = 'Chat not found!';
183
            }
184
        }
185
186
        $data['text'] = $text;
187
188
        return Request::sendMessage($data);
189
    }
190
}
191