| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | ** mail_fetch/functions.php |
| 5 | ** |
| 6 | ** Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | ** Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | ** |
| 9 | ** Functions for the mailfetch plugin. |
| 10 | ** |
| 11 | ** Original code from LexZEUS <lexzeus@mifinca.com> |
| 12 | ** and josh@superfork.com (extracted from php manual) |
| 13 | ** Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com> |
| 14 | ** |
| 15 | ** $Id$ |
| 16 | **/ |
| 17 | |
| 18 | function hex2bin( $data ) { |
| 19 | |
| 20 | /* Original code by josh@superfork.com */ |
| 21 | |
| 22 | $len = strlen($data); |
| 23 | $newdata = ''; |
| 24 | for( $i=0; $i < $len; $i += 2 ) { |
| 25 | $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) ); |
| 26 | } |
| 27 | return $newdata; |
| 28 | } |
| 29 | |
| 30 | function mf_keyED( $txt ) { |
| 31 | |
| 32 | global $MF_TIT; |
| 33 | |
| 34 | if( !isset( $MF_TIT ) ) { |
| 35 | $MF_TIT = "MailFetch Secure for SquirrelMail 1.x"; |
| 36 | } |
| 37 | |
| 38 | $encrypt_key = md5( $MF_TIT ); |
| 39 | $ctr = 0; |
| 40 | $tmp = ""; |
| 41 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
| 42 | if( $ctr == strlen( $encrypt_key ) ) $ctr=0; |
| 43 | $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 ); |
| 44 | $ctr++; |
| 45 | } |
| 46 | return $tmp; |
| 47 | } |
| 48 | |
| 49 | function encrypt( $txt ) { |
| 50 | |
| 51 | srand( (double) microtime() * 1000000 ); |
| 52 | $encrypt_key = md5( rand( 0, 32000 ) ); |
| 53 | $ctr = 0; |
| 54 | $tmp = ""; |
| 55 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
| 56 | if ($ctr==strlen($encrypt_key)) $ctr=0; |
| 57 | $tmp.= substr($encrypt_key,$ctr,1) . |
| 58 | (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); |
| 59 | $ctr++; |
| 60 | } |
| 61 | return bin2hex( mf_keyED( $tmp ) ); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | function decrypt( $txt ) { |
| 66 | |
| 67 | $txt = mf_keyED( hex2bin( $txt ) ); |
| 68 | $tmp = ''; |
| 69 | for ( $i=0; $i < strlen( $txt ); $i++ ) { |
| 70 | $md5 = substr( $txt, $i, 1 ); |
| 71 | $i++; |
| 72 | $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 ); |
| 73 | } |
| 74 | return $tmp; |
| 75 | } |
| 76 | |
| 77 | ?> |