forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditEntry.php
More file actions
130 lines (121 loc) · 3.81 KB
/
Copy patheditEntry.php
File metadata and controls
130 lines (121 loc) · 3.81 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
127
128
129
130
<?php
require __DIR__ . '/../vendor/autoload.php';
require_once('../initialize.php');
import('../form.Form');
import('../ttOrgHelper');
import('../ttUser');
import('../ttTimeHelper');
use \Firebase\JWT\JWT;
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//check if its post request
if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') {
throw new Exception('Only POST requests are allowed');
}
//check if format is json
$content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
if (stripos($content_type, 'application/json') === false) {
throw new Exception('Content-Type must be application/json');
}
$secret_key = SECRET_KEY;
$body = file_get_contents("php://input");
$object = json_decode($body);
$jwt = $object->jwt;
$id = $object->id;
$date = $object->date;
$user_id = $object->user_id;
$client = $object->client;
$project = $object->project;
$task = $object->task;
$start = $object->start;
$finish =$object->finish;
$duration = $object->duration;
$isLoggedIn = false;
//check fot token verification
if ($jwt) {
try {
error_log($jwt);
$decoded = JWT::decode($jwt, $secret_key, array('HS256'));
$isLoggedIn = true;
$userId = $decoded->data->id;
$user = new ttUser(null, $userId);
if ($isLoggedIn) {
if($duration){
//check is date is entered
if($date==""){
$success_response = ['success' => false, 'error' => 'enter the date' ];
$response = json_encode($success_response);
print_r($response);
}
else{
if ($start)
{
//return false if start exist while duration is entered
$success_response = ['success' => false, 'error' => 'remove start time while duration is given' ];
$response = json_encode($success_response);
print_r($response);
}
else{
//update the record
$record=ttTimeHelper::update(array(
'id' => $id,
'date' => $date,
'user_id' => $user_id,
'client' => $client,
'project' => $project,
'task' => $task,
'start' => $start,
'finish' => $finish,
'duration' => $duration
));
}
}
}
else{
if($start==""||$date==""){
//check if start and date both exist
$success_response = ['success' => false, 'error' => 'date or start time not entered' ];
$response = json_encode($success_response);
print_r($response);
}
else{
$record=ttTimeHelper::update(array(
'id' => $id,
'date' => $date,
'user_id' => $user_id,
'client' => $client,
'project' => $project,
'task' => $task,
'start' => $start,
'finish' => $finish,
'duration' => $duration
));
}
}
if($record){
//send true if update is done
$success_response = ['success' => $record, 'update done' => 'true' ];
$response = json_encode($success_response);
print_r($response);
}
else
{
//send error if any false value entered
$success_response = ['success' => false, 'error' => 'wrong values entered' ];
$response = json_encode($success_response);
print_r($response);
}
}
} catch (Exception $e) {
$isLoggedIn = false;
http_response_code(401);
print_r(json_encode(array(
"message" => "Access denied.",
"error" => $e->getMessage()
)));
}
}
exit();