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