forked from aloz77/vwmon
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvwmon-server.php
More file actions
169 lines (125 loc) · 4.68 KB
/
vwmon-server.php
File metadata and controls
169 lines (125 loc) · 4.68 KB
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
<?php
// Vaillant Monitor Server
// Alexey Ozerov (c) 2014
// GNU GENERAL PUBLIC LICENSE
// Ver. 0.10c
// Configuration
$db_host = 'localhost';
$db_name = 'dbname';
$db_user = 'user';
$db_pass = 'password';
$db_table_history = 'vwmon_history';
$db_table_commands = 'vwmon_commands';
$serverkey = 'somekey'; // Set a fairly long random server key to avoid misuse
$admin_email = 'mein@email.de'; // This will be the sender address for all emails sent
//********************************************************************
// DO NOT EDIT BELOW THIS LINE
//********************************************************************
if ($serverkey == 'somekey')
die('Please change the serverkey');
// Swith to test table on test installation
if (strstr($_SERVER['PHP_SELF'],'test-vwmon-server.php'))
$db_table_history = 'test_vwmon_history';
// Check server key
if (!isset($_GET["serverkey"]) || $_GET["serverkey"]!=$serverkey || $_GET["serverkey"]=="")
error('VWMon ERROR: wrong or empty server key');
else
unset($_GET["serverkey"]);
// Open mySQL
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_error())
error ("Database connection failed due to ".mysqli_connect_error());
// *********************************************************
// Action addrecord
// *********************************************************
if (isset($_GET["action"]) && $_GET["action"]=="addrecord")
{
// Check datetime
if (!isset($_GET["datetime"]))
error('VWMon ERROR: Empty datetime parameter');
$recordtime=strtotime($_GET["datetime"]);
if (!$recordtime || $recordtime==-1)
error('Frewe ERROR: Malformed datetime parameter, use YYYY-MM-DD HH:MM:SS');
else
{
// Build query
$query = "INSERT INTO $db_table_history SET ";
foreach ($_GET as $field => $value)
{ if ($field!="action")
{
if (isset($value) && $value!="NULL")
$query .= sprintf("`%s` = '%s', ", $mysqli->real_escape_string($field), $mysqli->real_escape_string($value));
else
$query .= sprintf("`%s` = %s, ", $mysqli->real_escape_string($field), "NULL");
}
}
$query = substr($query,0,-2);
$result = $mysqli->query($query);
if (!$result)
error('VWMon ERROR: Dabase query fault: ' . $mysqli->error. ' query: '. $query);
echo ("OK");
}
}
// *********************************************************
// Action getcommand
// *********************************************************
else if (isset($_GET["action"]) && $_GET["action"]=="getcommand")
{
$query = "SELECT id,command FROM $db_table_commands WHERE status=0 ORDER BY `datetime_created` ASC LIMIT 1";
$result = $mysqli->query($query);
if (!$result)
error('Frewe ERROR dabase query fault: ' . $mysqli->error. ' query: '. $query);
$lastrow = $result->fetch_assoc();
if ($lastrow)
{ echo ($lastrow['id'].":".$lastrow['command']);
$query = "UPDATE $db_table_commands SET status=1 WHERE id='".$lastrow['id']."'";
$result = $mysqli->query($query);
if (!$result)
error('Frewe ERROR dabase query fault: ' . $mysqli->error. ' query: '. $query);
}
else
echo ("Not found");
}
// *********************************************************
// Action setfeedback
// *********************************************************
else if (isset($_GET["action"]) && $_GET["action"]=="setfeedback" && isset($_GET["id"]) && isset($_GET["feedback"]))
{
$query = "UPDATE $db_table_commands SET status=2, feedback = '".$mysqli->real_escape_string($_GET["feedback"])."' WHERE id='".$mysqli->real_escape_string($_GET["id"])."'";
$result = $mysqli->query($query);
if (!$result)
error('Frewe ERROR dabase query fault: ' . $mysqli->error. ' query: '. $query);
echo ("OK");
}
// *********************************************************
// Action submiterror
// *********************************************************
else if (isset($_GET["action"]) && $_GET["action"]=="submiterror" && $admin_email!='')
{
if (!isset($_GET["email"]) || $_GET["email"]=="")
$receiver_email=$admin_email; // Compatibility
else
$receiver_email=$_GET["email"];
$header = "From: $admin_email";
mail($receiver_email, "vwmon-client error", isset($_GET["errortext"])?$_GET["errortext"]:"???", $header);
echo ("OK");
}
// *********************************************************
// No action found
// *********************************************************
else
error ('VWMon ERROR: Action not set');
// *********************************************************
// Log error and exit
// *********************************************************
function error($message)
{
global $link,$admin_email;
if ($link) mysql_close($link);
error_log ($message);
echo ($message);
$header = "From: $admin_email";
mail($admin_email, "vwmon-client error", $message, $header);
exit(1);
}
?>