www-thermferm/utilities.php

changeset 709
5b6d7b640e52
parent 708
13555c27b592
child 710
abe60578d695
equal deleted inserted replaced
708:13555c27b592 709:5b6d7b640e52
1 <?php
2 /*****************************************************************************
3 * Copyright (C) 2014-2015
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 $my_style = 'ui-redmond';
25
26
27 function open_socket()
28 {
29 $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
30
31 if (!($sock === false)) {
32 if (socket_connect($sock, "localhost", 6554)) {
33 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
34 } else {
35 socket_close($sock);
36 }
37 }
38 return $sock;
39 }
40
41
42
43 /*
44 * @param string $command to send to the server.
45 * Return: string with the complete reply from the
46 * server. This can be a multiline reply.
47 */
48 function send_cmd($command)
49 {
50 $sock = open_socket();
51 if ($sock == false) {
52 return "";
53 }
54 socket_write($sock, $command . "\r\n", 4096);
55
56 $answer = "";
57 while (1) {
58 $line = socket_read($sock, 4096);
59 if ($line === '')
60 break;
61 $answer .= $line;
62 }
63 socket_close($sock);
64
65 return $answer;
66 }
67
68
69
70 /*
71 * @param string array of $commands to send to the server.
72 * Return: string with the complete reply from the
73 * server. This can be a multiline reply.
74 */
75 function send_array($command)
76 {
77 $sock = open_socket();
78 if ($sock == false) {
79 return "";
80 }
81
82 foreach($command as $cmd) {
83 socket_write($sock, $cmd . "\r\n", 4096);
84 }
85
86 $answer = "";
87 while (1) {
88 $line = socket_read($sock, 4096);
89 if ($line === '')
90 break;
91 $answer .= $line;
92 }
93 socket_close($sock);
94
95 return $answer;
96 }
97
98
99
100 /*
101 * @param string $command to send to the server.
102 * Return: 0 = Ok.
103 * 1 = Server responden with an error.
104 */
105 function send_cmd_check($command)
106 {
107 $answer = send_cmd($command);
108
109 if (strlen($answer) && (($answer[0] == '1') || ($answer[0] == '2')))
110 return 0;
111
112 return 1;
113 }
114
115
116
117 /*
118 * @param array $command to send to the server.
119 * Return: 0 = Ok.
120 * 1 = Server responden with an error.
121 */
122 function send_array_check($command)
123 {
124 $answer = send_array($command);
125
126 if (strlen($answer) && (($answer[0] == '1') || ($answer[0] == '2')))
127 return 0;
128
129 return 1;
130 }
131
132
133
134 function startsWith($haystack, $needle)
135 {
136 return !strncmp($haystack, $needle, strlen($needle));
137 }
138
139
140 function build_header($heading)
141 {
142 global $my_style;
143
144 $answer = send_cmd('GLOBAL GET');
145 $arr = explode("\r\n", $answer);
146 $version = "?";
147
148 if (startsWith($arr[0], "213")) {
149 $j = 1;
150 while (1) {
151 if (strcmp($arr[$j], ".") == 0)
152 break;
153 $f = explode(",", $arr[$j]);
154
155 if ($f[0] == "RELEASE")
156 $version = $f[1];
157 $j++;
158 }
159 }
160
161 $outstr = '<!DOCTYPE html>'.PHP_EOL;
162 $outstr .= '<html>'.PHP_EOL;
163 $outstr .= ' <head>'.PHP_EOL;
164 $outstr .= ' <meta http-equiv="content-type" content="text/html; charset=utf-8" />'.PHP_EOL;
165 $outstr .= ' <title>'.$heading.'</title>'.PHP_EOL;
166 $outstr .= ' <link type="text/css" href="css/style.css" rel="stylesheet" media="all" />'.PHP_EOL;
167 $outstr .= ' <link type="text/css" href="jqwidgets/styles/jqx.base.css" rel="stylesheet" />'.PHP_EOL;
168 $outstr .= ' <link type="text/css" href="jqwidgets/styles/jqx.'.$my_style.'.css" rel="stylesheet" />'.PHP_EOL;
169 $outstr .= ' <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>'.PHP_EOL;
170 $outstr .= ' <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>'.PHP_EOL;
171 $outstr .= ' <script type="text/javascript" src="jqwidgets/jqxwindow.js"></script>'.PHP_EOL;
172 $outstr .= ' <script type="text/javascript" src="jqwidgets/jqxbuttons.js"></script>'.PHP_EOL;
173 $outstr .= ' </head>'.PHP_EOL;
174 $outstr .= ' <body class="default">'.PHP_EOL;
175 $outstr .= ' <div id="jqxWidget">'.PHP_EOL;
176 $outstr .= ' <div id="header">'.PHP_EOL;
177 $outstr .= ' <div id="title">'.PHP_EOL;
178 $outstr .= ' ThermFerm '.$version.PHP_EOL;
179 $outstr .= ' </div>'.PHP_EOL;
180 $outstr .= ' <form action="maintenance.php" style="margin:30px; float:right">'.PHP_EOL;
181 $outstr .= ' <input type="submit" id="maintenance" value="Maintenance panel" />'.PHP_EOL;
182 $outstr .= ' </form>'.PHP_EOL;
183 $outstr .= ' </div> <!-- header -->'.PHP_EOL;
184 $outstr .= ' <div id="content">'.PHP_EOL;
185
186 return $outstr;
187 }
188
189
190
191 function build_footer()
192 {
193 $outstr = ' </div> <!-- content -->'.PHP_EOL;
194 $outstr .= ' </div> <!-- jqxWidget -->'.PHP_EOL;
195 $outstr .= ' </body>'.PHP_EOL;
196 $outstr .= '</html>'.PHP_EOL;
197
198 return $outstr;
199 }
200
201
202
203 /*
204 * Goto URL. Works also after headers have been sent.
205 */
206 function load($url) {
207 echo'
208 <script>
209 if (top.location != self.location) {
210 top.location = "'.$url.'"
211 }
212 {location.href="'.$url.'";}
213 </script>';
214 }
215
216

mercurial