Skip to content

Commit

Permalink
Upgrade core
Browse files Browse the repository at this point in the history
- Cleanup source code.
- Refactoring the source code.
- Add more features.
- Extend the installation method.
  • Loading branch information
JackieDo committed Dec 28, 2019
1 parent 0020c81 commit 3b23ef9
Show file tree
Hide file tree
Showing 15 changed files with 878 additions and 565 deletions.
57 changes: 51 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Therefore, this project was born, in order to strengthen Xampp, helping users ta
* List all existing virtual hosts.
* Add SSL certificate to an existing virtual host.
* Remove SSL certificate of an existing virtual host.
* Stop Xampp Apache Httpd
* Start Xampp Apache Httpd
* Restart Xampp Apache Httpd.

# Overview
Expand All @@ -30,6 +32,8 @@ Look at one of the following topics to learn more about Xampp vHosts Manager
* [Compatibility](#compatibility)
* [Requirement](#requirement)
* [Installation](#installation)
- [Via Composer Create-Project](#via-composer-create-project)
- [Via Manual Download](#via-manual-download)
* [Usage](#usage)
- [Display the help message](#display-the-help-message)
- [Create new virtual host](#create-new-virtual-host)
Expand All @@ -38,7 +42,10 @@ Look at one of the following topics to learn more about Xampp vHosts Manager
- [Remove an existing virtual host](#remove-an-existing-virtual-host)
- [Add SSL certificate to an existing virtual host](#add-ssl-certificate-to-an-existing-virtual-host)
- [Remove SSL certificate of an existing virtual host](#remove-ssl-certificate-of-an-existing-virtual-host)
- [Stop Apache Httpd](#stop-apache-httpd)
- [Start Apache Httpd](#start-apache-httpd)
- [Restart Apache Httpd](#restart-apache-httpd)
- [Register path of application](#register-path-of-application)
* [Configuration](#configuration)
* [License](#license)
* [Thanks from author](#thanks-for-use)
Expand All @@ -47,18 +54,30 @@ Look at one of the following topics to learn more about Xampp vHosts Manager
Xampp vHosts Manager is compatible with all Xampp versions using PHP 5.4 or higher.

## Requirement
Xampp vHosts Manager takes full advantage of what's included in Xampp, nothing more needed. So, you just need two following things:
Xampp vHosts Manager takes full advantage of what's included in Xampp, nothing more needed. So, you just need following things:

* Xampp installed (of course).
* Added the path to PHP directory of Xampp to system environment variables.
* Installed Xampp (of course).
* Added the path to PHP directory of Xampp into Windows Path Environment Variable.
* (Optional) Installed Composer.

> _Note: See [here](https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/) to know how to add Windows path environment variable._
> _Note: See [here](https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/) to know how to add Windows Path Environment Variable._
## Installation
There are two installation methods, via Composer or manual download. It is recommended to use the method via Composer if you already have it installed.

#### Via Composer Create-Project
* Open a terminal.
* Navigate to the directory you want to install Xampp vHosts Manager into.
* Run composer create-project command:
```
$ composer create-project jackiedo/xampp-vhosts-manager xvhm "1.*"
```

#### Via Manual Download
* Download the [latest release](https://github.com/JackieDo/Xampp-vHosts-Manager/releases/latest)
* Extract the archive to a shared location `(example: D:\vhostsManager)`. Note: Should not place in `C:\Program Files` or anywhere else that would require Administrator access for modifying configuration files.
* Extract the archive to a shared location `(example: D:\xvhm)`. Note: Should not place in `C:\Program Files` or anywhere else that would require Administrator access for modifying configuration files.
* Open a terminal in Administrator mode `(run as Administrator)`.
* Navigate to the directory you have placed Xampp vHosts Manager `(example: cd /D D:\vhostsManager)`.
* Navigate to the directory you have placed Xampp vHosts Manager `(example: cd /D D:\xvhm)`.
* Execute the command `xvhosts install` and follow the required steps.
* Exit terminal.

Expand Down Expand Up @@ -143,13 +162,39 @@ Example:
$ xvhosts remove_ssl demo.local
```

#### Stop Apache Httpd

Syntax:
```
$ xvhosts stop_apache
```

#### Start Apache Httpd

Syntax:
```
$ xvhosts start_apache
```

#### Restart Apache Httpd

Syntax:
```
$ xvhosts restart_apache
```

#### Register path of application
This feature allows you to register the path to the application directory of Xampp vHosts Manager into the Windows Path Environment Variable. Usually, you rarely need this. It is only useful when you need to change the application directory name of Xampp vHosts Manager or move it to another location.

To do this, after you have changed the directory, navigate to the new location of application directory in the command prompt and run the following command:

Syntax:
```
$ xvhosts register_path
```

> Note: You need to accept this feature to be implemented in Administrator mode.
## Configuration
All onfiguration are put in an ini file with name `settings.ini` located in Xampp vHosts Manager application directory. The structure of this file looks like this:

Expand Down
18 changes: 0 additions & 18 deletions apppath_register.vbs

This file was deleted.

1 change: 0 additions & 1 deletion cacert_generate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ cls
setlocal EnableExtensions

:: Check necessary data
if not "%XVHM_APP_STARTED%"=="true" goto missing
if "%XVHM_APP_DIR%"=="" goto missing
if "%XVHM_TMP_DIR%"=="" goto missing
if "%XVHM_CACERT_DIR%"=="" goto missing
Expand Down
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "jackiedo/xampp-vhosts-manager",
"description": "Virtual hosts (SSL) management system (in console mode) for Xampp on Windows OS.",
"keywords": [
"xampp",
"virtual-hosts",
"vhosts",
"ssl-certificates",
"ssl-cert",
"ssl",
"domain",
"manager",
"console"
],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.4"
},
"scripts": {
"post-create-project-cmd": [
"xvhosts.bat install"
]
},
"config": {
"sort-packages": true,
"optimize-autoloader": true
}
}
26 changes: 14 additions & 12 deletions help.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ Virtual hosts manager for Xampp on Windows OS
Usage:
xvhosts <command> [HOST_NAME]


Available commands:
install Intergrate Xampp vHosts Manager into Xampp
(require running in Administrator mode).
new Create a new virtual host.
show Display information of specific virtual host.
remove Remove specific virtual host.
add_ssl Add SSL certificate to specific virtual host.
remove_ssl Remove SSL certificate of specific virtual host.
list List all existing virtual hosts.
restart_apache Restart Apache httpd.
help Display the help message.

install Intergrate Xampp vHosts Manager into Xampp
(require running in Administrator mode).
new Create a new virtual host.
show Display information of specific virtual host.
remove Remove specific virtual host.
add_ssl Add SSL certificate to specific virtual host.
remove_ssl Remove SSL certificate of specific virtual host.
list List all existing virtual hosts.
stop_apache Stop Apache httpd.
start_apache Start Apache httpd.
restart_apache Stop Apache httpd and start it again.
register_path Register path to Xampp vHosts Manager directory
into Windows Path Environment Variable.
help Display the help message.

Examples:
xvhosts new demo.local
Expand Down
145 changes: 145 additions & 0 deletions support/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

define('DS', DIRECTORY_SEPARATOR);

require_once __DIR__.'/Console.php';
require_once __DIR__.'/Setting.php';

class Application
{
protected $setting = null;
protected $paths = array();

public function __construct($detectXamppPaths = true)
{
Console::setDefaultMessages(array('terminate' => 'Xampp vHosts Manager is terminating...'));

$this->setting = new Setting;

$this->loadAppPaths();

if ($detectXamppPaths) {
$this->detectXamppPaths();
}
}

private function loadAppPaths()
{
$appDir = realpath(getenv('XVHM_APP_DIR'));

$this->paths['appDir'] = $appDir;
$this->paths['phpDir'] = dirname(PHP_BINARY);
$this->paths['tmpDir'] = $appDir . '\tmp';
$this->paths['caCertDir'] = $appDir . '\cacert';
$this->paths['caCertGenScript'] = $appDir . '\cacert_generate.bat';
$this->paths['caCertGenConfig'] = $appDir . '\cacert_generate.cnf';
$this->paths['vhostConfigTemplate'] = $appDir . '\templates\vhost_config\vhost.conf.tpl';
$this->paths['vhostSSLConfigTemplate'] = $appDir . '\templates\vhost_config\vhost_ssl.conf.tpl';
$this->paths['vhostCertGenScript'] = $appDir . '\vhostcert_generate.bat';
$this->paths['vhostCertGenConfig'] = $appDir . '\vhostcert_generate.cnf';
$this->paths['pathRegister'] = $appDir . '\support\PathRegister.vbs';
$this->paths['powerExecutor'] = $appDir . '\support\PowerExec.vbs';
$this->paths['winHostsFile'] = realpath($_SERVER['SystemRoot'] . '\System32\drivers\etc\hosts');

// Set environment variables
putenv('XVHM_TMP_DIR=' . $this->paths['tmpDir']);
putenv('XVHM_CACERT_DIR=' . $this->paths['caCertDir']);
putenv('XVHM_CACERT_GENERATE_CONFIG=' . $this->paths['caCertGenConfig']);
putenv('XVHM_VHOST_CERT_GENERATE_CONFIG=' . $this->paths['vhostCertGenConfig']);
}

protected function detectXamppPaths()
{
// Force reload settings
$this->setting->reloadSettings();

$xamppDir = realpath($this->setting->get('DirectoryPaths', 'Xampp'));
$apacheDir = $this->setting->get('DirectoryPaths', 'Apache');

if (! $xamppDir || ! is_file($xamppDir . '\xampp-control.exe')) {
Console::breakline();

$message = 'Cannot find Xampp directory.' . PHP_EOL;
$message .= 'Please check the configuration path to the Xampp directory in file "' . $this->paths['appDir'] . '\settings.ini".';

Console::terminate($message, 1);
}

$this->paths['xamppDir'] = $xamppDir;

if (! $apacheDir) {
$apacheDir = $xamppDir . '\apache';
}

if (! is_file($apacheDir . '\bin\httpd.exe')) {
Console::breakline();

$message = 'Cannot find Apache directory.' . PHP_EOL;
$message .= 'Please check the configuration path to the Apache directory in file "' . $this->paths['appDir'] . '\settings.ini".';

Console::terminate($message, 1);
}

$this->paths['apacheDir'] = $apacheDir;

return $this->loadAdditionalPaths();
}

protected function loadAdditionalPaths()
{
if (! array_key_exists('apacheDir', $this->paths)) {
Console::breakline();
Console::terminate('The identification of the path to Apache directory has not yet been conducted', 1);
}

$this->paths['vhostCertDir'] = $this->paths['apacheDir'] . '\conf\extra\certs';
$this->paths['vhostCertKeyDir'] = $this->paths['apacheDir'] . '\conf\extra\keys';
$this->paths['vhostConfigDir'] = $this->paths['apacheDir'] . '\conf\extra\vhosts';
$this->paths['vhostSSLConfigDir'] = $this->paths['apacheDir'] . '\conf\extra\vhosts_ssl';
$this->paths['opensslBin'] = $this->paths['apacheDir'] . '\bin\openssl.exe';

// Set environment variables
putenv('XVHM_VHOST_CERT_DIR=' . $this->paths['vhostCertDir']);
putenv('XVHM_VHOST_CERT_KEY_DIR=' . $this->paths['vhostCertKeyDir']);

putenv('XVHM_OPENSSL_BIN=' . $this->paths['opensslBin']);
putenv('XVHM_OPENSSL_SUBJECT_CN=Xampp Certificate Authority');
putenv('XVHM_OPENSSL_SUBJECT_O=OpenSSL Software Foundation');
putenv('XVHM_OPENSSL_SUBJECT_OU=Server Certificate Provider');

return $this;
}

protected function reduceApachePath($path, $directorySeparator = DS)
{
$apachePath = str_replace('/', DS, $this->paths['apacheDir']);
$path = str_replace('/', DS, $path);

if (substr($path, 0, strlen($apachePath)) == $apachePath) {
$path = substr($path, strlen($apachePath . DS));
}

return str_replace(DS, $directorySeparator, $path);
}

protected function powerExec($command, $arguments = null, &$outputValue = null, &$returnCode = null)
{
if (is_file($this->paths['powerExecutor'])) {
if (is_array($arguments)) {
$arguments = '"' . trim(implode('" "', $arguments)) . '"';
} elseif (is_string($arguments)) {
$arguments = trim($arguments);
} else {
$arguments = trim(strval($arguments));
}

return exec('cscript //NoLogo "' . $this->paths['powerExecutor'] . '" ' . $arguments . ' ' . $command, $outputValue, $returnCode);
}

$message = 'Cannot find the "' . $this->paths['powerExecutor'] . '" implementer.';
$outputValue = array($message);
$returnCode = 1;

return $message;
}
}
18 changes: 10 additions & 8 deletions support/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static function ask($message, $defaultValue = null)
return self::getSimpleConsole()->askInput($message, $defaultValue);
}

public static function line($message = null, $breakLine = true)
public static function line($message = null, $breakLine = true, $beginAtColumn = 0)
{
self::getSimpleConsole()->showMessage($message, $breakLine);
self::getSimpleConsole()->showMessage($message, $breakLine, $beginAtColumn);
}

public static function breakline($multiplier = 1)
Expand All @@ -35,9 +35,9 @@ public static function hrline($width = 83, $symbol = '-')
self::getSimpleConsole()->showMessage(str_repeat($symbol, $width));
}

public static function terminate($message = null)
public static function terminate($message = null, $exitStatus = 0)
{
self::getSimpleConsole()->terminate($message);
self::getSimpleConsole()->terminate($message, $exitStatus);
}

private static function getSimpleConsole()
Expand Down Expand Up @@ -97,23 +97,25 @@ public function askInput($message, $defaultValue = null)
return $answer;
}

public function showMessage($message = null, $breakLine = true)
public function showMessage($message = null, $breakLine = true, $beginAtColumn = 0)
{
echo $message;
$spaceBefore = ($beginAtColumn > 0) ? str_repeat(' ', $beginAtColumn) : '';

echo $spaceBefore . $message;

if ($breakLine) {
echo PHP_EOL;
}
}

public function terminate($message = null)
public function terminate($message = null, $exitStatus = 0)
{
if (! is_null($message)) {
$message .= PHP_EOL;
}

$this->showMessage($message . $this->defaultMessages['terminate']);
exit;
exit($exitStatus);
}

private function getInputFromKeyboard($defaultValue = null)
Expand Down
Loading

0 comments on commit 3b23ef9

Please sign in to comment.