modules/vhosts/include/vhosts.php
72518278
 <?php
 
 require_once("inc/base.php");
 require_once("inc/error.php");
 require_once("inc/security.php");
 
 require_once('class/domain.php');
 
709c9ea2
 
72518278
 function list_vhosts()
 {
   $uid = (int) $_SESSION['userinfo']['uid'];
832d06da
   $result = db_query("SELECT vh.id,fqdn,docroot,docroot_is_default,php,vh.options,logtype,errorlog,IF(dav.id IS NULL OR dav.type='svn', 0, 1) AS is_dav,IF(dav.id IS NULL OR dav.type='dav', 0, 1) AS is_svn, IF(webapps.id IS NULL, 0, 1) AS is_webapp FROM vhosts.v_vhost AS vh LEFT JOIN vhosts.dav ON (dav.vhost=vh.id) LEFT JOIN vhosts.webapps ON (webapps.vhost = vh.id) WHERE uid={$uid} ORDER BY domain,hostname");
72518278
   $ret = array();
   while ($item = mysql_fetch_assoc($result))
     array_push($ret, $item);
   return $ret;
 }
 
 
8d92bc6d
 function empty_vhost()
 {
   $vhost['hostname'] = '';
   
f1d119a9
   $vhost['domain_id'] = -1;
   $vhost['domain'] = $_SESSION['userinfo']['username'].'.schokokeks.org';
8d92bc6d
   
   $vhost['homedir'] = $_SESSION['userinfo']['homedir'];
   $vhost['docroot'] = NULL;
8e3ab1b2
   $vhost['php'] = 'fastcgi';
fc35e698
   $vhost['ssl'] = NULL;
8d92bc6d
   $vhost['logtype'] = NULL;
7fb0aeba
   $vhost['is_dav'] = 0;
   $vhost['is_svn'] = 0;
   $vhost['is_webapp'] = 0;
3c2fddca
   $vhsot['webapp_id'] = NULL;
8d92bc6d
     
   $vhost['options'] = '';
   return $vhost;
 }
 
 
709c9ea2
 function empty_alias()
 {
   $alias['hostname'] = '';
   
f1d119a9
   $alias['domain_id'] = -1;
   $alias['domain'] = $_SESSION['userinfo']['username'].'.schokokeks.org';
709c9ea2
   
   $alias['options'] = '';
   return $alias;
 }
 
 
8d92bc6d
 function domainselect($selected = NULL, $selectattribute = '')
72518278
 {
   global $domainlist;
   if ($domainlist == NULL)
     $domainlist = get_domain_list($_SESSION['customerinfo']['customerno'],
                                   $_SESSION['userinfo']['uid']);
   $selected = (int) $selected;
 
8d92bc6d
   $ret = '<select id="domain" name="domain" size="1" '.$selectattribute.' >';
f1d119a9
   $ret .= ' <option value="-1">'.$_SESSION['userinfo']['username'].'.schokokeks.org</option>';
   $ret .= ' <option value="" disabled="disabled">--------------------------------</option>';
72518278
   foreach ($domainlist as $dom)
   {
709c9ea2
     $s = ($selected == $dom->id) ? ' selected="selected" ': '';
72518278
     $ret .= "<option value=\"{$dom->id}\"{$s}>{$dom->fqdn}</option>\n";
   }
   $ret .= '</select>';
   return $ret;
 }
 
 
 
 function get_vhost_details($id)
 {
   $id = (int) $id;
   $uid = (int) $_SESSION['userinfo']['uid'];
832d06da
   $result = db_query("SELECT vh.*,IF(dav.id IS NULL OR dav.type='svn', 0, 1) AS is_dav,IF(dav.id IS NULL OR dav.type='dav', 0, 1) AS is_svn, IF(webapps.id IS NULL, 0, 1) AS is_webapp FROM vhosts.v_vhost AS vh LEFT JOIN vhosts.dav ON (dav.vhost=vh.id) LEFT JOIN vhosts.webapps ON (webapps.vhost = vh.id) WHERE uid={$uid} AND vh.id={$id}");
72518278
   if (mysql_num_rows($result) != 1)
     system_failure('Interner Fehler beim Auslesen der Daten');
 
   return mysql_fetch_assoc($result);
 }
 
 
 function get_aliases($vhost)
 {
   $result = db_query("SELECT id,fqdn,options FROM vhosts.v_alias WHERE vhost={$vhost}");
   $ret = array();
709c9ea2
   while ($item = mysql_fetch_assoc($result)) {
72518278
     array_push($ret, $item);
709c9ea2
   }
   return $ret;
 }
 
 
 
 function get_all_aliases($vhost)
 {
   $vhost = get_vhost_details( (int) $vhost );
   $aliases = get_aliases($vhost['id']);
   $ret = array();
   if (strstr($vhost['options'], 'aliaswww')) {
     array_push($ret, array('id' => 'www', 'fqdn' => 'www.'.$vhost['fqdn'], 'options' => (strstr($vhost['options'], 'forwardwww') ? 'forward' : '')));
   }
   foreach ($aliases as $item) {
     array_push($ret, $item);
     if (strstr($item['options'], 'aliaswww')) {
       array_push($ret, array('id' => 'www_'.$item['id'], 'fqdn' => 'www.'.$item['fqdn'], 'options' => (strstr($item['options'], 'forward') ? 'forward' : '')));
     }
   }
72518278
   return $ret;
 }
 
709c9ea2
 
3c2fddca
 function list_available_webapps()
 {
   $result = db_query("SELECT id,displayname FROM vhosts.global_webapps");
   $ret = array();
   while ($item = mysql_fetch_assoc($result))
     array_push($ret, $item);
   return $ret;
 }
 
 
8d92bc6d
 function delete_vhost($id)
 {
   $id = (int) $id;
   if ($id == 0)
     system_failure("id == 0");
   $vhost = get_vhost_details($id);
63a0529b
   logger('modules/vhosts/include/vhosts', 'vhosts', 'Removing vhost #'.$id.' ('.$vhost['hostname'].'.'.$vhost['domain'].')');
8d92bc6d
   db_query("DELETE FROM vhosts.vhost WHERE id={$vhost['id']} LIMIT 1");
 }
 
709c9ea2
 
7fb0aeba
 
 function make_svn_vhost($id) 
 {
   $id = (int) $id;
   if ($id == 0)
     system_failure("id == 0");
63a0529b
   logger('modules/vhosts/include/vhosts', 'vhosts', 'Converting vhost #'.$id.' to SVN');
4715fde4
   db_query("REPLACE INTO vhosts.dav (vhost, type) VALUES ({$id}, 'svn')");
3c2fddca
   db_query("DELETE FROM vhosts.webapps WHERE vhost={$id}");
7fb0aeba
 }
 
 function make_dav_vhost($id) 
 {
   $id = (int) $id;
   if ($id == 0)
     system_failure("id == 0");
63a0529b
   logger('modules/vhosts/include/vhosts', 'vhosts', 'Converting vhost #'.$id.' to WebDAV');
f0f3361f
   db_query("REPLACE INTO vhosts.dav (vhost, type, options) VALUES ({$id}, 'dav', 'nouserfile')");
3c2fddca
   db_query("DELETE FROM vhosts.webapps WHERE vhost={$id}");
7fb0aeba
 }
 
3c2fddca
 function make_regular_vhost($id)
7fb0aeba
 {
   $id = (int) $id;
   if ($id == 0)
     system_failure("id == 0");
63a0529b
   logger('modules/vhosts/include/vhosts', 'vhosts', 'Converting vhost #'.$id.' to regular');
7fb0aeba
   db_query("DELETE FROM vhosts.dav WHERE vhost={$id}");
3c2fddca
   db_query("DELETE FROM vhosts.webapps WHERE vhost={$id}");
7fb0aeba
 }
 
 
 function make_webapp_vhost($id, $webapp) 
 {
   $id = (int) $id;
   $webapp = (int) $webapp;
   if ($id == 0)
     system_failure("id == 0");
3c2fddca
   $result = db_query("SELECT displayname FROM vhosts.global_webapps WHERE id={$webapp};");
   if (mysql_num_rows($result) == 0)
     system_failure("webapp-id invalid");
   $webapp_name = mysql_fetch_object($result)->displayname;
63a0529b
   logger('modules/vhosts/include/vhosts', 'vhosts', 'Setting up webapp '.$webapp_name.' on vhost #'.$id);
3c2fddca
   db_query("REPLACE INTO vhosts.webapps (vhost, webapp) VALUES ({$id}, {$webapp})");
   mail('webapps-setup@schokokeks.org', 'setup', 'setup');
7fb0aeba
 }
 
 
8d92bc6d
 function save_vhost($vhost)
 {
   if (! is_array($vhost))
     system_failure('$vhost kein array!');
   $id = (int) $vhost['id'];
   $hostname = maybe_null($vhost['hostname']);
   $domain = (int) $vhost['domainid'];
   if ($domain == 0)
709c9ea2
     system_failure('$domain == 0');
f1d119a9
   if ($vhost['domainid'] == -1)
     $domain = 'NULL';
8d92bc6d
   $docroot = maybe_null($vhost['docroot']);
   $php = maybe_null($vhost['php']);
fc35e698
   $ssl = maybe_null($vhost['ssl']);
8d92bc6d
   $logtype = maybe_null($vhost['logtype']);
2b282fad
   $errorlog = (int) $vhost['errorlog'];
125ebada
   if ($vhost['is_svn']) {
389f593a
     if (! $vhost['options']) $vhost['options']='nodocroot';
     else $vhost['options']+=",nodocroot";
   }
8d92bc6d
   $options = mysql_real_escape_string( $vhost['options'] );
 
84aaa608
   if ($id != 0) {
63a0529b
     logger('modules/vhosts/include/vhosts', 'vhosts', 'Updating vhost #'.$id.' ('.$vhost['hostname'].'.'.$vhost['domain'].')');
2b282fad
     db_query("UPDATE vhosts.vhost SET hostname={$hostname}, domain={$domain}, docroot={$docroot}, php={$php}, `ssl`={$ssl}, logtype={$logtype}, errorlog={$errorlog}, options='{$options}' WHERE id={$id} LIMIT 1");
84aaa608
   }
   else {
63a0529b
     logger('modules/vhosts/include/vhosts', 'vhosts', 'Creating vhost '.$vhost['hostname'].'.'.$vhost['domain'].'');
7fb0aeba
     $result = db_query("INSERT INTO vhosts.vhost (user, hostname, domain, docroot, php, `ssl`, logtype, errorlog, options) VALUES ({$_SESSION['userinfo']['uid']}, {$hostname}, {$domain}, {$docroot}, {$php}, {$ssl}, {$logtype}, {$errorlog}, '{$options}')");
     $id = mysql_insert_id();
84aaa608
   }
3c2fddca
   $oldvhost = get_vhost_details($id);
   /*
     these vars may be 0 or 1.
     So newval > oldval means that it has been switched on yet.
   */
   if ($vhost['is_dav'] > $oldvhost['is_dav'])
7fb0aeba
       make_dav_vhost($id);
3c2fddca
   elseif ($vhost['is_svn'] > $oldvhost['is_svn'])
7fb0aeba
       make_svn_vhost($id);
3c2fddca
   elseif ($vhost['is_webapp'] > $oldvhost['is_webapp'])
       make_webapp_vhost($id, $vhost['webapp_id']);
   elseif ($vhost['is_dav'] == 0 && $vhost['is_svn'] == 0 && $vhost['is_webapp'] == 0)
       make_regular_vhost($id);
8d92bc6d
 }
 
 
709c9ea2
 function get_alias_details($id)
 {
   $id = (int) $id;
   $uid = (int) $_SESSION['userinfo']['uid'];
   $result = db_query("SELECT * FROM vhosts.v_alias WHERE id={$id}");
   
   if (mysql_num_rows($result) != 1)
     system_failure('Interner Fehler beim Auslesen der Alias-Daten');
   
   $alias = mysql_fetch_assoc($result);
f1d119a9
   
7acabcf1
   if ($alias['domain_id'] == NULL) {
f1d119a9
     $alias['domain_id'] = -1;
7acabcf1
   }
709c9ea2
 
   /* Das bewirkt, dass nur die eigenen Aliase gesehen werden k├Ânnen */
   get_vhost_details( (int) $alias['vhost'] );
 
   return $alias;
 }
 
 
 function delete_alias($id)
 {
   $id = (int) $id;
   $alias = get_alias_details($id);
 
63a0529b
   logger('modules/vhosts/include/vhosts', 'aliases', 'Removing alias #'.$id.' ('.$alias['hostname'].'.'.$alias['domain'].')');
709c9ea2
   db_query("DELETE FROM vhosts.alias WHERE id={$id}");
 }
 
 function save_alias($alias)
 {
   if (! is_array($alias))
     system_failure('$alias kein array!');
   $id = (int) $alias['id'];
   $hostname = maybe_null($alias['hostname']);
   $domain = (int) $alias['domainid'];
   if ($domain == 0)
     system_failure('$domain == 0');
f1d119a9
   if ($alias['domainid'] == -1)
     $domain = 'NULL';
709c9ea2
   $vhost = get_vhost_details( (int) $alias['vhost']);
   $options = mysql_real_escape_string( $alias['options'] );
   if ($id == 0) {
63a0529b
     logger('modules/vhosts/include/vhosts', 'aliases', 'Creating alias '.$alias['hostname'].'.'.$alias['domain'].' for VHost '.$vhost['id']);
709c9ea2
     db_query("INSERT INTO vhosts.alias (hostname, domain, vhost, options) VALUES ({$hostname}, {$domain}, {$vhost['id']}, '{$options}')");
   }
   else {
63a0529b
     logger('modules/vhosts/include/vhosts', 'aliases', 'Updating alias #'.$id.' ('.$alias['hostname'].'.'.$alias['domain'].')');
709c9ea2
     db_query("UPDATE vhosts.alias SET hostname={$hostname}, domain={$domain}, options='{$options}' WHERE id={$id} LIMIT 1");
   }
 }
 
72518278
 
 
 ?>