Twitter   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 24

4 Methods

Rating   Name   Duplication   Size   Complexity  
B requestToken() 0 26 1
B verify() 0 35 1
F auth() 0 71 21
B getInfo() 0 36 1
1
<?php
2
3
/**
4
 * Social helper vk
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Users\SocialHelper;
13
14
class Twitter extends \Users\SocialHelper {
15
16
    private static function requestToken() {
17
        $config = static::getConfig();
18
        $oauthNonce = md5(uniqid(rand(), true));
0 ignored issues
show
Bug introduced by
The call to rand() has too few arguments starting with min. ( Ignorable by Annotation )

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

18
        $oauthNonce = md5(uniqid(/** @scrutinizer ignore-call */ rand(), true));

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
19
        $oauthTimestamp = time();
20
        //string
21
        $oauth_base_text = "GET&";
22
        $oauth_base_text .= urlencode('https://api.twitter.com/oauth/request_token') . "&";
23
        $oauth_base_text .= urlencode("oauth_callback=" . urlencode('http://' . INJI_DOMAIN_NAME . '/users/social/auth/twitter') . "&");
24
        $oauth_base_text .= urlencode("oauth_consumer_key=" . $config['consumer_key'] . "&");
25
        $oauth_base_text .= urlencode("oauth_nonce=" . $oauthNonce . "&");
26
        $oauth_base_text .= urlencode("oauth_signature_method=HMAC-SHA1&");
27
        $oauth_base_text .= urlencode("oauth_timestamp=" . $oauthTimestamp . "&");
28
        $oauth_base_text .= urlencode("oauth_version=1.0");
29
        $oauthSignature = base64_encode(hash_hmac("sha1", $oauth_base_text, $config['consumer_secret'] . "&", true));
30
        //request
31
        $url = 'https://api.twitter.com/oauth/request_token';
32
        $url .= '?oauth_callback=' . urlencode('http://' . INJI_DOMAIN_NAME . '/users/social/auth/twitter');
33
        $url .= '&oauth_consumer_key=' . $config['consumer_key'];
34
        $url .= '&oauth_nonce=' . $oauthNonce;
35
        $url .= '&oauth_signature=' . urlencode($oauthSignature);
36
        $url .= '&oauth_signature_method=HMAC-SHA1';
37
        $url .= '&oauth_timestamp=' . $oauthTimestamp;
38
        $url .= '&oauth_version=1.0';
39
        $response = file_get_contents($url);
40
        parse_str($response, $result);
41
        return $result;
42
    }
43
44
    private static function verify() {
45
        $config = static::getConfig();
46
        $oauthNonce = md5(uniqid(rand(), true));
0 ignored issues
show
Bug introduced by
The call to rand() has too few arguments starting with min. ( Ignorable by Annotation )

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

46
        $oauthNonce = md5(uniqid(/** @scrutinizer ignore-call */ rand(), true));

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
47
        $oauthTimestamp = time();
48
        $oauth_token = $_GET['oauth_token'];
49
        $oauth_verifier = $_GET['oauth_verifier'];
50
        $oauth_token_secret = $_SESSION['oauth_token_secret'];
51
        //string
52
        $oauth_base_text = "GET&";
53
        $oauth_base_text .= urlencode('https://api.twitter.com/oauth/access_token') . "&";
54
        $oauth_base_text .= urlencode("oauth_consumer_key=" . $config['consumer_key'] . "&");
55
        $oauth_base_text .= urlencode("oauth_nonce=" . $oauthNonce . "&");
56
        $oauth_base_text .= urlencode("oauth_signature_method=HMAC-SHA1&");
57
        $oauth_base_text .= urlencode("oauth_token=" . $oauth_token . "&");
58
        $oauth_base_text .= urlencode("oauth_timestamp=" . $oauthTimestamp . "&");
59
        $oauth_base_text .= urlencode("oauth_verifier=" . $oauth_verifier . "&");
60
        $oauth_base_text .= urlencode("oauth_version=1.0");
61
62
        $key = $config['consumer_secret'] . "&" . $oauth_token_secret;
63
        //request
64
        $oauth_signature = base64_encode(hash_hmac("sha1", $oauth_base_text, $key, true));
65
        $url = 'https://api.twitter.com/oauth/access_token';
66
        $url .= '?oauth_nonce=' . $oauthNonce;
67
        $url .= '&oauth_signature_method=HMAC-SHA1';
68
        $url .= '&oauth_timestamp=' . $oauthTimestamp;
69
        $url .= '&oauth_consumer_key=' . $config['consumer_key'];
70
        $url .= '&oauth_token=' . urlencode($oauth_token);
71
        $url .= '&oauth_verifier=' . urlencode($oauth_verifier);
72
        $url .= '&oauth_signature=' . urlencode($oauth_signature);
73
        $url .= '&oauth_version=1.0';
74
75
76
        $response = file_get_contents($url);
77
        parse_str($response, $result);
78
        return $result;
79
    }
80
81
    private static function getInfo($result) {
82
        $config = static::getConfig();
83
        $oauth_nonce = md5(uniqid(rand(), true));
0 ignored issues
show
Bug introduced by
The call to rand() has too few arguments starting with min. ( Ignorable by Annotation )

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

83
        $oauth_nonce = md5(uniqid(/** @scrutinizer ignore-call */ rand(), true));

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
84
        $oauth_timestamp = time();
85
86
        $oauth_token = $result['oauth_token'];
87
        $oauth_token_secret = $result['oauth_token_secret'];
88
        $screen_name = $result['screen_name'];
89
90
        $oauth_base_text = "GET&";
91
        $oauth_base_text .= urlencode('https://api.twitter.com/1.1/users/show.json') . '&';
92
        $oauth_base_text .= urlencode('oauth_consumer_key=' . $config['consumer_key'] . '&');
93
        $oauth_base_text .= urlencode('oauth_nonce=' . $oauth_nonce . '&');
94
        $oauth_base_text .= urlencode('oauth_signature_method=HMAC-SHA1&');
95
        $oauth_base_text .= urlencode('oauth_timestamp=' . $oauth_timestamp . "&");
96
        $oauth_base_text .= urlencode('oauth_token=' . $oauth_token . "&");
97
        $oauth_base_text .= urlencode('oauth_version=1.0&');
98
        $oauth_base_text .= urlencode('screen_name=' . $screen_name);
99
100
        $key = $config['consumer_secret'] . '&' . $oauth_token_secret;
101
        $signature = base64_encode(hash_hmac("sha1", $oauth_base_text, $key, true));
102
103
104
        $url = 'https://api.twitter.com/1.1/users/show.json';
105
        $url .= '?oauth_consumer_key=' . $config['consumer_key'];
106
        $url .= '&oauth_nonce=' . $oauth_nonce;
107
        $url .= '&oauth_signature=' . urlencode($signature);
108
        $url .= '&oauth_signature_method=HMAC-SHA1';
109
        $url .= '&oauth_timestamp=' . $oauth_timestamp;
110
        $url .= '&oauth_token=' . urlencode($oauth_token);
111
        $url .= '&oauth_version=1.0';
112
        $url .= '&screen_name=' . $screen_name;
113
114
        $response = file_get_contents($url);
115
116
        return json_decode($response, true);
117
    }
118
119
    public static function auth() {
120
        if (empty($_GET['oauth_verifier']) || empty($_SESSION['oauth_token_secret'])) {
121
            $tokens = self::requestToken();
122
            $_SESSION['oauth_token_secret'] = $tokens['oauth_token_secret'];
123
            \Inji\Tools::redirect("https://api.twitter.com/oauth/authorize?oauth_token={$tokens['oauth_token']}");
124
        }
125
        $verify = self::verify();
126
127
        if (!$verify['user_id']) {
128
            \Inji\Tools::redirect('/', 'Не удалось авторизоваться через twitter');
129
        }
130
        $userDetail = self::getInfo($verify);
131
132
        $social = self::getObject();
133
        $userSocial = \Users\User\Social::get([['uid', $userDetail['id']], ['social_id', $social->id]]);
134
        if ($userSocial && $userSocial->user) {
135
            \App::$cur->users->newSession($userSocial->user);
136
            if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
137
                \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type]);
138
            }
139
        } else {
140
            if ($userSocial && !$userSocial->user) {
141
                $userSocial->delete();
142
            }
143
            if (!\Users\User::$cur->id) {
144
                $user = new \Users\User();
145
                $user->group_id = 2;
146
                $user->role_id = 2;
147
                $invite_code = (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : '')))));
148
                if (!empty($invite_code)) {
149
                    $invite = \Users\User\Invite::get($invite_code, 'code');
150
                    $inveiteError = false;
151
                    if (!$invite) {
152
                        \Inji\Msg::add('Такой код пришлашения не найден', 'danger');
153
                        $inveiteError = true;
154
                    }
155
                    if ($invite->limit && !($invite->limit - $invite->count)) {
0 ignored issues
show
Bug introduced by
The property limit does not exist on false.
Loading history...
Bug introduced by
The property count does not exist on false.
Loading history...
156
                        \Inji\Msg::add('Лимит приглашений для данного кода исчерпан', 'danger');
157
                        $inveiteError = true;
158
                    }
159
                    if (!$inveiteError) {
160
                        $user->parent_id = $invite->user_id;
161
                        $invite->count++;
162
                        $invite->save();
163
                    }
164
                }
165
                if (!$user->parent_id && !empty(\App::$cur->Users->config['defaultPartner'])) {
166
                    $user->parent_id = \App::$cur->Users->config['defaultPartner'];
167
                }
168
                $user->save();
169
                $userInfo = new \Users\User\Info();
170
                $userInfo->user_id = $user->id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on Users\User\Info. Since you implemented __set, consider adding a @property annotation.
Loading history...
171
                $userInfo->save();
172
            } else {
173
                $user = \Users\User::$cur;
174
            }
175
            $name = explode(' ', $userDetail['name']);
176
            $user->info->first_name = $name[0];
177
            $user->info->last_name = $name[1];
178
            $user->info->city = $userDetail['location'];
179
            $user->info->save();
180
            $userSocial = new \Users\User\Social();
181
            $userSocial->uid = $userDetail['id'];
0 ignored issues
show
Bug Best Practice introduced by
The property uid does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
182
            $userSocial->social_id = $social->id;
0 ignored issues
show
Bug Best Practice introduced by
The property social_id does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
183
            $userSocial->user_id = $user->id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on Users\User\Social. Since you implemented __set, consider adding a @property annotation.
Loading history...
184
            $userSocial->save();
185
            \App::$cur->users->newSession($user);
186
            if (!empty(\App::$cur->users->config['loginUrl'][\App::$cur->type])) {
187
                \Inji\Tools::redirect(\App::$cur->users->config['loginUrl'][\App::$cur->type], 'Вы успешно зарегистрировались через Twitter', 'success');
188
            } else {
189
                \Inji\Tools::redirect('/users/cabinet/profile', 'Вы успешно зарегистрировались через Twitter', 'success');
190
            }
191
        }
192
    }
193
194
}
195