-
Notifications
You must be signed in to change notification settings - Fork 21
/
search.php
565 lines (447 loc) · 16.5 KB
/
search.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
// +----------------------------------------------------------------+
// | search.php |
// +----------------------------------------------------------------+
// | Function: Search a users Mailbox |
// +----------------------------------------------------------------+
// | AtMail Open - Licensed under the Apache 2.0 Open-source License|
// | http://opensource.org/licenses/apache2.0.php |
// +----------------------------------------------------------------+
require_once('header.php');
require_once('Session.php');
require_once('Global.php');
require_once('GetMail.php');
session_start();
// Keep a storage of the months in a hash. Used to reformat the search date
$month = array(
'01' => 'Jan',
'02' => 'Feb',
'03' => 'Mar',
'04' => 'Apr',
'05' => 'May',
'06' => 'Jun',
'07' => 'Jul',
'08' => 'Aug',
'09' => 'Sep',
'10' => 'Oct',
'11' => 'Nov',
'12' => 'Dec'
);
// Load which function we run
if(isset($_REQUEST['func']))
$func = $_REQUEST['func'];
else
$func = '';
$atmail = new AtmailGlobal();
$auth =& $atmail->getAuthObj();
$atmail->httpheaders();
$atmail->status = $auth->getuser($atmail->SessionID);
$atmail->username = $auth->username;
$atmail->pop3host = $auth->pop3host;
// check for language version
if (!$atmail->Language)
$atmail->Language = $pref['Language'];
// Load the account preferences
$atmail->loadprefs();
// If using the XUL interface, toggle to use the XP HTML templates
if ($atmail->LoginType == 'xul')
{
$atmail->LoginType = 'xp';
if (!$func)
$func = 'start' ;
}
if ($func != 'searchhelp')
{
// Print the error screen if the account has auth errors, or session timeout.
if ( $atmail->status == 1 )
$atmail->auth_error();
if ( $atmail->status == 2 )
$atmail->session_error();
}
if (!$atmail->Langage && !$atmail->LoginType)
{
$atmail->Language = $settings['Language'];
$atmail->LoginType = 'xp';
}
$var['newfolder'] = $_REQUEST['NewFolder'];
$var['atmailstyle'] = $atmail->parse("html/$atmail->Language/$atmail->LoginType/atmailstyle.css" );
$var['atmailstyle'] .= $atmail->parse("html/$atmail->Language/$atmail->LoginType/atmailstyle-form.css");
$var['mailstyle'] = $atmail->parse("html/$atmail->Language/$atmail->LoginType/atmailstyle-mail.css");
// Make a new mail object, used to search and list the users folders
$mail = new GetMail(array(
'Username' => $atmail->username,
'Pop3host' => $atmail->pop3host,
'Password' => $auth->password,
'Type' => $atmail->MailType,
'Mode' => $atmail->Mode)
);
// Load an array of msgs selected to be moved
$msgs = $_REQUEST['id'];
// If a value exists in the array, start to move the messages.
if ($msgs[0])
{
$mail->login();
// Loop through the selected msgs to move, the new folder to move to
// is seperated by :: . e.g 56::Trash , msg 56 from the Trash folder
foreach ($msgs as $id)
{
if (preg_match('/::(.*)/', $id, $m))
$folder = $m[1];
$id = preg_replace('/::.*/', '', $id);
// Don't move messages if we are already in the same folder
if ( $folder == $var['newfolder'] )
continue;
$mail->move( $id, $folder, $var['newfolder'] );
$var['move']++;
}
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/searchmove.html", $var );
$atmail->end();
}
// apend 'search_' to the function name passed via $_REQUEST so that
// user cannot call some arbitrary function such as 'phpinfo()'
$func = 'search_'.$_REQUEST['func'];
// check that the requested function exists then call it
if (function_exists($func))
$func();
else
die("the function <b>'$func'</b> is not defined");
$atmail->end();
//
// Start function definitions
//
function search_searchhelp()
{
global $pref, $atmail;
$h = $search = $ignore = array();
$ignore["filexp.html"]++;
$ignore["search_match.html"]++;
$ignore["search.html"]++;
$ignore["index.html"]++;
$ignore["tipsjscript.html"]++;
// Toggle the permissions on which help files a user can see
if (!$pref['allow_SMS']) $ignore["sms.html"]++;
if (!$pref['allow_LDAP'])
{
$ignore["ldap.html"]++;
$ignore["ldap_faq.html"]++;
}
if (!$pref['allow_MultiAccounts'])
{
$ignore["addpop3.html"]++;
$ignore["addpop3_faq.html"]++;
}
if (!$pref['allow_Calendar'])
{
$ignore["calendar.html"]++;
$ignore["cal_faq.html"]++;
}
if (!$pref['allow_Sync']) $ignore["sync.html"]++;
if (!$pref['GlobalAbook']) $ignore["calshared.html"]++;
if (!$pref['allow_AbookImportExport']) $ignore["import.html"]++;
if (!file_exists("{$pref['install_dir']}/xhtml.php")) $ignore["wap.html"]++;
$var['keywords'] = $_REQUEST['keywords'];
$dh = opendir("{$pref['install_dir']}/html/$atmail->Language/help/");
$words = explode(' ', $var['keywords']);
while(false !== $file = readdir($dh))
{
if (!preg_match('/\.html$/', $file) || $ignore[$file])
continue;
$fh = fopen("{$pref['install_dir']}/html/$atmail->Language/help/$file", 'r');
while (false !== $line = fgets($fh))
{
if (preg_match('/<title>(.*?)<\/title>/i', $line, $m))
$h[$file]['title'] = $m[1];
if (strpos($line, '<') !== false)
$skip = 1 ;
if (strpos($line, '>') !== false)
$skip = 0 ;
if ($skip)
continue;
if (preg_match('/<p>(.*)<\/p>/i', $line, $m))
$h[$file]['head'] = $m[1];
if (preg_match('/<ul>(.*)<\/ul>/i', $line, $m) && !$h[$file]['head'])
$h[$file]['head'] = $m[1];
foreach ($words as $word)
{
$word = preg_quote($word, '/');
if (preg_match("/$word/i", $line))
$search[$file]++ ;
}
$i++;
}
fclose($fh);
}
$matches = array_keys($search);
sort($matches);
$p = $search[$matches[0]];
foreach ($matches as $m)
{
$percent = intval($search[$m] / $p * 100);
if ($percent < 25)
continue;
$text = '';
$fh = fopen("{$pref['install_dir']}/html/$atmail->Language/help/$m", 'r');
while(false !== $line = fgets($fh))
{
$line = preg_replace('/<.*>/', '', $line);
$text = str_replace(' ', '', $text);
foreach ($words as $w)
{
if (strpos($line, $w) !== false)
$text .= $line;
}
}
$text = substr($text, 0, 200) . "...";
fclose($fh);
$size = filesize("{$pref['install_dir']}/html/$atmail->Language/help/$m");
$size = intval($size / 1024) . "K";
$var['matches'] .= $atmail->parse("html/$atmail->Language/help/search_match.html", array('Title' => $h[$m]['title'], 'Percent' => $percent, 'Text' => $text, 'File' => $m, 'Size' => $size));
}
if (!$var['matches'])
$var['matches'] = "Your search shows 0 results . Try again";
print $atmail->parse("html/$atmail->Language/help/search.html", array('matches' => $var['matches'], 'Keywords' => $var['keywords']));
$atmail->end();
}
function search_searchframe()
{
global $atmail, $var;
$var['DefaultFolder'] = $_REQUEST['DefaultFolder'];
$var['DefaultFrom'] = $_REQUEST['DefaultFrom'];
// Just extract the email address
$emailexp = '([^":\s<>()\/;]*@[^":\s<>()\/;]*)';
if (preg_match("/$emailexp/", $var['DefaultFrom'], $m))
$var['DefaultFrom'] = $m[1];
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/searchframe.html", $var);
}
function search_search()
{
global $var, $atmail, $mail;
$var['DefaultFolder'] = $_REQUEST['DefaultFolder'];
$var['DefaultFrom'] = $_REQUEST['DefaultFrom'];
// Fetch an array containing our mailboxes
$folders = $mail->listfolders();
// Create a select box . No searching of the Inbox if remote pop3, since
// the messages are stored on the remote server. Searching too slow
if ( $atmail->MailType == 'pop3' ) {
$var['folder_select'] = $mail->folder_select('', $folders, false);
// Take out the Inbox, cannot search in POP3 mode. Make the Sent folder the default folder to search
$var['folder_select'] = preg_replace('/<option value="Inbox">.*?<\/option>/', '', $var['folder_select']);
if($var['DefaultFolder'] == 'Inbox' || !$var['DefaultFolder'])
$var['DefaultFolder'] = 'Sent';
}
else
$var['folder_select'] = $mail->folder_select('', $folders, false);
$var['folder_select'] = $mail->folder_select_lang($var['folder_select'], $atmail->Language);
// Take away the erase option for the search menu
//$var['folder_select'] = preg_replace('/<option value="erase">.*?<\/option>/', '', $var['folder_select']);
//$var['folder_select'] = preg_replace('/<option value="" style=\'color: gray;\'>.*?<\/option>/', '', $var['folder_select']);
//$var['folder_select'] = preg_replace('/<option value=\'\'>.*?<\/option>/', '', $var['folder_select']);
// Select our default folder name ( e.g when a user right-clicks on a folder to search )
if ($var['DefaultFolder'])
$var['folder_select'] = str_replace("<option value=\"{$var['DefaultFolder']}\"", "<option value=\"{$var['DefaultFolder']}\" selected", $var['folder_select']);
else
$var['folder_select'] = str_replace('<option value="Inbox"', '<option value="Inbox" selected', $var['folder_select']);
// insert default values for Received After selects
$after_date = strtotime("-61 days",time());
$after_day = date("d", $after_date);
$after_month = date("m", $after_date);
$after_year = date("y", $after_date);
$result = $atmail->parse("html/$atmail->Language/$atmail->LoginType/search.html", $var);
$result = preg_replace("/(<select name=\"AfterDay\".*)<option value=\"$after_day\"/", "\$1<option value=\"$after_day\" selected", $result);
$result = preg_replace("/(<select name=\"AfterMonth\".*)<option value=\"$after_month\"/", "\$1<option value=\"$after_month\" selected", $result);
$result = preg_replace("/(<select name=\"AfterYear\".*)<option value=\"$after_year\"/", "\$1<option value=\"$after_year\" selected", $result);
print $result;
}
function search_start()
{
global $atmail, $var, $mail, $month;
if ( $_REQUEST['All'] )
$EmailBox = '';
else
$EmailBox = $_REQUEST['EmailBox'];
$EmailMessage = $_REQUEST['EmailMessage'];
$EmailSubject = $_REQUEST['EmailSubject'];
$EmailAttach = $_REQUEST['EmailAttach'];
$EmailFlag = $_REQUEST['EmailFlag'];
// Cleanup the EmailTo & EmailFrom fields . Remove spaces from the front / back
$EmailTo = trim($_REQUEST['EmailTo']);
$EmailFrom = trim($_REQUEST['EmailFrom']);
// Fetch the dates, if supplied
$BeforeDay = $_REQUEST['BeforeDay'] ? $_REQUEST['BeforeDay'] : '31';
$BeforeMonth = $_REQUEST['BeforeMonth'] ? $_REQUEST['BeforeMonth'] : '12';
$BeforeYear = $_REQUEST['BeforeYear'] ? $_REQUEST['BeforeYear'] : date('y');
$AfterDay = $_REQUEST['AfterDay'] ? $_REQUEST['AfterDay'] : '01';
$AfterMonth = $_REQUEST['AfterMonth'] ? $_REQUEST['AfterMonth'] : '01';
$AfterYear = $_REQUEST['AfterYear'] ? $_REQUEST['AfterYear'] : '00';
if ($atmail->MailType == 'imap')
{
// Set default dates required for the IMAP search
$BeforeMonth = $month[$BeforeMonth] ? $month[$BeforeMonth] : 'Jan';
$AfterMonth = $month[$AfterMonth] ? $month[$AfterMonth] : 'Jan';
// Don't specify a date if the user has selected the default values
$before = "$BeforeDay-$BeforeMonth-20$BeforeYear";
if ( $before == "31-Dec-2009")
$before = '';
// Don't specify a date if the user has selected the default values
$after = "$AfterDay-$AfterMonth-20$AfterYear";
if ($after == "01-Jan-2000")
{
$after = "";
}
// Query the IMAP server for the search results
$id = $mail->search(array(
'SUBJECT' => $EmailSubject,
'TO' => $EmailTo,
'FROM' => $EmailFrom,
'EmailBox' => $EmailBox,
'EmailAttach' => $EmailAttach,
'BODY' => $EmailMessage,
'FLAGGED' => $EmailFlag,
'BEFORE' => $before,
'SINCE' => $after)
);
$results = array();
foreach ($id as $i)
{
list($folder, $seq_no) = explode('::', $i);
$results[$folder] .= "$seq_no,";
}
foreach ($results as $k => $v)
{
$sizes[$k] = $mail->mailer->listmailsizes(trim($v, ','), $k);
}
$var['fulltotal'] = $mail->mailer->stat(0);
}
else
{
$id = $mail->search(array(
'EmailFlag' => $EmailFlag,
'EmailSubject' => $EmailSubject,
'EmailTo' => $EmailTo,
'EmailFrom' => $EmailFrom,
'EmailAttach' => $EmailAttach,
'EmailBox' => $EmailBox,
'EmailMessage' => $EmailMessage,
'DateBefore' => "$BeforeYear$BeforeMonth$BeforeDay",
'DateAfter' => "$AfterYear$AfterMonth$AfterDay")
);
// Optionally search a users POP3 Inbox ( can be slow )
if ($mail->Type == "pop3" && $EmailBox == "Inbox")
{
$id = array_merge($id, $mail->search_pop3(array(
'EmailSubject' => $EmailSubject,
'EmailTo' => $EmailTo,
'EmailFrom' => $EmailFrom,
'EmailAttach' => $EmailAttach,
'EmailBox' => $EmailBox,
'EmailMessage' => $EmailMessage,
'DateBefore' => $BeforeYear.$BeforeMonth.$BeforeDay,
'DateAfter' => $AfterYear.$AfterMonth.$AfterDay)
));
// Retrieve a list of mailbox sizes
$size = $mail->list_("Inbox");
$var['fulltotal'] = count($size) + 1;
}
}
$folders = $mail->listfolders();
$var['folderbox'] = $mail->folder_select($var['folder'], $folders);
//$var['nrrecords'] = count($id);
if ($_REQUEST['ajax'])
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/showmail_popup_ajax.html", $var);
// Add a jscript prompt on the status-bar
$var['status'] = $atmail->parse("html/$atmail->Language/msg/searchnr.html", $var);
if (is_array($id))
{
foreach ($id as $i)
{
preg_match('/(.*?)::(.*)/', "$i", $m);
$folder = $m[1];
$i = $m[2];
// Skip if searching an SQL box via POP3, and SQL query return messages in Inbox ( since they are on pop3 )
if ($atmail->MailType == 'pop3' && $EmailBox != 'Inbox' && $folder == 'Inbox')
continue;
$var['nrrecords']++;
$EmailBox = $folder;
// Load a hash containing the message headers
$db = $mail->gethead($i, $EmailBox, 9);
if ( $atmail->MailType == "pop3" || $atmail->MailType == "imap" )
$db['EmailCache'] = $db['EmailUIDL'];
$db['id'] = $i;
// Find the size of the message if searching a POP3 mailbox
if ($atmail->MailType == "pop3" && $size[$i])
{
$db['EmailSize'] = $size[$i];
}
elseif ($atmail->MailType == 'imap')
{
$db['EmailSize'] = $sizes[$folder][$i];
// Get the message flag for IMAP search
$db['UIDL'] = $mail->mailer->mailer->getFlags($i, true);
}
$db = $mail->clean_header($db, array('TimeZone' => $atmail->TimeZone));
$db['folder'] = $db['EmailBox'] = $EmailBox;
// If using the sent folder, we want to display the To field
if ( $EmailBox == "Sent" && !$var['jscript_sent'] )
{
$db['EmailFrom'] = $db['EmailTo'];
$var['jscript_sent'] .= <<<EOF
oForm = top.main.topFrame.document.sent
if(oForm == null) {
top.main.topFrame.location.href='html/$atmail->Language/xp/heading/mail_searchsent.html';
}
EOF;
}
elseif (!$var['jscript_sent'] )
{
$var['jscript_sent'] .= <<<EOF
oForm = top.main.topFrame.document.mail
// If not, change the frame to the mailhead
if(oForm == null) {
top.main.topFrame.location.href='html/$atmail->Language/$atmail->LoginType/heading/mail_search.html';
}
EOF;
}
// Choose which colour the next row is
$var['class'] = ( $var['class'] == "item" ) ? "item2" : "item";
$db['class'] = $var['class'];
if ($_REQUEST['ajax'])
{
// If we are using the Ajax interface, fix the headers.
$db = $mail->fixheaders_ajax($db);
$db['folder'] = $db['EmailBox'];
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/emailentry_popup_ajax.html", $db);
}
else
$var['emails'] .= $atmail->parse("html/$atmail->Language/$atmail->LoginType/searchentry.html", $db);
$args = array($db['EmailUIDL']);
$atmail->pluginHandler->triggerEvent('onAddEmailSearchResult', $args);
}
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
$var['browser'] = "ie";
else
$var['browser'] = "ns";
if ($_REQUEST['ajax'])
{
// Display a blank page if no messages exist
if ( !$var['nrrecords'] )
{
print $atmail->parse(
"html/$atmail->Language/$atmail->LoginType/emailentry_blank_ajax.html",
array('FolderName' => "Search"));
}
$var['search'] = 1;
if (!$var['start'])
$var['start'] = '0';
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/showmail_bottom_ajax.html", $var);
}
else
{
// Add a jscript prompt on the status-bar
$var['status'] = $atmail->parse( "html/$atmail->Language/msg/searchnr.html", $var);
print $atmail->parse("html/$atmail->Language/$atmail->LoginType/searchresults.html", $var);
}
// Close the IMAP / POP3 connection
$mail->quit();
}
?>