1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class Horde_Oauth_Request
20: {
21: const VERSION = '1.0';
22:
23: protected $_params = array();
24: protected $_url;
25: protected $_method;
26:
27: function __construct($url, $params = array(), $method = 'POST')
28: {
29: if (!isset($params['oauth_version'])) {
30: $params['oauth_version'] = self::VERSION;
31: }
32: if (!isset($params['oauth_nonce'])) {
33: $params['oauth_nonce'] = self::_generateNonce();
34: }
35: if (!isset($params['oauth_timestamp'])) {
36: $params['oauth_timestamp'] = time();
37: }
38:
39: $this->_params = $params;
40: $this->_url = $url;
41: $this->_method = $method;
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51:
52: public function sign($signatureMethod, $consumer, $token = null)
53: {
54: if (empty($this->_params['oauth_consumer_key'])) {
55: $this->_params['oauth_consumer_key'] = $consumer->key;
56: }
57:
58: if (empty($this->_params['oauth_token']) && !empty($token)) {
59: $this->_params['oauth_token'] = $token->key;
60: }
61:
62: $this->_params['oauth_signature_method'] = $signatureMethod->getName();
63: $this->_params['oauth_signature'] = $signatureMethod->sign($this, $consumer, $token);
64:
65: return $this->_getNormalizedUrl() . '?' . $this->buildHttpQuery();
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function getSignatureBaseString()
75: {
76: $parts = array(
77: $this->_getNormalizedHttpMethod(),
78: $this->_getNormalizedUrl(),
79: $this->_getSignableParameters()
80: );
81:
82: return implode('&', array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), $parts));
83: }
84:
85: 86: 87:
88: public function buildHttpQuery()
89: {
90: $parts = array();
91: foreach ($this->_params as $k => $v) {
92: $parts[] = Horde_Oauth_Utils::urlencodeRfc3986($k) . '=' . Horde_Oauth_Utils::urlencodeRfc3986($v);
93: }
94: return implode('&', $parts);
95: }
96:
97: 98:
99: public function ($realm = '')
100: {
101: $header = '';
102: foreach ($this->_params as $k => $v) {
103: if (strpos($k, 'oauth_') !== false) {
104: $header .= Horde_Oauth_Utils::urlencodeRfc3986($k) . '="' . Horde_Oauth_Utils::urlencodeRfc3986($v) . '",';
105: }
106: }
107: $header = substr($header, 0, -1);
108: if (!empty($realm)) {
109: $header .= ',realm="' . Horde_Oauth_Utils::urlencodeRfc3986($realm) . '"';
110: }
111: return 'OAuth ' . $header;
112: }
113:
114: 115: 116:
117: protected static function _generateNonce()
118: {
119: $mt = microtime();
120: $rand = mt_rand();
121:
122: return hash('md5', microtime() . mt_rand());
123: }
124:
125: 126: 127: 128: 129: 130: 131: 132: 133: 134:
135: protected function _getSignableParameters()
136: {
137:
138: $params = $this->_params;
139:
140:
141: if (isset($params['oauth_signature'])) {
142: unset($params['oauth_signature']);
143: }
144:
145:
146: $keys = array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), array_keys($params));
147: $values = array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), array_values($params));
148: $params = array_combine($keys, $values);
149:
150:
151: uksort($params, 'strnatcmp');
152:
153:
154: $pairs = array();
155: foreach ($params as $key => $value) {
156: if (is_array($value)) {
157:
158:
159: natsort($value);
160: foreach ($value as $v2) {
161: $pairs[] = $key . '=' . $v2;
162: }
163: } else {
164: $pairs[] = $key . '=' . $value;
165: }
166: }
167:
168:
169: return implode('&', $pairs);
170: }
171:
172: 173: 174:
175: protected function _getNormalizedHttpMethod()
176: {
177: return strtoupper($this->_method);
178: }
179:
180: 181: 182:
183: protected function _getNormalizedUrl()
184: {
185: $parts = parse_url($this->_url);
186: $scheme = $parts['scheme'];
187: $port = !empty($parts['port'])
188: ? $parts['port']
189: : $scheme == 'https' ? '443' : '80';
190:
191: $host = $parts['host'];
192: $path = !empty($parts['path']) ? $parts['path'] : '';
193:
194: if (($scheme == 'https' && $port != '443') ||
195: ($scheme == 'http' && $port != '80')) {
196: $host = "$host:$port";
197: }
198:
199: return "$scheme://$host$path";
200: }
201: }
202: