|
| 1 | +<?php |
| 2 | +require_once('exceptions.php'); |
| 3 | +require_once('oauth_adapter.php'); |
| 4 | +require_once('oauth_application.php'); |
| 5 | +require_once('aweber_response.php'); |
| 6 | +require_once('aweber_collection.php'); |
| 7 | +require_once('aweber_entry.php'); |
| 8 | + |
| 9 | +/** |
| 10 | + * AWeberServiceProvider |
| 11 | + * |
| 12 | + * Provides specific AWeber information or implementing OAuth. |
| 13 | + * @uses OAuthServiceProvider |
| 14 | + * @package |
| 15 | + * @version $id$ |
| 16 | + */ |
| 17 | +class AWeberServiceProvider implements OAuthServiceProvider { |
| 18 | + |
| 19 | + /** |
| 20 | + * @var String Location for API calls |
| 21 | + */ |
| 22 | + public $baseUri = 'https://api.aweber.com/1.0'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var String Location to request an access token |
| 26 | + */ |
| 27 | + public $accessTokenUrl = 'https://auth.aweber.com/1.0/oauth/access_token'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var String Location to authorize an Application |
| 31 | + */ |
| 32 | + public $authorizeUrl = 'https://auth.aweber.com/1.0/oauth/authorize'; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var String Location to request a request token |
| 36 | + */ |
| 37 | + public $requestTokenUrl = 'https://auth.aweber.com/1.0/oauth/request_token'; |
| 38 | + |
| 39 | + |
| 40 | + public function getBaseUri() { |
| 41 | + return $this->baseUri; |
| 42 | + } |
| 43 | + |
| 44 | + public function getAccessTokenUrl() { |
| 45 | + return $this->accessTokenUrl; |
| 46 | + } |
| 47 | + |
| 48 | + public function getAuthorizeUrl() { |
| 49 | + return $this->authorizeUrl; |
| 50 | + } |
| 51 | + |
| 52 | + public function getRequestTokenUrl() { |
| 53 | + return $this->requestTokenUrl; |
| 54 | + } |
| 55 | + |
| 56 | + public function getAuthTokenFromUrl() { return ''; } |
| 57 | + public function getUserData() { return ''; } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * AWeberAPIBase |
| 63 | + * |
| 64 | + * Base object that all AWeberAPI objects inherit from. Allows specific pieces |
| 65 | + * of functionality to be shared across any object in the API, such as the |
| 66 | + * ability to introspect the collections map. |
| 67 | + * |
| 68 | + * @package |
| 69 | + * @version $id$ |
| 70 | + */ |
| 71 | +class AWeberAPIBase { |
| 72 | + |
| 73 | + /** |
| 74 | + * Maintains data about what children collections a given object type |
| 75 | + * contains. |
| 76 | + */ |
| 77 | + static protected $_collectionMap = array( |
| 78 | + 'account' => array('lists', 'integrations'), |
| 79 | + 'broadcast_campaign' => array('links', 'messages'), |
| 80 | + 'followup_campaign' => array('links', 'messages'), |
| 81 | + 'link' => array('clicks'), |
| 82 | + 'list' => array('campaigns', 'subscribers', |
| 83 | + 'web_forms', 'web_form_split_tests'), |
| 84 | + 'web_form' => array(), |
| 85 | + 'web_form_split_test' => array('components'), |
| 86 | + ); |
| 87 | + |
| 88 | + /** |
| 89 | + * loadFromUrl |
| 90 | + * |
| 91 | + * Creates an object, either collection or entry, based on the given |
| 92 | + * URL. |
| 93 | + * |
| 94 | + * @param mixed $url URL for this request |
| 95 | + * @access public |
| 96 | + * @return AWeberEntry or AWeberCollection |
| 97 | + */ |
| 98 | + public function loadFromUrl($url) { |
| 99 | + try { |
| 100 | + $data = $this->adapter->request('GET', $url); |
| 101 | + return $this->readResponse($data, $url); |
| 102 | + } catch (AWeberException $e) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * readResponse |
| 109 | + * |
| 110 | + * Interprets a response, and creates the appropriate object from it. |
| 111 | + * @param mixed $response Data returned from a request to the AWeberAPI |
| 112 | + * @param mixed $url URL that this data was requested from |
| 113 | + * @access protected |
| 114 | + * @return mixed |
| 115 | + */ |
| 116 | + protected function readResponse($response, $url) { |
| 117 | + $this->adapter->parseAsError($response); |
| 118 | + if (!empty($response['id'])) { |
| 119 | + return new AWeberEntry($response, $url, $this->adapter); |
| 120 | + } else if (isset($response['entries'])) { |
| 121 | + return new AWeberCollection($response, $url, $this->adapter); |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * AWeberAPI |
| 129 | + * |
| 130 | + * Creates a connection to the AWeberAPI for a given consumer application. |
| 131 | + * This is generally the starting point for this library. Instances can be |
| 132 | + * created directly with consumerKey and consumerSecret. |
| 133 | + * @uses AWeberAPIBase |
| 134 | + * @package |
| 135 | + * @version $id$ |
| 136 | + */ |
| 137 | +class AWeberAPI extends AWeberAPIBase { |
| 138 | + |
| 139 | + /** |
| 140 | + * @var String Consumer Key |
| 141 | + */ |
| 142 | + public $consumerKey = false; |
| 143 | + |
| 144 | + /** |
| 145 | + * @var String Consumer Secret |
| 146 | + */ |
| 147 | + public $consumerSecret = false; |
| 148 | + |
| 149 | + /** |
| 150 | + * @var Object - Populated in setAdapter() |
| 151 | + */ |
| 152 | + public $adapter = false; |
| 153 | + |
| 154 | + /** |
| 155 | + * Uses the app's authorization code to fetch an access token |
| 156 | + * |
| 157 | + * @param String Authorization code from authorize app page |
| 158 | + */ |
| 159 | + public static function getDataFromAweberID($string) { |
| 160 | + list($consumerKey, $consumerSecret, $requestToken, $tokenSecret, $verifier) = AWeberAPI::_parseAweberID($string); |
| 161 | + |
| 162 | + if (!$verifier) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + $aweber = new AweberAPI($consumerKey, $consumerSecret); |
| 166 | + $aweber->adapter->user->requestToken = $requestToken; |
| 167 | + $aweber->adapter->user->tokenSecret = $tokenSecret; |
| 168 | + $aweber->adapter->user->verifier = $verifier; |
| 169 | + list($accessToken, $accessSecret) = $aweber->getAccessToken(); |
| 170 | + return array($consumerKey, $consumerSecret, $accessToken, $accessSecret); |
| 171 | + } |
| 172 | + |
| 173 | + protected static function _parseAWeberID($string) { |
| 174 | + $values = split('\|', $string); |
| 175 | + if (count($values) < 5) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + return array_slice($values, 0, 5); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Sets the consumer key and secret for the API object. The |
| 183 | + * key and secret are listed in the My Apps page in the labs.aweber.com |
| 184 | + * Control Panel OR, in the case of distributed apps, will be returned |
| 185 | + * from the getDataFromAweberID() function |
| 186 | + * |
| 187 | + * @param String Consumer Key |
| 188 | + * @param String Consumer Secret |
| 189 | + * @return null |
| 190 | + */ |
| 191 | + public function __construct($key, $secret) { |
| 192 | + // Load key / secret |
| 193 | + $this->consumerKey = $key; |
| 194 | + $this->consumerSecret = $secret; |
| 195 | + |
| 196 | + $this->setAdapter(); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Returns the authorize URL by appending the request |
| 201 | + * token to the end of the Authorize URI, if it exists |
| 202 | + * |
| 203 | + * @return string The Authorization URL |
| 204 | + */ |
| 205 | + public function getAuthorizeUrl() { |
| 206 | + $requestToken = $this->user->requestToken; |
| 207 | + return (empty($requestToken)) ? |
| 208 | + $this->adapter->app->getAuthorizeUrl() |
| 209 | + : |
| 210 | + $this->adapter->app->getAuthorizeUrl() . "?oauth_token={$this->user->requestToken}"; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Sets the adapter for use with the API |
| 215 | + */ |
| 216 | + public function setAdapter($adapter=null) { |
| 217 | + if (empty($adapter)) { |
| 218 | + $serviceProvider = new AWeberServiceProvider(); |
| 219 | + $adapter = new OAuthApplication($serviceProvider); |
| 220 | + $adapter->consumerKey = $this->consumerKey; |
| 221 | + $adapter->consumerSecret = $this->consumerSecret; |
| 222 | + } |
| 223 | + $this->adapter = $adapter; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Fetches account data for the associated account |
| 228 | + * |
| 229 | + * @param String Access Token (Only optional/cached if you called getAccessToken() earlier |
| 230 | + * on the same page) |
| 231 | + * @param String Access Token Secret (Only optional/cached if you called getAccessToken() earlier |
| 232 | + * on the same page) |
| 233 | + * @return Object AWeberCollection Object with the requested |
| 234 | + * account data |
| 235 | + */ |
| 236 | + public function getAccount($token=false, $secret=false) { |
| 237 | + if ($token && $secret) { |
| 238 | + $user = new OAuthUser(); |
| 239 | + $user->accessToken = $token; |
| 240 | + $user->tokenSecret = $secret; |
| 241 | + $this->adapter->user = $user; |
| 242 | + } |
| 243 | + |
| 244 | + $body = $this->adapter->request('GET', '/accounts'); |
| 245 | + $accounts = $this->readResponse($body, '/accounts'); |
| 246 | + return $accounts[0]; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * PHP Automagic |
| 251 | + */ |
| 252 | + public function __get($item) { |
| 253 | + if ($item == 'user') return $this->adapter->user; |
| 254 | + trigger_error("Could not find \"{$item}\""); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Request a request token from AWeber and associate the |
| 259 | + * provided $callbackUrl with the new token |
| 260 | + * @param String The URL where users should be redirected |
| 261 | + * once they authorize your app |
| 262 | + * @return Array Contains the request token as the first item |
| 263 | + * and the request token secret as the second item of the array |
| 264 | + */ |
| 265 | + public function getRequestToken($callbackUrl) { |
| 266 | + $requestToken = $this->adapter->getRequestToken($callbackUrl); |
| 267 | + return array($requestToken, $this->user->tokenSecret); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Request an access token using the request tokens stored in the |
| 272 | + * current user object. You would want to first set the request tokens |
| 273 | + * on the user before calling this function via: |
| 274 | + * |
| 275 | + * $aweber->user->tokenSecret = $_COOKIE['requestTokenSecret']; |
| 276 | + * $aweber->user->requestToken = $_GET['oauth_token']; |
| 277 | + * $aweber->user->verifier = $_GET['oauth_verifier']; |
| 278 | + * |
| 279 | + * @return Array Contains the access token as the first item |
| 280 | + * and the access token secret as the second item of the array |
| 281 | + */ |
| 282 | + public function getAccessToken() { |
| 283 | + return $this->adapter->getAccessToken(); |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +?> |
0 commit comments