File: /srv/admin/htdocs/multiaddr.php
<?php
require('_util.php');
?>
<!doctype html>
<html lang="fr">
<meta charset="utf-8">
<title>Multi-address management</title>
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="<?= css_path(); ?>">
<link rel="shortcut icon" href="<?= favicon_path(); ?>">
</head>
<body>
<?php require(header_file()); ?>
<section id="main">
<h1>Multi-address management</h1>
<p>In the FROM section, select the address you want to link to the target address. In the TO section, select the target address.
<strong>Warning: The former 'htdocs' folder is just renamed into "htdocs_old", there is no deletion!</strong>
</p>
<?php
$path = '/srv/data/web/vhosts/';
if (!empty($_POST['site_from']) && !empty($_POST['site_to'])) {
$source = $path . $_POST['site_from'] .'/htdocs';
$target = $path . $_POST['site_to'] .'/htdocs/';
$target_link = '../' . $_POST['site_to'] .'/htdocs';
if ($_POST['site_from'] == $_POST['site_to']) {
echo '<p class="error"><strong>Error : \'From\' and \'To\' must not be the same</strong></p>';
}
else {
if (is_link($source)) {
unlink($source);
}
else {
rename($source, $source .'_old');
}
symlink($target_link, $source);
}
}
if (!empty($_POST['link'])) {
foreach($_POST['link'] as $vhost) {
if (is_link($path . $vhost .'/htdocs')) {
unlink($path . $vhost .'/htdocs');
if (file_exists($path . $vhost .'/htdocs_old')) {
rename($path . $vhost .'/htdocs_old', $path . $vhost .'/htdocs');
}
else {
mkdir($path . $vhost .'/htdocs');
}
}
}
}
header("Cache-Control: max-age=1");
// list vhosts
$MyDirectory = opendir($path) or die('Erreur');
while ($Entry = @readdir($MyDirectory)) {
if (is_dir($path) && $Entry != '.' && $Entry != '..' ) {
$ListDir = opendir($path . $Entry) or die('Erreur');
while ($Dir = @readdir($ListDir)) {
if (!file_exists($path . $Entry .'/htdocs')) {
mkdir($path . $Entry .'/htdocs');
}
if ($Dir == 'htdocs' && $Dir != '.' && $Dir != '..') {
if (is_link($path . $Entry .'/'. $Dir)) {
$link_list[] = $Entry;
}
else {
$dir[] = $Entry;
}
}
}
}
}
closedir($MyDirectory);
?>
<form method="post" class="column-66 left margin">
<fieldset>
<div class="columns">
<div class="column-50 left col-border">
<h2> From : </h2>
<?php
sort($dir);
foreach($dir as $site) {
echo '<p><input class="radio" type="radio" name="site_from" value="' . $site .'" id="' . $site .'"/><label for=' . $site .'>' . $site .'</label></p>';
}
?>
</div>
<div class="column-50 right">
<h2> To :</h2>
<?php
foreach($dir as $site) {
echo '<p><input class="radio" type="radio" name="site_to" value="' . $site .'" id="' . $site .'_to"/><label for=' . $site .'_to>' . $site .'</label></p>';
}
?>
</div>
</div>
<p class="clear"><input class="button" type="submit" value="Create the link"/></p>
</fieldset>
</form>
<form method="post" class="column-33 left">
<fieldset>
<h2>Current links</h2>
<?php
if (!empty($link_list)) {
foreach($link_list as $link) {
echo '<p>';
$full_path = '/srv/data/web/vhosts/'. $link .'/htdocs';
$link_path = readlink($full_path);
$pointing_to = str_replace('/htdocs', '', str_replace(array('../', '/srv/data/web/vhosts/'), '', $link_path));
echo '<input type="checkbox" name="link[]" id="'.$link.'" value="'.$link.'"/>';
echo '<label for="'. $link .'">'. $link .'</label>';
echo ' links to '. $pointing_to;
echo '</p>';
}
}
?>
<p class="clear"><input class="button" type="submit" value="Revert"/></p>
</fieldset>
</form>
</section>
<?php require(footer_file()); ?>
</body>
</html>