Skip to content

Commit 10d9ac4

Browse files
author
Derek Hall
committed
Initial commit for old AppDirect API
0 parents  commit 10d9ac4

6 files changed

+336
-0
lines changed

AppDirect.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This implementation relies on
5+
* http://code.42dh.com/oauth/
6+
*/
7+
if(!class_exists('OAuthException')) {
8+
require_once('OAuth/OAuth.php');
9+
}
10+
11+
require_once('AppDirectBase.php');
12+
require_once('AppDirectEvent.php');
13+
require_once('AppDirectUser.php');
14+
require_once('AppDirectException.php');
15+
require_once('AppDirectConnector.php');
16+
17+
?>

AppDirectBase.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
//Base class for AppDirect Data Objects.
3+
class AppDirectBase {
4+
5+
public function __construct(SimpleXMLElement $xml = null)
6+
{
7+
if ($xml) {
8+
//Load object dynamically and convert SimpleXMLElements into strings
9+
foreach($xml as $key => $element)
10+
{
11+
if(count($element) > 0)
12+
$this->$key = new AppDirectBase($element);
13+
else
14+
$this->$key = (string)$element;
15+
}
16+
}
17+
}
18+
19+
public function getXMLObject(&$xml = null) {
20+
if ($xml === null) {
21+
$xml = simplexml_load_string(sprintf("<?xml version='1.0' encoding='utf-8'?><%s></%s>", $this->getName(), $this->getName()));
22+
}
23+
foreach (get_object_vars($this) as $key=>$val) {
24+
if ($key != 'connector') {
25+
if (is_object($val) && method_exists($val, "getXMLObject")) {
26+
$node = $xml->addChild($key);
27+
$val->getXMLObject($node);
28+
} elseif ($val !== null) {
29+
$xml->addChild($key,htmlentities($val, ENT_QUOTES));
30+
}
31+
}
32+
}
33+
return $xml;
34+
}
35+
36+
public function getXML() {
37+
$xml = $this->getXMLObject();
38+
return $xml->asXML();
39+
}
40+
41+
}
42+
43+
?>

AppDirectConnector.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
class AppDirectConnector
4+
{
5+
var $key = APPDIRECT_CONSUMER_KEY;
6+
var $secret = APPDIRECT_CONSUMER_SECRET;
7+
var $endpoint = 'https://www.appdirect.com/rest/api/';
8+
9+
var $consumer = false;
10+
11+
function __construct($key = null, $secret = null)
12+
{
13+
if($key !== null)
14+
$this->key = $key;
15+
if($secret !== null)
16+
$this->secret = $secret;
17+
18+
// Create our OAuth Consumer
19+
$this->consumer = new OAuthConsumer($this->key, $this->secret);
20+
21+
}
22+
23+
function getSignedUrl($url)
24+
{
25+
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'GET', $url, NULL);
26+
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
27+
$url = $request->to_url();
28+
return $url;
29+
}
30+
31+
function get($path, $data = array())
32+
{
33+
// Create a signed request for this GET
34+
$url = $this->endpoint . $path;
35+
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'GET', $url, NULL);
36+
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
37+
$auth_header = $request->to_header();
38+
39+
// Setup curl
40+
$curl = curl_init($url);
41+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
42+
curl_setopt($curl, CURLOPT_FAILONERROR, false);
43+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
44+
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
45+
46+
// Fetch the data
47+
$response = curl_exec($curl);
48+
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
49+
50+
// Do some verification on it
51+
$curl_error = ($code > 0 ? null : curl_error($curl) . ' (' . curl_errno($curl) . ')');
52+
53+
curl_close($curl);
54+
55+
if ($curl_error) // Connection Error?
56+
{
57+
throw new AppDirectConnectionException('An error occurred while connecting to AppDirect: ' . $curl_error);
58+
return;
59+
}
60+
elseif ($code == 200) // Created Successfully
61+
{
62+
$result = simplexml_load_string($response);
63+
return $result;
64+
}
65+
elseif ($code == 422) { // Unprocessible Entity
66+
$errors = new SimpleXMLElement($response);
67+
throw new AppDirectValidationException($code, $errors);
68+
return;
69+
}
70+
}
71+
72+
function post($path, $data = array())
73+
{
74+
// Create a signed request for this POST
75+
$url = $this->endpoint . $path;
76+
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'POST', $url, null);
77+
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
78+
$auth_header = $request->to_header();
79+
80+
// Setup curl
81+
$curl = curl_init($url);
82+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
83+
curl_setopt($curl, CURLOPT_FAILONERROR, false);
84+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
85+
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header, 'Content-type: application/xml'));
86+
curl_setopt($curl, CURLOPT_POST, true);
87+
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
88+
89+
// Fetch the data
90+
$response = curl_exec($curl);
91+
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
92+
93+
94+
// Do some verification on it
95+
$curl_error = ($code > 0 ? null : curl_error($curl) . ' (' . curl_errno($curl) . ')');
96+
curl_close($curl);
97+
98+
if ($curl_error) // Connection Error?
99+
{
100+
throw new AppDirectConnectionException('An error occurred while connecting to AppDirect: ' . $curl_error);
101+
return;
102+
}
103+
elseif ($code == 200) // Created Successfully
104+
{
105+
$result = simplexml_load_string($response);
106+
return $result;
107+
}
108+
elseif ($code == 422) { // Unprocessible Entity
109+
$errors = new SimpleXMLElement($response);
110+
throw new AppDirectValidationException($code, $errors);
111+
return;
112+
}
113+
}
114+
115+
}
116+
117+
?>

AppDirectEvent.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Define the different event types and classifications
5+
*/
6+
define('SUBSCRIPTION_ORDER', 'SUBSCRIPTION_ORDER');
7+
define('SUBSCRIPTION_CHANGE', 'SUBSCRIPTION_CHANGE');
8+
define('SUBSCRIPTION_CANCEL', 'SUBSCRIPTION_CANCEL');
9+
define('SUBSCRIPTION_NOTICE', 'SUBSCRIPTION_NOTICE');
10+
define('USER_ASSIGNMENT', 'USER_ASSIGNMENT');
11+
define('USER_UNASSIGNMENT', 'USER_UNASSIGNMENT');
12+
define('ACCOUNT_SYNC', 'ACCOUNT_SYNC');
13+
define('ACCOUNT_UNSYNC', 'ACCOUNT_UNSYNC');
14+
define('USER_SYNC', 'USER_SYNC');
15+
define('USER_LIST_CHANGE', 'USER_LIST_CHANGE');
16+
17+
class AppDirectEvent extends AppDirectBase
18+
{
19+
var $type;
20+
var $payload;
21+
var $creator;
22+
var $flag;
23+
var $returnUrl;
24+
25+
private $connector;
26+
27+
public function __construct(SimpleXMLElement $xml = null)
28+
{
29+
$this->connector = new AppDirectConnector();
30+
parent::__construct($xml);
31+
}
32+
33+
/**
34+
* Return the name of this object - the name is used in the XML tags
35+
*/
36+
protected function getName()
37+
{
38+
return 'event';
39+
}
40+
41+
public function getByToken($token = null)
42+
{
43+
$path = 'events/'.$token;
44+
$xmlObject = $this->connector->get($path);
45+
return new AppDirectEvent($xmlObject);
46+
}
47+
48+
public function postUserListChange($accountIdentifier)
49+
{
50+
$endpoint = 'events/';
51+
52+
$xmlData = new SimpleXMLElement('<event></event>');
53+
$xmlData->addChild('type', USER_LIST_CHANGE);
54+
$xmlData->addChild('payload');
55+
$xmlData->payload->addChild('account');
56+
$xmlData->payload->account->addChild('accountIdentifier', $accountIdentifier);
57+
58+
$xmlResult = $this->connector->post($endpoint, $xmlData->asXML());
59+
return $xmlResult;
60+
}
61+
62+
public function signReturnUrl($params = array())
63+
{
64+
$url = $this->returnUrl;
65+
foreach($params as $key => $value)
66+
{
67+
$url .= '&'.$key.'='.$value;
68+
}
69+
return $this->connector->getSignedUrl($url);
70+
}
71+
72+
public function xmlResponse($success, $code, $message)
73+
{
74+
$xmlResult = new SimpleXMLElement('<result></result>');
75+
$xmlResult->addChild('success', ($success ? 'true' : 'false') );
76+
if(!$success)
77+
$xmlResult->addChild('errorCode', $code);
78+
$xmlResult->addChild('message', $message);
79+
80+
return $xmlResult->asXML();
81+
}
82+
}
83+
84+
?>

AppDirectException.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
class AppDirectException extends Exception {}
3+
4+
class AppDirectConnectionException extends AppDirectException {}
5+
6+
class AppDirectValidationException extends AppDirectException {
7+
var $errors;
8+
var $http_code;
9+
10+
public function AppDirectValidationException($http_code, $error) {
11+
$this->http_code = $http_code;
12+
13+
$message = '';
14+
$this->errors = array();
15+
foreach ($error as $key=>$value) {
16+
if ($key == 'error') {
17+
$this->errors[] = $value;
18+
$message .= $value . ' ';
19+
}
20+
}
21+
22+
parent::__construct($message, intval($http_code));
23+
}
24+
}
25+
26+
class AppDirectNotFoundException extends AppDirectException {
27+
var $errors;
28+
var $http_code;
29+
30+
public function AppDirectNotFoundException($http_code, $error) {
31+
$this->http_code = $http_code;
32+
33+
$message = '';
34+
$this->errors = array();
35+
foreach ($error as $key=>$value) {
36+
if ($key == 'error') {
37+
$this->errors[] = $value;
38+
$message .= $value . ' ';
39+
}
40+
}
41+
42+
parent::__construct($message, intval($http_code));
43+
}
44+
}
45+
46+
class AppDirectError
47+
{
48+
var $field;
49+
var $message;
50+
}
51+
52+
?>

AppDirectUser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class AppDirectUser extends AppDirectBase
4+
{
5+
var $email;
6+
var $firstName;
7+
var $lastName;
8+
var $openId;
9+
10+
public function __construct(SimpleXMLElement $xml = null)
11+
{
12+
$this->connector = new AppDirectConnector();
13+
if ($xml) {
14+
//Load object dynamically and convert SimpleXMLElements into strings
15+
foreach($xml as $key => $element)
16+
{
17+
$this->$key = (string)$element;
18+
}
19+
}
20+
}
21+
}
22+
23+
?>

0 commit comments

Comments
 (0)