-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewsocketmessage.php
183 lines (162 loc) · 6.12 KB
/
newsocketmessage.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
<?php
/* Copyright (C) 2019 Lee C. Bussy (@LBussy)
*
* This file is part of LBussy's BrewPi WWW Remix (BrewPi-WWW-RMX).
*
* BrewPi WWW RMX is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* BrewPi WWW RMX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi WWW RMX. If not, see <https://www.gnu.org/licenses/>.
*/
// Set error reporting level
error_reporting(E_ALL ^ E_WARNING);
// Define variables
$code = 200;
$codedesc = "Ok";
$response = "";
$messageType = "";
$message = "";
function startsWith($haystack, $needle) { // TODO: Was here
return !strncmp($haystack, $needle, strlen($needle));
}
function readFromSocket($sock) { // TODO: Was here
$msg = socket_read($sock, 65536);
if ($msg == false) {
global $code;
global $codedesc;
$code = 500;
$codedesc = "Internal server error";
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
$msg = "Could not read from socket: [$errorcode] $errormsg";
}
return $msg;
}
function writeToSocket($sock, $msg) { // TODO: Was here
$bytesWritten = socket_write($sock, $msg, 65536);
$msg = "";
if ($bytesWritten == false) {
global $code;
global $codedesc;
$code = 500;
$codedesc = "Internal server error";
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
$msg = "Could not write to socket: [$errorcode] $errormsg";
}
return $msg;
}
function open_socket() { // TODO: Also in ./program_arduino.php
$isWindows = defined('PHP_WINDOWS_VERSION_MAJOR');
$useInetSocket = getConfig("useInetSocket", $isWindows);
if ($useInetSocket)
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
else
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
if (!($sock === false)) {
if (
((!$useInetSocket) && socket_connect($sock, "$GLOBALS[scriptPath]/BEERSOCKET"))
|| (($useInetSocket) && socket_connect($sock, getConfig("scriptAddress", "localhost"),
getConfig("scriptPort",6332)))
)
{
socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
} else {
socket_close($sock);
}
}
return $sock;
}
function getConfig($key, $defaultValue) { // Was in ./socket_open.php
return isset($GLOBALS[$key]) ? $GLOBALS[$key] : $defaultValue;
}
if ($_SERVER["REQUEST_METHOD"] != "POST") {
$code = 403;
$codedesc = "Forbidden";
$response = "There was a problem with your submission, please try again.";
} elseif (!file_exists('config.php')) {
$code = 500;
$codedesc = "Internal server error";
$response = "Unable to read BrewPi configuration.";
} elseif (empty(strip_tags(trim($_POST["messageType"])))) {
$code = 400;
$codedesc = "Bad request";
$response = "Missing message type. Please complete the form and try again.";
} else {
require_once('config.php'); // Read config settings
$sock = open_socket();
if ($sock !== false){
// Get the form fields and remove whitespace
$messageType = strip_tags(trim($_POST["messageType"]));
$message = strip_tags(trim($_POST["message"]));
error_log("messageType = " + $messageType);
error_log("message = " + $message);
if (!empty($message)) {
switch ($messageType){ // Message with a message argument
case "api":
writeToSocket($sock, $messageType . "=" . $message);
break;
case "setActiveProfile":
case "startNewBrew":
default:
writeToSocket($sock, $messageType . "=" . $message);
$response = readFromSocket($sock);
break;
} // Switch with message
} else {
switch ($messageType) { // Message without a message argument
case "quit":
writeToSocket($sock, $messageType);
break;
case "ack":
writeToSocket($sock, $messageType);
$response = readFromSocket($sock);
break;
case "checkScript":
writeToSocket($sock, $messageType);
$response = readFromSocket($sock);
break;
case "lcd":
writeToSocket($sock, $messageType);
$response = readFromSocket($sock);
break;
case "status":
writeToSocket($sock, $messageType);
$response = readFromSocket($sock);
break;
default:
// Pass the command to the socket and read the answer if needed
writeToSocket($sock, $messageType);
if (startsWith($messageType, "get") or $messageType == "stopLogging" or
$messageType == "pauseLogging" or $messageType == "resumeLogging") {
// Return expected data, read from socket
$response = readFromSocket($sock);
}
} // Switch without message
}
socket_close($sock);
} else {
// Could not open socket
$code = 500;
$codedesc = "Internal server error";
$response = "Could not open socket.";
}
}
// Replace degree sign with °
$response = str_replace(chr(0xB0), "°", $response);
// Raise HTTP code and return text
http_response_code($code); // Send response code
$sendText;
$sendText = $code . " (" . $codedesc . ")";
if (!empty($response)) {
$sendText .= ": " . $response;
}
echo $sendText; // Send response text