Browse Source

added support for multiple removal

old-stable
Rahul Bansal 13 years ago
parent
commit
08446fc13e
  1. 104
      remove.php

104
remove.php

@ -1,89 +1,107 @@
<?php <?php
include_once('init.php'); include_once('init.php');
/* choice */
$choice = "";
/** /**
* prompt user to eneter domain name * prompt user to eneter domain name
*/ */
echo "Enter a domain name which needs to be removed...\n"; echo "Enter a domain name which needs to be removed...\n";
$usr_domain = (trim(fgets(STDIN))); $usr_domain = (trim(fgets(STDIN)));
echo "You have entered :: $usr_domain \n";
if(strlen($usr_domain) == 0 ){ $domain_arr = explode(" ", $usr_domain);
die("input cannot be empty!\n");
foreach($domain_arr as $domain){
echo "****************************************************************************";
echo "\n\nMoving :: $domain \n\n";
remove_domain(trim($domain));
} }
if(strpos($usr_domain,"..") !== false ){ function remove_domain($usr_domain){
global $local_env, $choice;
echo "You have entered :: $usr_domain \n";
if(strlen($usr_domain) == 0 ){
die("input cannot be empty!\n");
}
if(strpos($usr_domain,"..") !== false ){
die("directory traversal is not allowed\n"); die("directory traversal is not allowed\n");
} }
if(strpos($usr_domain,"\\") !== false ){ if(strpos($usr_domain,"\\") !== false ){
die("domain cannot contain \\ !"); die("domain cannot contain \\ !");
} }
/*
/*
* Set domain environment values * Set domain environment values
*/ */
$domain['name'] = $usr_domain; $domain['name'] = $usr_domain;
$domain['conf'] = $local_env['nginx_dir_sites_avilable'] . '/' . $domain['name'] ; $domain['conf'] = $local_env['nginx_dir_sites_avilable'] . '/' . $domain['name'] ;
$domain['rootdir'] = $local_env['webroot'] . '/' . $domain['name'] ; $domain['rootdir'] = $local_env['webroot'] . '/' . $domain['name'] ;
$domain['htdocs'] = $domain['rootdir'] . '/' . $local_env['htdocs'] ; $domain['htdocs'] = $domain['rootdir'] . '/' . $local_env['htdocs'] ;
$domain['logs'] = $domain['rootdir'] . '/' . $local_env['logs'] ; $domain['logs'] = $domain['rootdir'] . '/' . $local_env['logs'] ;
/** /**
* Check if domain config file already exists * Check if domain config file already exists
*/ */
echo "\n Domain Name - " . $domain['name'] . echo "\n Domain Name - " . $domain['name'] .
"\n Webroot Dir - " . realpath($domain['rootdir']) . "\n Webroot Dir - " . realpath($domain['rootdir']) .
"\n Database Name - " . $domain['name'] ; "\n Database Name - " . $domain['name'] ;
echo "\nDo you want to remove this domain, related files and databases for sure? [Y/N] (default=N): "; if($choice != "a"){
//ask for confirmation
echo "\nDo you want to remove this domain, related files and databases for sure? [Y(es)/N(o)/A(lways)] (default=N): ";
if ( strtolower(trim(fgets(STDIN))) != "y" ) { switch(strtolower(trim(fgets(STDIN)))){
case 'y' :
die("\nYou choose to terminate this script! The domain is NOT removed! \n"); die("\nYou choose to terminate this script! The domain is NOT removed! \n");
}
/** case 'a' : $choice = "a";
* At this point - user has confirmed domain removal }
//Drop Database }
/**
* MySQL Database Deletion
*/
$command = "mysql -h " . $local_env['mysql_host'] . " -u " . $local_env['mysql_user'] . " -p" . $local_env['mysql_pass'] . " -e 'drop database `'" . $domain['name'] . "'` '";
$result = system($command);
//remove htdocs
//remove htdocs if(file_exists(realpath($domain['rootdir']))){
if(file_exists(realpath($domain['rootdir']))){ if(dirname(realpath($domain['rootdir']))=="/var/www"){
if(dirname(realpath($domain['rootdir']))=="/var/www") echo "/nremoving webroot \n";
system("rm -rf ". realpath($domain['rootdir'])); system("rm -rf ". $domain['rootdir']);
}
else else
die("Try something else!"); die("Try something else!");
}else{ }else{
echo "\n Directory " . $domain['rootdir'] . " doesn't exists\n"; echo "\n Directory " . $domain['rootdir'] . " doesn't exists\n";
} }
//delete database //delete database
$command = "mysql -h " . $local_env['mysql_host'] . " -u " . $local_env['mysql_user'] . " -p" . $local_env['mysql_pass'] . " -e 'create database `'" . $domain['name'] . "'` '"; $command = "mysql -h " . $local_env['mysql_host'] . " -u " . $local_env['mysql_user'] . " -p" . $local_env['mysql_pass'] . " -e 'drop database `'" . $domain['name'] . "'` '";
$result = system($command); $result = system($command);
/** /**
* Remove config file * Remove config file
*/ */
if(file_exists($local_env['nginx_dir_sites_enabled']."/".$domain['name']) OR file_exists($local_env['nginx_dir_sites_avilable']."/".$domain['name'])){ if(file_exists($local_env['nginx_dir_sites_enabled']."/".$domain['name']) OR file_exists($local_env['nginx_dir_sites_avilable']."/".$domain['name'])){
unlink($local_env['nginx_dir_sites_enabled']."/".$domain['name']); unlink($local_env['nginx_dir_sites_enabled']."/".$domain['name']);
unlink($local_env['nginx_dir_sites_avilable']."/".$domain['name']); unlink($local_env['nginx_dir_sites_avilable']."/".$domain['name']);
}else{
echo "\nNginx config files for $usr_domain domain do not exist\n";
}
}//end func
/** /**
* ALL SEENS WELL - Restart nginx * ALL SEENS WELL - Restart nginx
*/ */
echo "\n Issuing nginx reboot command...\n\n"; echo "\n Issuing nginx reboot command...\n\n";
system('service nginx restart'); system('service nginx restart');
}else{
echo "\nNginx config files for this domain do not exist\n";
}
?> ?>
Loading…
Cancel
Save