forked from petewarden/handmadeimap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpeteutils.php
165 lines (135 loc) · 4.34 KB
/
peteutils.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
<?php
/**
* This is a fairly arbitrary collection of helper functions pulled from my production
* code that were needed by other files in this project.
Licensed under the 2-clause (ie no advertising requirement) BSD license,
making it easy to reuse for commercial or GPL projects:
(c) Pete Warden <[email protected]> http://petewarden.typepad.com/ - Mar 11th 2010
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
function get_url_input($inputname, $default=null)
{
if (isset($_POST[$inputname]))
return $_POST[$inputname];
if (isset($_GET[$inputname]))
return $_GET[$inputname];
if (isset($_COOKIE[$inputname]))
return $_COOKIE[$inputname];
return $default;
}
$g_start_time = 0;
$g_end_time = 0;
function pete_start_timer()
{
global $g_start_time;
list($usec, $sec) = explode(' ',microtime());
$g_start_time = ((float)$usec + (float)$sec);
}
function pete_end_timer($dolog=true)
{
global $g_start_time;
global $g_end_time;
list($usec, $sec) = explode(' ',microtime());
$g_end_time = ((float)$usec + (float)$sec);
$duration = ($g_end_time - $g_start_time);
if ($dolog)
{
$durationstring = 'pete_timer: %01.4f sec';
error_log(sprintf($durationstring, $duration));
}
return $duration;
}
$log_enabled_modules = array(
'handmadeimap' => true,
'accounts' => false,
'oauth' => false,
);
$log_print = false;
function pete_log_enable($module)
{
global $log_enabled_modules;
$log_enabled_modules[$module] = true;
}
function pete_log_disable($module)
{
global $log_enabled_modules;
unset($log_enabled_modules[$module]);
}
function pete_log($module, $message)
{
global $log_enabled_modules;
if (array_key_exists($module, $log_enabled_modules) &&
$log_enabled_modules[$module])
{
error_log($module.': '.$message);
}
global $log_print;
if ($log_print)
print htmlspecialchars("$module: $message")."<br/>";
}
function pete_remove_illegal_characters($input)
{
return iconv("ISO-8859-1", "ISO-8859-1//IGNORE", $input);
}
function make_tag($tagname, $text)
{
return "<".$tagname.">".$text."</".$tagname.">";
}
function make_cdata($text)
{
return htmlspecialchars(pete_remove_illegal_characters($text));
}
function make_cdatatag($tagname, $text)
{
return make_tag($tagname, make_cdata($text));
}
// Returns the current page's full URL. From http://blog.taragana.com/index.php/archive/how-to-find-the-full-url-of-the-page-in-php-in-a-platform-independent-and-configuration-independent-way/
function get_current_url()
{
$result = 'http';
$script_name = "";
if(isset($_SERVER['REQUEST_URI']))
{
$script_name = $_SERVER['REQUEST_URI'];
}
else
{
$script_name = $_SERVER['PHP_SELF'];
if($_SERVER['QUERY_STRING']>' ')
{
$script_name .= '?'.$_SERVER['QUERY_STRING'];
}
}
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on')
{
$result .= 's';
}
$result .= '://';
if($_SERVER['SERVER_PORT']!='80')
{
$result .= $_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$script_name;
}
else
{
$result .= $_SERVER['HTTP_HOST'].$script_name;
}
return $result;
}
?>