OAuthUtil::get_headers()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
1
<?php
2
3
namespace Omnipay\Pesapal\OAuth;
4
5
class OAuthUtil
6
{
7
    public static function urlencode_rfc3986($input)
8
    {
9
        if (is_array($input)) {
10
            return array_map([self::class, 'urlencode_rfc3986'], $input);
11
        } elseif (is_scalar($input)) {
12
            return str_replace(
13
                '+',
14
                ' ',
15
                str_replace('%7E', '~', rawurlencode($input))
16
            );
17
        } else {
18
            return '';
19
        }
20
    }
21
22
    // This decode function isn't taking into consideration the above
23
    // modifications to the encoding process. However, this method doesn't
24
    // seem to be used anywhere so leaving it as is.
25
    public static function urldecode_rfc3986($string)
26
    {
27
        return urldecode($string);
28
    }
29
30
    // Utility function for turning the Authorization: header into
31
    // parameters, has to do some unescaping
32
    // Can filter out any non-oauth parameters if needed (default behaviour)
33
    public static function split_header($header, $only_allow_oauth_parameters = true)
34
    {
35
        $pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
36
        $offset = 0;
37
        $params = [];
38
        while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
39
            $match = $matches[0];
40
            $header_name = $matches[2][0];
41
            $header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
42
            if (preg_match('/^oauth_/', $header_name) || !$only_allow_oauth_parameters) {
43
                $params[$header_name] = self::urldecode_rfc3986($header_content);
44
            }
45
            $offset = $match[1] + strlen($match[0]);
46
        }
47
48
        if (isset($params['realm'])) {
49
            unset($params['realm']);
50
        }
51
52
        return $params;
53
    }
54
55
    // helper to try to sort out headers for people who aren't running apache
56
    public static function get_headers()
57
    {
58
        if (function_exists('apache_request_headers')) {
59
            // we need this to get the actual Authorization: header
60
            // because apache tends to tell us it doesn't exist
61
            return apache_request_headers();
62
        }
63
        // otherwise we don't have apache and are just going to have to hope
64
        // that $_SERVER actually contains what we need
65
        $out = [];
66
        foreach ($_SERVER as $key => $value) {
67
            if (substr($key, 0, 5) == 'HTTP_') {
68
                // this is chaos, basically it is just there to capitalize the first
69
                // letter of every word that is not an initial HTTP and strip HTTP
70
                // code from przemek
71
                $key = str_replace(
72
                    ' ',
73
                    '-',
74
                    ucwords(strtolower(str_replace('_', ' ', substr($key, 5))))
75
                );
76
                $out[$key] = $value;
77
            }
78
        }
79
80
        return $out;
81
    }
82
83
    // This function takes a input like a=b&a=c&d=e and returns the parsed
84
    // parameters like this
85
    // array('a' => array('b','c'), 'd' => 'e')
86
    public static function parse_parameters($input)
87
    {
88
        if (!isset($input) || !$input) {
89
            return [];
90
        }
91
92
        $pairs = split('&', $input);
0 ignored issues
show
Deprecated Code introduced by
The function split() has been deprecated: 5.3.0 Use preg_split() instead ( Ignorable by Annotation )

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

92
        $pairs = /** @scrutinizer ignore-deprecated */ split('&', $input);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
93
94
        $parsed_parameters = [];
95
        foreach ($pairs as $pair) {
96
            $split = split('=', $pair, 2);
0 ignored issues
show
Deprecated Code introduced by
The function split() has been deprecated: 5.3.0 Use preg_split() instead ( Ignorable by Annotation )

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

96
            $split = /** @scrutinizer ignore-deprecated */ split('=', $pair, 2);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
97
            $parameter = self::urldecode_rfc3986($split[0]);
98
            $value = isset($split[1]) ? self::urldecode_rfc3986($split[1]) : '';
99
100
            if (isset($parsed_parameters[$parameter])) {
101
                // We have already recieved parameter(s) with this name, so add to the list
102
                // of parameters with this name
103
104
                if (is_scalar($parsed_parameters[$parameter])) {
105
                    // This is the first duplicate, so transform scalar (string) into an array
106
                    // so we can add the duplicates
107
                    $parsed_parameters[$parameter] = [$parsed_parameters[$parameter]];
108
                }
109
110
                $parsed_parameters[$parameter][] = $value;
111
            } else {
112
                $parsed_parameters[$parameter] = $value;
113
            }
114
        }
115
116
        return $parsed_parameters;
117
    }
118
119
    public static function build_http_query($params)
120
    {
121
        if (!$params) {
122
            return '';
123
        }
124
125
        // Urlencode both keys and values
126
        $keys = self::urlencode_rfc3986(array_keys($params));
127
        $values = self::urlencode_rfc3986(array_values($params));
128
        $params = array_combine($keys, $values);
129
130
        // Parameters are sorted by name, using lexicographical byte value ordering.
131
        // Ref: Spec: 9.1.1 (1)
132
        uksort($params, 'strcmp');
133
134
        $pairs = [];
135
        foreach ($params as $parameter => $value) {
136
            if (is_array($value)) {
137
                // If two or more parameters share the same name, they are sorted by their value
138
                // Ref: Spec: 9.1.1 (1)
139
                natsort($value);
140
                foreach ($value as $duplicate_value) {
141
                    $pairs[] = $parameter.'='.$duplicate_value;
142
                }
143
            } else {
144
                $pairs[] = $parameter.'='.$value;
145
            }
146
        }
147
        // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
148
        // Each name-value pair is separated by an '&' character (ASCII code 38)
149
        return implode('&', $pairs);
150
    }
151
}
152