-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
201 lines (173 loc) · 6.63 KB
/
config.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
$apiKey = $_COOKIE['onedionys_apillon_api_key'];
$apiSecret = $_COOKIE['onedionys_apillon_api_key_secret'];
$authorization = $_COOKIE['onedionys_apillon_authentication'];
$bucketUuid = $_COOKIE['onedionys_apillon_storage_uuid'];
$hostingUuid = $_COOKIE['onedionys_apillon_hosting_uuid'];
function toObject($array)
{
return json_decode(json_encode($array));
}
function callAPI($method, $url, $data = false, $authorization, $category = 'default')
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
break;
case "GET":
if ($data !== null && (is_array($data) || is_object($data))) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
break;
default:
break;
}
curl_setopt($curl, CURLOPT_URL, $url);
if($category == "default") {
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: ' . $authorization,
'Content-Type: application/json'
]);
}else {
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: ' . $authorization,
'Content-Type: application/json',
'x-api-key: ' . $_COOKIE['onedionys_apillon_api_key'],
'x-api-secret: ' . $_COOKIE['onedionys_apillon_api_key_secret']
]);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_error($curl)) {
return toObject(['error' => curl_error($curl)]);
}
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
switch ($statusCode) {
case 200:
case 201:
break;
case 400:
return toObject(['error' => 'Bad request. Check the request data and try again.']);
case 401:
return toObject(['error' => 'Unauthorized. Invalid API key or API key secret.']);
case 403:
return toObject(['error' => 'Forbidden. Insufficient permissions or unauthorized access to record.']);
case 404:
return toObject(['error' => 'Path not found. Invalid endpoint or resource.']);
case 422:
return toObject(['error' => 'Data validation failed. Invalid or missing fields.']);
case 500:
return toObject(['error' => 'Internal server error. Please try again later.']);
default:
return toObject(['error' => "Received HTTP code $statusCode"]);
}
$decodedResult = json_decode($result);
if (json_last_error() !== JSON_ERROR_NONE) {
return toObject(['error' => json_last_error_msg()]);
}
return toObject([
'id' => $decodedResult->id ?? null,
'status' => $statusCode,
'data' => $decodedResult->data ?? null
]);
}
function uploadToBucket($type = 'buckets', $fileData, $path = '', $wrapWithDirectory = false, $directoryPath = ''){
global $authorization, $bucketUuid, $hostingUuid;
$files = [];
foreach ($fileData as $data) {
$files[] = [
'fileName' => $data['fileName'],
'contentType' => $data['contentType'],
'path' => $path
];
}
if (empty($files)) {
return [
'status' => 0,
'status_code' => 422,
'message' => "No files to upload, make sure the files folder is filled with something",
"info_error" => 'Failed to upload the file.',
'data' => null
];
}
$url = "https://api.apillon.io/storage/buckets/$bucketUuid/upload";
if($type == "buckets") {
$url = "https://api.apillon.io/storage/buckets/$bucketUuid/upload";
}else if($type == "websites") {
$url = "https://api.apillon.io/hosting/websites/$hostingUuid/upload";
}
$postData = ['files' => $files];
$response = callAPI('POST', $url, $postData, $authorization);
if (isset($response->error)) {
return [
'status' => 0,
'status_code' => 422,
'message' => "API Error: " . $response->error,
"info_error" => 'Failed to upload the file.',
'data' => null
];
}
if (isset($response->data->sessionUuid)) {
$sessionUuid = $response->data->sessionUuid;
$uploadUrls = $response->data->files;
$uploadedFiles = [];
foreach ($fileData as $index => $data) {
$uploadUrl = $uploadUrls[$index]->url;
$filePath = $data['filePath'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
$uploadResponse = json_decode(curl_exec($ch), true);
curl_close($ch);
if (isset($uploadResponse['error'])) {
return [
'status' => 0,
'status_code' => 422,
'message' => "File Upload Error: " . $uploadResponse['error'],
"info_error" => 'Failed to upload the file.',
'data' => null
];
}
$uploadedFiles[] = $uploadResponse;
}
$endUpload = endUploadSession(
$sessionUuid,
$authorization,
$bucketUuid,
$wrapWithDirectory,
$directoryPath
);
return [
'status' => 1,
'status_code' => 200,
'message' => "Successfully uploaded the file to " . ($type == "buckets" ? "storage" : "hosting"),
"info_error" => null,
'data' => null
];
} else {
return [
'status' => 0,
'status_code' => 422,
'message' => "Failed to upload a file session",
"info_error" => 'Failed to upload the file.',
'data' => null
];
}
}
function endUploadSession($sessionUuid, $authorization, $bucketUuid, $wrapWithDirectory = false, $directoryPath = '')
{
$url = "https://api.apillon.io/storage/buckets/$bucketUuid/upload/$sessionUuid/end";
$data = [
'wrapWithDirectory' => $wrapWithDirectory,
'directoryPath' => $directoryPath
];
return callAPI('POST', $url, $data, $authorization);
}