File: fetch.php

package info (click to toggle)
squirrelmail 2%3A1.4.21-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,168 kB
  • ctags: 7,114
  • sloc: php: 35,360; perl: 3,414; sh: 162; ansic: 122; makefile: 63
file content (301 lines) | stat: -rw-r--r-- 11,078 bytes parent folder | download
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
<?php

/**
 * mail_fetch/fetch.php
 *
 * Fetch code.
 *
 * @copyright 1999-2010 The SquirrelMail Project Team
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: fetch.php 13893 2010-01-25 02:47:41Z pdontthink $
 * @package plugins
 * @subpackage mail_fetch
 */

define('SM_PATH','../../');

require_once(SM_PATH . 'include/validate.php');
include_once(SM_PATH . 'functions/imap.php');
include_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );

// don't load this page if this plugin is not enabled
//
global $plugins;
if (!in_array('mail_fetch', $plugins)) exit;

/* globals */
sqgetGlobalVar('username',   $username,   SQ_SESSION);
sqgetGlobalVar('key',        $key,        SQ_COOKIE);
sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
/* end globals */

function Mail_Fetch_Status($msg) {
    echo html_tag( 'table',
             html_tag( 'tr',
                 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
                     ),
                   '', '', 'width="90%"' );
    flush();
}

displayPageHeader($color, 'None');

$mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
$mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
if ($mailfetch_server_number<1) $mailfetch_server_number=0;
for ($i=0;$i<$mailfetch_server_number;$i++) {
    $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
    $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
    $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
    $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
    $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
    $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
    $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
    $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
    $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
    if( $mailfetch_cypher == 'on' ) {
        $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
    }
}

echo '<br><center>';

echo html_tag( 'table',
         html_tag( 'tr',
             html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
                 ) ,
         'center', '', 'width="95%" cols="1"' );

// get $server_to_fetch from globals, if not set display a choice to the user
if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {

    echo '<font size=-5><br></font>' .
        "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" .
        html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
        html_tag( 'tr' ) .
        html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
        html_tag( 'td', '', 'left' ) .
        '<select name="server_to_fetch" size="1">' .
        '<option value="all" selected>..' . _("All") . "...\n";
    for ($i=0;$i<$mailfetch_server_number;$i++) {
        echo "<option value=\"$i\">" .
            htmlspecialchars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
            '</option>' . "\n";
    }
    echo            '</select>' .
        '</td>' .
        '</tr>';

    //if password not set, ask for it
    for ($i=0;$i<$mailfetch_server_number;$i++) {
        if ($mailfetch_pass_[$i]=='') {
            echo html_tag( 'tr',
                     html_tag( 'td', _("Password for") . ' <b>' .
                         htmlspecialchars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
                         '</b>: &nbsp; &nbsp; ',
                         'right' ) .
                     html_tag( 'td', '<input type="password" name="pass_' . $i . '">', 'left' )
                           );
        }
    }
    echo html_tag( 'tr',
             html_tag( 'td', '&nbsp;' ) .
             html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' )
             ) .
        '</table></form>';
    exit();
}

if ( $server_to_fetch == 'all' ) {
    $i_start = 0;
    $i_stop = $mailfetch_server_number;
} else {
    $i_start = $server_to_fetch;
    $i_stop = $i_start+1;
}

for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
    $mailfetch_server=$mailfetch_server_[$i_loop];
    $mailfetch_port=$mailfetch_port_[$i_loop];
    $mailfetch_user=$mailfetch_user_[$i_loop];
    if ($mailfetch_pass_[$i_loop] == '') {
        sqgetGlobalVar("pass_$i_loop", $mailfetch_pass, SQ_POST);
    } else {
        $mailfetch_pass = $mailfetch_pass_[$i_loop];
    }
    $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
    $mailfetch_login=$mailfetch_login_[$i_loop];
    $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
    $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];

    $pop3 = new POP3($mailfetch_server, 60);

    echo '<br>' .
        html_tag( 'table',
            html_tag( 'tr',
                html_tag( 'td', '<b>' . _("Fetching from ") .
                    htmlspecialchars((($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop])) .
                    '</b>',
                'center' ) ,
            '', $color[9] ) ,
        '', '', 'width="90%"' );

    flush();

    if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
        Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
        continue;
    }

    Mail_Fetch_Status(_("Opening IMAP server"));
    $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);

    // check if destination folder is not set, is not subscribed and is not \noselect folder
    if($mailfetch_subfolder == '' || 
       ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
        $mailfetch_subfolder = 'INBOX';
    }

    Mail_Fetch_Status(_("Opening POP server"));
    $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
    if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
        Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
        continue;
    }

    //   register_shutdown_function($pop3->quit());

    $msglist = $pop3->uidl();

    $i = 1;
    for ($j = 1; $j < sizeof($msglist); $j++) {
        if ($msglist["$j"] == $mailfetch_uidl) {
            $i = $j+1;
            break;
        }
    }

    if ($Count < $i) {
        Mail_Fetch_Status(_("Login OK: No new messages"));
        $pop3->quit();
        continue;
    }
    if ($Count == 0) {
        Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
        $pop3->quit();
        continue;
    } else {
        $newmsgcount = $Count - $i + 1;
        Mail_Fetch_Status(sprintf(_("Login OK: Inbox contains %s messages"), $newmsgcount));
    }

    Mail_Fetch_Status(_("Fetching UIDL..."));
    // Faster to get them all at once
    $mailfetch_uidl = $pop3->uidl();

    if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
        Mail_Fetch_Status(_("Server does not support UIDL."));

    if ($mailfetch_lmos == 'on') {
        Mail_Fetch_Status(_("Leaving messages on server..."));
    } else {
        Mail_Fetch_Status(_("Deleting messages from server..."));
    }

    for (; $i <= $Count; $i++) {
        Mail_Fetch_Status(_("Fetching message ") . "$i" );
        if (!ini_get('safe_mode'))
            set_time_limit(20); // 20 seconds per message max
        $Message = "";
        $MessArray = $pop3->get($i);

        while ( (!$MessArray) or (gettype($MessArray) != "array")) {
            Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
            // re-connect pop3
            Mail_Fetch_Status(_("Server error. Disconnect"));
            $pop3->quit();
            Mail_Fetch_Status(_("Reconnect from dead connection"));
            if (!$pop3->connect($mailfetch_server)) {
                Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
                Mail_Fetch_Status(_("Saving UIDL"));
                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);

                continue;
            }
            $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
            if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
                Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) );
                Mail_Fetch_Status(_("Saving UIDL"));
                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);

                continue;
            }
            Mail_Fetch_Status(_("Refetching message ") . "$i" );
            $MessArray = $pop3->get($i);

        } // end while

        while (list($lineNum, $line) = each ($MessArray)) {
            $Message .= $line;
        }

        fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
        $Line = fgets($imap_stream, 1024);
        if (substr($Line, 0, 1) == '+') {
            fputs($imap_stream, $Message);
            fputs($imap_stream, "\r\n");
            sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
            if ( $response <> 'OK' ) {
                Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
                Mail_Fetch_Status(_("Closing POP"));
                $pop3->quit();
                Mail_Fetch_Status(_("Logging out from IMAP"));
                sqimap_logout($imap_stream);

                Mail_Fetch_Status(_("Saving UIDL"));
                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
                exit;
            } else {
                Mail_Fetch_Status(_("Message appended to mailbox"));
            }

            if ($mailfetch_lmos != 'on') {
                if( $pop3->delete($i) ) {
                    Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
                } else {
                    Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) );
                }
            }
        } else {
            echo "$Line";
            Mail_Fetch_Status(_("Error Appending Message!"));
            Mail_Fetch_Status(_("Closing POP"));
            $pop3->quit();
            Mail_Fetch_Status(_("Logging out from IMAP"));
            sqimap_logout($imap_stream);

            // not gurantee corect!
            Mail_Fetch_Status(_("Saving UIDL"));
            setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
            exit;
        }
    }

    Mail_Fetch_Status(_("Closing POP"));
    $pop3->quit();
    Mail_Fetch_Status(_("Logging out from IMAP"));
    sqimap_logout($imap_stream);
    if (is_array($mailfetch_uidl)) {
        Mail_Fetch_Status(_("Saving UIDL"));
        setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
    }

    Mail_Fetch_Status(_("Done"));
}
?>
</center>
</body>
</html>