aweber.php 8.9 KB

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