1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Serializers\MmexArraySerializer; |
7
|
|
|
use App\Services\Mmex\ClientApiService; |
8
|
|
|
use App\Services\Mmex\Functions; |
9
|
|
|
use App\Services\Mmex\MmexConstants; |
10
|
|
|
use App\Transformers\Mmex\TransactionTransformer; |
11
|
|
|
use Auth; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Log; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* API endpoint for client https://github.com/moneymanagerex/moneymanagerex/blob/master/src/webapp.cpp. |
17
|
|
|
*/ |
18
|
|
|
class MmexController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClientApiService |
22
|
|
|
*/ |
23
|
|
|
private $mmexService; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MmexController constructor. |
27
|
|
|
* |
28
|
|
|
* @param ClientApiService $mmexService |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ClientApiService $mmexService) |
31
|
|
|
{ |
32
|
|
|
$this->mmexService = $mmexService; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function handle(Request $request) |
36
|
|
|
{ |
37
|
|
|
$user = Auth::guard('api')->user(); |
38
|
|
|
abort_unless($user, 404, 'User not found!'); |
39
|
|
|
|
40
|
|
|
$debugData = ['url' => $request->fullUrl(), 'data' => $request->all(), 'method' => $request->method()]; |
41
|
|
|
|
42
|
|
|
Log::debug('Service Request', $debugData); |
43
|
|
|
|
44
|
|
|
$data = $request->all(); |
45
|
|
|
|
46
|
|
|
$postData = null; |
47
|
|
|
|
48
|
|
|
if (isset($data['MMEX_Post'])) { |
49
|
|
|
$postData = json_decode($data['MMEX_Post']); |
50
|
|
|
} elseif (isset($data['postData'])) { |
51
|
|
|
$postData = json_decode($data['postData']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($postData) { |
55
|
|
|
Log::debug('MmexController, $postData', [$postData]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$function = $this->getFunction($data); |
59
|
|
|
|
60
|
|
|
if ($function == Functions::CheckGuid) { |
61
|
|
|
// TODO |
62
|
|
|
return $this->returnSuccess(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($function == Functions::CheckApiVersion) { |
66
|
|
|
return $this->returnText(MmexConstants::$api_version); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($function == Functions::DownloadTransactions) { |
70
|
|
|
$transactions = $this->mmexService->getTransactions($user); |
71
|
|
|
|
72
|
|
|
$responseText = ''; |
73
|
|
|
if ($transactions->count()) { |
74
|
|
|
$result = fractal() |
75
|
|
|
->collection($transactions) |
76
|
|
|
->serializeWith(new MmexArraySerializer()) |
77
|
|
|
->transformWith(new TransactionTransformer()) |
78
|
|
|
->toArray(); |
79
|
|
|
|
80
|
|
|
// encodes the array as it its (with it keys "0"=> {}, as needed by client) |
81
|
|
|
$responseText = $this->toJson($result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->returnText($responseText); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($function == Functions::DonwloadAttachment) { |
88
|
|
|
|
89
|
|
|
// is something like: Transaction_3_test-receipt-3.png |
90
|
|
|
$fileName = $data['download_attachment']; |
91
|
|
|
|
92
|
|
|
$attachment = $this->mmexService->getAttachment($user, $fileName); |
93
|
|
|
|
94
|
|
|
// return file as download |
95
|
|
|
$filePath = $attachment->getPath(); |
96
|
|
|
|
97
|
|
|
$headers = [ |
98
|
|
|
'Content-Type' => '', |
99
|
|
|
'Cache-Control' => 'public', |
100
|
|
|
'Content-Description' => 'File Transfer', |
101
|
|
|
'Content-Disposition' => 'attachment; filename= '.$fileName, |
102
|
|
|
'Content-Transfer-Encoding' => 'binary', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
return response()->file($filePath, $headers); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($function == Functions::DeleteBankAccounts) { |
109
|
|
|
$this->mmexService->deleteAccounts($user); |
110
|
|
|
|
111
|
|
|
return $this->returnSuccess(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($function == Functions::ImportBankAccounts) { |
115
|
|
|
$this->mmexService->importBankAccounts($user, $postData); |
116
|
|
|
|
117
|
|
|
return $this->returnSuccess(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($function == Functions::DeletePayees) { |
121
|
|
|
$this->mmexService->deletePayees($user); |
122
|
|
|
|
123
|
|
|
return $this->returnSuccess(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($function == Functions::ImportPayees) { |
127
|
|
|
$this->mmexService->importPayees($user, $postData); |
128
|
|
|
|
129
|
|
|
return $this->returnSuccess(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($function == Functions::DeleteCategories) { |
133
|
|
|
$this->mmexService->deleteCategories($user); |
134
|
|
|
|
135
|
|
|
return $this->returnSuccess(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($function == Functions::ImportCategories) { |
139
|
|
|
$this->mmexService->importCategories($user, $postData); |
140
|
|
|
|
141
|
|
|
return $this->returnSuccess(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($function == Functions::DeleteTransactions) { |
145
|
|
|
$transactionId = $data['delete_group']; |
146
|
|
|
$this->mmexService->deleteTransactions($user, $transactionId); |
147
|
|
|
|
148
|
|
|
return $this->returnSuccess(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $data; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function getFunction($data) |
155
|
|
|
{ |
156
|
|
|
if (isset($data[Functions::CheckApiVersion])) { |
157
|
|
|
return Functions::CheckApiVersion; |
158
|
|
|
} |
159
|
|
|
if (isset($data[Functions::CheckGuid])) { |
160
|
|
|
return Functions::CheckGuid; |
161
|
|
|
} |
162
|
|
|
if (isset($data[Functions::DeleteAttachment])) { |
163
|
|
|
return Functions::DeleteAttachment; |
164
|
|
|
} |
165
|
|
|
if (isset($data[Functions::DeleteBankAccounts])) { |
166
|
|
|
return Functions::DeleteBankAccounts; |
167
|
|
|
} |
168
|
|
|
if (isset($data[Functions::DeleteCategories])) { |
169
|
|
|
return Functions::DeleteCategories; |
170
|
|
|
} |
171
|
|
|
if (isset($data[Functions::DeletePayees])) { |
172
|
|
|
return Functions::DeletePayees; |
173
|
|
|
} |
174
|
|
|
if (isset($data[Functions::DeleteTransactions])) { |
175
|
|
|
return Functions::DeleteTransactions; |
176
|
|
|
} |
177
|
|
|
if (isset($data[Functions::DonwloadAttachment])) { |
178
|
|
|
return Functions::DonwloadAttachment; |
179
|
|
|
} |
180
|
|
|
if (isset($data[Functions::DownloadTransactions])) { |
181
|
|
|
return Functions::DownloadTransactions; |
182
|
|
|
} |
183
|
|
|
if (isset($data[Functions::ImportBankAccounts])) { |
184
|
|
|
return Functions::ImportBankAccounts; |
185
|
|
|
} |
186
|
|
|
if (isset($data[Functions::ImportBankAccounts])) { |
187
|
|
|
return Functions::ImportBankAccounts; |
188
|
|
|
} |
189
|
|
|
if (isset($data[Functions::ImportPayees])) { |
190
|
|
|
return Functions::ImportPayees; |
191
|
|
|
} |
192
|
|
|
if (isset($data[Functions::DeleteCategories])) { |
193
|
|
|
return Functions::DeleteCategories; |
194
|
|
|
} |
195
|
|
|
if (isset($data[Functions::ImportCategories])) { |
196
|
|
|
return Functions::ImportCategories; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
throw new \Exception('No valid function request!'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function returnSuccess() |
203
|
|
|
{ |
204
|
|
|
return $this->returnText(MmexConstants::$operation_succeded); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
private function returnText($text) |
208
|
|
|
{ |
209
|
|
|
return response($text, 200) |
|
|
|
|
210
|
|
|
->header('Content-Type', 'text/plain; charset=UTF-8'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
private function toJson($data) |
214
|
|
|
{ |
215
|
|
|
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.