-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
75 lines (60 loc) · 1.91 KB
/
api.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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST');
class Api
{
public function getProjects()
{
$content = file_get_contents('content.json');
$content = json_decode($content);
return $content->projects;
}
public function getProjectDetails()
{
$projectName = $_GET['name'];
if (!$projectName) {
return null;
}
$content = file_get_contents('content.json');
$content = json_decode($content);
$projects = $content->projects;
foreach ($projects as $project) {
if ($projectName === $project->name) {
return $project;
}
}
}
public function sendMessage()
{
$requestBody = file_get_contents('php://input');
$requestBody = json_decode($requestBody);
if (
empty($requestBody->name) ||
empty($requestBody->email) ||
empty($requestBody->message)
) {
throw new \Exception('Niepoprawne dane');
}
$contactEmail = 'hello@apsensa.pl';
$to = $contactEmail;
$subject = 'Nowa wiadomość od klienta Apsensy';
$message = 'Nowa wiadomość od klienta zainteresowanego ofertą. <br/><br/>
Sender data:<br/>Name: ' . $requestBody->name
. '<br/>Email: ' . $requestBody->email
. '<br/>Message: ' . $requestBody->message . '<br/>';
$headers = 'From: apsensa.pl' . "\r\n" .
'Reply-To: ' . $contactEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, $to);
return 'success';
}
}
$response = '';
$action = $_GET['action'];
$api = new Api();
if (method_exists($api, $action)) {
$response = $api->$action();
}
echo(json_encode($response));
die;