Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

virtualmaildel.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. //
  3. // Virtual Mail Delete
  4. // by George Vieira <george at citadelcomputer dot com dot au>
  5. //
  6. // You can run this from your crontab with something like
  7. //
  8. // 0 4 * * * * vmail php -q virtualmaildel.php >/dev/null
  9. //
  10. // Setup location of postfixadmin config files. Needed to login to mysql
  11. //
  12. $conf = '/home/httpd/mail/admin/config.inc.php';
  13. //
  14. // Where's the homedir accounts stored. (GET THIS RIGHT OTHERWISE IT THINK NONE EXIST AND DELETES ALL)
  15. //
  16. $homedir = '/home/virtual';
  17. //
  18. // Make sure everything is everything before continuing
  19. //
  20. if ( ! file_exists( $conf ) )
  21. die( "Cannot find config file $conf\n" );
  22. if ( ! is_dir( $homedir ) )
  23. die( "Cannot find home directory for virtual mailboxes in $homedir\n" );
  24. //
  25. // Load mysql authentication from postfixadmin
  26. //
  27. include( $conf );
  28. //
  29. // Recursive Delete Function
  30. //
  31. function deldir($dir)
  32. {
  33. $current_dir = opendir($dir);
  34. while($entryname = readdir($current_dir))
  35. {
  36. if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!=".."))
  37. {
  38. deldir("${dir}/${entryname}");
  39. }
  40. elseif($entryname != "." and $entryname!="..")
  41. {
  42. unlink("${dir}/${entryname}");
  43. }
  44. }
  45. closedir($current_dir);
  46. @rmdir(${dir});
  47. }
  48. // --- Main Start ---
  49. //
  50. // Get list of directories
  51. //
  52. $fr = opendir( $homedir );
  53. while ( ($domain = readdir($fr)) !== false)
  54. {
  55. //
  56. // Check if it's a dir
  57. //
  58. if ( $domain != "." and $domain != ".." and filetype($homedir .'/'. $domain) == "dir" )
  59. {
  60. //
  61. // Open the (assumed) DOMAIN directory
  62. //
  63. $ff = opendir( $homedir .'/'. $domain );
  64. while ( ($user = readdir($ff)) !== false)
  65. {
  66. //
  67. // Check for directories assuming it's a user account
  68. //
  69. if ( $user!="." and $user!=".." and filetype($homedir .'/'. $domain .'/'. $user) == "dir" )
  70. {
  71. //
  72. // if the dir 'new' exists inside then it's an account
  73. //
  74. if ( file_exists($homedir .'/'. $domain .'/'. $user .'/'. "new") )
  75. {
  76. $dir[$domain][$user] = "";
  77. }
  78. else
  79. {
  80. //
  81. // Alert that the dir doesn't have a 'new' dir, possibly not an account. Leave it.
  82. //
  83. echo "UNKNOWN : " . $homedir ."/". $domain ."/". $user ."/new NOT FOUND. Possibly not an account. Leaving untouched\n";
  84. }
  85. }
  86. }
  87. }
  88. }
  89. //
  90. // OK, got an array of accounts from the dir, Now connect to the DB and check them
  91. //
  92. $conx = mysql_connect( $CONF['database_host'],$CONF['database_user'],$CONF['database_password'] );
  93. //
  94. // Is there a problem connecting?
  95. //
  96. if ( $conx != false )
  97. {
  98. //
  99. // Select the database
  100. //
  101. mysql_select_db( $CONF['database_name'] , $conx) or die ("Can't access database postfix : " . mysql_error());
  102. //
  103. // Select all mailboxes to verify against dirs listed in array
  104. //
  105. $query = "SELECT * FROM mailbox";
  106. $result = mysql_query( $query );
  107. //
  108. // Query the mailbox table
  109. //
  110. if ( $result != false )
  111. {
  112. //
  113. // Fetch the list of results
  114. //
  115. while ( $row = mysql_fetch_assoc( $result ) )
  116. {
  117. //
  118. // Pull apart the maildir field, needed to figure out the directory structure to compare
  119. //
  120. $strip = explode("/",$row['maildir']);
  121. //
  122. // Unset the array if it exists. This stops it being erased later.
  123. //
  124. unset( $dir[ $strip[0] ][ $strip[1] ] );
  125. }
  126. //
  127. // If there are results. unset the domain too.
  128. //
  129. if ( count($dir[$strip[0]])==0 and mysql_num_rows($result)>0 )
  130. unset( $dir[$strip[0]] );
  131. }
  132. else
  133. die( "Failed SELECT in mailboxes\n" );
  134. }
  135. else
  136. die( 'Cannot connect to the database!\n' );
  137. //
  138. // OK, time to clean up. All known users/domains have been removed from the list.
  139. //
  140. //
  141. // If the array still exists (incase nothing there)
  142. //
  143. if ( is_array($dir) )
  144. {
  145. //
  146. // Go through each dir
  147. //
  148. foreach ( $dir as $key => $value )
  149. {
  150. //
  151. // Is this a user array?
  152. //
  153. if ( is_array( $value) )
  154. {
  155. //
  156. // Go through and nuke the folders
  157. //
  158. foreach ( $value as $user => $value2 )
  159. {
  160. //
  161. // Nuke.. need any more explanations?
  162. //
  163. echo "REMOVING : " . $homedir."/".$key."/".$user."\n" ;
  164. deldir( $homedir."/".$key."/".$user ) ;
  165. }
  166. }
  167. }
  168. }
  169. //
  170. // And we are outta here....
  171. //
  172. echo "Cleanup process completed\n";
  173. ?>