This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gmail_archiver.php
165 lines (135 loc) · 3.8 KB
/
gmail_archiver.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
<?php
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
ini_set('memory_limit', '512M');
set_time_limit(0);
function lbtrim($str)
{
return trim($str, "\r\n");
}
function check_error($conn, $condition, $message, $code)
{
if (!$condition)
{
fputs(STDERR, "\n$message\n" . imap_last_error() . "\n");
if ($conn)
imap_close($conn);
exit($code);
}
}
$argc = count($argv);
if ($argc > 1 && $argv[1] == 'help')
{
echo "Usage: php ${argv[0]} [IMAP server [port [account name [password]]]]\n";
exit(1);
}
$server = $port = $name = $password = '';
if ($argc < 2)
{
echo 'Server: ';
$server = lbtrim(fgets(STDIN));
}
else
$server = lbtrim($argv[1]);
if ($argc < 3)
{
echo 'Port: ';
$port = (int)fgets(STDIN);
}
else
$port = (int)$argv[2];
check_error(NULL, $port > 0 && $port < 65536, "Invalid port number: $port.", 2);
if ($argc < 4)
{
echo 'Account name: ';
$name = lbtrim(fgets(STDIN));
}
else
$name = lbtrim($argv[3]);
if ($argc < 5)
{
echo 'Password: ';
system('stty -echo');
$password = lbtrim(fgets(STDIN));
system('stty echo');
echo "\n";
}
else
$password = lbtrim($argv[4]);
echo "Connecting to $server:$port as $name... ";
$ref = '{' . "$server:$port/imap/ssl}";
$conn = imap_open ($ref, $name, $password);
check_error($conn, $conn !== false, "Unable to connect to $server:$port as $name.", 3);
echo "OK\n";
echo "Fetching list of folders... ";
$folders = imap_list($conn, $ref, "*");
check_error($conn, is_array($folders), 'Error retrieving list of IMAP folders.', 4);
echo "OK\n";
imap_close($conn);
if (!is_dir("./$name"))
{
echo "Creating directory: $name...\n";
$mkdir = mkdir("./$name", 0700);
check_error(NULL, $mkdir !== false, "Error creating directory: ./$name.", 5);
}
$bytes = 0;
$folder_count = count($folders);
$current_folder = 0;
foreach ($folders as $folder)
{
++$current_folder;
$folder_name = substr($folder, strlen($ref));
if (!is_dir("./$name/$folder_name"))
{
echo "Creating directory: ./$name/$folder_name... ";
$mkdir = mkdir("./$name/$folder_name", 0700, true);
check_error(NULL, $mkdir !== false, "Error creating directory: ./$name/$folder_name.", 6);
echo "OK\n";
}
echo "Fetching mail from $folder_name... \n";
$conn = imap_open($folder, $name, $password);
check_error($conn, $conn !== false, "Unable to connect to $server:$port as $name.", 7);
$messages = imap_search($conn, 'ALL', SE_UID);
if (!is_array($messages))
{
fputs(STDERR, "\nError retrieving list of messages in folder: $folder_name. SKIPPED\n" . imap_last_error() . "\n");
continue;
}
$all = count($messages);
$current = 0;
$status = '';
$last_percent = -1;
foreach ($messages as $uid)
{
++$current;
$percent = ((int)round(($current / $all) * 10000)) / 100;
if ($percent != $last_percent)
{
$last_percent = $percent;
echo str_repeat("\x08", strlen($status));
$status = "$percent% ($bytes bytes downloaded, folders: $current_folder/$folder_count) ";
echo "$status";
}
$msgfile = "./$name/$folder_name/$uid.txt.gz";
if (!file_exists($msgfile))
{
$msg = imap_fetchbody($conn, $uid, '', FT_UID | FT_PEEK);
$to_be_written = gzencode($msg, 9);
if ($to_be_written === false)
{
fputs(STDERR, "\nError compressing message, writing plaintext: $uid\n");
$to_be_written = $msg;
$msgfile = "./$name/$folder_name/$uid.txt";
}
$written = file_put_contents($msgfile, $to_be_written);
check_error($conn, $written !== false, "Error writing file: $msgfile.", 9);
$bytes += strlen($msg);
}
}
echo "\nAll mail from $folder_name has been downloaded.\n";
imap_close($conn);
}
?>