Post new topic Reply to topic  [ 37 posts ] 
Author Message
 Post subject: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2009-06-28 21:36 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
DO NOT USE AT ALL

BACKUP YOUR DATABASE AND DATA DIRECTORY BEFORE YOU USE THIS SCRIPT

If you have been using hMail for long periods of time and upgraded all the way up from V4 as new releases have emerged then you may have orphaned files lurking in the data and sub directories. AV and other anti spam mechanisms could sometimes lock a file at which point it's not removed (this doesn't happen any more).

I have written a PHP script that recurses through the accounts (user selected) and checks files against MySQL database entries. If there is no corresponding database entry the file is orphaned and can be removed. This script also can remove empty directories for you if you select the option.

I doubt I will expand on this script any further and will not be adding any more features unless I feel it's extremely useful. I will not be adding support to recurse through every domain and account in one go as PHP would time out before its done (unless you start messing with timeout values under PHP.ini) and will place your database under heavy load. If you want it to do that then feel free to modify this script to do so.

Once you have downloaded the file open it up in notepad++ or similar and alter the config values at the top of the script and uploaded it to your webserver.
Make sure that PHP has the correct permissions to delete files and directories from hMail's data directory else this script is pretty useless.

This script is potentially dangerous if you don't know what you are doing. I have restricted the script to only allow access to specific IP addresses (configurable at the top of the script).

Attachment:
img1.png
img1.png [ 46.08 KiB | Viewed 10101 times ]

Attachment:
img2.png
img2.png [ 14.86 KiB | Viewed 10055 times ]

Attachment:
img3.png
img3.png [ 18.52 KiB | Viewed 10055 times ]

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [MySQL & PHP Required]
PostPosted: 2009-07-11 05:13 
New user
New user
User avatar

Joined: 2007-01-06 07:48
Posts: 19
Location: INDONESIA
how to set database port at Configuration variables below?
my hmailserver database work at port 3307.

regards

eko s

// -------------------- Configuration variables ----------------------
$pData->host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$pData->pass = "bla bla bla"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Program Files\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'domain.com', 'domain2.com' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [MySQL & PHP Required]
PostPosted: 2009-07-12 17:50 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
In the host put 127.0.0.1:3307

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [MySQL & PHP Required]
PostPosted: 2009-09-14 18:07 
New user
New user

Joined: 2008-10-30 16:53
Posts: 12
Hey, I ported the script to use the new Microsoft SQL Server Driver for PHP for those of us using MSSQL.

Softek


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [MySQL & PHP Required]
PostPosted: 2009-09-14 19:15 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Nicely done!

Nice idea adding the timeout as well, were you having issues with the timeout? I have my timeout set high by default for upload purposes.

Edit: I updated my Script with the timeout value and placed your script in the first post as well.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2009-10-15 17:51 
New user
New user

Joined: 2008-10-30 16:53
Posts: 12
*sigh* I need to remember to check these forums more often.

Yes I was having timeout issues. The php timeout is probably still default on that server.

The hMail server we have handles 20-50 domains each with several email addresses. We also have some pretty heavy users that would get 500-1000 emails a day. Several of the users had around 20-50 orphaned files or empty folders, and ended up timing out when I tried to run the cleanup.


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2009-11-13 07:05 
New user
New user

Joined: 2009-11-12 04:15
Posts: 14
Hi, Doom.

Nice script!! I'm looking for a program to automatically remove those empty directories for my domain and user directories.

May I know how to change your program to predefined remove those empty directories(without select Domain, Account and Process)?

Regards,


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2009-11-13 10:53 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Feel free to rip the script apart and alter it to your own needs.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-05-11 19:45 
New user
New user

Joined: 2009-03-06 17:06
Posts: 8
helo every body
is it possible to allow all ip ?

$pData->AllowedIP = array( '123.123.123.123' );


Thank you for all


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-05-11 20:05 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Which version are you using MySQL or MsSQL?

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-06-02 12:54 
New user
New user

Joined: 2009-03-06 17:06
Posts: 8
i use mysql


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-06-02 13:18 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Ok start at line 97 find the following code

Code:
if( in_array( $pData->ConnectingIP, $pData->AllowedIP ) )
    ShowMainPage();
else
    echo '<table align="center" class="bordered" width="600" border="1" cellpadding="2" cellspacing="0"><tr><td colspan="2" align="center"><strong>Access Denied!!</strong></td></tr></table>';


Replace all of it with

Code:
ShowMainPage();


That should bypass the check completely.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-11-21 22:40 
New user
New user

Joined: 2009-12-09 14:43
Posts: 11
Hello.

I have tried to use the script but I can not. My configuration is:
$pData->host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$pData->pass = "password"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Archivos de programa\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'dominio.es', 'dominio2.es' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script

set_time_limit(240); // Script Time Limit, for larger mailboxes a longer timeout is needed.

I can see this error:

host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$[i]
pData->pass = "password"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Archivos de programa\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'dominio.es', 'dominio2.es' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script
set_time_limit(240); // Script Time Limit, for larger mailboxes a longer timeout is needed.
// ------------------ End Config Do Not Alter Below ------------------
$pData->DomainID = 0;
$pData->AccountID = 0;
$pData->DomainID = $_GET['domainid']+0;
$pData->AccountID = $_POST['lstaccountid']+0;
$pData->Process = addslashes( $_POST['lstprocess'] ); $pData->ConnectingIP = $_SERVER['REMOTE_ADDR']; ?> ConnectingIP, $pData->AllowedIP ) ) ShowMainPage(); else echo '
Access Denied!![/i]

I can connect with my phpmyadmin.

What is wrong?.

Thanks in advance


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-11-22 00:39 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
You see the code in the error? That is usually indicative of a php config error but you say you are running phpmyadmin fine.

Without an actual error it's hard to diagnose.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2010-11-22 11:30 
New user
New user

Joined: 2009-12-09 14:43
Posts: 11
Ok. I will check it out.


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-02-01 13:54 
New user
New user

Joined: 2009-12-09 14:43
Posts: 11
Sorry for the delay, too many things in my head.

These are my versions:

Apache: 2.2.17 (Win32)
PHP: 5.3.4
Mysql: 5.1.54-community
Versión del cliente: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
My hmailserver program is in this folder:

I receive this error when I try to run the php script:
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: domainid in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 45
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: lstaccountid in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 46
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: lstprocess in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 47
[Tue Feb 01 12:35:14 2011] [error] [client 127.0.0.1] script 'C:/appsrv/www/phpwebadmin/hMailDBtoFileCleanup.php' not found or unable to stat

How can I resolve this error?


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-02-01 13:56 
New user
New user

Joined: 2009-12-09 14:43
Posts: 11
The hmailserver version: 5.3.3-B1879


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-02-01 14:10 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
The first three are notices. php is warning because i don't define the variables at the start of the script. Either disable the notice error messages in your php.ini or add the vars to the top of the script with empty values.

The last error is because you renamed the script. it must be called hMailDBtoFileCleanup.php

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-02-01 15:41 
New user
New user

Joined: 2009-12-09 14:43
Posts: 11
Thank you very for your quick response.

After I renamed the script to its original name the script started to function.

Thanks for your support


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-16 09:15 
New user
New user

Joined: 2011-04-06 15:07
Posts: 20
i tryed out the script, but where do i find the user and password?
i installed the the latest version of hmailserver and the db that came with it, but havent got a clue what username and password it used. so cant run the script. and the server do make alot of folders that becomes emty then the user have read the mail and then delete it.

greetings
kento


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-16 10:41 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
MySQL http://www.hmailserver.com/documentatio ... t_to_mysql
MSSQL http://www.hmailserver.com/documentatio ... t_to_mssql

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-16 12:46 
New user
New user

Joined: 2011-04-06 15:07
Posts: 20
^DooM^ wrote:

changed the server to mysql, seamed a bit more easy becource i already have mysql running (yep that was quite a work, but guess its worth it) :mrgreen:
ill have a look at the cleanup php page when i have used the server a day or 2 so there is some folders to work on.
ill be back if i cant get it up and running.

greetings
kento.


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-25 17:07 
Normal user

Joined: 2010-05-23 16:24
Posts: 79
i used this script today and ended up loosing almost all emails in all domains, the db entries remain and the eml file also remain but all get erased with with the same content of

The mail server could not deliver the message to you since the file D:\Program Files (x86)\hMailServer\Data\domain.com\sales\52\{52868C66-463D-4A07-9F32-FAAF962409AD}.eml does not exist on the server.

The file may have been deleted by anti virus software running on the server.

hMailServer


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-25 18:18 
Normal user
User avatar

Joined: 2010-03-30 19:30
Posts: 227
Location: uʍop ǝpısdn
xbipin wrote:
i used this script today and ended up loosing almost all emails in all domains, the db entries remain and the eml file also remain but all get erased

What version are you using? Did you make a backup?

_________________
hMailServer 5.4-B1929 Alpha on Server 2008 R2 VM
MySQL 5.5.15, IIS 7.5, PHP 5.3.6 via FastCGI, RoundCube 0.5.4
XenServer 6.0 on 2x HP DL380 G5 16GB RAM each
VM data on Thecus 1U4200XXXR iSCSI SAN


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-25 18:41 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
As rjk posted, what version of hmail, did you backup first, and did you test it with the report only option before you blindly ran it on all of your accounts on all of your domains?

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-28 07:49 
Normal user

Joined: 2010-05-23 16:24
Posts: 79
im using HM 5.3.3, i didnt backup, i tested it with the report only on a single account and looked fine, then when i ran it, it replaced the content of all the eml files with a single message


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-28 09:21 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Why did you not backup? It says quite specifically on the first post and in the script where you add your DB username and pass to backup before use. Many users have used this script without issue and I ran the script today on my 5.3 box as a test and it worked perfectly fine. The script did not create those messages, hmail created them when it was unable to locate the emails that the database was pointing to. The script will only remove an orphaned file, it will not remove one that has a database record. The only explination I can think of is that those files were already removed by AV or you broke the script when you installed it. The source is there for anyone to verify it does what it's coded to do. It even err's on the side of caution.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-28 11:14 
Normal user

Joined: 2010-05-23 16:24
Posts: 79
it could possibly be something else or mayb hmailserver itself but let me point it out to u

- the eml modified timestamp is same as when i ran the script
- there is no antivirus on the server
- the database is pointing to those files with such data and says couldnt find that file but the fact is the file name is the same which it says is missing so actually something happened and all the eml files went down to 1KB and content as those messages and suppose if i open xxxx.eml, the message says xxx.eml does not exist in its content.

after that i though something went terribly wrong so i ran the data synchronize tool and mayb that did the rest of the damage and 12GB worth of eml files with same content now.

neways im not blaming the script in total, could be that or data sync tool or hmail itself but i just wanted to bring to notice that such a thing happened after i initially ran the script


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-04-28 11:31 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Well if it was the script i can only apologise but i still do not see how it could be possible without testing the script on your setup which isn't likely to happen. I'll put up a bigger warning to backup in the first post.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-05-04 11:11 
New user
New user

Joined: 2005-06-27 15:51
Posts: 29
Dear Doom,

Any chance to run this cleanup utility on 5.4 ?

Thanks
Blue


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-05-04 14:24 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Not currently due to file paths no longer being full in the database. If your file paths are still full in hm_messages, this script will work.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-08-03 12:26 
New user
New user

Joined: 2011-08-03 12:21
Posts: 2
Hi,

Many thanks for the script.

Just one suggestion for anyone who wants to develop the script further.

I've noticed that if the user's directory does not exist in the data directory (no idea why it wouldn't - maybe the address has never received an email?) then the script will return an infinite number of 'orphaned file' results with no filename.

I'll try to modify the script myself and post back but someone may be able to beat me to it - I'm much better at identifying things than fixing them, sadly :)

Many thanks though, this script is helping considerably!

Regards,

James.


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-08-03 12:47 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
If the user directory doesn't exist then there should be no emails to check against. seems strange it should return an infinate number of orphaned files. Perhaps i have misunderstood

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-08-03 13:03 
New user
New user

Joined: 2011-08-03 12:21
Posts: 2
That's exactly what I thought. It could well be something specific to the local configuration on this server, perhaps. I've no idea why the user folder doesn't exist, but for accounts where it doesn't, running the script shows an output such as:

Results for Blackberry@beag****.co.uk
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File

Whereas if I create the folder manually (my initial post here was a question asking what was going wrong until I managed to work out the cause of the sympoms at least!) the output is simply:

Results for Blackberry@beag****.co.uk
Process Complete

Hardly a major issue, although like you say, it shouldn't be able to find anything to display. It's running on a Windows server with MS SQL though, so could be to do with the return value from windows-php when it cannot find the directory.


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2011-08-03 13:22 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
I can't see the bug right now and i don't have time to check through it. I'll decommission this script as it's potentially dangerous.

I suspect this is what happened to xbipin except instead of checking like you did he just ran it blindly.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2012-05-08 18:11 
New user
New user

Joined: 2012-05-08 17:52
Posts: 1
Where can I download this Cleanup Utility?

Some of my Users become an Error 0x800CCC0F when using Outlook to fetch latest Mails.
Maybe this Tool can help me to check if this POP3 Account fine.

Thanks
jogep


Top
 Profile  
 
 Post subject: Re: hMail Server Cleanup Utility [PHP Required]
PostPosted: 2012-05-08 19:27 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
You can't it's not safe to run.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ] 


Who is online

Users browsing this forum: No registered users and 3 guests



Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group