Created
June 23, 2016 16:38
-
-
Save tperalta82/285d61fed458bb7d3d0be1a53560d84d to your computer and use it in GitHub Desktop.
Apache Vhost Split
This file contains hidden or 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
<?php | |
/** | |
* Description of index | |
* @copyright Copyright (c) 2016 FRK Agency. All rights reserved | |
* @author Tiago Peralta <[email protected]> | |
*/ | |
if (!isset($argv[1]) || !is_file($argv[1])) { | |
die("Please specify an input file, preferably one that exists. e.g. php split.php vhosts.conf exportdir(optional) "); | |
} | |
if (isset($argv[2]) && is_dir($argv[2])) { | |
$dir = realpath($argv[2]).DIRECTORY_SEPARATOR; | |
} else { | |
echo "No Export dir specified, or non existent one, using default".PHP_EOL; | |
$dir = getcwd().DIRECTORY_SEPARATOR."vhosts".DIRECTORY_SEPARATOR; | |
if (!is_dir($dir)) { | |
mkdir($dir); | |
} | |
} | |
$handle = fopen($argv[1], "r"); | |
$vhstart = "<VirtualHost"; | |
$vhend = "</VirtualHost"; | |
$sname = "ServerName"; | |
$vhost = null; | |
$filename = null; | |
while (( $line = \fgets($handle) ) !== false) { | |
$line = trim($line); | |
if ($line != "") { | |
$vhost .= $line.PHP_EOL; | |
if (stristr($line, $sname)) { | |
$servername = explode(" ", $line); | |
$filename = $servername[1].".conf"; | |
} elseif (stristr($line, $vhend)) { | |
file_put_contents($dir.$filename, $vhost); | |
$vhost = null; | |
} | |
} | |
} | |
fclose($handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment