-
Notifications
You must be signed in to change notification settings - Fork 5
/
common_functions_v2.php
executable file
·363 lines (323 loc) · 14.2 KB
/
common_functions_v2.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
// TODO: Remove this hard-coded timezone
date_default_timezone_set("Europe/Copenhagen");
/** Returns a handle to the standard database
@return object
*/
function std_db($user_type=NULL){
// These two xml files will be merged in an upcomming PR
$xml=simplexml_load_file("../site_settings.xml");
$global_settings=simplexml_load_file("../global_settings.xml");
if ($user_type == 'alarm_user'){
$db_user = $global_settings->mail_alarm_settings->alarm_user;
$db_password = $global_settings->mail_alarm_settings->alarm_pw;
} else {
$db_user = $xml->db_user;
$db_password = $xml->db_password;
}
$conn_str = 'mysql:host=' . $xml->db_host . ';dbname=' . $xml->db_database . ';charset=utf8';
$pdo = new PDO($conn_str, $db_user, $db_password);
return $pdo;
}
function single_sql_value($db, $query, $column){
$stmt = $db->prepare($query);
$stmt->execute();
$row = $stmt->fetch();
$value = $row[$column];
return($value);
}
function latest_sql_row($db, $query){
// Returns the lastest row from the result of the query
if (strpos($query, " desc") == false){
$query .= " desc";
}
if (strpos($query, " limit ") == false){
$query .= " limit 1";
}
$stmt = $db->prepare($query);
$stmt->execute();
$row = $stmt->fetch();
return($row);
}
function get_xy_values($query,$db,$offset=0){
$result = mysql_query($query,$db);
while ($row = mysql_fetch_array($result)){
$data["y"][] = $row[1] + $offset;
$data["x"][] = $row[0];
}
return($data);
}
function xscale($max,$min,$manual,$log){
if ($log){
$max = log10($max);
$min = log10($min);
}
$xscale["max"] = ($max == "") ? 2 : $max; //Default values if no values is given: 2 and 1
$xscale["min"] = ($min == "") ? 1 : $min;
$xscale["manual"] = $manual==="checked"; // If a manual scale is chosen the variable
// $manual will have the value "checked"
return($xscale);
}
/** Returns the default values for a date-xscale or an array of the manually selected scale
If non-default values are provided, these will simply be returned
The return value is formatted as a string suitable for injection into an sql-query
@param string $from The from-value to be returned. If an emptry string is given, the default value will be returned
@param string $to The to-value to be returned. If an emptry string is given, the default value will be returned
@param string $defaulthours The default amount of hours to go back in time if defaults are to be returned
@return array
*/
function date_xscale($from="",$to="",$defaulthours=24){
$xscale["to"] = date('Y-m-d H:i',time() + 60 * 60 * 24 * 30); // Default, 1 month into the feature
$xscale["from"] = date('Y-m-d H:i',time() - 60 * 60 * $defaulthours);
$xscale["from"] = empty($from) ? $xscale["from"] : $from; // If we get an argument, skip the defaults
$xscale["to"] = empty($to) ? $xscale["to"] : $to;
return($xscale);
}
/** Formats a given number as a scientific number (exponentials of 10 notation) in HTML
@param real $number The number to be html-formatted
@return string
*/
function science_number($number){
if($number == 0){
$result = 0;
}
else {
$exponent = floor(log10($number));
$digits = $number * pow(10,-1*$exponent);
$digits = round($digits,2);
$result = $digits . " × " . "10<sup>" . $exponent . "</sup>";
}
return($result);
}
/** Makes sure that a string only contains a-zA-Z0-9 \{} characters and replace
the string with a warning if that is not the case
$str string
@return string
*/
function weed($str){
$expressions = Array('/\"/', '/&/', '/=/', '/Æ/', '/Ø/','/Å/','/æ/','/ø/',
'/å/');
foreach($expressions as $expr){
$str = preg_replace($expr, "?", $str);
}
return $str;
}
/** Returns strings with standard html-header and -footer
* @return string
*/
function html_header($root="../", $title="Data viewer", $includehead="", $charset="UTF-8", $width=null, $html5=false){
if ($width != null){
$width = " style=\"width:{$width}px\" ";
} else {
$width = " style=\"max-width:95%\" ";
}
if(is_it_christmas()){
$header = html_header_x($root, $title, $includehead, $charset, $width, $html5);
} else {
$header = html_header_normal($root, $title, $includehead, $charset, $width, $html5);
}
return $header;
}
function html_footer($root="../", $valid_html5=false){
if(is_it_christmas()){
$footer = html_footer_x($root, $valid_html5);
} else {
$footer = html_footer_normal($root, $valid_html5);
}
return $footer;
}
function html_header_normal($root, $title, $includehead, $charset, $width, $html5){
$header = "";
if ($html5){
$header = $header . "<!DOCTYPE html>\n";
} else {
$header = $header . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
}
$header = $header . "<html>\n";
$header = $header . " <head>\n";
$header = $header . " <meta http-equiv=\"Content-Type\" content=\"text/html; charset={$charset}\">\n";
$header = $header . " <title>SurfCat data logging</title>\n";
$header = $header . " <link rel=\"StyleSheet\" href=\"{$root}css/style.css\" type=\"text/css\" media=\"screen\">\n";
if ($root == "../"){
# $header = $header . " <script type=\"text/javascript\" src=\"dygraph/dygraph-dev.js\"></script>\n";
$header = $header . " <script type=\"text/javascript\" src=\"https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.js\"></script>\n";
$header = $header . " <link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.css\">\n";
}
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/update.js\"></script> \n";
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/toogle.js\"></script>\n";
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/output.js\"></script>\n";
$header = $header . $includehead . "\n";
$header = $header . " </head>\n";
$header = $header . " <body>\n";
$header = $header . " <div class=\"container\" $width>\n";
$header = $header . " <div class=\"caption\">\n";
$header = $header . " {$title}\n";
$header = $header . " <a href=\"/\"><img class=\"logo\" src=\"{$root}images/cinf_logo_beta_greek.png\" alt=\"CINF data viewer\"></a>\n";
$header = $header . " <div class=\"header_utilities\">\n";
$header = $header . " <a class=\"header_links\" href=\"https://cinfwiki.fysik.dtu.dk/cinfwiki/Software/DataWebPageUserDocumentation\">Help</a><br>\n";
$header = $header . " <a class=\"header_links\" href=\"test_configuration_file.php\">Config</a>\n";
$header = $header . " </div>\n";
$header = $header . " </div>\n";
$header = $header . " <div class=\"plotcontainer\">\n";
return($header);
}
function header_v2(){
$header = "";
$header = $header . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
$header = $header . "<html>\n";
$header = $header . " <head>\n";
$header = $header . " <title>SurfCat data logging</title>\n";
$header = $header . " <link rel=\"StyleSheet\" href=\"../css/style.css\" type=\"text/css\" media=\"screen\">\n";
$header = $header . " </head>\n";
$header = $header . " <body>\n";
$header = $header . " <div class=\"container\">\n";
$header = $header . " <div class=\"caption\">Data viewer\n";
$header = $header . " <a href=\"/\"><img class=\"logo\" src=\"../images/cinf_logo_beta_greek.png\"></a>\n";
$header = $header . " <div class=\"header_utilities\">\n";
$header = $header . " <a class=\"header_links\" href=\"https://cinfwiki.fysik.dtu.dk/cinfwiki/Software/DataWebPageUserDocumentation\">Help</a><br>\n";
$header = $header . " <a class=\"header_links\" href=\"test_configuration_file.php\">Config</a>\n";
$header = $header . " </div>\n";
$header = $header . " </div>\n";
return($header);
}
function html_footer_normal($root, $valid_html5){
$footer = "";
$footer = $footer . " </div>\n";
if ($valid_html5){
$footer = $footer . " <div class=\"copyright\" style=\"clear:both\">\n";
$footer = $footer . " <div style=\"float:left;width:100px\"> </div>\n";
$footer = $footer . " <div style=\"float:right;width:100px\"><img src=\"{$root}images/badge-w3c-valid-html5_h30.png\" height=\"20\" style=\"padding:5px\" alt=\"Valid HTML5\" title=\"Valid HTML5\"></div>\n";
$footer = $footer . " <div style=\"margin:0 auto;width:100px\">...</div>\n";
$footer = $footer . " </div>\n";
} else {
$footer = $footer . " <div class=\"copyright\" style=\"clear:both\">...</div>\n";
}
$footer = $footer . " </div>\n";
$footer = $footer . " </body>\n";
$footer = $footer . "</html>\n";
#
return($footer);
}
function html_footer_v2(){
$footer = "";
$footer = $footer . " <div class=\"copyright\">...</div>\n";
$footer = $footer . " </div>\n";
$footer = $footer . " </body>\n";
$footer = $footer . "</html>\n";
return($footer);
}
function html_code_header($file){
$header = "";
$header = $header . "<head><title>SurfCat data logging</title>\n";
$header = $header . "<link rel=\"StyleSheet\" href=\"../css/screen.css\" type=\"text/css\" media=\"screen\">\n";
$header = $header . "</head>\n";
$header = $header . "<body>\n";
$header = $header . "<div class=\"container\">\n";
$header = $header . "<div class=\"caption\">Code viewer: ".$file."\n";
$header = $header . "<a href=\"/\"><img class=\"logo\" src=\"../images/cinf_logo_beta_greek.png\"></a>\n";
$header = $header . "</div>\n";
return($header);
}
function html_header_x($root, $title, $includehead, $charset, $width, $html5){
$files = Array();
if ($handle = opendir("{$root}images/xmas-shift")) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$files[] = $entry;
}
}
closedir($handle);
}
$rand_img = $files[array_rand($files)];
$header = "";
$header = $header . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
$header = $header . "<html>\n";
$header = $header . " <head>\n";
$header = $header . " <meta http-equiv=\"Content-Type\" content=\"text/html; charset={$charset}\">\n";
$header = $header . " <title>SurfCat data logging</title>\n";
$header = $header . " <link rel=\"StyleSheet\" href=\"{$root}css/style.css\" type=\"text/css\" media=\"screen\">\n";
if ($root == "../"){
#$header = $header . " <script type=\"text/javascript\" src=\"dygraph/dygraph-dev.js\"></script>\n";
#$header = $header . " <script type=\"text/javascript\" src=\"dygraph-combined.js\"></script>\n";
$header = $header . " <script type=\"text/javascript\" src=\"https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.js\"></script>\n";
$header = $header . " <link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.css\">\n";
}
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/update.js\"></script> \n";
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/toogle.js\"></script>\n";
$header = $header . " <script type=\"text/javascript\" src=\"{$root}js/output.js\"></script>\n";
$header = $header . $includehead . "\n";
$header = $header . " </head>\n";
$header = $header . " <body>\n";
$header = $header . " <div class=\"container\" $width>\n";
$header = $header . " <div class=\"caption_alt\">\n";
# $header = $header . " <img src=\"../images/fir_branch_with_cones.png\" height=\"42\" width=\"42\">\n";
$header = $header . " {$title}\n";
$header = $header . " <a href=\"/\"><img class=\"logo_alt\" src=\"{$root}images/cinf_logo_beta_greek.png\" alt=\"CINF data viewer\"></a>\n";
$header = $header . " <div class=\"header_utilities\">\n";
$header = $header . " <a class=\"header_links\" href=\"https://cinfwiki.fysik.dtu.dk/cinfwiki/Software/DataWebPageUserDocumentation\">Help</a><br>\n";
$header = $header . " <a class=\"header_links\" href=\"test_configuration_file.php\">Config</a>\n";
$header = $header . " </div>\n";
$header = $header . " <div class=\"header_decorations_shift\">\n";
$header = $header . " <img class=\"header_img_shift\" src=\"{$root}images/xmas-shift/{$rand_img}\" title=\"Merry Christmas\" alt=\"CINF data viewer\">\n";
$header = $header . " </div>\n";
$header = $header . " <div class=\"header_decorations_static\">\n";
$header = $header . " <img class=\"edition\" src=\"{$root}images/xmas_edition.png\" alt=\"Merry Christmas\">\n";
$header = $header . " </div>\n";
$header = $header . " </div>\n";
$header = $header . " <div class=\"plotcontainer\">\n";
return($header);
}
function html_footer_x($root, $valid_html5){
$footer = "";
$footer = $footer . " </div>\n";
$footer = $footer . " <div class=\"copyright_decorations\" style=\"clear:both\"></div>\n";
$footer = $footer . " </div>\n";
$footer = $footer . " </body>\n";
$footer = $footer . "</html>\n";
return($footer);
}
function indent($level, $str){
foreach (explode("\n", $str) as $line){
echo(str_repeat(" ", $level) . $line) . "\n";
}
}
function right_float_menu($items, $indent){
indent($indent, "" .
"<!-- RIGHT FLOAT MENU -->\n" .
"<div class=\"right_float_menu\">\n" .
" <table cellspacing=0>");
foreach($items as $key => $value){
indent($indent + 4, "" .
"<tr>\n" .
" <td><a href=\"$value\">$key</a></td>\n" .
"</tr>"
);
}
indent($indent, "" .
" </table>\n" .
"</div>\n");
}
function is_it_christmas()
{
/* It is x-mas from 14/11 to 31/12 */
$month = (int) date("n");
switch ($month){
case 12:
return true;
case 11:
$day = (int) date("d");
if ($day >=14){
return true;
} else {
return false;
}
default:
return false;
}
}
function pprint($var){
echo("<pre>");
print_r($var);
echo("</pre>");
}
?>