Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- // Edit this to your needs (only need to change source, as target is your local server)
- $source_connectionstring = "{127.0.0.1:993/imap/ssl/novalidate-cert}";
- $target_connectionstring = "{localhost:143/imap/notls}";
- // Basic requirements check
- if (!function_exists('mcrypt_decrypt'))
- die('You need php\'s mcrypt extension in order for this tool to work.'.PHP_EOL);
- if (!function_exists('imap_open'))
- die('You need php\'s imap extension in order for this tool to work.'.PHP_EOL);
- if (!function_exists('mb_convert_encoding'))
- die('You need php\'s multibyte extension in order for this tool to work.'.PHP_EOL);
- $mycnf = parse_ini_file('/etc/mysql/debian.cnf');
- $secret = file_get_contents('/etc/psa/private/secret_key');
- $db = new mysqli($mycnf['host'], $mycnf['user'], $mycnf['password'], 'psa' );
- function connect_imap($con, $ro = false)
- {
- global $user, $password;
- $imap = @imap_open($con, $user, $password, ($ro ? OP_READONLY : OP_HALFOPEN), 10);
- if (!$imap)
- {
- echo 'Unable to connect to ' . $con . ' : ' . imap_last_error() . PHP_EOL;
- return false;
- }
- return $imap;
- }
- $result = $db->query("SELECT CONCAT(mail_name,'@',name) as email,accounts.password FROM mail LEFT JOIN domains on domains.id=mail.dom_id LEFT JOIN accounts on accounts.id=mail.account_id");
- while ($row = $result->fetch_assoc())
- {
- // No password? Then it's a forward, no need to sync
- if (empty($row['password']))
- continue;
- $user = $row['email'];
- $hash = explode('$', $row['password']);
- $iv = base64_decode($hash[2]);
- $ct = base64_decode($hash[3]);
- $password = str_replace("\0", '', mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secret, $ct , MCRYPT_MODE_CBC, $iv));
- echo 'Syncing account '. $user . PHP_EOL;
- $source_imap = connect_imap($source_connectionstring, true);
- if (!$source_imap) continue;
- $target_imap = connect_imap($target_connectionstring);
- if (!$target_imap) continue;
- $targetfolders = array();
- $target_folders = imap_list($target_imap, $target_connectionstring, '*');
- if ($target_folders !== false)
- {
- foreach($target_folders AS $folder)
- $targetfolders[] = str_replace($target_connectionstring, '', $folder);
- }
- $source_folders = imap_list($source_imap, $source_connectionstring, '*');
- if ($source_folders !== false)
- {
- $migratedfolders = array();
- foreach ($source_folders as $value)
- {
- $folderpath = str_replace($source_connectionstring, '', $value);
- if(in_array($folderpath, $migratedfolders))
- continue;
- $targetroot = mb_convert_encoding($folderpath, "UTF7-IMAP","ISO-8859-15");
- echo ' - Folder: ' . $targetroot . PHP_EOL;
- if (!in_array($targetroot, $targetfolders))
- {
- echo 'Creating ' . $targetroot . PHP_EOL;
- if(!imap_createmailbox($target_imap, $target_connectionstring . $targetroot))
- {
- echo 'Error creating the targetfolder ' . $folderpath . ':' . imap_last_error() . PHP_EOL;
- continue;
- }
- }
- imap_reopen($source_imap, $source_connectionstring . $targetroot, OP_READONLY);
- $MC = imap_check($source_imap);
- $count = intval($MC->Nmsgs);
- echo ' - Messages: ' . $count . PHP_EOL;
- if ($count)
- {
- $src_messages = array();
- $counter = 0;
- $fetchresult = imap_fetch_overview($source_imap, '1:' . $count, 0);
- foreach ($fetchresult as $overview)
- {
- $flags = array();
- if ($overview->flagged) $flags[] = '\Flagged';
- if ($overview->answered) $flags[] = '\Answered';
- if ($overview->deleted) $flags[] = '\Deleted';
- if ($overview->seen) $flags[] = '\Seen';
- if ($overview->draft) $flags[] = '\Draft';
- $src_messages[$counter] = $overview;
- $src_messages[$counter]->flags = implode(' ',$flags);
- $src_messages[$counter]->internaldate = gmdate('d-M-Y H:i:s O', $overview->udate);
- if (!isset($overview->message_id))
- $src_messages[$counter]->message_id = md5($overview->udate . '+' . $overview->size);
- $counter++;
- }
- // Remove messages we already have
- imap_reopen($target_imap, $target_connectionstring . $targetroot);
- $MC = imap_check($target_imap);
- $fetchresult2 = imap_fetch_overview($target_imap, '1:' . $MC->Nmsgs, 0);
- foreach ($fetchresult2 as $overview)
- {
- foreach(array_keys($src_messages) AS $src)
- {
- if (!isset($overview->message_id))
- $overview->message_id = md5($overview->udate . '+' . $overview->size);
- if ($src_messages[$src]->message_id == $overview->message_id)
- {
- unset($src_messages[$src]);
- break;
- }
- }
- }
- echo ' - Need to copy ' . count($src_messages) . ' mails.' . PHP_EOL;
- if (count($src_messages))
- {
- foreach ($src_messages AS $message)
- {
- echo ' - Transfering ' . $message->message_id . PHP_EOL;
- $msg = imap_fetchheader($source_imap, $message->msgno) . imap_body($source_imap, $message->msgno);
- if(!imap_append($target_imap, $target_connectionstring . $targetroot, $msg, $message->flags, $message->internaldate))
- echo ' - Unable to migrate the email : ' . imap_last_error() . PHP_EOL;
- }
- }
- }
- $migratedfolders[] = $folderpath;
- }
- }
- imap_close($source_imap);
- imap_close($target_imap);
- }
- ?>
Add Comment
Please, Sign In to add comment