| 1 |
<?php
|
| 2 |
// $Id: vhosts.module,v 1.3 2008/07/27 04:09:41 boombatower Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Simple vhost management and related tools.
|
| 6 |
*
|
| 7 |
* Copyright 2008 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_menu().
|
| 12 |
*/
|
| 13 |
function vhosts_menu() {
|
| 14 |
$items = array();
|
| 15 |
$items['vhosts'] = array(
|
| 16 |
'title' => 'VHosts',
|
| 17 |
'description' => 'View all vhosts.',
|
| 18 |
'page callback' => 'vhosts_display',
|
| 19 |
'access arguments' => array('manage vhosts')
|
| 20 |
);
|
| 21 |
$items['vhosts/error_log'] = array(
|
| 22 |
'title' => 'Error log',
|
| 23 |
'description' => 'View error log.',
|
| 24 |
'page callback' => 'vhosts_display_error_log',
|
| 25 |
'access arguments' => array('manage vhosts')
|
| 26 |
);
|
| 27 |
$items['vhosts/error_log/clear'] = array(
|
| 28 |
'title' => 'Clear error log',
|
| 29 |
'page callback' => 'vhosts_clear_error_log',
|
| 30 |
'access arguments' => array('manage vhosts'),
|
| 31 |
'type' => MENU_CALLBACK
|
| 32 |
);
|
| 33 |
$items['admin/settings/vhosts'] = array(
|
| 34 |
'title' => 'VHosts',
|
| 35 |
'description' => 'Configure vhosts.',
|
| 36 |
'page callback' => 'drupal_get_form',
|
| 37 |
'page arguments' => array('vhosts_settings_form'),
|
| 38 |
'access arguments' => array('manage vhosts')
|
| 39 |
);
|
| 40 |
|
| 41 |
return $items;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implementation of hook_perm().
|
| 46 |
*/
|
| 47 |
function uts_perm() {
|
| 48 |
return array('manage vhosts');
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_theme().
|
| 53 |
*/
|
| 54 |
function vhosts_theme() {
|
| 55 |
return array(
|
| 56 |
'vhosts_vhost' => array(
|
| 57 |
'arguments' => array('version' => NULL, 'base' => NULL, 'aliases' => NULL, 'admin' => NULL, 'document_root' => NULL),
|
| 58 |
'template' => 'vhosts-vhost'
|
| 59 |
)
|
| 60 |
);
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Display list of vhosts.
|
| 65 |
*
|
| 66 |
* @return string HTML output.
|
| 67 |
*/
|
| 68 |
function vhosts_display() {
|
| 69 |
$vhosts = vhosts_parse_vhosts(TRUE);
|
| 70 |
if ($vhosts) {
|
| 71 |
$out = '';
|
| 72 |
foreach ($vhosts as $version => $vhost) {
|
| 73 |
$out .= '<p>' . t('<b>Drupal @version</b>', array('@version' => $version));
|
| 74 |
|
| 75 |
// Create vhost list.
|
| 76 |
$list = array();
|
| 77 |
$list[] = l("drupal-$version.dev.loc", "http://drupal-$version.dev.loc");
|
| 78 |
foreach ($vhost['aliases'] as $alias) {
|
| 79 |
$list[] = l($alias, "http://$alias");
|
| 80 |
}
|
| 81 |
$out .= theme('item_list', $list);
|
| 82 |
$out .= '</p>';
|
| 83 |
}
|
| 84 |
return $out;
|
| 85 |
}
|
| 86 |
return t('<p>No valid vhosts found. Make sure you have configured the <a href="!settings">settings</a> properly.</p>',
|
| 87 |
array('!settings' => url('admin/settings/vhosts')));
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* Display the error log.
|
| 92 |
*
|
| 93 |
* @return string HTML output.
|
| 94 |
*/
|
| 95 |
function vhosts_display_error_log() {
|
| 96 |
$file = variable_get('vhosts_error_log', '');
|
| 97 |
if (file_exists($file) && is_readable($file)) {
|
| 98 |
$contents = file_get_contents($file);
|
| 99 |
preg_match_all("/^[[](.*?)[]] [[](.*?)[]] [[](.*?)[]] (.*?)$/m", $contents, $matches, PREG_SET_ORDER);
|
| 100 |
|
| 101 |
$headers = array(t('Type'), t('Message'));
|
| 102 |
$rows = array();
|
| 103 |
foreach ($matches as $match) {
|
| 104 |
$row = array();
|
| 105 |
$row[] = $match[2];
|
| 106 |
$row[] = $match[4];
|
| 107 |
|
| 108 |
$rows[] = $row;
|
| 109 |
}
|
| 110 |
if (empty($rows)) {
|
| 111 |
return t('<p>Error log is empty.</p>');
|
| 112 |
}
|
| 113 |
return theme('table', $headers, $rows) . l(t('Clear log'), 'vhosts/error_log/clear');
|
| 114 |
}
|
| 115 |
return t('Unable to read log file');
|
| 116 |
}
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Clear error log file.
|
| 120 |
*/
|
| 121 |
function vhosts_clear_error_log() {
|
| 122 |
$file = variable_get('vhosts_error_log', '');
|
| 123 |
if (file_exists($file) && is_writable($file)) {
|
| 124 |
file_put_contents($file, '');
|
| 125 |
drupal_set_message(t('Error log cleared.'));
|
| 126 |
drupal_goto('vhosts/error_log');
|
| 127 |
}
|
| 128 |
drupal_set_message(t('Log file is not writable.'));
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* Settings form.
|
| 133 |
*/
|
| 134 |
function vhosts_settings_form(&$form_state) {
|
| 135 |
$form = array();
|
| 136 |
$form['apache'] = array(
|
| 137 |
'#type' => 'fieldset',
|
| 138 |
'#title' => t('Apache'),
|
| 139 |
'#description' => t('Configure Apache file locations, vhost settings.')
|
| 140 |
);
|
| 141 |
$form['apache']['vhosts'] = array(
|
| 142 |
'#type' => 'fieldset',
|
| 143 |
'#title' => t('VHosts'),
|
| 144 |
'#description' => t('Configure VHost settings.')
|
| 145 |
);
|
| 146 |
$form['apache']['vhosts']['vhosts_conf'] = array(
|
| 147 |
'#type' => 'textfield',
|
| 148 |
'#title' => t('File'),
|
| 149 |
'#description' => t('Location of writable vhosts file. Example: /etc/apache2/vhosts.d/dev.conf'),
|
| 150 |
'#default_value' => variable_get('vhosts_conf', '')
|
| 151 |
);
|
| 152 |
$form['apache']['vhosts']['vhosts_base'] = array(
|
| 153 |
'#type' => 'textfield',
|
| 154 |
'#title' => t('Base domain'),
|
| 155 |
'#description' => t('The base domain to create all the default vhosts as sub-domains of. Example: drupal-6.dev.loc.'),
|
| 156 |
'#default_value' => variable_get('vhosts_base', 'dev.loc')
|
| 157 |
);
|
| 158 |
$form['apache']['vhosts']['vhosts_document_root'] = array(
|
| 159 |
'#type' => 'textfield',
|
| 160 |
'#title' => t('Document root'),
|
| 161 |
'#description' => t('The document root in which the drupal version will be found. Drupal version must be placed
|
| 162 |
in folders following the format drupal-<i>version#</i>. Example: drupal-6.'),
|
| 163 |
'#default_value' => variable_get('vhosts_document_root', '')
|
| 164 |
);
|
| 165 |
$form['apache']['vhosts']['vhosts_aliases'] = array(
|
| 166 |
'#type' => 'textarea',
|
| 167 |
'#title' => t('Aliases'),
|
| 168 |
'#description' => t('Create aliases of Drupal versions. Use an XML like format where the Drupal version is the containing
|
| 169 |
tag and the aliases are the inner text.'),
|
| 170 |
'#default_value' => variable_get('vhosts_aliases', "<6>\nexample.loc\n</6>"),
|
| 171 |
'#rows' => 10
|
| 172 |
);
|
| 173 |
$form['apache']['vhosts']['actions'] = array(
|
| 174 |
'#type' => 'fieldset',
|
| 175 |
'#title' => t('Actions'),
|
| 176 |
'#description' => t('Parse the vhosts file and fill the aliases field the vhosts, or write
|
| 177 |
the vhosts file based on the aliases specified.')
|
| 178 |
);
|
| 179 |
$form['apache']['vhosts']['actions']['parse'] = array(
|
| 180 |
'#type' => 'submit',
|
| 181 |
'#value' => t('Parse'),
|
| 182 |
'#submit' => array('vhosts_settings_form_submit')
|
| 183 |
);
|
| 184 |
$form['apache']['vhosts']['actions']['write'] = array(
|
| 185 |
'#type' => 'submit',
|
| 186 |
'#value' => t('Write'),
|
| 187 |
'#submit' => array('vhosts_settings_form_submit')
|
| 188 |
);
|
| 189 |
|
| 190 |
$form['apache']['vhosts_error_log'] = array(
|
| 191 |
'#type' => 'textfield',
|
| 192 |
'#title' => t('Error log file'),
|
| 193 |
'#description' => t('Location of writable error log file. Example: /var/log/apache2/error_log'),
|
| 194 |
'#default_value' => variable_get('vhosts_error_log', '')
|
| 195 |
);
|
| 196 |
|
| 197 |
return system_settings_form($form);
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Handle settings actions.
|
| 202 |
*/
|
| 203 |
function vhosts_settings_form_submit($form, &$form_state) {
|
| 204 |
if ($form_state['values']['op'] == t('Parse')) {
|
| 205 |
$vhosts = vhosts_parse_vhosts();
|
| 206 |
|
| 207 |
$aliases = '';
|
| 208 |
$count = 0;
|
| 209 |
foreach ($vhosts as $version => $vhost) {
|
| 210 |
$aliases .= "<$version>\n";
|
| 211 |
foreach ($vhost['aliases'] as $alias) {
|
| 212 |
$aliases .= "$alias\n";
|
| 213 |
$count++;
|
| 214 |
}
|
| 215 |
$aliases .= "</$version>\n";
|
| 216 |
}
|
| 217 |
drupal_set_message(t('%count aliases found.', array('%count' => $count)));
|
| 218 |
variable_set('vhosts_aliases', trim($aliases));
|
| 219 |
}
|
| 220 |
else if ($form_state['values']['op'] == t('Write')) {
|
| 221 |
// Parse alias input.
|
| 222 |
preg_match_all('/<(\d+)>(.*?)<\/\1>/s', $form_state['values']['vhosts_aliases'], $matches, PREG_SET_ORDER);
|
| 223 |
$vhosts = array();
|
| 224 |
foreach ($matches as $match) {
|
| 225 |
$version = $match[1];
|
| 226 |
$aliases = explode("\n", trim($match[2]));
|
| 227 |
|
| 228 |
// Clean and check values.
|
| 229 |
foreach ($aliases as $key => $alias) {
|
| 230 |
$aliases[$key] = trim($alias);
|
| 231 |
if ($aliases[$key] == '') {
|
| 232 |
unset($aliases[$key]);
|
| 233 |
}
|
| 234 |
}
|
| 235 |
|
| 236 |
if (!array_key_exists($version, $vhosts)) {
|
| 237 |
$vhost = array();
|
| 238 |
$vhost['aliases'] = $aliases;
|
| 239 |
$vhost['document_root'] = variable_get('vhosts_document_root', '') . "/drupal-$version";
|
| 240 |
$vhosts[$version] = $vhost;
|
| 241 |
}
|
| 242 |
else {
|
| 243 |
$vhosts[$version]['aliases'] += $aliases;
|
| 244 |
}
|
| 245 |
}
|
| 246 |
if (vhosts_write_vhosts($vhosts)) {
|
| 247 |
drupal_set_message(t('VHosts written.'));
|
| 248 |
}
|
| 249 |
else {
|
| 250 |
drupal_set_message(t('Failed to write VHost.'));
|
| 251 |
}
|
| 252 |
}
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Parse virtual hosts.
|
| 257 |
*
|
| 258 |
* Array structure:
|
| 259 |
* [version#] => array(
|
| 260 |
* ['aliases'] => array(
|
| 261 |
* 'alias1.dev.loc',
|
| 262 |
* 'alias2.dev.loc',
|
| 263 |
* ),
|
| 264 |
* ['document_root'] => '/document/root'
|
| 265 |
* )
|
| 266 |
*
|
| 267 |
* @return array List of virtual hosts.
|
| 268 |
*/
|
| 269 |
function vhosts_parse_vhosts() {
|
| 270 |
$file = variable_get('vhosts_conf', '');
|
| 271 |
if (file_exists($file) && is_readable($file)) {
|
| 272 |
$contents = file_get_contents($file);
|
| 273 |
preg_match_all('/<VirtualHost.*?>(.*?)<\/VirtualHost>/s', $contents, $matches, PREG_SET_ORDER);
|
| 274 |
|
| 275 |
$vhosts = array();
|
| 276 |
foreach ($matches as $match) {
|
| 277 |
$vhost = array();
|
| 278 |
|
| 279 |
// Get Drupal version.
|
| 280 |
$name = vhosts_get_detail('ServerName', $match[1]);
|
| 281 |
if (!preg_match('/drupal-(\d+)/', $name, $version)) {
|
| 282 |
// Not Drupal VHost.
|
| 283 |
continue;
|
| 284 |
}
|
| 285 |
$version = $version[1];
|
| 286 |
|
| 287 |
// Get aliases.
|
| 288 |
$alias = vhosts_get_detail('ServerAlias', $match[1]);
|
| 289 |
if ($alias) {
|
| 290 |
$vhost['aliases'] = explode(' ', $alias);
|
| 291 |
}
|
| 292 |
else {
|
| 293 |
$vhost['aliases'] = array();
|
| 294 |
}
|
| 295 |
|
| 296 |
// Get document root.
|
| 297 |
$vhost['document_root'] = vhosts_get_detail('DocumentRoot', $match[1]);
|
| 298 |
|
| 299 |
$vhosts[$version] = $vhost;
|
| 300 |
}
|
| 301 |
return $vhosts;
|
| 302 |
}
|
| 303 |
return NULL;
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
* Get detail from vhost definition body.
|
| 308 |
*
|
| 309 |
* @param string $key detail key to get.
|
| 310 |
* @param string $body VHost definition body.
|
| 311 |
* @return string Detail value.
|
| 312 |
*/
|
| 313 |
function vhosts_get_detail($key, $body) {
|
| 314 |
if (preg_match('/' . $key . ' (.*?)$/m', $body, $matches)) {
|
| 315 |
return $matches[1];
|
| 316 |
}
|
| 317 |
return NULL;
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* Write vhosts to file.
|
| 322 |
*
|
| 323 |
* @param array $vhosts List of vhosts following format given on vhosts_parse_vhosts().
|
| 324 |
* @return boolean Success.
|
| 325 |
*/
|
| 326 |
function vhosts_write_vhosts($vhosts) {
|
| 327 |
$file = variable_get('vhosts_conf', '');
|
| 328 |
if (file_exists($file) && is_writable($file)) {
|
| 329 |
$out = '';
|
| 330 |
$admin = variable_get('site_mail', 'admin@example.com');
|
| 331 |
$base = variable_get('vhosts_base', 'dev.loc');
|
| 332 |
foreach ($vhosts as $version => $vhost) {
|
| 333 |
$out .= theme('vhosts_vhost', $version, $base, implode(' ', $vhost['aliases']), $admin, $vhost['document_root']);
|
| 334 |
}
|
| 335 |
file_put_contents($file, $out);
|
| 336 |
return TRUE;
|
| 337 |
}
|
| 338 |
return FALSE;
|
| 339 |
}
|