-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
142 lines (113 loc) · 3.81 KB
/
index.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
<?php
$start = (float) microtime(false);
/*
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
header('Access-Control-Allow-Headers: token, Content-Type');
header('Access-Control-Max-Age: 1728000');
header('Content-Length: 0');
header('Content-Type: text/plain');
die();
}
*/
/*
var_dump($_SERVER['HTTP_ORIGIN']);
var_dump($_SERVER);
return 0;
*/
if(!empty($_SERVER['HTTP_ORIGIN'])){
$http_origin = $_SERVER['HTTP_ORIGIN'];
} else {
//var_dump( $_SERVER );
//$http_origin = $_SERVER['HTTP_REFERER'];
$http_origin = 'http://' . $_SERVER['HTTP_HOST'];
}
$allowed_origins = [
"https://app.jloads.com",
"http://app.jloads.com",
"https://js.jloads.com",
"http://js.jloads.com",
"https://localhost",
"https://localhost:8000",
"http://localhost",
"http://localhost:3000",
"http://localhost:3001",
"http://app.faas.ovh",
"https://app.faas.ovh",
"http://api.faas.ovh",
"https://api.faas.ovh",
"http://www.faas.ovh",
"https://www.faas.ovh",
"http://localhost:8000",
"http://localhost:8080",
"http://localhost:80",
];
if (in_array($http_origin, $allowed_origins))
{
header("Access-Control-Allow-Origin: $http_origin");
}
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Expose-Headers: Content-Length, X-JSON');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Requested-With, X-Auth-Token , Authorization, Access-Control-Allow-Headers, Access-Control-Expose-Headers, Access-Control-Allow-Methods');
header('Content-Type: application/json');
/*
* Handle All Requests from Frontend
* defualt Ajax / REST requests
*/
//http://localhost/origini-app/visitor/newsletter/php/index.php
//http://localhost/origini-app/visitor/newsletter/page/index.html
include_once 'src/helpers.php';
include_once 'src/Config.php';
include_once 'src/Message.php';
include_once 'src/Rest.php';
//var_dump($_SERVER);
//if(!empty($_GET)){
// $method = $_SERVER['REQUEST_METHOD'];
//}
//$id = explode('/', substr(@$_SERVER['PATH_INFO'], 1));
$message = new Visitor\Newsletter\Message();
$json = '{}';
$rest = new Visitor\Newsletter\Rest();
$method = 'GET';
if (!empty($_SERVER['REQUEST_METHOD'])) {
$method = $_SERVER['REQUEST_METHOD'];
}
$result = [];
switch ($method) {
case 'POST':
// $model = getFromArray($_REQUEST);
parse_str(file_get_contents("php://input"), $put_vars);
$model = getFromArray($put_vars);
$model = json_decode($model, true);
// var_dump($_REQUEST);
$result = $rest->post($model);
break;
case 'GET':
$result = $rest->get();
break;
case 'PUT':
parse_str(file_get_contents("php://input"), $put_vars);
$model = getFromArray($put_vars);
$model = json_decode($model, true);
$result = $rest->put($model);
break;
case 'DELETE':
$result = $rest->delete();
break;
default:
$message->error('Problem z połączeniem, metoda nie rozpoznana', $method);
break;
}
$result['message']['error'] = $message->showType('error');
$result['time'] = [];
//var_dump($start);
//die;
$result['time']['start'] = number_format( $start, 5, '.', '');
$stop = (float) microtime(false);
$result['time']['stop'] = number_format( $stop, 5, '.', '');
$result['time']['during'] = number_format( $stop - $start, 5, '.', '');
$json = json_encode($result);
echo $json;