|
#!/usr/bin/env php |
|
<?php |
|
/** |
|
* Copy this file in /usr/local/bin to use it globally |
|
* |
|
* Create project for Apache2/PHP/MySQL. |
|
* Tested with PHP 7.2 and Debian 9. |
|
|
|
* CHANGELOG (mini) |
|
* 1.9 Adding --https |
|
* 1.8 Adding --salt and check args |
|
* 1.7 Adding --lang=* |
|
* 1.6 PHP port, Removing --reload |
|
* 1.5 Adding colors |
|
* 1.4 Adding --dry-run, --auto, --project=*, --reload |
|
* 1.3 Adding --help, --version, --use-db |
|
* 1.2 Adding database creation |
|
* 1.1 Some Fixes |
|
* 1.0 First version in Bash |
|
*/ |
|
|
|
if(php_sapi_name() !== 'cli') { |
|
echo 'Command-line only :)'.PHP_EOL; |
|
exit(1); |
|
} |
|
|
|
|
|
/** |
|
* |
|
* CONFIGURATION |
|
* |
|
* Change "my-hosting.com" with your website |
|
* Change IP_HOST by your server IP |
|
* |
|
*/ |
|
define('MAIL_ADMIN', '[email protected]'); |
|
define('IP_HOST', 'XX.XX.XX.XX'); |
|
define('BASE_LOG', '/var/log/apache2/com.my-hosting.'); |
|
define('SUFFIX_SD', '.my-hosting.com'); |
|
define('PATH_WEB', '/home/web/'); |
|
define('PATH_VHOSTS', '/etc/apache2/sites-available/'); |
|
define('VERSION', '1.9'); |
|
|
|
|
|
// Template virtualhost |
|
$TPL_VHOST = <<<EOF |
|
## |
|
## Auto-generated virtualhost by RSeon's create_hosting vVERSION |
|
## Creation date : DATETIME |
|
## |
|
<VirtualHost *:80> |
|
ServerAdmin MAIL_ADMIN |
|
|
|
ServerName PROJECT_URL |
|
DocumentRoot PATH_PROJECT |
|
|
|
<Directory PATH_PROJECT> |
|
Options FollowSymLinks |
|
AllowOverride All |
|
Require all granted |
|
</Directory> |
|
|
|
ErrorLog BASE_LOGproject_name-error.log |
|
CustomLog BASE_LOGproject_name-access.log combined |
|
</Virtualhost> |
|
EOF; |
|
|
|
// Template robots.txt |
|
$TPL_ROBOTS_TXT = <<<EOF |
|
User-agent: * |
|
Disallow: / |
|
EOF; |
|
|
|
|
|
/** |
|
* |
|
* TRANSLATIONS |
|
* |
|
*/ |
|
|
|
$available_languages = [ |
|
'fr', 'en', |
|
]; |
|
|
|
$translations = [ |
|
'[O/n]' => ['en' => '[Y/n]'], |
|
' [[ MODE TEST ACTIF ]]' => ['en' => ' [[ DRY RUN ACTIVE ]]'], |
|
'Activation du site' => ['en' => 'Enabling website'], |
|
'Activation HTTPS :' => ['en' => 'Enabling HTTPS'], |
|
'Activer HTTPS' => ['en' => 'Enable HTTPS'], |
|
'Allez à l\'adresse suivante pour l\'installer :' => ['en' => 'Go to the following address to install it :'], |
|
'automatique' => ['en' => 'automatic'], |
|
'Base de donnée' => ['en' => 'Database '], |
|
'Base de donnée en attente de création' => ['en' => 'Database waiting creation'], |
|
'Ce nom sera utilisé pour le nom du dossier, le sous-domaine (%s) ainsi que le nom de la base de donnée' => ['en' => 'This name will be used for folder name, sub-domain (%s) and database name'], |
|
'Certbot doit être installé sur votre machine pour mettre en place le SSL' => ['en' => 'Certbot must be installed on your machine to set up SSL'], |
|
'Chemin des fichiers' => ['en' => 'Files path '], |
|
'Connexion à MySQL...' => ['en' => 'Connecting MySQL...'], |
|
'Création de l\'hébergement' => ['en' => 'Hosting creation'], |
|
'Création de l\'hébergement en cours' => ['en' => 'Creating hosting'], |
|
'Création de la base' => ['en' => 'Database creation'], |
|
'Création du fichier de configuration Apache' => ['en' => 'Creating Apache configuration file'], |
|
'Création du répertoire' => ['en' => 'Folder creation'], |
|
'Création du robots.txt' => ['en' => 'robots.txt creation'], |
|
'Créer l\'hébergement' => ['en' => 'Create hosting'], |
|
'Créer un utilisateur et une table' => ['en' => 'Create user and table'], |
|
'Droits pour les accès' => ['en' => 'Rights for accesses'], |
|
'Erreur : option invalide' => ['en' => 'Error : invalid option'], |
|
'Erreur : seul l\'utilisateur root peut créer un projet avec cet outil' => ['en' => 'Error : only root user can create project with this tool'], |
|
' Création d\'un espace d\'hébergement' => ['en' => ' Creating an hosting space'], |
|
'Fichiers en attente de création' => ['en' => 'Files waiting creation'], |
|
'Hébergement créé avec succès !' => ['en' => 'Hosting created successfully !'], |
|
'Host ' => ['en' => 'Host '], |
|
'IP du serveur ' => ['en' => 'Server IP '], |
|
'Le nom du projet ne peut être vide' => ['en' => 'Project name cannot be empty'], |
|
'Mot de passe ' => ['en' => 'Password '], |
|
'Nom de l\'utilisateur' => ['en' => 'User name'], |
|
'Nom du projet' => ['en' => 'Project name'], |
|
'Nom du projet ' => ['en' => 'Project name'], |
|
'Note : un grain de sel a été ajouté automatiquement (option --salt)' => ['en' => 'A salt phrase has been added (option --salt)'], |
|
'O' => ['en' => 'Y'], |
|
'Pas de base de donnée' => ['en' => 'No database'], |
|
'Pas de soucis, à bientôt !' => ['en' => 'No worries, see you soon!'], |
|
'Récapitulatif' => ['en' => 'Summary'], |
|
'Rechargement de Apache' => ['en' => 'Reloading Apache...'], |
|
'Recharger Apache' => ['en' => 'Reload Apache'], |
|
'Relancez ce script sans le paramètre --dry-run pour créer l\'hébergement.' => ['en' => 'Restart this script without the --dry-run parameter to create the hosting.'], |
|
'URL d\'accès au site' => ['en' => 'Website URL '], |
|
'Utilisateur ' => ['en' => 'User name'], |
|
'Veillez à garder précieusement ces informations !' => ['en' => 'Keep carefully these informations !'], |
|
'Vous avez visualisé les commandes effectuées, l\'hébergement n\'a pas été créé.' => ['en' => 'You have viewed the commands made, the hosting has not been created.'], |
|
'Vous devrez relancer manuellement la commande' => ['en' => 'You must will re-run command'], |
|
]; |
|
|
|
/** |
|
* |
|
* CHECK OPTIONS |
|
* |
|
*/ |
|
$available_options = [ |
|
'-n', '--dry-run', |
|
'-p', '--project', |
|
'-a', '--auto', |
|
'-d', '--use-db', |
|
'-l', '--lang', |
|
'-s', '--salt', |
|
'-v', '--version', |
|
'-h', '--help', |
|
'-c', '--ssl', |
|
]; |
|
foreach($argv as $i => $arg) { |
|
if($i === 0) { |
|
continue; |
|
} |
|
|
|
if(strpos($arg, '=') !== false) { |
|
list($arg) = explode('=', $arg); |
|
} |
|
|
|
if(!in_array($arg, $available_options)) { |
|
echo PHP_EOL; |
|
echo str_style('error', __('Erreur : option invalide') . ' ' . $arg).PHP_EOL; |
|
echo display_usage(); |
|
exit(1); |
|
} |
|
} |
|
|
|
// Check language |
|
if(($_lang = get_arg('--lang=*', '-l=*')) && !in_array($_lang, $available_languages)) { |
|
echo PHP_EOL; |
|
echo str_style('error', 'Error : invalid language : ' . $_lang).PHP_EOL; |
|
echo 'Available : ' . implode(', ', $available_languages).PHP_EOL; |
|
exit(1); |
|
} |
|
|
|
/** |
|
* |
|
* GET OPTIONS |
|
* |
|
*/ |
|
|
|
$options = [ |
|
'dry-run' => get_arg('--dry-run', '-n'), |
|
'database' => get_arg('--use-db', '-d'), |
|
'auto' => get_arg('--auto', '-a'), |
|
'force_project' => get_arg('--project=*', '-p=*'), |
|
'lang' => ($_lang = get_arg('--lang=*', '-l=*')) ? $_lang : 'fr', |
|
'url_salt' => get_arg('--salt', '-s') ? '-'.str_replace('-', '', str_replace('_', '', strtolower(generate_password(4)))) : '', |
|
'ssl' => get_arg('--ssl', '-c'), |
|
]; |
|
|
|
// Display help |
|
if(get_arg('--help', '-h')) { |
|
echo display_usage(); |
|
exit(0); |
|
} |
|
|
|
// Display version |
|
if(get_arg('--version', '-v')) { |
|
echo display_version(); |
|
exit(0); |
|
} |
|
|
|
// Check if root |
|
if(trim(shell_exec('whoami')) !== 'root') { |
|
echo str_style('error', __('Erreur : seul l\'utilisateur root peut créer un projet avec cet outil')).PHP_EOL; |
|
exit(1); |
|
} |
|
|
|
// Force project name |
|
if($options['force_project']) { |
|
$options['project'] = strtolower(str_replace(' ', '-', trim(get_arg('--project=*', '-p=*')))); |
|
} |
|
|
|
|
|
/** |
|
* |
|
* GO ! |
|
* |
|
*/ |
|
|
|
// Clear console |
|
system('clear'); |
|
|
|
// Title |
|
echo str_title('----- RSeon - Hosting v'.VERSION.' -----'); |
|
echo __(' Création d\'un espace d\'hébergement').PHP_EOL; |
|
echo display_dry_run(true); |
|
echo PHP_EOL; |
|
|
|
// Project infos |
|
|
|
// - Name |
|
echo str_block('---------- '.__('Nom du projet').' -----------'); |
|
echo str_style('green', sprintf(__('Ce nom sera utilisé pour le nom du dossier, le sous-domaine (%s) ainsi que le nom de la base de donnée'), "\e[4mprojet".SUFFIX_SD."\e[24m")).PHP_EOL; |
|
echo PHP_EOL; |
|
echo __('Nom du projet') .' : '; |
|
if(!$options['force_project']) { |
|
$options['project'] = strtolower(str_replace(' ', '-', trim(fgets(STDIN)))); |
|
} |
|
else { |
|
echo str_style('green', $options['project']).PHP_EOL; |
|
} |
|
|
|
if(trim($options['project']) === '') { |
|
echo str_style('error', __('Le nom du projet ne peut être vide')).PHP_EOL.PHP_EOL; |
|
exit(1); |
|
} |
|
$options['project_path'] = PATH_WEB.$options['project']; |
|
$options['project_url'] = $options['project'].$options['url_salt'].SUFFIX_SD; |
|
$options['vhost'] = PATH_VHOSTS.$options['project'].'.conf'; |
|
|
|
// - Path |
|
echo PHP_EOL; |
|
echo __('Chemin des fichiers'); |
|
if(!$options['force_project']) { |
|
echo ' [' . str_style('yellow', $options['project_path']) . '] : '; |
|
$new_project_path = trim(fgets(STDIN)); |
|
if($new_project_path !== '') { |
|
$options['project_path'] = $new_project_path; |
|
} |
|
} |
|
else { |
|
echo ' : ' . str_style('green', $options['project_path']).PHP_EOL; |
|
} |
|
echo PHP_EOL; |
|
echo __('URL d\'accès au site').' : ' . str_style('green', $options['project_url']).PHP_EOL; |
|
if($options['url_salt']) { |
|
echo PHP_EOL; |
|
echo str_style('yellow', __('Note : un grain de sel a été ajouté automatiquement (option --salt)')).PHP_EOL; |
|
} |
|
echo PHP_EOL; |
|
echo str_style('success', __('Fichiers en attente de création')).PHP_EOL; |
|
|
|
// Database |
|
echo str_block('---------- '.__('Base de donnée').' ----------'); |
|
echo PHP_EOL; |
|
echo __('Créer un utilisateur et une table'). ' '.__('[O/n]').' : '; |
|
|
|
if(!$options['database']) { |
|
$create_database = strtoupper(trim(fgets(STDIN))); |
|
} |
|
else { |
|
echo str_style('green', __('O')).PHP_EOL; |
|
$create_database = 'O'; |
|
} |
|
|
|
if($create_database === 'O' || $create_database === 'Y') { |
|
$options['database'] = true; |
|
$options['database_name'] = substr($options['project'], 0, 16); |
|
echo PHP_EOL; |
|
if(!$options['auto']) { |
|
echo __('Nom de l\'utilisateur').' ['. str_style('yellow', $options['project']) .'] : '; |
|
$db_user = strtolower(trim(fgets(STDIN))); |
|
if($db_user !== '') { |
|
$options['database_user'] = $db_user; |
|
} |
|
else { |
|
$options['database_user'] = $options['project']; |
|
} |
|
} |
|
else { |
|
echo __('Nom de l\'utilisateur').' : '. str_style('green', $options['project']).PHP_EOL; |
|
$options['database_user'] = $options['project']; |
|
} |
|
|
|
echo PHP_EOL; |
|
|
|
if(!$options['auto']) { |
|
echo __('Mot de passe').' ['.str_style('yellow', __('automatique')).'] : '; |
|
$db_pwd = trim(fgets(STDIN)); |
|
if($db_pwd !== '') { |
|
$options['database_pass'] = $db_pwd; |
|
} |
|
else { |
|
$options['database_pass'] = generate_password(); |
|
} |
|
} |
|
else { |
|
echo __('Mot de passe').' : '. str_style('green', __('automatique')).PHP_EOL; |
|
$options['database_pass'] = generate_password(); |
|
} |
|
|
|
echo PHP_EOL; |
|
echo str_style('success', __('Base de donnée en attente de création')).PHP_EOL; |
|
} |
|
else { |
|
$options['database'] = false; |
|
echo PHP_EOL; |
|
echo str_style('success', __('Pas de base de donnée')).PHP_EOL; |
|
} |
|
|
|
|
|
// SSL |
|
echo str_block('---------- '.__('HTTPS').' ----------'); |
|
echo PHP_EOL; |
|
echo __('Activer HTTPS').' '.__('[O/n]').' : '; |
|
if(!$options['ssl']) { |
|
$use_ssl = strtoupper(trim(fgets(STDIN))); |
|
} |
|
else { |
|
echo str_style('green', __('O')).PHP_EOL; |
|
$use_ssl = 'O'; |
|
} |
|
if($use_ssl == 'O' || $use_ssl === 'Y') { |
|
$options['ssl'] = true; |
|
|
|
// Check if Certbot exists |
|
if(!shell_exec('which certbot')) { |
|
echo PHP_EOL; |
|
echo str_style('error', __('Certbot doit être installé sur votre machine pour mettre en place le SSL')).PHP_EOL.PHP_EOL; |
|
echo __('Allez à l\'adresse suivante pour l\'installer :').PHP_EOL; |
|
echo 'https://certbot.eff.org/lets-encrypt/debianstretch-apache'.PHP_EOL; |
|
echo PHP_EOL; |
|
exit(0); |
|
} |
|
} |
|
|
|
// Hosting |
|
echo str_block('----- '.__('Création de l\'hébergement').' -----'); |
|
echo display_dry_run(true); |
|
echo PHP_EOL; |
|
if(!$options['auto']) { |
|
echo __('Créer l\'hébergement').' '.__('[O/n]').' : '; |
|
$create_host = strtoupper(trim(fgets(STDIN))); |
|
if($create_host !== 'O' && $create_host !== 'Y') { |
|
echo str_style('success', __('Pas de soucis, à bientôt !')).PHP_EOL.PHP_EOL; |
|
exit(0); |
|
} |
|
echo PHP_EOL; |
|
} |
|
echo str_style('yellow', __('Création de l\'hébergement en cours')).PHP_EOL; |
|
|
|
// - Folders |
|
echo PHP_EOL; |
|
echo __('Création du répertoire').' ' . $options['project'] .PHP_EOL; |
|
$cmd = 'mkdir(\''.$options['project_path'].'\', 0777, true);'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
eval($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
|
|
// - robots.txt |
|
echo PHP_EOL; |
|
echo __('Création du robots.txt').PHP_EOL; |
|
$cmd = 'file_put_contents(\''.$options['project_path'].'/robots.txt\', \''.addslashes($TPL_ROBOTS_TXT).'\');'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
eval($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
|
|
// - Files rights |
|
echo PHP_EOL; |
|
echo __('Droits pour les accès').PHP_EOL; |
|
$cmd = 'chown -R www-data:www-data ' . $options['project_path'].';'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
system($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
|
|
// - Virtualhost |
|
echo PHP_EOL; |
|
echo __('Création du fichier de configuration Apache').PHP_EOL; |
|
$tpl_vhost_ok = $TPL_VHOST; |
|
$tpl_vhost_ok = preg_replace('/DATETIME/', date('Y-m-d H:i:s'), $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/VERSION/', VERSION, $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/MAIL_ADMIN/', MAIL_ADMIN, $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/PROJECT_URL/', $options['project_url'], $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/PATH_PROJECT/', $options['project_path'], $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/BASE_LOG/', BASE_LOG, $tpl_vhost_ok); |
|
$tpl_vhost_ok = preg_replace('/project_name/', $options['project'], $tpl_vhost_ok); |
|
|
|
$cmd = 'file_put_contents(\''.$options['vhost'].'\', \''.addslashes($tpl_vhost_ok).'\');'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
eval($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
|
|
// - Activation |
|
echo PHP_EOL; |
|
echo __('Activation du site').PHP_EOL; |
|
$cmd = 'a2ensite '.$options['project'].'.conf;'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
system($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
|
|
// - Database |
|
if($options['database']) { |
|
echo PHP_EOL; |
|
echo __('Création de la base').PHP_EOL; |
|
|
|
$cmd = 'mysql -u root -p <<EOFMYSQL |
|
CREATE DATABASE \`'.$options['database_name'].'\`; |
|
GRANT ALL PRIVILEGES ON \`'.$options['database_name'].'\`.* TO \`'.$options['database_user'].'\`@localhost IDENTIFIED BY \''.$options['database_pass'].'\'; |
|
FLUSH PRIVILEGES; |
|
EOFMYSQL'; |
|
|
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
echo str_style('blue', __('Connexion à MySQL...')).PHP_EOL; |
|
system($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
} |
|
|
|
// - Apache |
|
echo PHP_EOL; |
|
echo __('Recharger Apache').' '.__('[O/n]').' : '; |
|
$cmd = 'systemctl reload apache2'; |
|
|
|
if(!$options['auto']) { |
|
$reload_apache = strtoupper(trim(fgets(STDIN))); |
|
} |
|
else { |
|
echo str_style('green', 'O').PHP_EOL; |
|
$reload_apache = 'O'; |
|
} |
|
|
|
if($reload_apache === 'O' || $reload_apache === 'Y') { |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
echo str_style('blue', __('Rechargement de Apache...')).PHP_EOL; |
|
system($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
} |
|
else { |
|
echo str_style('yellow', __('Vous devrez relancer manuellement la commande')) . ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
|
|
// - SSL |
|
if($options['ssl']) { |
|
echo PHP_EOL; |
|
echo __('Activation HTTPS').PHP_EOL; |
|
|
|
$cmd = 'certbot --apache'; |
|
if($options['dry-run']) { |
|
echo display_dry_run(); |
|
echo ' ' . str_style('magenta', $cmd).PHP_EOL; |
|
} |
|
else { |
|
system($cmd); |
|
echo str_style('success', 'OK').PHP_EOL; |
|
} |
|
} |
|
|
|
// Summary |
|
if($options['ssl']) { |
|
$project_url = 'https://'.$options['project_url']; |
|
} |
|
else { |
|
$project_url = 'http://'.$options['project_url']; |
|
} |
|
|
|
echo PHP_EOL; |
|
echo str_title('-- '.__('Hébergement créé avec succès !').' --', 'green'); |
|
echo display_dry_run(true); |
|
echo PHP_EOL; |
|
|
|
echo str_block('---------- '.__('Récapitulatif').' ----------'); |
|
echo str_style('yellow', __('Veillez à garder précieusement ces informations !')).PHP_EOL; |
|
echo PHP_EOL; |
|
echo ' '.__('Nom du projet ').' : ' . str_style('green', $options['project']).PHP_EOL; |
|
echo ' '.__('Chemin des fichiers').' : '. str_style('green', $options['project_path']).PHP_EOL; |
|
echo ' '.__('URL d\'accès au site').' : ' . str_style('green', $project_url).PHP_EOL; |
|
echo ' '.__('IP du serveur ').' : ' . str_style('green', IP_HOST).PHP_EOL; |
|
echo PHP_EOL; |
|
|
|
echo __('Base de donnée').PHP_EOL; |
|
echo PHP_EOL; |
|
if($options['database']) { |
|
echo ' '.__('Host ').' : ' . str_style('green', 'localhost').PHP_EOL; |
|
echo ' '.__('Base de donnée').' : ' . str_style('green', $options['database_name']).PHP_EOL; |
|
echo ' '.__('Utilisateur ').' : ' . str_style('green', $options['database_user']).PHP_EOL; |
|
echo ' '.__('Mot de passe ').' : ' . str_style('green', $options['database_pass']).PHP_EOL; |
|
} |
|
else { |
|
echo ' ' . str_style('error', __('Pas de base de donnée')).PHP_EOL; |
|
} |
|
echo PHP_EOL; |
|
|
|
if($options['dry-run']) { |
|
echo display_dry_run(true); |
|
echo PHP_EOL; |
|
echo str_style('red', __('Vous avez visualisé les commandes effectuées, l\'hébergement n\'a pas été créé.')).PHP_EOL; |
|
echo str_style('red', __('Relancez ce script sans le paramètre --dry-run pour créer l\'hébergement.')).PHP_EOL; |
|
} |
|
|
|
echo PHP_EOL; |
|
echo PHP_EOL; |
|
|
|
exit(0); |
|
|
|
|
|
|
|
/** |
|
* |
|
* FONCTIONS |
|
* |
|
*/ |
|
|
|
// Get cli arguments |
|
function get_arg($key, $alias = '') { |
|
global $argv; |
|
|
|
if(is_array($argv)) { |
|
if(in_array($key, $argv)) { |
|
return true; |
|
} |
|
if(strpos($key, '=*')) { |
|
list($key) = explode('=*', $key); |
|
|
|
foreach($argv as $arg) { |
|
if(strpos($arg, $key) !== false) { |
|
list(, $value) = explode('=', $arg); |
|
return $value; |
|
} |
|
} |
|
} |
|
} |
|
|
|
if($alias) { |
|
return get_arg($alias, false); |
|
} |
|
|
|
return false; |
|
} |
|
|
|
// Display help message |
|
function display_usage() { |
|
global $argv; |
|
|
|
$str = PHP_EOL; |
|
$str .= 'Usage: '.$argv[0].' [ --dry-run ] [ --project= ] [ --use-db ] [ --auto ] [ --ssl ]'.PHP_EOL; |
|
$str .= PHP_EOL; |
|
$str .= 'Outil permettant la création d\'un hébergement web avec Apache :'.PHP_EOL; |
|
$str .= ' - Création du chemin vers le projet (dans /home/web, ou alors défini par l\'utilisateur) avec un robots.txt par défaut'.PHP_EOL; |
|
$str .= ' - Création du virtualhost'.PHP_EOL; |
|
$str .= ' - Création d\'une base de donnée au nom du projet dont l\'utilisateur est le nom du projet (ou alors défini par l\'utilisateur) et le mot de passe est généré automatiquement (ou défini par l\'utilisateur)'.PHP_EOL; |
|
$str .= 'Le site sera accessible en sous-domaine de '.ltrim(SUFFIX_SD, '.').PHP_EOL; |
|
$str .= PHP_EOL; |
|
$str .= ' ' . str_style('green', '-n | --dry-run') . ' Mode test. Permet d\'afficher les commandes executées, mais l\'hébergement ne sera pas créé'.PHP_EOL; |
|
$str .= ' ' . str_style('green', "-p=\e[4mtest\e[24m | --project=\e[4mtest\e[24m") . ' Spécifie le nom et le chemin du projet ainsi que la base de donnée. Valide chaque étape automatiquement (pas de personnalisation possible)'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-a | --auto') . ' Permet de valider automatiquement chaque étape (pas de personnalisation possible)'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-d | --use-db') . ' Crée la base de donnée. L\'utilisateur sera le nom du projet et le mot de passe sera généré automatiquement'.PHP_EOL; |
|
$str .= ' ' . str_style('green', "-l=\e[4mfr\e[24m | --lang=\e[4mfr\e[24m") . ' Traduction (fr, en)'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-s | --salt') . ' Ajoute des caractères aléatoires à la fin de l\'url (ex : test-lqhe5'.SUFFIX_SD.')'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-v | --version') . ' Affichage de la version'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-c | --ssl') . ' Activer HTTPS sur le site'.PHP_EOL; |
|
$str .= ' ' . str_style('green', '-h | --help') . ' Affichage de ce message'.PHP_EOL; |
|
$str .= PHP_EOL; |
|
return $str; |
|
} |
|
|
|
// Display tool version |
|
function display_version() { |
|
return 'Version ' . str_style('blue', VERSION).PHP_EOL; |
|
} |
|
|
|
// Display if in dry-run |
|
function display_dry_run($block = false) { |
|
global $options; |
|
|
|
if($options['dry-run']) { |
|
if($block) { |
|
return PHP_EOL . str_style('red', __(' [[ MODE TEST ACTIF ]]')) . PHP_EOL; |
|
} |
|
|
|
return str_style('red', 'DRY-RUN').PHP_EOL; |
|
} |
|
|
|
return ''; |
|
} |
|
|
|
// Return title formatting |
|
function str_title($title, $style = null) |
|
{ |
|
$style = $style ?: 'blue'; |
|
|
|
$str = PHP_EOL; |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
$str .= str_style($style, $title.PHP_EOL); |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
return $str; |
|
} |
|
|
|
// Return string styled with color |
|
function str_style($style, $message) |
|
{ |
|
// ANSI color codes |
|
$styles = array( |
|
'reset' => "\033[0m", |
|
'red' => "\033[31m", |
|
'green' => "\033[32m", |
|
'yellow' => "\033[33m", |
|
'gray' => "\033[1;30m", |
|
'blue' => "\033[1;34m", |
|
'magenta' => "\033[1;35m", |
|
'error' => "\033[37;41m", |
|
'success' => "\033[37;42m", |
|
); |
|
$supports = has_color_support(); |
|
|
|
return ($supports ? $styles[$style] : '').$message.($supports ? $styles['reset'] : ''); |
|
} |
|
|
|
// Return block formatting |
|
function str_block($title, $style = null) |
|
{ |
|
$style = $style ?: 'gray'; |
|
|
|
$str = PHP_EOL; |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
$str .= str_style($style, $title.PHP_EOL); |
|
$str .= str_style($style, str_repeat('-', mb_strlen($title)).PHP_EOL); |
|
return $str; |
|
} |
|
|
|
// Check if terminal can show colors |
|
function has_color_support() |
|
{ |
|
static $support; |
|
|
|
if (null === $support) { |
|
if (DIRECTORY_SEPARATOR == '\\') { |
|
$support = false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'); |
|
} else { |
|
$support = function_exists('posix_isatty') && @posix_isatty(STDOUT); |
|
} |
|
} |
|
|
|
return $support; |
|
} |
|
|
|
// Generate password for database |
|
function generate_password($ln = 32) { |
|
$chars = str_split('-_abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ-_1234567890-_'); |
|
$nb_chars = count($chars); |
|
mt_srand(); |
|
|
|
$pwd = ''; |
|
for($i = 0; $i <= $ln; $i++) { |
|
$pwd .= $chars[mt_rand(1, $nb_chars) - 1]; |
|
} |
|
return $pwd; |
|
} |
|
|
|
// Translate a string |
|
function __($str) { |
|
global $translations; |
|
global $options; |
|
|
|
$lang = $options['lang']; |
|
|
|
if($lang !== 'fr') { |
|
if(isset($translations[$str][$lang])) { |
|
$str = $translations[$str][$lang]; |
|
} |
|
} |
|
|
|
return $str; |
|
} |