-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
224 lines (178 loc) · 6 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
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
<?php
/*
Script memerlukan file bernama ".htaccess" dengan isi seperti ini:
-----------------------------------
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
-----------------------------------
Struktur URL adalah /function/key?var1=val1&var2=val2....
Misalnya:
Untuk menyimpan = http://server.com/simpan/lokasiku?lan=0.11222&lat=1.883773
Untuk membaca text = http://server.com/baca/lokasiku
Untuk membaca JSON = http://server.com/baca/lokasiku/json
Author: Komputronika.com
*/
// Konfigurasi dasar
date_default_timezone_set('Asia/Jakarta');
set_time_limit(60*10);
error_reporting(0&~E_WARNING&~E_STRICT&~E_NOTICE);
// Include file konfigurasi database
require_once("config.php");
// Panggil fungsi untuk mendapatkan URL
$base_url = parsingURL();
// Masukan setiap bagian pada URL yang dipisahkan '/'
// ke dalam array 4routes
$routes = array();
$routes = explode('/', $base_url);
foreach($routes as $route) {
if(trim($route) != '')
array_push($routes, $route);
}
// Set $function sebagai bagian pertama dari URL
$function = trim(strtolower($routes[1]));
// Set $key sebagai bagian kedua dari URL
$key = trim(strtolower($routes[2]));
// Set $format sebagai bagian ketiga dari URL
$format = strtolower(trim(strtolower($routes[3])));
// Sesuaikan variable dengan config
$host = $CONFIG["hostname"];
$user = $CONFIG["username"];
$pass = $CONFIG["password"];
$db = $CONFIG["database"];
$table = "data";
// Konek ke database MySQL
// Struktur database bisa diimpor dari file iotserver.sql
$mysql = mysqli_connect( $host, $user, $pass, $db );
// Periksa aoakah isi dari variabel $function
switch ($function) {
// Kalau 'baca', panggil fungsi baca()
case "baca" :
baca($key);
break;
// Kalau 'simpan', panggil fungsi simpan()
case "simpan":
simpan($key);
break;
// Kalau bukan 'baca' atau 'simpan', panggil fungsi ngaco()
default:
ngaco();
}
//----------------------------
// Fungsi untuk membaca data IoT
//----------------------------
function baca($key) {
// Variabel $table diambil dari var global
global $table, $format, $mysql;
// Baca dari MySQL semua data data $key
if ( strpos($format, 'single') !== false ) {
$limit = 1;
} else {
$limit = 60;
}
// Query ke database MySQL
$q = mysqli_query($mysql, "select CONVERT_TZ(created_at, @@session.time_zone, '+07:00') as created, content
from $table
where `key` = '$key'
order by created_at desc
limit 0,$limit");
// Susun text nya
$res = "";
if (strpos($format, 'json') !== false) {
$jsondata = array();
}
if ($limit == 1 and strpos($format, 'json') !== false) {
$d = mysqli_fetch_object($q);
$jsondata["created_at"] = $d->created;
$json = json_decode($d->content);
foreach($json as $k => $v){
$jsondata["$k"] = $v;
}
} else {
while ($d = mysqli_fetch_object($q)) {
// Di awali dengan 'tanggal/'
$res .= $d->created."/";
// JSON yang tersimpan di table, dikonver jadi array
$json = json_decode($d->content);
// Baca setiap field dalam JSON, ke dalam array
if (strpos($format, 'json') !== false) {
$temp = array();
$temp["created_at"] = $d->created;
}
$line = array();
foreach($json as $k => $v){
$line[] = "$k:$v";
if (strpos($format, 'json') !== false) {
$temp["$k"] = $v;
$jsondata[] = $temp;
}
}
// Jadikan array tersebut menjadi string dengan pemisah '/'
$res .= implode("/",$line)."\n";
}
}
// Kirim output
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
if (strpos($format, 'json') !== false) {
// Kirim response berupa JSON
header('Content-Type: application/json');
echo json_encode($jsondata, JSON_PRETTY_PRINT);
} else {
// Kirim response berupa plain text
header('Content-Type: text/plain');
echo $res;
}
}
//----------------------------
// Fungsi untuk menyimpan data IoT
//----------------------------
function simpan($key) {
// Variabel $table diambil dari var global
global $table, $mysql;
// Konversi dulu semua variable yang terbaca di URL
// Menjadi kode JSON, untuk disimpan dalam tabel
$content = json_encode($_GET);
// Simpan ke mysql
mysqli_query($mysql, "insert into $table(`key`,content) values('$key','$content')");
// Cek apakah ada error dalam insert ke database
$success = empty(mysqli_error($mysql));
// Response 'Error' atau 'Ok'
header('Content-Type:text/plain');
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
// Tampilkan 'Ok' atau 'Error'
if ($success) {
echo "Ok";
} else {
echo "Error";
}
}
//----------------------------
// Fungsi untuk menampilkan error
//----------------------------
function ngaco() {
header('Content-Type:text/plain');
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
echo "Wrong";
}
//----------------------------
// Fungsi untuk memparsing URL
//----------------------------
function parsingURL() {
// Pecahkan nama script dari variabe $_SERVER
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
// Hilangkan alamat servernya
$uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
// Bila ditemui tanda '?', potong dan buang
if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
$uri = '/' . trim($uri, '/');
return $uri;
}
?>