-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.php
165 lines (131 loc) · 4.96 KB
/
public.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
//error_reporting(E_ALL);
error_reporting(0);
require 'lib/XeroOAuth.php';
require 'xero_settings.php'; // Xero settings file
// Functions required for Xero API call
/**
* Persist the OAuth access token and session handle somewhere
* In my example I am just using the session, but in real world, this is should be a storage engine
*
* @param array $params the response parameters as an array of key=value pairs
*/
function persistSession($response)
{
if (isset($response)) {
$_SESSION['access_token'] = $response['oauth_token'];
$_SESSION['oauth_token_secret'] = $response['oauth_token_secret'];
if(isset($response['oauth_session_handle'])) $_SESSION['session_handle'] = $response['oauth_session_handle'];
} else {
return false;
}
}
/**
* Retrieve the OAuth access token and session handle
* In my example I am just using the session, but in real world, this is should be a storage engine
*
*/
function retrieveSession()
{
if (isset($_SESSION['access_token'])) {
$response['oauth_token'] = $_SESSION['access_token'];
$response['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
$response['oauth_session_handle'] = $_SESSION['session_handle'];
return $response;
} else {
return false;
}
}
function outputError($XeroOAuth)
{
echo 'Error: ' . $XeroOAuth->response['response'] . PHP_EOL;
pr($XeroOAuth);
}
/**
* Debug function for printing the content of an object
*
* @param mixes $obj
*/
function pr($obj)
{
if (!is_cli())
echo '<pre style="word-wrap: break-word">';
if (is_object($obj))
print_r($obj);
elseif (is_array($obj))
print_r($obj);
else
echo $obj;
if (!is_cli())
echo '</pre>';
}
function is_cli()
{
return (PHP_SAPI == 'cli' && empty($_SERVER['REMOTE_ADDR']));
}
// END
if (XRO_APP_TYPE == "Private" || XRO_APP_TYPE == "Partner") {
$signatures ['rsa_private_key'] = BASE_PATH . '/certs/privatekey.pem';
$signatures ['rsa_public_key'] = BASE_PATH . '/certs/publickey.cer';
}
if (XRO_APP_TYPE == "Partner") {
$signatures ['curl_ssl_cert'] = BASE_PATH . '/certs/entrust-cert-RQ3.pem';
$signatures ['curl_ssl_password'] = '1234';
$signatures ['curl_ssl_key'] = BASE_PATH . '/certs/entrust-private-RQ3.pem';
}
$XeroOAuth = new XeroOAuth ( array_merge ( array (
'application_type' => XRO_APP_TYPE,
'oauth_callback' => OAUTH_CALLBACK,
'user_agent' => $useragent
), $signatures ) );
$initialCheck = $XeroOAuth->diagnostics ();
$checkErrors = count ( $initialCheck );
if ($checkErrors > 0) {
// you could handle any config errors here, or keep on truckin if you like to live dangerously
foreach ( $initialCheck as $check ) {
echo 'Error: ' . $check . PHP_EOL;
}
} else {
$here = XeroOAuth::php_self ();
session_start ();
$oauthSession = retrieveSession ();
include 'tests/tests.php'; /// All xero Methods would be listed
if (isset ( $_REQUEST ['oauth_verifier'] )) {
$XeroOAuth->config ['access_token'] = $_SESSION ['oauth'] ['oauth_token'];
$XeroOAuth->config ['access_token_secret'] = $_SESSION ['oauth'] ['oauth_token_secret'];
$code = $XeroOAuth->request ( 'GET', $XeroOAuth->url ( 'AccessToken', '' ), array (
'oauth_verifier' => $_REQUEST ['oauth_verifier'],
'oauth_token' => $_REQUEST ['oauth_token']
) );
if ($XeroOAuth->response ['code'] == 200) {
$response = $XeroOAuth->extract_params ( $XeroOAuth->response ['response'] );
$session = persistSession ( $response );
unset ( $_SESSION ['oauth'] );
header("Location:xeroauthorisation.php?authorize=success");
//header ( "Location: {$here}" );
} else {
outputError ( $XeroOAuth );
}
// start the OAuth dance
} elseif (isset ( $_REQUEST ['authenticate'] ) || isset ( $_REQUEST ['authorize'] )) {
$params = array (
'oauth_callback' => OAUTH_CALLBACK
);
$response = $XeroOAuth->request ( 'GET', $XeroOAuth->url ( 'RequestToken', '' ), $params );
if ($XeroOAuth->response ['code'] == 200) {
$scope = "";
// $scope = 'payroll.payrollcalendars,payroll.superfunds,payroll.payruns,payroll.payslip,payroll.employees,payroll.TaxDeclaration';
if ($_REQUEST ['authenticate'] > 1)
$scope = 'payroll.employees,payroll.payruns';
//print_r ( $XeroOAuth->extract_params ( $XeroOAuth->response ['response'] ) );
$_SESSION ['oauth'] = $XeroOAuth->extract_params ( $XeroOAuth->response ['response'] );
$authurl = $XeroOAuth->url ( "Authorize", '' ) . "?oauth_token={$_SESSION['oauth']['oauth_token']}&scope=" . $scope;
//echo '<p>To complete the OAuth flow follow this URL: <a href="' . $authurl . '">' . $authurl . '</a></p>';
header("Location:".$authurl);
} else {
outputError ( $XeroOAuth );
}
}
//header("Location:xeroauthorisation.php");
//testLinks ();
}