-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpensifyAPI.php
143 lines (104 loc) · 3.89 KB
/
ExpensifyAPI.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
<?php
include('oocurl/OOCurl.php');
class ExpensifyAPI {
public $api_url = 'https://www.expensify.com/api';
private $username, $password, $login_cookie, $user_agent;
public function __construct($username = null, $password = null, $login_cookie = false, $user_agent = 'ExpensifyAPI v1.0 (https://github.com/ckeboss/expensify-api)') {
if(empty($username) || empty($password)) {
throw new Exception('You must provide a username and password.');
}
$this->username = $username;
$this->password = $password;
if(!$login_cookie) {
if(!is_writable(dirname(__FILE__) .'/cookies/login_cookie')) {
if(!is_writable(dirname(__FILE__) ) && !is_writable(dirname(__FILE__) .'/cookies')) {
throw new Exception('Directory '.dirname(__FILE__).'/cookies could not be created or is not writable');
}
if(!touch(dirname(__FILE__).'/cookies/login_cookie')) {
throw new Exception('Could not create new cookie file in '.dirname(__FILE__) . '/cookies/login_cookie');
}
}
$login_cookie = dirname(__FILE__).'/cookies/login_cookie';
} else if(!is_writable($login_cookie)) {
throw new Exception('Cookie file '.$login_cookie.' could not be found or is not writable.');
}
$this->login_cookie = $login_cookie;
$this->user_agent = $user_agent;
}
private function authenticate() {
$curl = new Curl($this->api_url);
if(!$curl) {
throw new Exception('Could not create Curl object.');
}
$curl->useragent = $this->user_agent;
$curl->cookiesession = true;
$curl->cookiejar = $this->login_cookie;
$curl->cookiefile = $this->login_cookie;
$curl->post = 3;
$curl->postfields = 'command=SignIn&email='.urlencode($this->username).'&password='.urlencode($this->password);
$login_resp = json_decode($curl->exec());
$curl->close();
if($login_resp->jsonCode != 200) {
return false;
}
return true;
}
private function check_if_authenticated() {
$curl = new Curl($this->api_url);
$curl->useragent = $this->user_agent;
$curl->cookiesession = true;
$curl->cookiefile = $this->login_cookie;
$curl->post = 3;
$curl->postfields = 'command=Get&returnValueList=reportListBeta&limit=1';
$api_resp = json_decode($curl->exec());
$curl->close();
if($api_resp->jsonCode != 200) {
return false;
}
return true;
}
private function login_if_not_already($respCode, $callback, &$resp) {
if($respCode == 408) {
if(!$this->authenticate()) {
return false;
} else {
return call_user_func_array($callback[0], $callback[1]);
}
}
return $resp;
}
public function getReports($limit = 10, $offset = 0, $sort_by = 'started', $retry = false) {
$curl = new Curl($this->api_url);
$curl->useragent = $this->user_agent;
$curl->cookiesession = true;
$curl->cookiefile = $this->login_cookie;
$curl->post = 5;
$curl->postfields = 'command=Get&returnValueList=reportListBeta&limit='.urlencode($limit).'&offset='.urlencode($offset).'&sortBy='.urlencode($sort_by);
$reports_resp = json_decode($curl->exec());
$curl->close();
if(!$retry) {
return $this->login_if_not_already($reports_resp->jsonCode, array(array($this, 'getReports'), array($limit, $offset, $sort_by, true)), $reports_resp);
}
if($reports_resp->jsonCode != 200) {
return false;
}
return $reports_resp;
}
public function getReport($report_id, $retry = false) {
$curl = new Curl($this->api_url);
$curl->useragent = $this->user_agent;
$curl->cookiesession = true;
$curl->cookiefile = $this->login_cookie;
$curl->post = 2;
$curl->postfields = 'command=GetReportStuff&reportID='.urlencode($report_id);
$report_resp = json_decode($curl->exec());
$curl->close();
if(!$retry) {
return $this->login_if_not_already($report_resp->jsonCode, array(array($this, 'getReport'), array($report_id, true)), $report_resp);
}
if($report_resp->jsonCode != 200) {
return false;
}
return $report_resp;
}
}