| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
define('FEEDBURNER_API_MANAGEMENT', 'management');
|
| 5 |
define('FEEDBURNER_API_AWARENESS', 'awareness');
|
| 6 |
|
| 7 |
function _feedburner_api_query_encode($data, $keyprefix = '', $keypostfix = '') {
|
| 8 |
assert(is_array($data));
|
| 9 |
$vars = null;
|
| 10 |
foreach ($data as $key => $value) {
|
| 11 |
if (is_array($value)) {
|
| 12 |
$vars .= $this->data_encode($value, $keyprefix . $key . $keypostfix . urlencode('['), urlencode(']'));
|
| 13 |
}
|
| 14 |
else {
|
| 15 |
$vars .= $keyprefix . $key . $keypostfix . '=' . urlencode($value) . '&';
|
| 16 |
}
|
| 17 |
}
|
| 18 |
return substr($vars, 0, -1);
|
| 19 |
}
|
| 20 |
|
| 21 |
function _feedburner_api_request($function, $args = array()) {
|
| 22 |
$requst = new stdClass();
|
| 23 |
if (!_feedburner_can_api()) {
|
| 24 |
$request->error = 'SimpleXML is not enabled and is required for use of the FeedBurner API';
|
| 25 |
return $request;
|
| 26 |
}
|
| 27 |
|
| 28 |
switch ($function) {
|
| 29 |
case 'FindFeeds':
|
| 30 |
case 'GetFeed':
|
| 31 |
case 'AddFeed':
|
| 32 |
case 'ModifyFeed':
|
| 33 |
case 'DeleteFeed':
|
| 34 |
case 'ResyncFeed':
|
| 35 |
$api = FEEDBURNER_API_MANAGEMENT;
|
| 36 |
break;
|
| 37 |
case 'GetFeedData':
|
| 38 |
$api = FEEDBURNER_API_AWARENESS;
|
| 39 |
break;
|
| 40 |
default:
|
| 41 |
$request->error = 'Function not found';
|
| 42 |
return $request;
|
| 43 |
}
|
| 44 |
|
| 45 |
/*$functions = array(
|
| 46 |
'FindFeeds' => FEEDBURENR_API_MANAGEMENT,
|
| 47 |
'GetFeed' => FEEDBURENR_API_MANAGEMENT,
|
| 48 |
'AddFeed' => FEEDBURENR_API_MANAGEMENT,
|
| 49 |
'ModifyFeed' => FEEDBURENR_API_MANAGEMENT,
|
| 50 |
'DeleteFeed' => FEEDBURENR_API_MANAGEMENT,
|
| 51 |
'ResyncFeed' => FEEDBURENR_API_MANAGEMENT,
|
| 52 |
'GetFeedData' => FEEDBURNER_API_AWARENESS,
|
| 53 |
);
|
| 54 |
if (!isset($functions[$function])) {
|
| 55 |
return false;
|
| 56 |
}
|
| 57 |
|
| 58 |
$api = $functions[$function];*/
|
| 59 |
$headers = array();
|
| 60 |
|
| 61 |
// Insert authorization tokens/header for the management API
|
| 62 |
$manual_auth = isset($args['user']) && isset($args['password']);
|
| 63 |
if ($api == FEEDBURNER_API_MANAGEMENT && !$manual_auth) {
|
| 64 |
$auth = variable_get('feedburner_auth', null);
|
| 65 |
if (empty($auth)) {
|
| 66 |
$request->error = 'Authorization required';
|
| 67 |
return $request;
|
| 68 |
}
|
| 69 |
else {
|
| 70 |
$headers['Authorization'] = 'Basic '. $auth;
|
| 71 |
unset($args['user']);
|
| 72 |
unset($args['password']);
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
// Construct the API url
|
| 77 |
$url = 'http://api.feedburner.com/'. $api .'/1.0/'. $function .'?'. _feedburner_api_query_encode($args);
|
| 78 |
|
| 79 |
$request = drupal_http_request($url, $headers);
|
| 80 |
if (isset($request->data)) {
|
| 81 |
|
| 82 |
// Convert XML data to an object so it can be easily manipulated in PHP using SimpleXML
|
| 83 |
//_feedburner_include('xml');
|
| 84 |
//$request->data = XML_unserialize($request->data);
|
| 85 |
$request->data = simplexml_load_string($request->data);
|
| 86 |
|
| 87 |
// If the API errored, load the api error string into the request error string
|
| 88 |
if (isset($request->data->err)) {
|
| 89 |
$request->error = (string) $request->data->err['msg'];
|
| 90 |
|
| 91 |
// Unset authentication token if the error involves an incorrect username or password (and not using manual authorization)
|
| 92 |
if (!$manual_auth && ($request->error == 'Unknown user' || $request->error == 'Invalid password')) {
|
| 93 |
variable_set('feedburner_auth', null);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
return $request;
|
| 99 |
}
|
| 100 |
|
| 101 |
function _feedburner_api_verify_account(&$account) {
|
| 102 |
$account['auth'] = null;
|
| 103 |
$api_request = _feedburner_api_request('FindFeeds', $account);
|
| 104 |
if (isset($api_request->error)) {
|
| 105 |
$account['error'] = $api_request->error;
|
| 106 |
}
|
| 107 |
else if (isset($api_request->data)) {
|
| 108 |
$account['auth'] = base64_encode($account['user'] .':'. $account['password']);
|
| 109 |
}
|
| 110 |
else {
|
| 111 |
$account['error'] = 'Unknown error during FeedBurner account verification.';
|
| 112 |
}
|
| 113 |
}
|