| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 138 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public static function getResponse(array $parameters) |
||
| 45 | { |
||
| 46 | $parameters['server'] = parse_url($parameters['url']); |
||
| 47 | |||
| 48 | if (!isset($parameters['server']['port'])) { |
||
| 49 | $parameters['server']['port'] = ($parameters['server']['scheme'] == 'https') ? 443 : 80; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!isset($parameters['server']['path'])) { |
||
| 53 | $parameters['server']['path'] = '/'; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (isset($parameters['server']['user']) && isset($parameters['server']['pass'])) { |
||
| 57 | $parameters['headers'][] = 'Authorization: Basic ' . base64_encode($parameters['server']['user'] . ':' . $parameters['server']['pass']); |
||
| 58 | } |
||
| 59 | |||
| 60 | unset($parameters['url']); |
||
| 61 | |||
| 62 | if (!isset($parameters['headers']) || !is_array($parameters['headers'])) { |
||
| 63 | $parameters['headers'] = []; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!isset($parameters['method'])) { |
||
| 67 | if (isset($parameters['parameters'])) { |
||
| 68 | $parameters['method'] = 'post'; |
||
| 69 | } else { |
||
| 70 | $parameters['method'] = 'get'; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $curl = curl_init($parameters['server']['scheme'] . '://' . $parameters['server']['host'] . $parameters['server']['path'] . (isset($parameters['server']['query']) ? '?' . $parameters['server']['query'] : '')); |
||
| 75 | |||
| 76 | $curl_options = [ |
||
| 77 | CURLOPT_PORT => $parameters['server']['port'], |
||
| 78 | CURLOPT_HEADER => true, |
||
| 79 | CURLOPT_RETURNTRANSFER => true, |
||
| 80 | CURLOPT_FORBID_REUSE => true, |
||
| 81 | CURLOPT_FRESH_CONNECT => true, |
||
| 82 | CURLOPT_ENCODING => '', // disable gzip |
||
| 83 | CURLOPT_FOLLOWLOCATION => false // does not work with open_basedir so a workaround is implemented below |
||
| 84 | ]; |
||
| 85 | |||
| 86 | if (!empty($parameters['headers'])) { |
||
| 87 | $curl_options[CURLOPT_HTTPHEADER] = $parameters['headers']; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($parameters['server']['scheme'] == 'https') { |
||
| 91 | $verify_ssl = (defined('OSCOM_HTTP_VERIFY_SSL') && (OSCOM_HTTP_VERIFY_SSL === 'True')) ? true : false; |
||
| 92 | |||
| 93 | if (isset($parameters['verify_ssl']) && is_bool($parameters['verify_ssl'])) { |
||
| 94 | $verify_ssl = $parameters['verify_ssl']; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($verify_ssl === true) { |
||
| 98 | $curl_options[CURLOPT_SSL_VERIFYPEER] = true; |
||
| 99 | $curl_options[CURLOPT_SSL_VERIFYHOST] = 2; |
||
| 100 | } else { |
||
| 101 | $curl_options[CURLOPT_SSL_VERIFYPEER] = false; |
||
| 102 | $curl_options[CURLOPT_SSL_VERIFYHOST] = false; |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!isset($parameters['cafile'])) { |
||
| 106 | $parameters['cafile'] = OSCOM::getConfig('dir_root', 'Shop') . 'includes/cacert.pem'; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (is_file($parameters['cafile'])) { |
||
| 110 | $curl_options[CURLOPT_CAINFO] = $parameters['cafile']; |
||
| 111 | } |
||
| 112 | |||
| 113 | if (isset($parameters['certificate'])) { |
||
| 114 | $curl_options[CURLOPT_SSLCERT] = $parameters['certificate']; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($parameters['method'] == 'post') { |
||
| 119 | if (!isset($parameters['parameters'])) { |
||
| 120 | $parameters['parameters'] = ''; |
||
| 121 | } |
||
| 122 | |||
| 123 | $curl_options[CURLOPT_POST] = true; |
||
| 124 | $curl_options[CURLOPT_POSTFIELDS] = $parameters['parameters']; |
||
| 125 | } |
||
| 126 | |||
| 127 | $proxy = defined('OSCOM_HTTP_PROXY') ? OSCOM_HTTP_PROXY : ''; |
||
| 128 | |||
| 129 | if (isset($parameters['proxy'])) { |
||
| 130 | $proxy = $parameters['proxy']; |
||
| 131 | } |
||
| 132 | |||
| 133 | if (!empty($proxy)) { |
||
| 134 | $curl_options[CURLOPT_HTTPPROXYTUNNEL] = true; |
||
| 135 | $curl_options[CURLOPT_PROXY] = $proxy; |
||
| 136 | } |
||
| 137 | |||
| 138 | curl_setopt_array($curl, $curl_options); |
||
| 139 | $result = curl_exec($curl); |
||
| 140 | |||
| 141 | if ($result === false) { |
||
| 142 | trigger_error(curl_error($curl)); |
||
| 143 | |||
| 144 | curl_close($curl); |
||
| 145 | |||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 150 | |||
| 151 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
||
| 152 | $headers = trim(substr($result, 0, $header_size)); |
||
| 153 | $body = substr($result, $header_size); |
||
| 154 | |||
| 155 | curl_close($curl); |
||
| 156 | |||
| 157 | if (($http_code == 301) || ($http_code == 302)) { |
||
| 158 | if (!isset($parameters['redir_counter']) || ($parameters['redir_counter'] < 6)) { |
||
| 159 | if (!isset($parameters['redir_counter'])) { |
||
| 160 | $parameters['redir_counter'] = 0; |
||
| 161 | } |
||
| 162 | |||
| 163 | $matches = []; |
||
| 164 | preg_match('/(Location:|URI:)(.*?)\n/i', $headers, $matches); |
||
| 165 | |||
| 166 | $redir_url = trim(array_pop($matches)); |
||
| 167 | |||
| 168 | $parameters['redir_counter']++; |
||
| 169 | |||
| 170 | $redir_params = [ |
||
| 171 | 'url' => $redir_url, |
||
| 172 | 'method' => $parameters['method'], |
||
| 173 | 'redir_counter', $parameters['redir_counter'] |
||
| 174 | ]; |
||
| 175 | |||
| 176 | $body = static::getResponse($redir_params); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $body; |
||
| 181 | } |
||
| 182 | |||
| 226 |