Completed
Push — master ( 9b4aa4...587cf8 )
by Дмитрий
03:12
created

Request   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 39
lcom 2
cbo 4
dl 0
loc 236
ccs 0
cts 137
cp 0
rs 8.2857
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A fromConsumerAndToken() 0 16 2
A setParameter() 0 17 4
A getParameter() 0 4 2
A getParameters() 0 4 1
A unsetParameter() 0 4 1
A getSignableParameters() 0 13 2
A getSignatureBaseString() 0 12 1
A getNormalizedHttpMethod() 0 4 1
B getNormalizedHttpUrl() 0 14 10
A toUrl() 0 9 2
A toPostData() 0 4 1
B toHeader() 0 25 6
A __toString() 0 4 1
A signRequest() 0 6 1
A buildSignature() 0 5 1
A generateNonce() 0 4 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth\Provider\OAuth1;
8
9
use SocialConnect\Auth\Provider\Consumer;
10
use SocialConnect\Auth\Provider\OAuth1\Signature\AbstractSignatureMethod;
11
12
class Request
13
{
14
    public $parameters;
15
16
    public $http_method;
17
18
    public $http_url;
19
20
    // for debug purposes
21
    public $base_string;
22
23
    public static $version = '1.0';
24
25
    public static $POST_INPUT = 'php://input';
26
27
    public function __construct($http_method, $http_url, $parameters = null)
28
    {
29
        $parameters = ($parameters) ? $parameters : array();
30
        $parameters = array_merge(Util::parseParameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
31
        $this->parameters  = $parameters;
32
        $this->http_method = $http_method;
33
        $this->http_url    = $http_url;
34
    }
35
    
36
    /**
37
     * @param Consumer $consumer
38
     * @param Token $token
39
     * @param string $method
40
     * @param string $url
41
     * @param array $parameters
42
     * @return Request
43
     */
44
    public static function fromConsumerAndToken(Consumer $consumer, Token $token, $method, $url, array $parameters = array())
45
    {
46
        $defaults   = array(
47
            'oauth_version' => self::$version,
48
            'oauth_nonce' => self::generateNonce(),
49
            'oauth_timestamp' => time(),
50
            'oauth_consumer_key' => $consumer->getKey()
51
        );
52
53
        if ($token) {
54
            $defaults['oauth_token'] = $token->getKey();
55
        }
56
57
        $parameters = array_merge($defaults, $parameters);
58
        return new self($method, $url, $parameters);
59
    }
60
61
    public function setParameter($name, $value, $allow_duplicates = true)
62
    {
63
        if ($allow_duplicates && isset($this->parameters[$name])) {
64
            // We have already added parameter(s) with this name, so add to the list
65
            if (is_scalar($this->parameters[$name])) {
66
                // This is the first duplicate, so transform scalar (string)
67
                // into an array so we can add the duplicates
68
                $this->parameters[$name] = array(
69
                    $this->parameters[$name]
70
                );
71
            }
72
73
            $this->parameters[$name][] = $value;
74
        } else {
75
            $this->parameters[$name] = $value;
76
        }
77
    }
78
    public function getParameter($name)
79
    {
80
        return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
81
    }
82
    public function getParameters()
83
    {
84
        return $this->parameters;
85
    }
86
    public function unsetParameter($name)
87
    {
88
        unset($this->parameters[$name]);
89
    }
90
91
    /**
92
     * The request parameters, sorted and concatenated into a normalized string.
93
     *
94
     * @return string
95
     */
96
    public function getSignableParameters()
97
    {
98
        // Grab all parameters
99
        $params = $this->parameters;
100
101
        // Remove oauth_signature if present
102
        // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
103
        if (isset($params['oauth_signature'])) {
104
            unset($params['oauth_signature']);
105
        }
106
107
        return Util::buildHttpQuery($params);
108
    }
109
110
    /**
111
     * Returns the base string of this request
112
     *
113
     * The base string defined as the method, the url
114
     * and the parameters (normalized), each urlencoded
115
     * and the concated with &.
116
     *
117
     * @return string
118
     */
119
    public function getSignatureBaseString()
120
    {
121
        $parts = array(
122
            $this->getNormalizedHttpMethod(),
123
            $this->getNormalizedHttpUrl(),
124
            $this->getSignableParameters()
125
        );
126
127
        $parts = Util::urlencodeRFC3986($parts);
128
129
        return implode('&', $parts);
130
    }
131
132
    /**
133
     * just uppercases the http method
134
     */
135
    public function getNormalizedHttpMethod()
136
    {
137
        return strtoupper($this->http_method);
138
    }
139
140
    /**
141
     * parses the url and rebuilds it to be
142
     * scheme://host/path
143
     */
144
    public function getNormalizedHttpUrl()
145
    {
146
        $parts = parse_url($this->http_url);
147
148
        $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
149
        $port   = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
150
        $host   = (isset($parts['host'])) ? strtolower($parts['host']) : '';
151
        $path   = (isset($parts['path'])) ? $parts['path'] : '';
152
153
        if (($scheme == 'https' && $port != '443') || ($scheme == 'http' && $port != '80')) {
154
            $host = "$host:$port";
155
        }
156
        return "$scheme://$host$path";
157
    }
158
159
    /**
160
     * builds a url usable for a GET request
161
     */
162
    public function toUrl()
163
    {
164
        $post_data = $this->toPostData();
165
        $out       = $this->getNormalizedHttpUrl();
166
        if ($post_data) {
167
            $out .= '?' . $post_data;
168
        }
169
        return $out;
170
    }
171
172
    /**
173
     * builds the data one would send in a POST request
174
     */
175
    public function toPostData()
176
    {
177
        return Util::buildHttpQuery($this->parameters);
178
    }
179
180
    /**
181
     * builds the Authorization: header
182
     * @param mixed $realm
183
     * @return array
184
     */
185
    public function toHeader($realm = null)
186
    {
187
        $first = true;
188
        if ($realm) {
189
            $out   = 'OAuth realm="' . Util::urlencodeRFC3986($realm) . '"';
190
            $first = false;
191
        } else {
192
            $out = 'OAuth';
193
        }
194
195
        foreach ($this->parameters as $k => $v) {
196
            if (substr($k, 0, 5) != "oauth") {
197
                continue;
198
            }
199
            if (is_array($v)) {
200
                continue;
201
            }
202
            $out .= ($first) ? ' ' : ', ';
203
            $out .= Util::urlencodeRFC3986($k) . '="' . Util::urlencodeRFC3986($v) . '"';
204
            $first = false;
205
        }
206
        return array(
207
            'Authorization' => $out
208
        ); //- hacked into this to make it return an array. 15/11/2014.
209
    }
210
211
    public function __toString()
212
    {
213
        return $this->toUrl();
214
    }
215
216
    /**
217
     * @param AbstractSignatureMethod $signature_method
218
     * @param Consumer $consumer
219
     * @param Token $token
220
     */
221
    public function signRequest(AbstractSignatureMethod $signature_method, Consumer $consumer, Token $token)
222
    {
223
        $this->setParameter('oauth_signature_method', $signature_method->getName(), false);
224
        $signature = $this->buildSignature($signature_method, $consumer, $token);
225
        $this->setParameter('oauth_signature', $signature, false);
226
    }
227
228
    /**
229
     * @param AbstractSignatureMethod $signatureMethod
230
     * @param Consumer $consumer
231
     * @param Token $token
232
     * @return string
233
     */
234
    public function buildSignature(AbstractSignatureMethod $signatureMethod, Consumer $consumer, Token $token)
235
    {
236
        $signature = $signatureMethod->buildSignature($this, $consumer, $token);
237
        return $signature;
238
    }
239
240
    /**
241
     * util function: current nonce
242
     */
243
    private static function generateNonce()
244
    {
245
        return md5(microtime() . mt_rand());
246
    }
247
}
248