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_stdout.php
141 lines (109 loc) · 3.44 KB
/
gmail_archiver_stdout.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
<?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);
}
}
function status($text, $add_newline=true)
{
fputs(STDERR, $text);
if ($add_newline) {
fputs(STDERR, "\n");
}
}
$argc = count($argv);
if ($argc > 1 && $argv[1] == 'help') {
status("Usage: php ${argv[0]} [IMAP server [port [account name [password]]]]");
exit(1);
}
$server = $port = $name = $password = '';
if ($argc < 2) {
status('Server: ', false);
$server = lbtrim(fgets(STDIN));
} else {
$server = lbtrim($argv[1]);
}
if ($argc < 3) {
status('Port: ', false);
$port = (int)fgets(STDIN);
} else {
$port = (int)$argv[2];
}
check_error(NULL, $port > 0 && $port < 65536, "Invalid port number: $port.", 2);
if ($argc < 4) {
status('Account name: ', false);
$name = lbtrim(fgets(STDIN));
} else {
$name = lbtrim($argv[3]);
}
if ($argc < 5) {
status('Password: ', false);
system('stty -echo');
$password = lbtrim(fgets(STDIN));
system('stty echo');
status("");
} else {
$password = lbtrim($argv[4]);
}
status("Connecting to $server:$port as $name... ", false);
$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);
status("OK");
status("Fetching list of folders... ", false);
$folders = imap_list($conn, $ref, "*");
check_error($conn, is_array($folders), 'Error retrieving list of IMAP folders.', 4);
status("OK");
imap_close($conn);
$bytes = 0;
$folder_count = count($folders);
$current_folder = 0;
foreach ($folders as $folder) {
++$current_folder;
$folder_name = substr($folder, strlen($ref));
status("Fetching mail from $folder_name...");
$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;
status(str_repeat("\x08", strlen($status)), false);
$status = "$percent% ($bytes bytes downloaded, folders: $current_folder/$folder_count) ";
status($status, false);
}
$msg = imap_fetchbody($conn, $uid, '', FT_UID | FT_PEEK);
echo "########## MESSAGE $folder_name / $uid ##########\n$msg\n";
echo "";
$bytes += strlen($msg);
}
status("\nAll mail from $folder_name has been downloaded.");
imap_close($conn);
}
?>