Improve unused account detection script

This commit is contained in:
Daniel Winzen
2020-01-13 06:55:27 +01:00
parent 68b4458c88
commit cdbf8958b2

View File

@ -1,25 +1,25 @@
<?php <?php
include('common.php'); require('common.php');
$db = get_db_instance(); $db = get_db_instance();
//update quota usage //update quota usage
$stmt=$db->query('SELECT id, system_account FROM users WHERE id NOT IN (SELECT user_id FROM new_account) AND todelete!=1;'); $stmt=$db->query('SELECT id, system_account FROM users WHERE id NOT IN (SELECT user_id FROM new_account) AND todelete!=1;');
$update=$db->prepare('UPDATE disk_quota SET quota_size_used = ?, quota_files_used = ? WHERE user_id = ?;'); $update=$db->prepare('UPDATE disk_quota SET quota_size_used = ?, quota_files_used = ? WHERE user_id = ?;');
while($tmp=$stmt->fetch(PDO::FETCH_NUM)){ while($tmp=$stmt->fetch(PDO::FETCH_ASSOC)){
$quota = shell_exec('quota -pu ' . escapeshellarg($tmp[1])); $quota = shell_exec('quota -pu ' . escapeshellarg($tmp['system_account']));
$quota_array = explode("\n", $quota); $quota_array = explode("\n", $quota);
if(!empty($quota_array[2])){ if(!empty($quota_array[2])){
$quota_size=(int) preg_replace('~^\s+[^\s]+\s+([^\s]+).*~', '$1', $quota_array[2]); $quota_size=(int) preg_replace('~^\s+[^\s]+\s+([^\s]+).*~', '$1', $quota_array[2]);
$quota_files=(int) preg_replace('~^\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+([^\s]+).*~', '$1', $quota_array[2]); $quota_files=(int) preg_replace('~^\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+([^\s]+).*~', '$1', $quota_array[2]);
$update->execute([$quota_size, $quota_files, $tmp[0]]); $update->execute([$quota_size, $quota_files, $tmp['id']]);
} }
} }
//delete tmp files older than 24 hours //delete tmp files older than 24 hours
$stmt=$db->query('SELECT system_account FROM users;'); $stmt=$db->query('SELECT system_account FROM users;');
$all=$stmt->fetchAll(PDO::FETCH_NUM); $all=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($all as $tmp){ foreach($all as $tmp){
exec('find '.escapeshellarg("/home/$tmp[0]/tmp").' -path '.escapeshellarg("/home/$tmp[0]/tmp/*").' -cmin +1440 -delete'); exec('find '.escapeshellarg("/home/$tmp[system_account]/tmp").' -path '.escapeshellarg("/home/$tmp[system_account]/tmp/*").' -cmin +1440 -delete');
} }
exec("find /var/www/tmp -path '/var/www/tmp/*' -cmin +1440 -delete"); exec("find /var/www/tmp -path '/var/www/tmp/*' -cmin +1440 -delete");
@ -28,29 +28,31 @@ $last_month=time()-60*60*24*30;
$del=$db->prepare('UPDATE users SET todelete=1 WHERE id=?;'); $del=$db->prepare('UPDATE users SET todelete=1 WHERE id=?;');
$stmt=$db->prepare('SELECT system_account, id FROM users WHERE dateadded<?;'); $stmt=$db->prepare('SELECT system_account, id FROM users WHERE dateadded<?;');
$stmt->execute([$last_month]); $stmt->execute([$last_month]);
$all=$stmt->fetchAll(PDO::FETCH_NUM); $all=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($all as $tmp){ foreach($all as $tmp){
//check modification times //check modification times
if(filemtime("/home/$tmp[0]/")>$last_month){ if(filemtime("/home/$tmp[system_account]/data/")>$last_month){
continue; continue;
} }
if(filemtime("/home/$tmp[0]/data/")>$last_month){ if(filemtime("/home/$tmp[system_account]/www/")>$last_month){
continue; continue;
} }
if(filemtime("/home/$tmp[0]/www/")>$last_month){ $count_www=count(scandir("/home/$tmp[system_account]/www/"));
continue;
}
$count_www=count(scandir("/home/$tmp[0]/www/"));
//check data empty and www no more than 1 file //check data empty and www no more than 1 file
if($count_www>3 || count(scandir("/home/$tmp[0]/data/"))>2){ if($count_www>3 || count(scandir("/home/$tmp[system_account]/data/"))>2){
continue; continue;
} }
//check www empty or index unmodified //check www empty or index unmodified
if($count_www===3){ if($count_www===3){
if(!file_exists("/home/$tmp[0]/www/index.hosting.html") || !in_array(md5_file("/home/$tmp[0]/www/index.hosting.html"), INDEX_MD5S)){ if(!(
( file_exists("/home/$tmp[system_account]/www/index.hosting.html") && in_array(md5_file("/home/$tmp[system_account]/www/index.hosting.html"), INDEX_MD5S, true) ) ||
( file_exists("/home/$tmp[system_account]/www/index.html") && in_array(md5_file("/home/$tmp[system_account]/www/index.html"), INDEX_MD5S, true) ) ||
( file_exists("/home/$tmp[system_account]/www/index.htm") && in_array(md5_file("/home/$tmp[system_account]/www/index.htm"), INDEX_MD5S, true) ) ||
( file_exists("/home/$tmp[system_account]/www/index.php") && in_array(md5_file("/home/$tmp[system_account]/www/index.php"), INDEX_MD5S, true) )
)){
continue; continue;
} }
} }
//no data found, safe to delete //no data found, safe to delete
$del->execute([substr($tmp[1], 0, 16)]); $del->execute([substr($tmp['id'], 0, 16)]);
} }