-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.inc.php
323 lines (230 loc) · 8.42 KB
/
functions.inc.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
session_start();
include_once("./config.php");
// ///////////////////////////////////////////////////////
// VARIABLES
// ///////////////////////////////////////////////////////
$remote_base_url = $config["api"]["base"];
$remote_request_url = $config["api"]["requests"];
$remote_products_url = $config["api"]["products"];
$internal_err = $config["messages"]["global_error"];
$envato_personal_token = $config["access_tokens"]["envato_personal_token"];
$gitlab_private_token = $config["access_tokens"]["gitlab_private_token"];
// ///////////////////////////////////////////////////////
// HELPER FUNCTIONS
// ///////////////////////////////////////////////////////
function redirect_to_home() {
header("Location: index.php");
exit();
}
function debug($msg) {
var_dump($msg);
exit();
}
// ///////////////////////////////////////////////////////
// ERROR
// ///////////////////////////////////////////////////////
function show_err($msg)
{
$_SESSION["form-error-global"] = $msg;
}
function show_success($msg)
{
$_SESSION["form-success-global"] = $msg;
}
function set_field_error($field, $msg) {
$_SESSION["form-error"][$field] = $msg;
}
// ///////////////////////////////////////////////////////
// FORM
// ///////////////////////////////////////////////////////
function set_form_data($email, $username, $purchase_code, $item_id) {
$_SESSION["form-data"]["email"] = $email;
$_SESSION["form-data"]["username"] = $username;
$_SESSION["form-data"]["purchase_code"] = $purchase_code;
$_SESSION["form-data"]["requested_repo"] = $item_id;
}
function clear_form_data() {
$_SESSION["form-data"] = [];
}
function validate_data($email, $username, $purchase_code, $item_id) {
$is_err_set = false;
// Email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
set_field_error("email", "Invalid Email!");
$is_err_set = true;
}
// username
if(!$username) {
set_field_error("username", "Username required!");
$is_err_set = true;
}
// purchase code
if(!$purchase_code) {
set_field_error("purchase_code", "Purchase Code required!");
$is_err_set = true;
}
// item
if(!$item_id) {
set_field_error("requested_repo", "Please select theme!");
$is_err_set = true;
}
if($is_err_set) { redirect_to_home(); }
}
// ///////////////////////////////////////////////////////
// REQUESTS
// ///////////////////////////////////////////////////////
function make_request($url, $headers, $err_field="requested_repo", $post_data_arr=[]) {
global $internal_err;
$data = [];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// if post request
if(count($post_data_arr) > 0) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data_arr));
}
// set headers
$headers ? curl_setopt($curl, CURLOPT_HTTPHEADER, $headers) : "";
$curl_response = curl_exec($curl);
// err handling
if(curl_errno($curl)) {
show_err($internal_err);
set_field_error($err_field, curl_error($curl));
redirect_to_home();
}
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// close
curl_close($curl);
return array("status" => $status_code, "result" => json_decode($curl_response));
}
function req_validate_purchase_code($code) {
global $envato_personal_token;
$verify_envato_url = "https://api.envato.com/v3/market/author/sale?code=$code";
$envato_headers = [
"Content-type: application/json",
"Authorization: Bearer $envato_personal_token",
];
return make_request($verify_envato_url, $envato_headers);
}
function req_check_req_already_given($code) {
global $remote_request_url;
$req_existance_url = $remote_request_url . "?purchase_code=" . $code;
$req_existance_headers = [ "Content-type: application/json" ];
return make_request($req_existance_url, $req_existance_headers, "username");
}
function req_fetch_user_id($username) {
$url = "https://gitlab.com/api/v4/users?username=$username";
$headers = [ "Content-type: application/json" ];
return make_request($url, $headers, "username");
}
function req_get_gitlab_project_id($envato_item_id) {
global $remote_products_url;
$url = $remote_products_url . "?envato_id=" . $envato_item_id;
$headers = [ "Content-type: application/json" ];
return make_request($url, $headers);
}
function req_grant_access_to_repo($project_id, $user_id) {
global $gitlab_private_token;
$access_level = 20; //reporter
$url = "https://gitlab.com/api/v4/projects/" . $project_id . "/members";
$headers = [
"PRIVATE-TOKEN: $gitlab_private_token"
];
$post_data_arr = array("user_id"=> $user_id, "access_level"=> $access_level);
return make_request($url, $headers, "username", $post_data_arr);
}
function req_save_record_in_db($email, $username, $purchase_code, $product_id) {
global $remote_request_url;
// $headers = [ "Content-type: application/json" ];
$post_data_arr = array("email"=> $email, "username"=> $username, "purchase_code"=> $purchase_code, "product_id"=> $product_id);
return make_request($remote_request_url, [], "username", $post_data_arr);
}
// ///////////////////////////////////////////////////////
// RESPONSE HANDLERS
// ///////////////////////////////////////////////////////
// If error redirect
function handle_validate_purchase_code_response($response) {
global $internal_err;
if($response["status"] !== 200) {
// If error exist
if($response["status"] === 404) {
set_field_error("purchase_code", "Invalid Purchase Code!");
}else {
set_field_error("purchase_code", json_decode(json_encode($response["result"]), true)["error"]);
show_err($internal_err);
}
redirect_to_home();
}
return $response["result"]->item;
}
// If error redirect
function handle_check_req_exist_response($response) {
global $internal_err;
if($response["status"] !== 200) {
set_field_error("purchase_code", json_encode($response["result"]));
show_err($internal_err);
redirect_to_home();
}
if(count($response["result"]) > 0) {
set_field_error("purchase_code", "Access for this purchase code is already given!");
show_err("Only one user can have access to purchased item for security reasons.");
redirect_to_home();
}
}
// return user id
function handle_fetch_user_id_response($response) {
global $internal_err;
if($response["status"] !== 200) {
set_field_error("username", "Error Fetching your gitlab user details!");
show_err($internal_err);
redirect_to_home();
}
if(count($response["result"]) > 0) {
return $response["result"][0]->id;
}else {
set_field_error("username", "Error Fetching your user details! Please check your gitlab username.");
redirect_to_home();
}
}
// return gitlab project id from database
function handle_get_gitlab_project_id_response($response) {
global $internal_err;
if($response["status"] !== 200) {
set_field_error("requested_repo", "Error Fetching your gitlab project details!");
show_err($internal_err);
redirect_to_home();
}
if(count($response["result"]) > 0) {
return $response["result"][0]->gitlab_project_id;
}else {
set_field_error("requested_repo", "Error Fetching selected theme details! Please check your selected theme.");
redirect_to_home();
}
}
// If error redirect
function handle_grant_access_to_repo_response($response) {
global $internal_err;
if($response["status"] !== 201) {
// If error exist
if($response["status"] === 404) {
set_field_error("requested_repo", "Error granting access to this project.");
show_err($internal_err);
}else if($response["status"] === 409) {
set_field_error("username", "Access is already given to this user.");
}else {
set_field_error("requested_repo", json_encode($response["result"]));
show_err($internal_err);
}
redirect_to_home();
}
}
// Save data in remote db
function handle_save_record_in_db_response($response) {
global $internal_err;
if($response["status"] !== 200) {
set_field_error("username", "Error saving your user details!");
show_err($internal_err);
redirect_to_home();
}
}
?>