-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
204 lines (164 loc) · 4.56 KB
/
common.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
chdir(dirname(__FILE__));
if(!isset($config_file)) {
$config = parse_ini_file('config.properties');
}
else {
$config = parse_ini_file('config.properties');
}
if(!$config) {
echo "Could not read configuration file.\n";
die(3);
}
$db_name = $config['db_database'];
$db_host = $config['db_host'];
$db = new PDO("mysql:dbname=$db_name;host=$db_host", $config['db_username'], $config['db_password']);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$db->setAttribute(PDO::ATTR_TIMEOUT, $config['db_timeout']);
db_query("SET SESSION MAX_STATEMENT_TIME = ${config['db_timeout']}");
db_query('SET NAMES utf8');
unset($db_name);
unset($db_host);
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', '11211');
$memcached_prefix = 'ipwe';
if(isset($http_auth) && $http_auth && !is_cli()) {
if($config['api_authentication'] == 0) {
/* do nothing */
}
else if($config['api_authentication'] == 1) {
http_auth();
}
else {
echo "Wrong value for configuration setting 'api_authentication'.\n";
die(3);
}
}
function db_query_single($query, $parameters = array()) {
$data = db_query($query, $parameters);
if(count($data) == 0) {
return null;
}
if(count($data) > 1) {
// TODO
}
return $data[0];
}
function db_query($query, $parameters = array(), $cache_expiration = -1) {
global $memcached, $memcached_prefix;
if($cache_expiration > -1) {
$cache_key = $memcached_prefix . '_query_' . sha1($query . serialize($parameters));
if($data = $memcached->get($cache_key)) {
return unserialize($data);
}
}
$stmt = db_query_resultset($query, $parameters);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
db_stmt_close($stmt);
if($cache_expiration > -1) {
$memcached->set($cache_key, serialize($data), $cache_expiration);
}
return $data;
}
function db_stmt_close($stmt) {
if(!$stmt->closeCursor()) {
$error = $stmt->errorInfo();
db_error($error[2], debug_backtrace(), $query, $parameters);
}
}
function db_query_resultset($query, $parameters = array()) {
global $db;
$query_start = microtime(true);
if(!($stmt = $db->prepare($query))) {
$error = $db->errorInfo();
db_error($error[2], debug_backtrace(), $query, $parameters);
}
foreach($parameters as $key => $value) {
$stmt->bindValue($key+1, $value);
}
if(!$stmt->execute()) {
$error = $stmt->errorInfo();
db_error($error[2], debug_backtrace(), $query, $parameters);
}
return $stmt;
}
function db_error($error, $stacktrace, $query, $parameters) {
global $config;
$report_email = $config['error_mails_rcpt'];
$email_from = $config['error_mails_from'];
ob_start();
require(dirname(__FILE__) . '/mail_db_error.php');
$message = ob_get_contents();
ob_end_clean();
$headers = "From: $email_from\n";
$headers .= "Content-Type: text/plain; charset = \"UTF-8\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$subject = 'Database error';
mail($report_email, $subject, $message, $headers);
header('HTTP/1.1 500 Internal Server Error');
echo "A database error has just occurred. Please don't freak out, the administrator has already been notified.";
die();
}
function noauth() {
header('WWW-Authenticate: Basic realm="Sensors"');
header('HTTP/1.0 401 Unauthorized');
// TODO XML reply
die();
}
function http_auth() {
global $mysqli, $user_id;
/* HTTP basic authentication */
if(!isset($_SERVER['PHP_AUTH_USER'])) {
noauth();
}
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$query = 'SELECT id, hash FROM api_accounts WHERE username = ?';
$data = db_query($query, array($username), 3600);
if(count($data) == 1) {
$db_hash = $data[0]['hash'];
$hash = crypt($password, $db_hash);
if($hash == $db_hash) {
$user_id = $data[0]['id'];
return;
}
}
noauth();
}
function round_local($value, $decimals) {
global $config;
$ret = round($value, $decimals);
$ret = str_replace('.', $config['decimal_mark'], $ret);
return $ret;
}
function is_cli() {
return (php_sapi_name() == 'cli');
}
function get_rain_raw() {
global $memcached, $memcached_prefix;
$memcached_key = "${memcached_prefix}_daily_rain";
return $memcached->get($memcached_key);
}
function get_rain() {
$memcached_data = get_rain_raw();
if($memcached_data === null) {
return 'unbekannt';
}
$rain = $memcached_data;
// TODO hard-coded constants
// TODO number formatting
$rain = round($rain, 2);
if($rain <= '0.1') {
$rain = 0;
}
$rain .= ' mm';
return $rain;
}
function get_last_cron_run() {
$query = 'SELECT UNIX_TIMESTAMP(MAX(timestamp)) timestamp FROM sensor_cache';
$data = db_query($query);
if(count($data) == 0) {
return '';
}
return $data[0]['timestamp'];
}