|
1 <?php |
|
2 /***************************************************************************** |
|
3 * Copyright (C) 2014 |
|
4 * |
|
5 * Michiel Broek <mbroek at mbse dot eu> |
|
6 * |
|
7 * This file is part of ThermFerm |
|
8 * |
|
9 * This is free software; you can redistribute it and/or modify it |
|
10 * under the terms of the GNU General Public License as published by the |
|
11 * Free Software Foundation; either version 2, or (at your option) any |
|
12 * later version. |
|
13 * |
|
14 * ThermFerm is distributed in the hope that it will be useful, but |
|
15 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 * General Public License for more details. |
|
18 * |
|
19 * You should have received a copy of the GNU General Public License |
|
20 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
21 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
22 *****************************************************************************/ |
|
23 |
|
24 function open_socket() |
|
25 { |
|
26 $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
|
27 |
|
28 if (!($sock === false)) { |
|
29 if (socket_connect($sock, "localhost", 6554)) { |
|
30 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0)); |
|
31 } else { |
|
32 socket_close($sock); |
|
33 } |
|
34 } |
|
35 return $sock; |
|
36 } |
|
37 |
|
38 |
|
39 |
|
40 function startsWith($haystack, $needle) |
|
41 { |
|
42 return !strncmp($haystack, $needle, strlen($needle)); |
|
43 } |
|
44 |
|
45 |
|
46 |
|
47 error_reporting(E_ALL ^ E_WARNING); |
|
48 if(isset($_POST['messageType'])){ |
|
49 $messageType = $_POST['messageType']; |
|
50 } else { |
|
51 die("messageType not set"); |
|
52 } |
|
53 |
|
54 |
|
55 |
|
56 if(isset($_POST['message'])){ |
|
57 // message with data |
|
58 $data = $_POST['message']; |
|
59 } |
|
60 |
|
61 |
|
62 |
|
63 $sock = open_socket(); |
|
64 if ($sock !== false) { |
|
65 if (isset($data) && $data != "") { |
|
66 socket_write($sock, $messageType . " " . $data, 4096); |
|
67 } else { |
|
68 socket_write($sock, $messageType, 4096); |
|
69 } |
|
70 |
|
71 /* |
|
72 * Multiple reads until the remote closed the connection |
|
73 */ |
|
74 $answer = ""; |
|
75 while (1) { |
|
76 $line = socket_read($sock, 4096); |
|
77 if ($line === '') |
|
78 break; |
|
79 $answer .= $line; |
|
80 } |
|
81 |
|
82 /* |
|
83 * Can be a multiline answer, the answer includes \r\n sequences. |
|
84 */ |
|
85 echo $answer; |
|
86 socket_close($sock); |
|
87 } |
|
88 |
|
89 |