-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·146 lines (127 loc) · 4.16 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
<?php
error_reporting(0);
ini_set('display_errors', 0);
$content_type = 'application/json';
$status = 200;
$codes = Array(
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
500 => 'Internal Server Error',
501 => 'Not Implemented',
);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
header('Content-type: ' . $content_type);
// set the status
$status_header = 'HTTP/1.1 '.$status.' '.$codes[$status];
header($status_header);
if($_GET["write"] != 1 ) {
// Get mode : reading value from NFC chip
$vs_reading_line = 8;
if (isset($_GET["line"]) && $_GET["line"] >= 0 && $_GET["line"] <= 8) {
$vs_reading_line = $_GET["line"];
}
if (isset($_GET["usingreader"]) && $_GET["usingreader"] >= 0) {
$vs_usingreader = " --usingreader " . $_GET["usingreader"];
}
$vs_command = "python NFCReader.py --read " . $vs_reading_line . $vs_usingreader . " 2>&1";
if (PHP_OS == "Darwin") {
$vs_command = "sudo " . $vs_command;
}
$vs_command = "cd " . __DIR__ . " && " . $vs_command;
exec($vs_command, $output, $return_var);
$result = array();
// return reader used
$result["usingreader"] = substr($output[1], 9, 2) * 1;
if (isset($output[0]) && substr($output[0], 0, 9) == "Traceback") {
// Traceback return : we have a crash
foreach ($output as $row) {
if (substr($row, 0, 12) == "ImportError:") {
$result["result"] = "crash";
$result["hint"] = $row;
}
if (substr($row, 0, 29) == "smartcard.pcsc.PCSCExceptions") {
$result["result"] = "crash";
$result["hint"] = $row;
}
}
} else {
if (isset($output[0]) && substr($output[0], 0, 18) == "Available readers:") {
$result["readers"] = substr($output[0], 19);
}
// Everything seems ok
if (isset($output[2])) {
$result["line"] = $vs_reading_line;
if (strlen($output[2]) == 8) {
$result["result"] = "success";
$result["value"] = $output[2];
for ($i = 3; $i <= 20; $i++) {
if (isset($output[$i]) && (strlen($output[$i]) == 8)) {
$result["value"] .= $output[$i];
}
}
} else {
$result["result"] = "error";
$result["value"] = $output[2];
}
} else {
$result["result"] = "crash";
$result["hint"] = "sorry. arg. only arg. no hint at all.";
}
}
print json_encode($result, JSON_PRETTY_PRINT);
} else {
// Post mode : writing value to NFC chip
$value = $_GET["value"];
$vs_writing_line = 8;
if (isset($_GET["line"]) && $_GET["line"] >= 0 && $_GET["line"] <= 8) {
$vs_writing_line = $_GET["line"];
}
if (isset($_GET["usingreader"]) && $_GET["usingreader"] >= 0) {
$vs_usingreader = " --usingreader " . $_GET["usingreader"];
}
$vs_command = "python NFCReader.py --write ". str_pad($vs_writing_line, 8)." ".$value. $vs_usingreader . " 2>&1";
if (PHP_OS == "Darwin") {
$vs_command = "sudo " . $vs_command;
}
$vs_command = "cd " . __DIR__ . " && " . $vs_command;
exec($vs_command, $output, $return_var);
$result = array();
// return reader used
$result["usingreader"] = substr($output[1], 9, 2) * 1;
if (isset($output[0]) && substr($output[0], 0, 9) == "Traceback") {
// Traceback return : we have a crash
foreach ($output as $row) {
if (substr($row, 0, 12) == "ImportError:") {
$result["result"] = "crash";
$result["hint"] = $row;
}
if (substr($row, 0, 29) == "smartcard.pcsc.PCSCExceptions") {
$result["result"] = "crash";
$result["hint"] = $row;
}
}
} else {
if (isset($output[0]) && substr($output[0], 0, 18) == "Available readers:") {
$result["readers"] = substr($output[0], 19);
}
// Everything seems ok
if (isset($output[2])) {
$result["line"] = $vs_writing_line;
if (substr($output[2],0,5) == "Wrote") {
$result["result"] = "success";
} else {
$result["result"] = "error";
}
} else {
$result["result"] = "crash";
$result["hint"] = "sorry. arg. only arg. no hint at all.";
}
}
print json_encode($result, JSON_PRETTY_PRINT);
}