GetProperty("C_System_Storage_Dir_MailPath"); function createGUI() { ?>
Charset:
Old webmail path(blank for default):
127) { return true; } } return false; } function loadInputParameters() { global $sourceCharset, $rootDirectory, $_GET; if (!isset($_GET['charset'])) return false; if (!$_GET['charset']) { echo 'Charset not set'; return false; } if ($_GET['non_standard_path']) { $rootDirectory = $_GET['non_standard_path']; //Fix path $end = substr($rootDirectory,-1,1); if($end!='\/' && $end!='\\') $rootDirectory.='/'; } $sourceCharset = $_GET['charset']; return true; } function prepareDirectoryName($directoryName) { if (!$directoryName) { $directoryName = './'; } else { if (!is_dir($directoryName)) { echo 'Invalid directory'; exit(1); } if (ereg('[^/\\]$', $directoryName)) $directoryName .= '/'; } return $directoryName; } function makeBackupName($fileName) { $n = ''; while (is_file($fileName.'.bak'.$n)) $n++; return $fileName.'.bak'.$n; } // Becaluse of PHP4. In PHP5 there is the // function file_put_contents function writeFile($fileName, $content) { if (!$handle = fopen($fileName, 'w+')) return false; if (fwrite($handle, $content) === FALSE) return false; fclose($handle); return true; } function processResponderDat($path) { global $sourceCharset; echo 'File: '.realpath($path).' ... '; if (($content = file_get_contents($path)) === false) { echo 'Cannot read file'; return; } if (!isUTF8($content)) { if (($content = iconv($sourceCharset, 'UTF-8', $content)) === false) { echo 'Error in iconv'; return false; } // Create backup before writing back if (!copy($path, makeBackupName($path))) { echo 'Cannot create backup file!'; return; } if (writeFile($path, $content) === false) { echo 'Error while writing file'; return; } } echo 'OK'; } function processSettingsDat($path) { echo 'File: '.realpath($path).' ... '; if (($content = readLines($path)) === false) return; if ($content[0]) if (($content[0] = processLine($content[0])) === false) return; if (!writeLines($path, $content)) return; echo 'OK'; } function processAddressDat($path) { echo 'File: '.realpath($path).' ... '; if (($sourceContent = readLines($path)) === false) return; $outputArray = array(); foreach ($sourceContent as $line) { if (($processedLine = processLine($line)) === false) return; $outputArray[] = $processedLine; } if (!writeLines($path, $outputArray)) return; echo 'OK'; } function processAccount() { global $api, $account, $dom; $mailPath = $api->GetProperty("C_System_Storage_Dir_MailPath"); $configPath = $api->GetProperty("c_configpath"); // For every domain $dCount = $api -> GetDomainCount(); for ($i=0;$i<$dCount;$i++) { // Domain $dName = $api->GetDomain($i); $responderFolders = Array(); if ($account->FindInitQuery($dName, "")) { $responderFiles = Array(); while ($account->FindNext()) { $uname = $account->GetProperty("u_name"); $ucomment = $account->GetProperty("u_comment"); echo 'Name: '.$uname.' ... '; if (!isUTF8($uname)) { if (($translatedName = iconv($sourceCharset, 'UTF-8', $uname)) !== false) { $account->SetProperty("u_name", $translatedName); echo 'OK'; } else { echo 'Error in iconv'; } } echo 'Comment: ... '; if (!isUTF8($ucomment)) { if (($translatedComment = iconv($sourceCharset, 'UTF-8', $ucomment)) !== false) { $account->SetProperty("u_comment", $translatedComment); echo 'OK'; } else { echo 'Error in iconv'; } } $respondWith = $account->GetProperty("u_respondwith"); if (!$respondWith) { if ($account->GetProperty("u_mailboxpath")) { $responderPath = getusermailboxpath($mailPath, $account->GetProperty("u_mailboxpath")) . "responder.dat"; if (is_file($responderPath)) processResponderDat($responderPath); } } else { $responderPath = getfilevarpath($value, $configPath . $account->Domain . '/autoresp/' . $respondWith); $responderFiles[$responderPath] = true; } $account->Save(); } foreach ($responderFiles as $responderFile => $value) { if (is_file($responderFile)) { processResponderDat($responderFile); } } $account->FindDone(); } } } function processLine($line) { global $sourceCharset; $strippedLine = preg_replace('/[\n\r]*$/', '', $line); $delimiter = substr($line, strlen($strippedLine)); $outputLine = ''; foreach (explode(':', $strippedLine) as $column) { $decodedColumn = rawurldecode($column); if (isUTF8($decodedColumn)) { $translatedColumn = $decodedColumn; } else { if (($translatedColumn = iconv($sourceCharset, 'UTF-8', $decodedColumn)) === false) { echo 'Error in iconv'; return false; } } $outputLine .= rawurlencode($translatedColumn).':'; } return substr($outputLine, 0, strlen($outputLine)-1).$delimiter; } function readLines($path) { if (($lines = file($path)) === false) echo 'Cannot read file'; return $lines; } function writeLines($path, $lines) { // Create backup before writing back if (!copy($path, makeBackupName($path))) { echo 'Cannot create backup file!'; return false; } if (writeFile($path, implode('', $lines)) === false) { echo 'Error while writing file'; return false; } return true; } function processDirectory($directoryName, $actions) { if ($dh = opendir($directoryName)) { while (($name = readdir($dh)) !== false) { if (is_file($directoryName.$name)) { // Call specific function if (isset($actions[$name])) $actions[$name]($directoryName.$name); } else if (is_dir($directoryName.$name) && $name[0] != '.') { processDirectory($directoryName.$name.'/', $actions); } } closedir($dh); } } /****************************** UTF-8 Detection *******************************/ class Stream { function Stream($string) { $this->i = 0; $this->string = $string; $this->length = strlen($string); } function hasNext() { return $this->i < $this->length; } function getNext() { return ord($this->string[$this->i++]); } } function checkConsequentValue($stream) { if ($stream->hasNext() && (($stream->getNext() & 0xC0) == 0x80)) { return true; } else return false; } function lengthOfSequence($v) { if (!($v & 0x40)) return -1; else if (!($v & 0x20)) return 2; else if (!($v & 0x10)) return 3; else if (!($v & 0x08)) return 4; else return -1; } function isUTF8($string) { $hasUTF8 = false; $stream = new Stream($string); while ($stream->hasNext()) { $v = $stream->getNext(); // Is $v begin of sequence? if ($v & 0x80) { switch (lengthOfSequence($v)) { case '2': // Check restricted UTF-8 characters (by RFC 3629) if ($v == 0xC0 || $v == 0xC1) return false; if (checkConsequentValue($stream)) $hasUTF8 = true; else return false; break; case '3': if (checkConsequentValue($stream) && checkConsequentValue($stream)) $hasUTF8 = true; else return false; break; case '4': // Check restricted UTF-8 characters (by RFC 3629) if ($v == 0xF5 || $v == 0xF6 || $v == 0xF7) return false; if (checkConsequentValue($stream) && checkConsequentValue($stream) && checkConsequentValue($stream)) $hasUTF8 = true; else return false; break; default: return false; } } } return $hasUTF8; } function renameDirectory($directory) { if(isabovechar($directory)){ //echo $directory." - ".mb_detect_encoding($directory)." - "; $dir = substr($directory,strrpos($directory,'/')+1); $path = substr($directory,0,strrpos($directory,'/')+1); $dir = iconv('','UTF-8',$dir); $dir = mb_convert_encoding($dir,'UTF7-IMAP','UTF-8'); rename($directory,$path.$dir); echo "
Directory $directory renamed to "; $directory = $path.$dir; echo $directory; } if ($dh = opendir($directory.'/')) { while (($name = readdir($dh)) !== false) { if (is_dir($directory.'/'.$name) && $name!='.' && $name!='..') { renameDirectory($directory.'/'.$name); } } } } /******************************************************************************/ createGUI(); echo ''; if (loadInputParameters()) { $actions = Array( 'address.dat' => 'processAddressDat', 'groups.dat' => 'processAddressDat', 'settings.dat' => 'processSettingsDat' ); // proces {MAIL_PATH} echo $api->GetProperty("C_System_Storage_Dir_MailPath").'
'; // processDirectory($api->GetProperty("C_System_Storage_Dir_MailPath"),$actions); // proces {INSTALL_PATH}/webmail // echo $api->GetProperty("c_installpath").'webmail/
'; if(!$rootDirectory) $rootDirectory = $api->GetProperty("c_installpath").'webmail/'; processDirectory($rootDirectory, $actions); renameDirectory($api->GetProperty("C_System_Storage_Dir_MailPath"),$_REQUEST['charset']); // processAccount(); } echo '
'; ?>