-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions.php
267 lines (239 loc) · 7.43 KB
/
functions.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
<?
function check_email_address($email)
{
if (strlen ($email) < 5) return false;
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
{
return false;
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++)
{
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
{
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))
{
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2)
{
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++)
{
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
{
return false;
}
}
}
return true;
}
function getRandomEMails ($anz)
{
global $maildomain;
$mail = array ();
$names = getRandomNames ($anz);
foreach ($names as $name)
{
$entry['name'] = strtolower ($name['firstname']) . "." .
strtolower ($name['lastname']);
$entry['adress'] = $entry['name'] . "@" . $maildomain;
$mail[] = $entry;
}
return $mail;
}
function getRandomNames ($anz)
{
$ret = array ();
$names = getNames ();
$firstnames = array_merge ($names['malenames'], $names['femalenames']);
shuffle ($firstnames);
shuffle ($names['surnames']);
for ($i = 0; $i < $anz; $i++)
{
$entry['firstname'] = trim ($firstnames[$i]);
$entry['lastname'] = trim ($names['surnames'][$i]);
$ret[] = $entry;
}
return $ret;
}
function getNames ()
{
$ret = array ();
global $malenames,$femalenames,$familynames;
$ret['malenames'] = file($malenames);
$ret['femalenames'] = file($femalenames);
$ret['surnames'] = file($familynames);
return $ret;
}
function getmsg($mbox,$mid) {
// input $mbox = IMAP stream, $mid = message id
// output all the following:
global $htmlmsg,$plainmsg,$charset,$attachments;
// the message may in $htmlmsg, $plainmsg, or both
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
// HEADER
$h = imap_header($mbox,$mid);
// add code here to get date, from, to, cc, subject...
// BODY
$s = imap_fetchstructure($mbox,$mid);
if (!isset ($s->parts) || !$s->parts) // not multipart
getpart($mbox,$mid,$s,0); // no part-number, so pass 0
else { // multipart: iterate through each part
foreach ($s->parts as $partno0=>$p)
getpart($mbox,$mid,$p,$partno0+1);
}
}
function getpart($mbox,$mid,$p,$partno) {
// $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart
global $htmlmsg,$plainmsg,$charset,$attachments;
// DECODE DATA
$data = ($partno)?
imap_fetchbody($mbox,$mid,$partno): // multipart
imap_body($mbox,$mid); // not multipart
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
// no need to decode 7-bit, 8-bit, or binary
// PARAMETERS
// get all parameters, like charset, filenames of attachments, etc.
$params = array();
if (isset ($p->parameters))
foreach ($p->parameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;
if (isset ($p->dparameters))
foreach ($p->dparameters as $x)
$params[ strtolower( $x->attribute ) ] = $x->value;
// ATTACHMENT
// Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
if (isset ($params['filename']) || isset ($params['name'])) {
// filename may be given as 'Filename' or 'Name' or both
$filename = (isset ($params['filename']))? $params['filename'] : $params['name'];
// filename may be encoded, so see imap_mime_header_decode()
$attachments[$filename] = $data; // this is a problem if two files have same name
}
// TEXT
elseif ($p->type==0 && $data) {
// Messages may be split in different parts because of inline attachments,
// so append parts together with blank row.
if (strtolower($p->subtype)=='plain')
$plainmsg .= trim($data) ."\n\n";
else
$htmlmsg .= $data ."<br><br>";
$charset = $params['charset']; // assume all parts are same charset
}
// EMBEDDED MESSAGE
// Many bounce notifications embed the original message as type 2,
// but AOL uses type 1 (multipart), which is not handled here.
// There are no PHP functions to parse embedded messages,
// so this just appends the raw source to the main message.
elseif ($p->type==2 && $data) {
$plainmsg .= trim($data) ."\n\n";
}
// SUBPART RECURSION
if (isset ($p->parts)) {
foreach ($p->parts as $partno0=>$p2)
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.
}
}
function object2array($object)
{
$return = NULL;
if(is_array($object))
{
foreach($object as $key => $value)
$return[$key] = object2array($value);
} else {
$var = get_object_vars($object);
if($var)
{
foreach($var as $key => $value)
$return[$key] = object2array($value);
} else {
return strval($object); // strval and everything is fine
}
}
return $return;
}
function short_url($longurl, $retry = 0)
{
global $maxretry;
if ($retry > $maxretry) return "";
$url = "http://p0i.de/api.php?action=shorturl&url=" . urlencode ($longurl);
$poi = file_get_contents($url);
// logToFile ("short_url " . $url . " " . $retry . " " . strlen ($poi));
if (strlen ($poi) > 20)
{
$poi = object2array (simplexml_load_string ($poi));
$shorturl = trim ($poi["shorturl"]);
// logToFile ("short_url " . $shorturl);
} else {
// Retry on error
sleep (3);
return short_url($longurl, $retry + 1);
}
return $shorturl;
}
function get_qr_code ($text, $size)
{
//QR Code Configuration
$encoding = "UTF-8";
$ecl = "H";
$longurl = "http://" . $_SERVER['SERVER_NAME'] . "/?search=" . $text;
$shorturl = short_url($longurl);
$url = "https://chart.googleapis.com/chart?cht=qr&chl=" .
$shorturl . "&chs=" . $size . "&choe=" . $encoding . "&chld=" . $ecl;
$qr_code = file_get_contents($url);
return $qr_code;
}
function logToFile( $msg)
{
global $logfile;
$fd = fopen($logfile, "a");
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
fwrite($fd, $str . "\n");
fclose($fd);
}
function myErrorHandler($fehlercode, $fehlertext, $fehlerdatei, $fehlerzeile)
{
switch ($fehlercode) {
case E_NOTICE:
case E_USER_NOTICE:
$errors = "Notice";
break;
case E_WARNING:
case E_USER_WARNING:
$errors = "Warning";
break;
case E_ERROR:
case E_USER_ERROR:
$errors = "Fatal Error";
break;
default:
$errors = "Unknown";
break;
}
logToFile( $errors . " [$fehlercode] $fehlertext Line: $fehlerzeile File: $fehlerdatei");
return true;
}
function checkMail($name)
{
global $maildomain;
$mail = $name . "@" . $maildomain;
if(!preg_match("/^[A-Z0-9._%+-ÄÖÜäöü]+@[A-Z0-9.-ÄÖÜäöü]+\.[A-Z]{2,6}$/i", $mail))
return false;
$string = preg_replace('/[^a-zA-Z0-9]*/', '', $name);
if (strlen ($string) < 1)
return false;
return true;
}
// set_error_handler("myErrorHandler");
?>