adding own pop client implementation
[squirrelmail.git] / plugins / mail_fetch / functions.php
1 <?php
2
3 /**
4 * mail_fetch/functions.php
5 *
6 * Functions for the mail_fetch plugin.
7 *
8 * Original code from LexZEUS <lexzeus@mifinca.com>
9 * and josh@superfork.com (extracted from php manual)
10 * Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com>
11 *
12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @version $Id$
15 * @package plugins
16 * @subpackage mail_fetch
17 */
18
19
20 /** pop3 class */
21 include_once (SM_PATH . 'plugins/mail_fetch/class.POP3.php');
22
23 /** declare plugin globals */
24 global $mail_fetch_allow_unsubscribed;
25
26 /**
27 * Controls use of unsubscribed folders in plugin
28 * @global boolean $mail_fetch_allow_unsubscribed
29 * @since 1.5.1 and 1.4.5
30 */
31 $mail_fetch_allow_unsubscribed = false;
32
33 /** load site config */
34 if (file_exists(SM_PATH . 'config/mail_fetch_config.php')) {
35 include_once(SM_PATH . 'config/mail_fetch_config.php');
36 } elseif (file_exists(SM_PATH . 'plugins/mail_fetch/config.php')) {
37 include_once(SM_PATH . 'plugins/mail_fetch/config.php');
38 }
39
40 // hooked functions
41
42 /**
43 * internal function used to load user's preferences
44 * @since 1.5.1
45 * @private
46 */
47 function mail_fetch_load_pref_function() {
48 global $data_dir, $username;
49 global $mailfetch_server_number;
50 global $mailfetch_cypher, $mailfetch_port_;
51 global $mailfetch_server_,$mailfetch_alias_,$mailfetch_user_,$mailfetch_pass_;
52 global $mailfetch_lmos_, $mailfetch_uidl_, $mailfetch_login_, $mailfetch_fref_;
53 global $PHP_SELF;
54
55 if( stristr( $PHP_SELF, 'mail_fetch' ) ) {
56 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number', 0);
57 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher', 'on' );
58 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
59 for ($i=0;$i<$mailfetch_server_number;$i++) {
60 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
61 $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i");
62 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
63 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
64 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
65 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
66 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
67 $mailfetch_fref_[$i] = getPref($data_dir, $username, "mailfetch_fref_$i");
68 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
69 if( $mailfetch_cypher == 'on' ) $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
70 }
71 }
72 }
73
74 /**
75 * Internal function used to fetch pop3 mails on login
76 * @since 1.5.1
77 * @private
78 */
79 function mail_fetch_login_function() {
80 //include_once (SM_PATH . 'include/validate.php');
81 include_once (SM_PATH . 'functions/imap_general.php');
82
83 global $username, $data_dir, $imapServerAddress, $imapPort;
84
85 $mailfetch_newlog = getPref($data_dir, $username, 'mailfetch_newlog');
86
87 $outMsg = '';
88
89 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
90 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
91 $mailfetch_cypher = getPref($data_dir, $username, 'mailfetch_cypher');
92 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
93
94 for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++) {
95
96 $mailfetch_login_[$i_loop] = getPref($data_dir, $username, "mailfetch_login_$i_loop");
97 $mailfetch_fref_[$i_loop] = getPref($data_dir, $username, "mailfetch_fref_$i_loop");
98 $mailfetch_pass_[$i_loop] = getPref($data_dir, $username, "mailfetch_pass_$i_loop");
99 if( $mailfetch_cypher == 'on' )
100 $mailfetch_pass_[$i_loop] = decrypt( $mailfetch_pass_[$i_loop] );
101
102 if( $mailfetch_pass_[$i_loop] <> '' && // Empty passwords no allowed
103 ( ( $mailfetch_login_[$i_loop] == 'on' && $mailfetch_newlog == 'on' ) || $mailfetch_fref_[$i_loop] == 'on' ) ) {
104
105 $mailfetch_server_[$i_loop] = getPref($data_dir, $username, "mailfetch_server_$i_loop");
106 $mailfetch_port_[$i_loop] = getPref($data_dir, $username , "mailfetch_port_$i_loop");
107 $mailfetch_alias_[$i_loop] = getPref($data_dir, $username, "mailfetch_alias_$i_loop");
108 $mailfetch_user_[$i_loop] = getPref($data_dir, $username, "mailfetch_user_$i_loop");
109 $mailfetch_lmos_[$i_loop] = getPref($data_dir, $username, "mailfetch_lmos_$i_loop");
110 $mailfetch_uidl_[$i_loop] = getPref($data_dir, $username, "mailfetch_uidl_$i_loop");
111 $mailfetch_subfolder_[$i_loop] = getPref($data_dir, $username, "mailfetch_subfolder_$i_loop");
112
113 $mailfetch_server=$mailfetch_server_[$i_loop];
114 $mailfetch_port=$mailfetch_port_[$i_loop];
115 $mailfetch_user=$mailfetch_user_[$i_loop];
116 $mailfetch_alias=$mailfetch_alias_[$i_loop];
117 $mailfetch_pass=$mailfetch_pass_[$i_loop];
118 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
119 $mailfetch_login=$mailfetch_login_[$i_loop];
120 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
121 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
122
123 // $outMsg .= "$mailfetch_alias checked<br />";
124
125 // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
126
127 $pop3 = new POP3($mailfetch_server, 60);
128
129 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
130 $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
131 continue;
132 }
133
134 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
135
136 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
137 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
138 $outMsg .= _("Login Failed:") . ' ' . $pop3->ERROR;
139 continue;
140 }
141
142 // register_shutdown_function($pop3->quit());
143
144 $msglist = $pop3->uidl();
145
146 $i = 1;
147 for ($j = 1; $j < sizeof($msglist); $j++) {
148 if ($msglist["$j"] == $mailfetch_uidl) {
149 $i = $j+1;
150 break;
151 }
152 }
153
154 if ($Count < $i) {
155 $pop3->quit();
156 continue;
157 }
158 if ($Count == 0) {
159 $pop3->quit();
160 continue;
161 }
162
163 // Faster to get them all at once
164 $mailfetch_uidl = $pop3->uidl();
165
166 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
167 $outMsg .= _("Server does not support UIDL.");
168
169 for (; $i <= $Count; $i++) {
170 if (!ini_get('safe_mode'))
171 set_time_limit(20); // 20 seconds per message max
172 $Message = "";
173 $MessArray = $pop3->get($i);
174
175 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
176 $outMsg .= _("Warning:") . ' ' . $pop3->ERROR;
177 continue 2;
178 }
179
180 while (list($lineNum, $line) = each ($MessArray)) {
181 $Message .= $line;
182 }
183
184 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
185 if ($mailfetch_subfolder=='' ||
186 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
187 fputs($imap_stream, "A3$i APPEND INBOX {" . strlen($Message) . "}\r\n");
188 } else {
189 fputs($imap_stream, "A3$i APPEND $mailfetch_subfolder {" . strlen($Message) . "}\r\n");
190 }
191 $Line = fgets($imap_stream, 1024);
192 if (substr($Line, 0, 1) == '+') {
193 fputs($imap_stream, $Message);
194 fputs($imap_stream, "\r\n");
195 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
196
197 if ($mailfetch_lmos != 'on') {
198 $pop3->delete($i);
199 }
200 } else {
201 echo "$Line";
202 $outMsg .= _("Error Appending Message!");
203 }
204 }
205
206 $pop3->quit();
207 sqimap_logout($imap_stream);
208 if (is_array($mailfetch_uidl)) {
209 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
210 }
211 }
212 }
213
214 if( trim( $outMsg ) <> '' ) {
215 echo '<br /><font size="1">' . _("Mail Fetch Result:") . "<br />$outMsg</font>";
216 }
217 if( $mailfetch_newlog == 'on' ) {
218 setPref($data_dir, $username, 'mailfetch_newlog', 'off');
219 }
220 }
221
222 /**
223 * Internal function used to detect new logins
224 */
225 function mail_fetch_setnew_function() {
226 global $data_dir, $username;
227
228 setPref( $data_dir, $username, 'mailfetch_newlog', 'on' );
229 }
230
231 /**
232 * Internal function used to register option block
233 * @since 1.5.1
234 * @private
235 */
236 function mailfetch_optpage_register_block_function() {
237 global $optpage_blocks;
238
239 $optpage_blocks[] = array(
240 'name' => _("POP3 Fetch Mail"),
241 'url' => '../plugins/mail_fetch/options.php',
242 'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
243 'js' => false
244 );
245 }
246
247 /**
248 * Internal function used to update mail_fetch settings
249 * when folders are renamed or deleted.
250 * @since 1.5.1
251 * @private
252 */
253 function mail_fetch_folderact_function($args) {
254 global $username, $data_dir;
255
256 if (empty($args) || !is_array($args)) {
257 return;
258 }
259
260 /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
261 if (count($args) != 3) {
262 return;
263 }
264
265 list($old_folder, $action, $new_folder) = $args;
266
267 $mailfetch_server_number = getPref($data_dir, $username, 'mailfetch_server_number');
268
269 for ($i = 0; $i < $mailfetch_server_number; $i++) {
270 $mailfetch_subfolder = getPref($data_dir, $username, 'mailfetch_subfolder_' . $i);
271
272 if ($mailfetch_subfolder != $old_folder) {
273 continue;
274 }
275
276 if ($action == 'delete') {
277 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, 'INBOX');
278 } elseif ($action == 'rename') {
279 setPref($data_dir, $username, 'mailfetch_subfolder_' . $i, $new_folder);
280 }
281 }
282 }
283 // end of hooked functions
284
285 /**
286 * hex2bin - document me
287 */
288 function hex2bin( $data ) {
289
290 /* Original code by josh@superfork.com */
291
292 $len = strlen($data);
293 $newdata = '';
294 for( $i=0; $i < $len; $i += 2 ) {
295 $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
296 }
297 return $newdata;
298 }
299
300 function mf_keyED( $txt ) {
301
302 global $MF_TIT;
303
304 if( !isset( $MF_TIT ) ) {
305 $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
306 }
307
308 $encrypt_key = md5( $MF_TIT );
309 $ctr = 0;
310 $tmp = "";
311 for( $i = 0; $i < strlen( $txt ); $i++ ) {
312 if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
313 $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
314 $ctr++;
315 }
316 return $tmp;
317 }
318
319 function encrypt( $txt ) {
320
321 srand( (double) microtime() * 1000000 );
322 $encrypt_key = md5( rand( 0, 32000 ) );
323 $ctr = 0;
324 $tmp = "";
325 for( $i = 0; $i < strlen( $txt ); $i++ ) {
326 if ($ctr==strlen($encrypt_key)) $ctr=0;
327 $tmp.= substr($encrypt_key,$ctr,1) .
328 (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
329 $ctr++;
330 }
331 return bin2hex( mf_keyED( $tmp ) );
332
333 }
334
335 function decrypt( $txt ) {
336
337 $txt = mf_keyED( hex2bin( $txt ) );
338 $tmp = '';
339 for ( $i=0; $i < strlen( $txt ); $i++ ) {
340 $md5 = substr( $txt, $i, 1 );
341 $i++;
342 $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
343 }
344 return $tmp;
345 }
346
347 /**
348 * check mail folder
349 * @param stream $imap_stream imap connection resource
350 * @param string $imap_folder imap folder name
351 * @return boolean true, when folder can be used to store messages.
352 * @since 1.5.1 and 1.4.5
353 */
354 function mail_fetch_check_folder($imap_stream,$imap_folder) {
355 global $mail_fetch_allow_unsubscribed;
356
357 // check if folder is subscribed or only exists.
358 if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
359 $ret = true;
360 } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
361 $ret = true;
362 } else {
363 $ret = false;
364 }
365
366 // make sure that folder can store messages
367 if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
368 $ret = false;
369 }
370
371 return $ret;
372 }
373
374 /**
375 * Checks if folder is noselect (can't store messages)
376 *
377 * Function does not check if folder subscribed.
378 * @param stream $imap_stream imap connection resource
379 * @param string $imap_folder imap folder name
380 * @return boolean true, when folder has noselect flag. false in any other case.
381 * @since 1.5.1 and 1.4.5
382 */
383 function mail_fetch_check_noselect($imap_stream,$imap_folder) {
384 $boxes=sqimap_mailbox_list($imap_stream);
385 foreach($boxes as $box) {
386 if ($box['unformatted']==$imap_folder) {
387 return (bool) check_is_noselect($box['raw']);
388 }
389 }
390 return false;
391 }