forked from box/Anemometer
/
Helpers.php
98 lines (89 loc) · 1.92 KB
/
Helpers.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
<?php
/**
*
* Global helper functions for the weatherstation app
* @author Gavin Towey <gavin@box.net>
* @license Apache 2.0 license. See LICENSE document for more info
* @created 2012-01-01
*/
// @todo validation?
/**
* search global request variables $_POST and $_GET in that order and return
* the first defined value for the given key
*
* @param string $name
* @return mixed the value of the variable name (if any) or null.
*/
function get_var($name)
{
$sources = array($_POST, $_GET);
foreach ($sources as $s)
{
if (isset($s[$name]))
{
return $s[$name];
}
}
return null;
}
/**
* return the full URL for the base page of the site.
*
* @return string
*/
function site_url()
{
if (isset($_SERVER['HTTPS']))
{
$proto = 'https://';
}
else
{
$proto = 'http://';
}
return $proto . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
}
/**
* wrap html pre tags around the given string, with class="prettyprint"
*
* @param string $string
*/
function prettyprint($string)
{
print '<pre class="prettyprint">';
print $string;
print "</pre>";
}
/**
* die with the error message if the given result handle or mysqli object has an error
*
* @param MySQLi_Result $result
* @param MySQLi $mysqli
*/
function check_mysql_error($result, $mysqli)
{
if (!$result)
{
// @TODO put markup in a view
die("<div class=\"alert alert-error\"><strong>".$mysqli->errno."</strong><br>".$mysqli->error ."</div>");
}
}
/**
* Flatten a multiple dim array into a single list
*
* @param array $array
* @return array
*/
function flatten_array($array)
{
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = flatten_array($value);
$return = array_merge($return, $value);
}
else
$return[] = $value;
}
return $return;
}