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