@@ -1085,6 +1085,34 @@ protected function _nonce($length = 8)
1085
1085
}
1086
1086
return substr (md5 (microtime(true )), 0 , $ length );
1087
1087
}
1088
+
1089
+ /**
1090
+ * Signature helper
1091
+ *
1092
+ * @param string $httpmethod Usually either 'GET' or 'POST' or 'DELETE'
1093
+ * @param string $method The API method to call
1094
+ * @param array $base_params The signature base parameters
1095
+ *
1096
+ * @return string signature
1097
+ */
1098
+ protected function _getSignature ($ httpmethod , $ method , $ base_params )
1099
+ {
1100
+ // convert params to string
1101
+ $ base_string = '' ;
1102
+ foreach ($ base_params as $ key => $ value ) {
1103
+ $ base_string .= $ key . '= ' . $ value . '& ' ;
1104
+ }
1105
+
1106
+ // trim last ampersand
1107
+ $ base_string = substr ($ base_string , 0 , -1 );
1108
+
1109
+ // hash it
1110
+ return $ this ->_sha1 (
1111
+ $ httpmethod . '& ' .
1112
+ $ this ->_url ($ method ) . '& ' .
1113
+ $ this ->_url ($ base_string )
1114
+ );
1115
+ }
1088
1116
1089
1117
/**
1090
1118
* Generates an OAuth signature
@@ -1101,45 +1129,43 @@ protected function _sign($httpmethod, $method, $params = [], $append_to_get = fa
1101
1129
if (self ::$ _oauth_consumer_key === null ) {
1102
1130
throw new \Exception ('To generate a signature, the consumer key must be set. ' );
1103
1131
}
1104
- $ sign_params = [
1105
- 'consumer_key ' => self ::$ _oauth_consumer_key ,
1106
- 'version ' => '1.0 ' ,
1107
- 'timestamp ' => time (),
1108
- 'nonce ' => $ this ->_nonce (),
1109
- 'signature_method ' => 'HMAC-SHA1 '
1110
- ];
1111
- $ sign_base_params = [];
1112
- foreach ($ sign_params as $ key => $ value ) {
1113
- $ sign_base_params ['oauth_ ' . $ key ] = $ this ->_url ($ value );
1114
- }
1132
+ $ sign_base_params = array_map (
1133
+ [$ this , '_url ' ],
1134
+ [
1135
+ 'oauth_consumer_key ' => self ::$ _oauth_consumer_key ,
1136
+ 'oauth_version ' => '1.0 ' ,
1137
+ 'oauth_timestamp ' => time (),
1138
+ 'oauth_nonce ' => $ this ->_nonce (),
1139
+ 'oauth_signature_method ' => 'HMAC-SHA1 '
1140
+ ]
1141
+ );
1115
1142
if ($ this ->_oauth_token !== null ) {
1116
1143
$ sign_base_params ['oauth_token ' ] = $ this ->_url ($ this ->_oauth_token );
1117
1144
}
1118
1145
$ oauth_params = $ sign_base_params ;
1119
- foreach ($ params as $ key => $ value ) {
1120
- $ sign_base_params [$ key ] = $ this ->_url ($ value );
1121
- }
1146
+
1147
+ // merge in the non-OAuth params
1148
+ $ sign_base_params = array_merge (
1149
+ $ sign_base_params ,
1150
+ array_map ([$ this , '_url ' ], $ params )
1151
+ );
1122
1152
ksort ($ sign_base_params );
1123
- $ sign_base_string = '' ;
1124
- foreach ($ sign_base_params as $ key => $ value ) {
1125
- $ sign_base_string .= $ key . '= ' . $ value . '& ' ;
1126
- }
1127
- $ sign_base_string = substr ($ sign_base_string , 0 , -1 );
1128
- $ signature = $ this ->_sha1 ($ httpmethod . '& ' . $ this ->_url ($ method ) . '& ' . $ this ->_url ($ sign_base_string ));
1153
+
1154
+ $ signature = $ this ->_getSignature ($ httpmethod , $ method , $ sign_base_params );
1129
1155
1130
1156
$ params = $ append_to_get ? $ sign_base_params : $ oauth_params ;
1131
1157
$ params ['oauth_signature ' ] = $ signature ;
1132
- $ keys = $ params ;
1133
- ksort ($ keys );
1158
+
1159
+ ksort ($ params );
1134
1160
if ($ append_to_get ) {
1135
1161
$ authorization = '' ;
1136
- foreach ($ keys as $ key => $ value ) {
1162
+ foreach ($ params as $ key => $ value ) {
1137
1163
$ authorization .= $ key . '=" ' . $ this ->_url ($ value ) . '", ' ;
1138
1164
}
1139
1165
return substr ($ authorization , 0 , -1 );
1140
1166
}
1141
1167
$ authorization = 'OAuth ' ;
1142
- foreach ($ keys as $ key => $ value ) {
1168
+ foreach ($ params as $ key => $ value ) {
1143
1169
$ authorization .= $ key . "= \"" . $ this ->_url ($ value ) . "\", " ;
1144
1170
}
1145
1171
return substr ($ authorization , 0 , -2 );
0 commit comments