PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 261001
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v261001
261001 260927 260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 All 191 releases
s2member / src / includes / externals / aweber / aweber.php

aweber.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 261001, at src/includes/externals/aweber/aweber.php

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