class-aweber-oauth.php
3 years ago
class-aweber-oauth2.php
5 months ago
class-wp-aweber-api-exception.php
3 years ago
class-wp-aweber-api-not-found-exception.php
3 years ago
class-wp-aweber-api.php
5 months ago
class-aweber-oauth.php
175 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Addon_Aweber_Oauth class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Hustle_Addon_Aweber_Oauth |
| 10 | * Helpers for OAuth |
| 11 | */ |
| 12 | class Hustle_Addon_Aweber_Oauth { |
| 13 | |
| 14 | /** |
| 15 | * Create Signature Base |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | * |
| 19 | * @param mixed $method String name of HTTP method, such as "GET". |
| 20 | * @param mixed $url URL where this request will go. |
| 21 | * @param mixed $data Array of params for this request. This should |
| 22 | * include ALL oauth properties except for the signature. |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | public static function create_signature_base( $method, $url, $data ) { |
| 27 | $request_method = $method; |
| 28 | $request_url = $url; |
| 29 | $request_data = $data; |
| 30 | |
| 31 | $method = rawurlencode( strtoupper( $method ) ); |
| 32 | $query = wp_parse_url( $url, PHP_URL_QUERY ); |
| 33 | if ( $query ) { |
| 34 | $parts = explode( '?', $url, 2 ); |
| 35 | $url = array_shift( $parts ); |
| 36 | $items = explode( '&', $query ); |
| 37 | foreach ( $items as $item ) { |
| 38 | list( $key, $value ) = explode( '=', $item ); |
| 39 | $data[ rawurldecode( $key ) ] = rawurldecode( $value ); |
| 40 | } |
| 41 | } |
| 42 | $url = rawurlencode( $url ); |
| 43 | $data = rawurlencode( self::collapse_data_for_signature( $data ) ); |
| 44 | |
| 45 | $signature_base = $method . '&' . $url . '&' . $data; |
| 46 | |
| 47 | /** |
| 48 | * Filter Signature Base that will be used to sign AWeber request |
| 49 | * |
| 50 | * @since 1.3 |
| 51 | * |
| 52 | * @param string $signature_base |
| 53 | * @param string $request_method |
| 54 | * @param string $request_url |
| 55 | * @param array $request_data |
| 56 | */ |
| 57 | $signature_base = apply_filters( 'hustle_addon_aweber_oauth_signature_base', $signature_base, $request_method, $request_url, $request_data ); |
| 58 | |
| 59 | return $signature_base; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Collapse data for signature |
| 64 | * |
| 65 | * @since 1.0 |
| 66 | * |
| 67 | * Turns an array of request data into a string, as used by the oauth |
| 68 | * signature |
| 69 | * |
| 70 | * @param mixed $data Data. |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public static function collapse_data_for_signature( $data ) { |
| 75 | ksort( $data ); |
| 76 | $collapse = ''; |
| 77 | foreach ( $data as $key => $val ) { |
| 78 | if ( ! empty( $collapse ) ) { |
| 79 | $collapse .= '&'; |
| 80 | } |
| 81 | $collapse .= $key . '=' . rawurlencode( $val ); |
| 82 | } |
| 83 | |
| 84 | return $collapse; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Creates a signature on the signature base and the signature key |
| 89 | * |
| 90 | * @since 1.0 |
| 91 | * |
| 92 | * @param mixed $base Base string of data to sign. |
| 93 | * @param mixed $key Key to sign the data with. |
| 94 | * |
| 95 | * @access public |
| 96 | * @return string The signature |
| 97 | */ |
| 98 | public static function create_signature( $base, $key ) { |
| 99 | |
| 100 | $signature = base64_encode( hash_hmac( 'sha1', $base, $key, true ) );// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 101 | |
| 102 | /** |
| 103 | * Signature that will be used on AWeber oauth_signature |
| 104 | * |
| 105 | * @since 1.3 |
| 106 | * |
| 107 | * @param string $signature |
| 108 | * @param string $base |
| 109 | * @param string $key |
| 110 | */ |
| 111 | $signature = apply_filters( 'hustle_addon_aweber_oauth_signature', $signature, $base, $key ); |
| 112 | |
| 113 | return $signature; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates a key that will be used to sign our signature. Signatures |
| 118 | * are signed with the consumerSecret for this consumer application and |
| 119 | * the token secret of the user that the application is acting on behalf |
| 120 | * of. |
| 121 | * |
| 122 | * @since 1.0 |
| 123 | * |
| 124 | * @param string $application_key Application key. |
| 125 | * @param string $oauth_token_secret Auth token secret. |
| 126 | * |
| 127 | * @return string |
| 128 | */ |
| 129 | public static function create_signature_key( $application_key, $oauth_token_secret ) { |
| 130 | $signature_key = $application_key . '&' . $oauth_token_secret; |
| 131 | |
| 132 | /** |
| 133 | * Signature that will be used on AWeber oauth_signature |
| 134 | * |
| 135 | * @since 1.3 |
| 136 | * |
| 137 | * @param string $signature_key |
| 138 | * @param string $application_key |
| 139 | * @param string $oauth_token_secret |
| 140 | */ |
| 141 | $signature_key = apply_filters( 'hustle_addon_aweber_oauth_signature_key', $signature_key, $application_key, $oauth_token_secret ); |
| 142 | |
| 143 | return $signature_key; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Generate oauth_nonce |
| 148 | * |
| 149 | * @since 1.0 |
| 150 | * |
| 151 | * @param int $timestamp Timestamp. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public static function generate_oauth_nonce( $timestamp = 0 ) { |
| 156 | if ( ! $timestamp ) { |
| 157 | $timestamp = time(); |
| 158 | } |
| 159 | |
| 160 | $oauth_nonce = md5( $timestamp . '-' . wp_rand( 10000, 99999 ) . '-' . uniqid() ); |
| 161 | |
| 162 | /** |
| 163 | * Filter required oauth_nonce data of AWeber that need to be send on API Request |
| 164 | * |
| 165 | * @since 1.3 |
| 166 | * |
| 167 | * @param string $oauth_nonce |
| 168 | * @param int $timestamp current timestamp for future reference |
| 169 | */ |
| 170 | $oauth_nonce = apply_filters( 'hustle_addon_aweber_oauth_nonce', $oauth_nonce, $timestamp ); |
| 171 | |
| 172 | return $oauth_nonce; |
| 173 | } |
| 174 | } |
| 175 |