| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | namespace SimpleID\Protocols\OAuth; |
| 23: | |
| 24: | use \Base; |
| 25: | use SimpleID\Protocols\HTTPResponse; |
| 26: | use SimpleID\Util\ArrayWrapper; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | class Request extends ArrayWrapper { |
| 33: | |
| 34: | protected $headers = []; |
| 35: | |
| 36: | |
| 37: | private $immediate = false; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function __construct($params = NULL, $headers = NULL) { |
| 46: | if ($params == NULL) $params = $_REQUEST; |
| 47: | parent::__construct($params); |
| 48: | |
| 49: | if ($headers == NULL) { |
| 50: | |
| 51: | if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) $this->headers['authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; |
| 52: | if (isset($_SERVER['PHP_AUTH_TYPE'])) { |
| 53: | if (isset($_SERVER['PHP_AUTH_DIGEST'])) { |
| 54: | $this->headers['authorization'] = 'Digest ' . $_SERVER['PHP_AUTH_DIGEST']; |
| 55: | } elseif (isset($_SERVER['PHP_AUTH_PW'])) { |
| 56: | $this->headers['authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']); |
| 57: | } |
| 58: | } |
| 59: | |
| 60: | foreach ($_SERVER as $name => $value) { |
| 61: | if (substr($name, 0, 5) != 'HTTP_') continue; |
| 62: | |
| 63: | $this->headers[HTTPResponse::httpCase(strtr(substr($name, 5), '_', '-'))] = $value; |
| 64: | } |
| 65: | } else { |
| 66: | foreach ($headers as $name => $value) $this->headers[HTTPResponse::httpCase($name)] = $value; |
| 67: | } |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public function isImmediate() { |
| 77: | return $this->immediate; |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function setImmediate($immediate) { |
| 88: | $this->immediate = $immediate; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function getHeader($header) { |
| 98: | return ($this->hasHeader($header)) ? $this->headers[HTTPResponse::httpCase($header)] : null; |
| 99: | } |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public function hasHeader($header) { |
| 108: | return array_key_exists(HTTPResponse::httpCase($header), $this->headers); |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function getAuthorizationHeader($parse_credentials = false) { |
| 131: | if (!$this->hasHeader('Authorization') || $this->getHeader('Authorization') == '') return null; |
| 132: | |
| 133: | $results = []; |
| 134: | |
| 135: | $header = $this->getHeader('Authorization'); |
| 136: | $array = preg_split('/\s+/', $header, 2); |
| 137: | if ($array != false) { |
| 138: | list($scheme, $credentials) = $array; |
| 139: | } else { |
| 140: | return [ '#credentials' => $header ]; |
| 141: | } |
| 142: | |
| 143: | $results['#scheme'] = HTTPResponse::httpCase($scheme); |
| 144: | $results['#credentials'] = $credentials; |
| 145: | |
| 146: | if ($parse_credentials) { |
| 147: | if ($results['#scheme'] == 'Basic') { |
| 148: | list($username, $password) = explode(':', base64_decode($credentials)); |
| 149: | $results['#username'] = $username; |
| 150: | $results['#password'] = $password; |
| 151: | } else { |
| 152: | $matches = []; |
| 153: | preg_match_all('/([-a-zA-Z]+)=\"([^\"]+)\"/', $credentials, $matches, PREG_SET_ORDER); |
| 154: | foreach ($matches as $match) $results[strval($match[1])] = $match[2]; |
| 155: | } |
| 156: | } |
| 157: | |
| 158: | return $results; |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | public function paramToArray($param, $delimiter = '/\s+/') { |
| 172: | if (!isset($this->container[$param])) return null; |
| 173: | $split = preg_split($delimiter, $this->container[$param]); |
| 174: | if ($split == false) return $this->container[$param]; |
| 175: | return $split; |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | public function paramContains($param, $contains, $delimiter = '/\s+/') { |
| 190: | if (!isset($this->container[$param])) return false; |
| 191: | $items = $this->paramToArray($param); |
| 192: | return in_array($contains, $items); |
| 193: | } |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | public function paramRemove($param, $value, $delimiter = '/\s+/') { |
| 206: | if (!isset($this->container[$param])) return; |
| 207: | if (!$this->paramContains($param, $value)) return; |
| 208: | |
| 209: | $items = preg_split($delimiter, $this->container[$param]); |
| 210: | if ($items == false) $items = $this->container[$param]; |
| 211: | $items = array_diff($items, [ $value ]); |
| 212: | |
| 213: | preg_match($delimiter, $this->container[$param], $matches); |
| 214: | $this->container[$param] = implode($matches[0], $items); |
| 215: | } |
| 216: | } |
| 217: | |
| 218: | ?> |
| 219: | |