Passed
Push — main ( fce709...e7b3ac )
by Miaad
02:07
created

is::isUsername()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 2
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace BPT\tools;
4
5
use BPT\api\request;
6
use BPT\api\telegram;
7
use BPT\constants\chatMemberStatus;
8
use BPT\tools;
9
use BPT\types\user;
10
11
trait is {
12
    /**
13
     * Check the given username format
14
     *
15
     * e.g. => tools::isUsername('BPT_CH');
16
     *
17
     * e.g. => tools::isUsername(username: 'BPT_CH');
18
     *
19
     * @param string $username Your text to be check is it username or not , @ is not needed
20
     *
21
     * @return bool
22
     */
23
    public static function isUsername (string $username): bool {
24
        $length = strlen($username);
25
        return !str_contains($username, '__') && $length >= 5 && $length <= 33 && preg_match('/^@?([a-zA-Z])(\w{4,31})$/', $username);
26
    }
27
28
    /**
29
     * Check given IP is in the given IP range or not
30
     *
31
     * e.g. => tools::ipInRange('192.168.1.1','149.154.160.0/20');
32
     *
33
     * e.g. => tools::ipInRange(ip: '192.168.1.1',range: '149.154.160.0/20');
34
     *
35
     * @param string $ip    Your ip
36
     * @param string $range Your range ip for check , if you didn't specify the block , it will be 32
37
     *
38
     * @return bool
39
     */
40
    public static function ipInRange (string $ip, string $range): bool {
41
        if (!str_contains($range, '/')) {
42
            $range .= '/32';
43
        }
44
        $range_full = explode('/', $range, 2);
45
        $netmask_decimal = ~(pow(2, (32 - $range_full[1])) - 1);
46
        return (ip2long($ip) & $netmask_decimal) == (ip2long($range_full[0]) & $netmask_decimal);
47
    }
48
49
    /**
50
     * Check the given IP is from telegram or not
51
     *
52
     * e.g. => tools::isTelegram('192.168.1.1');
53
     *
54
     * e.g. => tools::isTelegram(ip: '192.168.1.1');
55
     *
56
     * @param string $ip Your ip to be check is telegram or not e.g. '192.168.1.1'
57
     *
58
     * @return bool
59
     */
60
    public static function isTelegram (string $ip): bool {
61
        return tools::ipInRange($ip, '149.154.160.0/20') || tools::ipInRange($ip, '91.108.4.0/22');
62
    }
63
64
    /**
65
     * Check the given IP is from CloudFlare or not
66
     *
67
     * e.g. => tools::isCloudFlare('192.168.1.1');
68
     *
69
     * e.g. =>tools::isCloudFlare(ip: '192.168.1.1');
70
     *
71
     * @param string $ip Your ip to be check is CloudFlare or not
72
     *
73
     * @return bool
74
     */
75
    public static function isCloudFlare (string $ip): bool {
76
        $cf_ips = ['173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/12', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22'];
77
        foreach ($cf_ips as $cf_ip) {
78
            if (self::ipInRange($ip,$cf_ip)) {
79
                return true;
80
            }
81
        }
82
        return false;
83
    }
84
85
    /**
86
     * Check the given IP is from ArvanCloud or not
87
     *
88
     * e.g. => tools::isCloudFlare('192.168.1.1');
89
     *
90
     * e.g. =>tools::isCloudFlare(ip: '192.168.1.1');
91
     *
92
     * @param string $ip Your ip to be checked is ArvanCloud or not
93
     *
94
     * @return bool
95
     */
96
    public static function isArvanCloud (string $ip): bool {
97
        $ar_ips = ['185.143.232.0/22', '92.114.16.80/28', '2.146.0.0/28', '46.224.2.32/29', '89.187.178.96/29', '195.181.173.128/29', '89.187.169.88/29', '188.229.116.16/29', '83.123.255.56/31', '164.138.128.28/31', '94.182.182.28/30', '185.17.115.176/30', '5.213.255.36/31', '138.128.139.144/29', '5.200.14.8/29', '188.122.68.224/29', '188.122.83.176/29', '213.179.217.16/29', '185.179.201.192/29', '43.239.139.192/29', '213.179.197.16/29', '213.179.201.192/29', '109.200.214.248/29', '138.128.141.16/29', '188.122.78.136/29', '213.179.211.32/29', '103.194.164.24/29', '185.50.105.136/29', '213.179.213.16/29', '162.244.52.120/29', '188.122.80.240/29', '109.200.195.64/29', '109.200.199.224/29', '185.228.238.0/28', '94.182.153.24/29', '94.101.182.0/27', '37.152.184.208/28', '78.39.156.192/28', '158.255.77.238/31', '81.12.28.16/29', '176.65.192.202/31', '2.144.3.128/28', '89.45.48.64/28', '37.32.16.0/27', '37.32.17.0/27', '37.32.18.0/27'];
98
        foreach ($ar_ips as $ar_ip) {
99
            if (self::ipInRange($ip,$ar_ip)) {
100
                return true;
101
            }
102
        }
103
        return false;
104
    }
105
106
    /**
107
     * Check the given token format
108
     *
109
     * if you want to verify token with telegram , you should set `verify` parameter => true.
110
     * in that case , if token was right , you will receive getMe result , otherwise you will receive false
111
     *
112
     * e.g. => tools::isToken('123123123:abcabcabcabc');
113
     *
114
     * @param string $token  your token e.g. => '123123123:abcabcabcabc'
115
     * @param bool   $verify check token with telegram or not
116
     *
117
     * @return bool|user return array when verify is active and token is true array of telegram getMe result
118
     */
119
    public static function isToken (string $token, bool $verify = false): bool|user {
120
        if (preg_match('/^(\d{8,10}):[\w\-]{35}$/', $token)) {
121
            if ($verify){
122
                $res = telegram::me($token);
123
                if (telegram::$status) {
124
                    return $res;
125
                }
126
                return false;
127
            }
128
            return true;
129
        }
130
        return false;
131
    }
132
133
    /**
134
     * check user joined in channels or not
135
     *
136
     * this method only return true or false, if user join in all channels true, and if user not joined in one channel false
137
     *
138
     * this method does not care about not founded channel and count them as joined channel
139
     *
140
     * ids parameter can be array for multi channels or can be string for one channel
141
     *
142
     * NOTE : each channel will decrease speed a little(because of request count)
143
     *
144
     * e.g. => tools::isJoined('BPT_CH','442109602');
145
     *
146
     * e.g. => tools::isJoined(['BPT_CH','-1005465465454']);
147
     *
148
     * @param array|string|int $ids     could be username or id, you can pass multi or single id
149
     * @param int|null         $user_id if not set , will generate by request::catchFields method
150
     *
151
     * @return bool
152
     */
153
    public static function isJoined (array|string|int $ids , int|null $user_id = null): bool {
154
        if (!is_array($ids)) {
0 ignored issues
show
introduced by
The condition is_array($ids) is always true.
Loading history...
155
            $ids = [$ids];
156
        }
157
        $user_id = $user_id ?? request::catchFields('user_id');
158
159
        foreach ($ids as $id) {
160
            $check = telegram::getChatMember($id,$user_id);
161
            if (telegram::$status) {
162
                $check = $check->status;
163
                if ($check === chatMemberStatus::LEFT || $check === chatMemberStatus::KICKED) {
164
                    return false;
165
                }
166
            }
167
        }
168
        return true;
169
    }
170
171
    /**
172
     * check user joined in channels or not
173
     *
174
     * ids parameter can be array for multi channels or can be string for one channel
175
     *
176
     * NOTE : each channel will decrease speed a little(because of request count)
177
     *
178
     * e.g. => tools::joinChecker('BPT_CH','442109602');
179
     *
180
     * e.g. => tools::joinChecker(['BPT_CH','-1005465465454']);
181
     *
182
     * @param array|string|int $ids     could be username or id, you can pass multi or single id
183
     * @param int|null         $user_id if not set , will generate by request::catchFields method
184
     *
185
     * @return array keys will be id and values will be bool(null for not founded ids)
186
     */
187
    public static function joinChecker (array|string|int $ids , int|null $user_id = null): array {
188
        if (!is_array($ids)) {
0 ignored issues
show
introduced by
The condition is_array($ids) is always true.
Loading history...
189
            $ids = [$ids];
190
        }
191
        $user_id = $user_id ?? request::catchFields('user_id');
192
193
        $result = [];
194
        foreach ($ids as $id) {
195
            $check = telegram::getChatMember($id,$user_id);
196
            if (telegram::$status) {
197
                $check = $check->status;
198
                $result[$id] = $check !== chatMemberStatus::LEFT && $check !== chatMemberStatus::KICKED;
199
            }
200
            else $result[$id] = null;
201
        }
202
        return $result;
203
    }
204
205
    /**
206
     * check is it short encoded or not
207
     *
208
     * e.g. => tools::isShorted(123456789);
209
     *
210
     * @param string $text
211
     *
212
     * @return bool
213
     */
214
    public static function isShorted(string $text): bool{
215
        return preg_match('/^[a-zA-Z0-9]+$/',$text);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^[a-zA-Z0-9]+$/', $text) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
216
    }
217
}