public function testURLDecode() { $raw = 'http://www.joestump.net/~foobar foo'; $exp = 'http%3A%2F%2Fwww.joestump.net%2F~foobar%20foo'; $res = HTTP_OAuth::urldecode($exp); $this->assertEquals($res, $raw); $res = HTTP_OAuth::urldecode(array($exp)); $this->assertEquals($res, array($raw)); }
/** * Set parameters from the incoming request * * @return void */ public function setParametersFromRequest() { $params = array(); $auth = $this->getHeader('Authorization'); if ($auth !== null) { $this->debug('Using OAuth data from header'); $parts = explode(',', $auth); foreach ($parts as $part) { list($key, $value) = explode('=', trim($part)); if (strstr(strtolower($key), 'oauth ') || strstr(strtolower($key), 'uth re') || substr(strtolower($key), 0, 6) != 'oauth_') { continue; } $value = trim($value); $value = str_replace('"', '', $value); $params[$key] = $value; } } if ($this->getRequestMethod() == 'POST') { $this->debug('getting data from POST'); $contentType = substr($this->getHeader('Content-Type'), 0, 33); if ($contentType !== 'application/x-www-form-urlencoded') { throw new HTTP_OAuth_Provider_Exception_InvalidRequest('Invalid ' . 'content type for POST request'); } $params = array_merge($params, $this->parseQueryString($this->getPostData())); } else { $this->debug('getting data from GET'); $params = array_merge($params, $this->parseQueryString($this->getQueryString())); } if (empty($params)) { throw new HTTP_OAuth_Provider_Exception_InvalidRequest('No oauth ' . 'data found from request'); } $this->setParameters(HTTP_OAuth::urldecode($params)); }
/** * Parses a query string * * Does not use built-in urldecoding of name or values like $_GET and * $_POST. Instead, names and values are decoded using RFC 3986 as required * by OAuth. * * @param string $string Query string * * @return array Data from the query string */ protected function parseQueryString($string) { $data = array(); if (empty($string)) { return $data; } foreach (explode('&', $string) as $part) { if (!strstr($part, '=')) { continue; } list($key, $value) = explode('=', $part); $key = HTTP_OAuth::urldecode($key); $value = HTTP_OAuth::urldecode($value); if (isset($data[$key])) { if (is_array($data[$key])) { $data[$key][] = $value; } else { $data[$key] = array($data[$key], $value); } } else { $data[$key] = $value; } } return $data; }