This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
126 lines (111 loc) · 4.26 KB
/
Copy pathexport.php
File metadata and controls
126 lines (111 loc) · 4.26 KB
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
<?php
require_once '../model/Config.php';
require_once '../model/DB.php';
require_once '../model/Response.php';
try {
$writeDB = DB::connectWriteDB();
}
catch (PDOException $e) {
error_log("Exception: " . $e->getMessage(), 0);
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Error: connection to database could not be established.");
$response->send();
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
try {
$raw_post_data = file_get_contents('php://input');
if (!$json_data = json_decode($raw_post_data)) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Error: request body is not valid JSON.");
$response->send();
exit();
}
if (!property_exists($_GET, 'id') || !isset($json_data->startDate, $json_data->endDate)) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
(!property_exists($_GET, 'id')) ? $response->addMessage("Error: account ID not supplied.") : false;
(!isset($json_data->startDate)) ? $response->addMessage("Error: start date not supplied.") : false;
(!isset($json_data->endDate)) ? $response->addMessage("Error: end date not supplied.") : false;
$response->send();
exit();
}
sleep(5);
$start_date = new DateTime($json_data->startDate);
$end_date = new DateTime($json_data->endDate);
$query_id = $_GET['id'];
$query_start_date = $start_date->format("Y-m-d");
$query_end_date = $end_date->format("Y-m-d");
$query = $writeDB->prepare("SELECT `name`, `phone`, `arr` AS arrival, `dep` AS departure FROM `contacts` WHERE `account_id`=:id AND `arr` BETWEEN :s AND :e LIMIT 500");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->bindParam(':s', $query_start_date, PDO::PARAM_STR);
$query->bindParam(':e', $query_end_date, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count === 0) {
$response = new Response();
$response->setHttpStatusCode(404);
$response->setSuccess(false);
$response->addMessage("Error: No results found for the given period.");
$response->send();
exit();
}
$query = $writeDB->prepare("INSERT INTO `data_exports` (`account_id`, `rows_exported`) VALUES (:id, :r)");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->bindParam(':r', $row_count, PDO::PARAM_INT);
$query->execute();
$row_count_b = $query->rowCount();
if ($row_count_b === 0) {
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Error: could not store data request. Contact database administrator.");
$response->send();
exit();
}
$response = new Response();
$response->setHttpStatusCode(200);
$response->setSuccess(true);
($row_count === 500) ? $response->addMessage("Number of rows may exceed limit. Try reducing the search period.") : false;
$response->addMessage("Data export query successful.");
$response->setData($query->fetchAll());
$response->send();
Config::RegisterAPIAccess($query_id, "export");
exit();
}catch (PDOException $e) {
error_log("Exception: " . $e->getMessage());
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Error: database error.");
$response->send();
exit();
} catch (APIException $e) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("API Error: " . $e->getMessage());
$response->send();
exit();
} catch (Error $e) {
error_log("Exception: " . $e->getMessage());
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Unknown error.");
$response->send();
exit();
}
} else {
$response = new Response();
$response->setHttpStatusCode(405);
$response->setSuccess(false);
$response->addMessage("Error: request method not permitted on the user endpoint.");
$response->send();
exit();
}