Created
April 20, 2012 11:17
-
-
Save JulienBreux/2427846 to your computer and use it in GitHub Desktop.
Remove website (sudo ./removeSite.php <user> <host>)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
define('SUCCESS', "\033[32m".'OK'."\033[37m - "); | |
define('WARN', "\033[31m".'INFO'."\033[37m - "); | |
define('EOL', "\n"); | |
## FUNCTIONS | |
function pecho($string) {fwrite(STDOUT, $string);} | |
## LOAD | |
if ($argc < 3) | |
die(pecho(WARN.'Le nom du site et de l\'utilisateur ne sont pas définis. ('.$argc.')'.EOL)); | |
$user = $argv[1]; | |
$site = $argv[2]; | |
if (!is_dir('/home/'.$user)) | |
die(pecho(WARN.'L\'utilisateur '.$user.' n\'existe pas.'.EOL)); | |
if (!preg_match('#^[^\.]+[a-z0-9\.]+[^\.]$#', $site)) | |
die(pecho(WARN.'Le nom du site '.$site.' n\'est pas conforme.'.EOL)); | |
define('USER', $user); | |
define('SITE', $site); | |
define('TLD', '.local'); | |
define('ACTION_DIRS', true); | |
define('ACTION_HOST', true); | |
define('ACTION_VHOST', true); | |
define('TAB', "\t"); | |
define('DS', DIRECTORY_SEPARATOR); | |
define('DIR_ETC', DS.'etc'); | |
define('DIR_HOME', DS.'home'); | |
define('DIR_APACHE', DIR_ETC.DS.'apache2'); | |
define('DIR_SITES', DIR_HOME.DS.USER.DS.'Sites'); | |
define('DIR_SITE', DIR_SITES.DS.SITE.TLD); | |
define('DIR_SITES_ENABLED', DIR_APACHE.DS.'sites-enabled'); | |
define('FILE_HOSTS', 'hosts'); | |
define('FILE_VHOST', SITE.TLD); | |
## ACTION VHOST | |
if (ACTION_VHOST) | |
{ | |
if (file_exists(DIR_SITES_ENABLED.DS.SITE.TLD)) | |
{ | |
exec('rm -Rf '.DIR_SITES_ENABLED.DS.SITE.TLD); | |
exec('/etc/init.d/apache2 restart'); | |
pecho(SUCCESS.'Suppression de l\'hôte virtuel '.SITE.TLD.EOL); | |
} | |
else | |
pecho(WARN.'L\'hôte virtuel '.SITE.TLD.' n\'existe pas!'.EOL); | |
} | |
## ACTION DIRS | |
if (ACTION_DIRS) | |
{ | |
if (is_dir(DIR_SITE)) | |
{ | |
exec('rm -Rf '.DIR_SITE); | |
pecho(SUCCESS.'Le site '.SITE.TLD.' a été supprimé du dossier '.DIR_SITES.EOL); | |
} | |
else | |
pecho(WARN.'Le site '.SITE.TLD.' n\'existe pas !'.EOL); | |
} | |
## ACTION HOST | |
if (ACTION_HOST) | |
{ | |
$lines = file(DIR_ETC.DS.FILE_HOSTS); | |
foreach ($lines as $k => $line) | |
{ | |
if (preg_match('`'.SITE.TLD.'$`', $line)) | |
{ | |
unset($lines[$k]); | |
$hostExists = true; | |
} | |
} | |
if (isset($hostExists)) | |
{ | |
file_put_contents(DIR_ETC.DS.FILE_HOSTS, implode($lines, '')); | |
pecho(SUCCESS.'Suppression du domaine '.SITE.TLD.EOL); | |
} | |
else | |
pecho(WARN.'Le domaine '.SITE.TLD.' n\'existe pas !'.EOL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment