Newer
Older
//* The vhost is written and apache has been restarted, so we
// can reset the ssl changed var to false and cleanup some files
$this->ssl_certificate_changed = false;
$ssl_dir = $data['new']['document_root'].'/ssl';
$domain = $data['new']['ssl_domain'];
$key_file = $ssl_dir.'/'.$domain.'.key.org';
$key_file2 = $ssl_dir.'/'.$domain.'.key';
$csr_file = $ssl_dir.'/'.$domain.'.csr';
$crt_file = $ssl_dir.'/'.$domain.'.crt';
$bundle_file = $ssl_dir.'/'.$domain.'.bundle';
if(@is_file($key_file.'~')) $app->system->unlink($key_file.'~');
if(@is_file($key2_file.'~')) $app->system->unlink($key2_file.'~');
if(@is_file($crt_file.'~')) $app->system->unlink($crt_file.'~');
if(@is_file($csr_file.'~')) $app->system->unlink($csr_file.'~');
if(@is_file($bundle_file.'~')) $app->system->unlink($bundle_file.'~');
// Remove the backup copy of the config file.
if(@is_file($vhost_file.'~')) $app->system->unlink($vhost_file.'~');
//* Unset action to clean it for next processed vhost.
$this->action = '';
function delete($event_name, $data) {
global $app, $conf;
// load the server configuration options
$app->uses('getconf');
$app->uses('system');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') $app->system->web_folder_protection($data['old']['document_root'], false);
tbrehm
committed
//* Check if this is a chrooted setup
if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
tbrehm
committed
$apache_chrooted = true;
} else {
$apache_chrooted = false;
}
//* Remove the mounts
$log_folder = 'log';
if($data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
$tmp = $app->db->queryOneRecord('SELECT `domain`,`document_root` FROM web_domain WHERE domain_id = ?', $data['old']['parent_domain_id']);
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
if($tmp['domain'] != ''){
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['old']['domain']);
} else {
// get log folder from /etc/fstab
/*
$bind_mounts = $app->system->file_get_contents('/etc/fstab');
$bind_mount_lines = explode("\n", $bind_mounts);
if(is_array($bind_mount_lines) && !empty($bind_mount_lines)){
foreach($bind_mount_lines as $bind_mount_line){
$bind_mount_line = preg_replace('/\s+/', ' ', $bind_mount_line);
$bind_mount_parts = explode(' ', $bind_mount_line);
if(is_array($bind_mount_parts) && !empty($bind_mount_parts)){
if($bind_mount_parts[0] == '/var/log/ispconfig/httpd/'.$data['old']['domain'] && $bind_mount_parts[2] == 'none' && strpos($bind_mount_parts[3], 'bind') !== false){
$subdomain_host = str_replace($data['old']['document_root'].'/log/', '', $bind_mount_parts[1]);
}
}
}
}
*/
// we are deleting the parent domain, so we can delete everything in the log directory
$subdomain_hosts = array();
$files = array_diff(scandir($data['old']['document_root'].'/'.$log_folder), array('.', '..'));
if(is_array($files) && !empty($files)){
foreach($files as $file){
if(is_dir($data['old']['document_root'].'/'.$log_folder.'/'.$file)){
$subdomain_hosts[] = $file;
}
}
}
}
if(is_array($subdomain_hosts) && !empty($subdomain_hosts)){
$log_folders = array();
foreach($subdomain_hosts as $subdomain_host){
$log_folders[] = $log_folder.'/'.$subdomain_host;
}
} else {
if($subdomain_host == '') $subdomain_host = 'web'.$data['old']['domain_id'];
$log_folder .= '/' . $subdomain_host;
}
$web_folder = $data['old']['web_folder'];
}
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias'){
if(is_array($log_folders) && !empty($log_folders)){
foreach($log_folders as $log_folder){
//if($app->system->is_mounted($data['old']['document_root'].'/'.$log_folder)) exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder));
//exec('fuser -km '.escapeshellarg($data['old']['document_root'].'/'.$log_folder).' 2>/dev/null');
exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder).' 2>/dev/null');
//if($app->system->is_mounted($data['old']['document_root'].'/'.$log_folder)) exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder));
//exec('fuser -km '.escapeshellarg($data['old']['document_root'].'/'.$log_folder).' 2>/dev/null');
exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder).' 2>/dev/null');
//* remove mountpoint from fstab
if(is_array($log_folders) && !empty($log_folders)){
foreach($log_folders as $log_folder){
$fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.' none bind';
$app->system->removeLine('/etc/fstab', $fstab_line);
}
} else {
$fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.' none bind';
$app->system->removeLine('/etc/fstab', $fstab_line);
if($data['old']['type'] != 'vhost' && $data['old']['type'] != 'vhostsubdomain' && $data['old']['type'] != 'vhostalias' && $data['old']['parent_domain_id'] > 0) {
//* This is a alias domain or subdomain, so we have to update the website instead
$parent_domain_id = intval($data['old']['parent_domain_id']);
$tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ? AND active = 'y'", $parent_domain_id);
$data['new'] = $tmp;
$data['old'] = $tmp;
$this->action = 'update';
// just run the update function
$this->update($event_name, $data);
} else {
//* This is a website
// Deleting the vhost file, symlink and the data directory
$vhost_file = escapeshellcmd($web_config['vhost_conf_dir'].'/'.$data['old']['domain'].'.vhost');
$vhost_symlink = escapeshellcmd($web_config['vhost_conf_enabled_dir'].'/'.$data['old']['domain'].'.vhost');
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);
}
$vhost_symlink = escapeshellcmd($web_config['vhost_conf_enabled_dir'].'/900-'.$data['old']['domain'].'.vhost');
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);
}
$vhost_symlink = escapeshellcmd($web_config['vhost_conf_enabled_dir'].'/100-'.$data['old']['domain'].'.vhost');
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);
$app->system->unlink($vhost_file);
$app->log('Removing vhost file: '.$vhost_file, LOGLEVEL_DEBUG);
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
$docroot = escapeshellcmd($data['old']['document_root']);
if($docroot != '' && !stristr($docroot, '..')) {
if($data['old']['type'] == 'vhost') {
// this is a vhost - we delete everything in here.
exec('rm -rf '.$docroot);
} elseif(!stristr($data['old']['web_folder'], '..')) {
// this is a vhost subdomain
// IMPORTANT: do some folder checks before we delete this!
$do_delete = true;
$delete_folder = preg_replace('/[\/]{2,}/', '/', $web_folder); // replace / occuring multiple times
if(substr($delete_folder, 0, 1) === '/') $delete_folder = substr($delete_folder, 1);
if(substr($delete_folder, -1) === '/') $delete_folder = substr($delete_folder, 0, -1);
$path_elements = explode('/', $delete_folder);
if($path_elements[0] == 'web' || $path_elements[0] === '') {
// paths beginning with /web should NEVER EVER be deleted, empty paths should NEVER occur - but for safety reasons we check it here!
// we use strict check as otherwise directories named '0' may not be deleted
$do_delete = false;
} else {
// read all vhost subdomains and alias with same parent domain
$tmp = $app->db->queryAllRecords("SELECT `web_folder` FROM web_domain WHERE (type = 'vhostsubdomain' OR type = 'vhostalias') AND parent_domain_id = ? AND domain_id != ?", $data['old']['parent_domain_id'], $data['old']['domain_id']);
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
foreach($tmp as $tmprec) {
// we normalize the folder entries because we need to compare them
$tmp_folder = preg_replace('/[\/]{2,}/', '/', $tmprec['web_folder']); // replace / occuring multiple times
if(substr($tmp_folder, 0, 1) === '/') $tmp_folder = substr($tmp_folder, 1);
if(substr($tmp_folder, -1) === '/') $tmp_folder = substr($tmp_folder, 0, -1);
// add this path and it's parent paths to used_paths array
while(strpos($tmp_folder, '/') !== false) {
if(in_array($tmp_folder, $used_paths) == false) $used_paths[] = $tmp_folder;
$tmp_folder = substr($tmp_folder, 0, strrpos($tmp_folder, '/'));
}
if(in_array($tmp_folder, $used_paths) == false) $used_paths[] = $tmp_folder;
}
unset($tmp);
// loop and check if the path is still used and stop at first used one
// set do_delete to false so nothing gets deleted if the web_folder itself is still used
$do_delete = false;
while(count($path_elements) > 0) {
$tmp_folder = implode('/', $path_elements);
if(in_array($tmp_folder, $used_paths) == true) break;
// this path is not used - set it as path to delete, strip the last element from the array and set do_delete to true
$delete_folder = $tmp_folder;
$do_delete = true;
array_pop($path_elements);
}
unset($tmp_folder);
unset($used_paths);
}
if($do_delete === true && $delete_folder !== '') exec('rm -rf '.$docroot.'/'.$delete_folder);
unset($delete_folder);
unset($path_elements);
}
}
//remove the php fastgi starter script if available
if ($data['old']['php'] == 'fast-cgi') {
$fastcgi_starter_path = str_replace('[system_user]', $data['old']['system_user'], $fastcgi_config['fastcgi_starter_path']);
if($data['old']['type'] == 'vhost') {
if (is_dir($fastcgi_starter_path)) {
exec('rm -rf '.$fastcgi_starter_path);
}
} else {
$fcgi_starter_script = $fastcgi_starter_path.$fastcgi_config['fastcgi_starter_script'].'_web'.$data['old']['domain_id'];
if (file_exists($fcgi_starter_script)) {
exec('rm -f '.$fcgi_starter_script);
}
}
}
// remove PHP-FPM pool
if ($data['old']['php'] == 'php-fpm') {
$this->php_fpm_pool_delete($data, $web_config);
} elseif($data['old']['php'] == 'hhvm') {
$this->hhvm_update($data, $web_config);
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
}
//remove the php cgi starter script if available
if ($data['old']['php'] == 'cgi') {
// TODO: fetch the date from the server-settings
$web_config['cgi_starter_path'] = $web_config['website_basedir'].'/php-cgi-scripts/[system_user]/';
$cgi_starter_path = str_replace('[system_user]', $data['old']['system_user'], $web_config['cgi_starter_path']);
if($data['old']['type'] == 'vhost') {
if (is_dir($cgi_starter_path)) {
exec('rm -rf '.$cgi_starter_path);
}
} else {
$cgi_starter_script = $cgi_starter_path.'php-cgi-starter_web'.$data['old']['domain_id'];
if (file_exists($cgi_starter_script)) {
exec('rm -f '.$cgi_starter_script);
}
}
}
$app->log('Removing website: '.$docroot, LOGLEVEL_DEBUG);
// Delete the symlinks for the sites
$client = $app->db->queryOneRecord('SELECT client_id FROM sys_group WHERE sys_group.groupid = ?', $data['old']['sys_groupid']);
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
$client_id = intval($client['client_id']);
unset($client);
$tmp_symlinks_array = explode(':', $web_config['website_symlinks']);
if(is_array($tmp_symlinks_array)) {
foreach($tmp_symlinks_array as $tmp_symlink) {
$tmp_symlink = str_replace('[client_id]', $client_id, $tmp_symlink);
$tmp_symlink = str_replace('[website_domain]', $data['old']['domain'], $tmp_symlink);
// Remove trailing slash
if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
// delete the symlink
if(is_link($tmp_symlink)) {
$app->system->unlink($tmp_symlink);
$app->log('Removing symlink: '.$tmp_symlink, LOGLEVEL_DEBUG);
}
}
}
// end removing symlinks
}
// Delete the log file directory
$vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data['old']['domain']);
if($data['old']['domain'] != '' && !stristr($vhost_logfile_dir, '..')) exec('rm -rf '.$vhost_logfile_dir);
$app->log('Removing website logfile directory: '.$vhost_logfile_dir, LOGLEVEL_DEBUG);
if($data['old']['type'] == 'vhost') {
//delete the web user
$command = 'killall -u '.escapeshellcmd($data['old']['system_user']).' ; userdel';
$command .= ' '.escapeshellcmd($data['old']['system_user']);
exec($command);
if($apache_chrooted) $this->_exec('chroot '.escapeshellcmd($web_config['website_basedir']).' '.$command);
}
//* Remove the awstats configuration file
if($data['old']['stats_type'] == 'awstats') {
$this->awstats_delete($data, $web_config);
}
if($data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
$app->system->web_folder_protection($parent_web_document_root, true);
}
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
//* Delete the web-backups
if($data['old']['type'] == 'vhost') {
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
$backup_dir = $server_config['backup_dir'];
$mount_backup = true;
Florian Schaal
committed
if($server_config['backup_dir'] != '' && $server_config['backup_delete'] == 'y') {
//* mount backup directory, if necessary
if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
if($mount_backup){
$web_backup_dir = $backup_dir.'/web'.$data_old['domain_id'];
//** do not use rm -rf $web_backup_dir because database(s) may exits
exec(escapeshellcmd('rm -f '.$web_backup_dir.'/web'.$data_old['domain_id'].'_').'*');
//* cleanup database
$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename LIKE ?";
$app->db->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
$app->log('Deleted the web backup files', LOGLEVEL_DEBUG);
if($data['old']['type'] != 'vhost') $app->system->web_folder_protection($data['old']['document_root'], true);
//* This function is called when a IP on the server is inserted, updated or deleted
function server_ip($event_name, $data) {
global $app, $conf;
// load the server configuration options
$app->uses('getconf');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$app->load('tpl');
$tpl = new tpl();
$tpl->newTemplate('apache_ispconfig.conf.master');
$tpl->setVar('apache_version', $app->system->getapacheversion());
$records = $app->db->queryAllRecords("SELECT * FROM server_ip WHERE server_id = ? AND virtualhost = 'y'", $conf['server_id']);
$records_out= array();
if(is_array($records)) {
foreach($records as $rec) {
if($rec['ip_type'] == 'IPv6') {
$ip_address = '['.$rec['ip_address'].']';
} else {
$ip_address = $rec['ip_address'];
}
$ports = explode(',', $rec['virtualhost_port']);
if(is_array($ports)) {
foreach($ports as $port) {
$port = intval($port);
if($port > 0 && $port < 65536 && $ip_address != '') {
$records_out[] = array('ip_address' => $ip_address, 'port' => $port);
}
}
}
}
}
if(count($records_out) > 0) {
$tpl->setLoop('ip_adresses', $records_out);
$vhost_file = escapeshellcmd($web_config['vhost_conf_dir'].'/ispconfig.conf');
$app->system->file_put_contents($vhost_file, $tpl->grab());
$app->log('Writing the conf file: '.$vhost_file, LOGLEVEL_DEBUG);
unset($tpl);
//* Create or update the .htaccess folder protection
function web_folder_user($event_name, $data) {
if($event_name == 'web_folder_user_delete') {
$folder_id = $data['old']['web_folder_id'];
} else {
$folder_id = $data['new']['web_folder_id'];
}
$folder = $app->db->queryOneRecord("SELECT * FROM web_folder WHERE web_folder_id = ?", $folder_id);
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $folder['parent_domain_id']);
if(!is_array($folder) or !is_array($website)) {
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
if(substr($folder['path'], -1) == '/') $folder['path'] = substr($folder['path'], 0, -1);
$folder_path = escapeshellcmd($website['document_root'].'/' . $web_folder . '/'.$folder['path']);
if(substr($folder_path, -1) != '/') $folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(stristr($folder_path, '..') || stristr($folder_path, './') || stristr($folder_path, '\\')) {
$app->log('Folder path "'.$folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
//* Create the folder path, if it does not exist
Dominik
committed
$app->system->mkdirpath($folder_path, 0755, $website['system_user'], $website['system_group']);
//* Create empty .htpasswd file, if it does not exist
if(!is_file($folder_path.'.htpasswd')) {
$app->system->touch($folder_path.'.htpasswd');
$app->system->chmod($folder_path.'.htpasswd', 0751);
$app->system->chown($folder_path.'.htpasswd', $website['system_user']);
$app->system->chgrp($folder_path.'.htpasswd', $website['system_group']);
$app->log('Created file '.$folder_path.'.htpasswd', LOGLEVEL_DEBUG);
if(($data['new']['username'] != $data['old']['username'] || $data['new']['active'] == 'n') && $data['old']['username'] != '') {
$app->system->removeLine($folder_path.'.htpasswd', $data['old']['username'].':');
$app->log('Removed user: '.$data['old']['username'], LOGLEVEL_DEBUG);
//* Add or remove the user from .htpasswd file
if($event_name == 'web_folder_user_delete') {
$app->system->removeLine($folder_path.'.htpasswd', $data['old']['username'].':');
$app->log('Removed user: '.$data['old']['username'], LOGLEVEL_DEBUG);
$app->system->replaceLine($folder_path.'.htpasswd', $data['new']['username'].':', $data['new']['username'].':'.$data['new']['password'], 0, 1);
$app->log('Added or updated user: '.$data['new']['username'], LOGLEVEL_DEBUG);
//if(!is_file($folder_path.'.htaccess')) {
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
$ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
if(file_exists($folder_path.'.htaccess')) {
$old_content = $app->system->file_get_contents($folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
$ht_file = str_replace($matches[0], $ht_file, $old_content);
} else {
$ht_file .= $old_content;
}
}
unset($old_content);
$app->system->file_put_contents($folder_path.'.htaccess', $ht_file);
$app->system->chmod($folder_path.'.htaccess', 0751);
$app->system->chown($folder_path.'.htaccess', $website['system_user']);
$app->system->chgrp($folder_path.'.htaccess', $website['system_group']);
$app->log('Created/modified file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
//* Remove .htaccess and .htpasswd file, when folder protection is removed
function web_folder_delete($event_name, $data) {
$folder_id = $data['old']['web_folder_id'];
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $folder['parent_domain_id']);
if(!is_array($folder) or !is_array($website)) {
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
if(substr($folder['path'], -1) == '/') $folder['path'] = substr($folder['path'], 0, -1);
$folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$folder['path']);
if(substr($folder_path, -1) != '/') $folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(substr($folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('Folder path is outside of docroot.', LOGLEVEL_DEBUG);
//* Remove .htpasswd file
if(is_file($folder_path.'.htpasswd')) {
$app->system->unlink($folder_path.'.htpasswd');
$app->log('Removed file '.$folder_path.'.htpasswd', LOGLEVEL_DEBUG);
//* Remove .htaccess file
if(is_file($folder_path.'.htaccess')) {
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
$ht_file = $app->system->file_get_contents($folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($folder_path.'.htaccess');
$app->log('Removed file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
//* Update folder protection, when path has been changed
function web_folder_update($event_name, $data) {
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $data['new']['parent_domain_id']);
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($data['old']['path'], 0, 1) == '/') $data['old']['path'] = substr($data['old']['path'], 1);
if(substr($data['old']['path'], -1) == '/') $data['old']['path'] = substr($data['old']['path'], 0, -1);
$old_folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$data['old']['path']);
if(substr($old_folder_path, -1) != '/') $old_folder_path .= '/';
if(substr($data['new']['path'], 0, 1) == '/') $data['new']['path'] = substr($data['new']['path'], 1);
if(substr($data['new']['path'], -1) == '/') $data['new']['path'] = substr($data['new']['path'], 0, -1);
$new_folder_path = escapeshellcmd($website['document_root'].'/' . $web_folder . '/'.$data['new']['path']);
if(substr($new_folder_path, -1) != '/') $new_folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(stristr($new_folder_path, '..') || stristr($new_folder_path, './') || stristr($new_folder_path, '\\')) {
$app->log('Folder path "'.$new_folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
if(stristr($old_folder_path, '..') || stristr($old_folder_path, './') || stristr($old_folder_path, '\\')) {
$app->log('Folder path "'.$old_folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
//* Check if the resulting path is inside the docroot
if(substr($old_folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('Old folder path '.$old_folder_path.' is outside of docroot.', LOGLEVEL_DEBUG);
if(substr($new_folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('New folder path '.$new_folder_path.' is outside of docroot.', LOGLEVEL_DEBUG);
if(!is_dir($new_folder_path)) $app->system->mkdirpath($new_folder_path);
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
if($data['old']['path'] != $data['new']['path']) {
//* move .htpasswd file
if(is_file($old_folder_path.'.htpasswd')) {
$app->system->rename($old_folder_path.'.htpasswd', $new_folder_path.'.htpasswd');
$app->log('Moved file '.$old_folder_path.'.htpasswd to '.$new_folder_path.'.htpasswd', LOGLEVEL_DEBUG);
//* delete old .htaccess file
$ht_file = $app->system->file_get_contents($old_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$old_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($old_folder_path.'.htaccess');
$app->log('Removed file '.$old_folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($old_folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$old_folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
}
}
if($data['new']['active'] == 'y') {
$ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
if(file_exists($new_folder_path.'.htaccess')) {
$old_content = $app->system->file_get_contents($new_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
$ht_file = str_replace($matches[0], $ht_file, $old_content);
} else {
$ht_file .= $old_content;
}
}
$app->system->file_put_contents($new_folder_path.'.htaccess', $ht_file);
$app->system->chmod($new_folder_path.'.htaccess', 0751);
$app->system->chown($new_folder_path.'.htaccess', $website['system_user']);
$app->system->chgrp($new_folder_path.'.htaccess', $website['system_group']);
$app->log('Created/modified file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
//* Create empty .htpasswd file, if it does not exist
if(!is_file($folder_path.'.htpasswd')) {
$app->system->touch($new_folder_path.'.htpasswd');
$app->system->chmod($new_folder_path.'.htpasswd', 0751);
$app->system->chown($new_folder_path.'.htpasswd', $website['system_user']);
$app->system->chgrp($new_folder_path.'.htpasswd', $website['system_group']);
$app->log('Created file '.$new_folder_path.'.htpasswd', LOGLEVEL_DEBUG);
}
//* Remove .htaccess file
if($data['new']['active'] == 'n' && is_file($new_folder_path.'.htaccess')) {
$ht_file = $app->system->file_get_contents($new_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($new_folder_path.'.htaccess');
$app->log('Removed file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($new_folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
}
public function ftp_user_delete($event_name, $data) {
$ftpquota_file = $data['old']['dir'].'/.ftpquota';
if(file_exists($ftpquota_file)) $app->system->unlink($ftpquota_file);
/**
* This function is called when a Webdav-User is inserted, updated or deleted.
*
* @author Oliver Vogel
* @param string $event_name
* @param array $data
*/
public function webdav($event_name, $data) {
/*
* load the server configuration options
*/
$app->uses('getconf');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
if (($event_name == 'webdav_user_insert') || ($event_name == 'webdav_user_update')) {
/*
* Get additional informations
*/
$sitedata = $app->db->queryOneRecord('SELECT document_root, domain, system_user, system_group FROM web_domain WHERE domain_id = ?', $data['new']['parent_domain_id']);
$documentRoot = $sitedata['document_root'];
$domain = $sitedata['domain'];
$user = $sitedata['system_user'];
$group = $sitedata['system_group'];
$webdav_user_dir = $documentRoot . '/webdav/' . $data['new']['dir'];
/* Check if this is a chrooted setup */
if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
$apache_chrooted = true;
$app->log('Info: Apache is chrooted.', LOGLEVEL_DEBUG);
} else {
$apache_chrooted = false;
}
//* We dont want to have relative paths here
if(stristr($webdav_user_dir, '..') || stristr($webdav_user_dir, './')) {
$app->log('Folder path '.$webdav_user_dir.' contains ./ or .. '.$documentRoot, LOGLEVEL_WARN);
//* Check if the resulting path exists if yes, if it is inside the docroot
if(is_dir($webdav_user_dir) && substr(realpath($webdav_user_dir), 0, strlen($documentRoot)) != $documentRoot) {
$app->log('Folder path '.$webdav_user_dir.' is outside of docroot '.$documentRoot, LOGLEVEL_WARN);
/*
* First the webdav-root - folder has to exist
*/
if(!is_dir($webdav_user_dir)) {
$app->log('Webdav User directory '.$webdav_user_dir.' does not exist. Creating it now.', LOGLEVEL_DEBUG);
$app->system->mkdirpath($webdav_user_dir);
* The webdav - Root needs the group/user as owner and the apache as read and write
//$this->_exec('chown ' . $user . ':' . $group . ' ' . escapeshellcmd($documentRoot . '/webdav/'));
//$this->_exec('chmod 770 ' . escapeshellcmd($documentRoot . '/webdav/'));
$app->system->chown($documentRoot . '/webdav', $user);
$app->system->chgrp($documentRoot . '/webdav', $group);
$app->system->chmod($documentRoot . '/webdav', 0770);
* The webdav folder (not the webdav-root!) needs the same (not in ONE step, because the
* pwd-files are owned by root)
//$this->_exec('chown ' . $user . ':' . $group . ' ' . escapeshellcmd($webdav_user_dir.' -R'));
//$this->_exec('chmod 770 ' . escapeshellcmd($webdav_user_dir.' -R'));
$app->system->chown($webdav_user_dir, $user);
$app->system->chgrp($webdav_user_dir, $group);
$app->system->chmod($webdav_user_dir, 0770);
/*
* if the user is active, we have to write/update the password - file
* if the user is inactive, we have to inactivate the user by removing the user from the file
*/
if ($data['new']['active'] == 'y') {
$this->_writeHtDigestFile( $webdav_user_dir . '.htdigest', $data['new']['username'], $data['new']['dir'], $data['new']['password']);
}
else {
/* empty pwd removes the user! */
$this->_writeHtDigestFile( $webdav_user_dir . '.htdigest', $data['new']['username'], $data['new']['dir'], '');
/*
* Next step, patch the vhost - file
*/
$vhost_file = escapeshellcmd($web_config['vhost_conf_dir'] . '/' . $domain . '.vhost');
$this->_patchVhostWebdav($vhost_file, $documentRoot . '/webdav');
/*
* Last, restart apache
*/
if($apache_chrooted) {
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
}
}
if ($event_name == 'webdav_user_delete') {
/*
* Get additional informations
*/
$sitedata = $app->db->queryOneRecord('SELECT document_root, domain FROM web_domain WHERE domain_id = ?', $data['old']['parent_domain_id']);
$documentRoot = $sitedata['document_root'];
/*
* We dont't want to destroy any (transfer)-Data. So we do NOT delete any dir.
* So the only thing, we have to do, is to delete the user from the password-file
$this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['old']['dir'] . '.htdigest', $data['old']['username'], $data['old']['dir'], '');
/*
* Next step, patch the vhost - file
*/
$vhost_file = escapeshellcmd($web_config['vhost_conf_dir'] . '/' . $domain . '.vhost');
$this->_patchVhostWebdav($vhost_file, $documentRoot . '/webdav');
/*
* Last, restart apache
*/
if($apache_chrooted) {
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
}
}
/**
* This function writes the htdigest - files used by webdav and digest
* more info: see http://riceball.com/d/node/424
* @author Oliver Vogel
* @param string $filename The name of the digest-file
* @param string $username The name of the webdav-user
* @param string $authname The name of the realm
* @param string $pwd The password-hash of the user
private function _writeHtDigestFile($filename, $username, $authname, $pwdhash ) {
if(is_file($filename) && !is_link($filename)) {
$in = fopen($filename, 'r');
$output = '';
/*
* read line by line and search for the username and authname
*/
while (preg_match("/:/", $line = fgets($in))) {
$line = rtrim($line);
$tmp = explode(':', $line);
if ($tmp[0] == $username && $tmp[1] == $authname) {
/*
* found the user. delete or change it?
*/
if ($pwdhash != '') {
$output .= $tmp[0] . ':' . $tmp[1] . ':' . $pwdhash . "\n";
$changed = true;
}
else {
$output .= $line . "\n";
}
fclose($in);
}
/*
* if we didn't change anything, we have to add the new user at the end of the file
*/
if (!$changed) {
$output .= $username . ':' . $authname . ':' . $pwdhash . "\n";
/*
* Now lets write the new file
*/
$app->system->unlink($filename);
$app->system->file_put_contents($filename, $output);
}
/**
* This function patches the vhost-file and adds all webdav - user.
* This function is written, because the creation of the vhost - file is sophisticated and
* i don't want to make it more "heavy" by also adding this code too...
* @author Oliver Vogel
* @param string $fileName The Name of the .vhost-File (path included)
* @param string $webdavRoot The root of the webdav-folder
*/
private function _patchVhostWebdav($fileName, $webdavRoot) {
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
$in = fopen($fileName, 'r');
$output = '';
$inWebdavSection = false;
/*
* read line by line and search for the username and authname
*/
while ($line = fgets($in)) {
/*
* is the "replace-comment" found...
*/
if (trim($line) == '# WEBDAV BEGIN') {
/*
* The begin of the webdav - section is found, so ignore all lines til the end is found
*/
$inWebdavSection = true;
$output .= " # WEBDAV BEGIN\n";
/*
* add all the webdav-dirs to the webdav-section
*/
tbrehm
committed
$files = @scandir($webdavRoot);
if(is_array($files)) {
foreach($files as $file) {
if (substr($file, strlen($file) - strlen('.htdigest')) == '.htdigest' && preg_match("/^[a-zA-Z0-9\-_\.]*$/", $file)) {
/*
* found a htdigest - file, so add it to webdav
*/
$fn = substr($file, 0, strlen($file) - strlen('.htdigest'));
$output .= "\n";
// $output .= " Alias /" . $fn . ' ' . $webdavRoot . '/' . $fn . "\n";
// $output .= " <Location /" . $fn . ">\n";
$output .= " Alias /webdav/" . $fn . ' ' . $webdavRoot . '/' . $fn . "\n";
$output .= " <Location /webdav/" . $fn . ">\n";
$output .= " DAV On\n";
$output .= ' BrowserMatch "MSIE" AuthDigestEnableQueryStringHack=On'."\n";
$output .= " AuthType Digest\n";
$output .= " AuthName \"" . $fn . "\"\n";
$output .= " AuthUserFile " . $webdavRoot . '/' . $file . "\n";
$output .= " Require valid-user \n";
$output .= " Options +Indexes \n";
$output .= " Order allow,deny \n";
$output .= " Allow from all \n";
$output .= " </Location> \n";
}
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
}
}
}
/*
* is the "replace-comment-end" found...
*/
if (trim($line) == '# WEBDAV END') {
/*
* The end of the webdav - section is found, so stop ignoring
*/
$inWebdavSection = false;
}
/*
* Write the line to the output, if it is not in the section
*/
if (!$inWebdavSection) {
$output .= $line;
}
}
fclose($in);
/*
* Now lets write the new file
*/
$app->system->file_put_contents($fileName, $output);
//* Update the awstats configuration file
private function awstats_update ($data, $web_config) {
$web_folder = $data['new']['web_folder'];
if($data['new']['type'] == 'vhost') $web_folder = 'web';
$awstats_conf_dir = $web_config['awstats_conf_dir'];
if(!is_dir($data['new']['document_root']."/" . $web_folder . "/stats/")) mkdir($data['new']['document_root']."/" . $web_folder . "/stats");
if(!@is_file($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf') || ($data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain'])) {
if ( @is_file($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf') ) {
$app->system->unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');
if (is_file($awstats_conf_dir."/awstats.conf")) {
$include_file = $awstats_conf_dir."/awstats.conf";
} elseif (is_file($awstats_conf_dir."/awstats.model.conf")) {
$include_file = $awstats_conf_dir."/awstats.model.conf";
}
$content .= "LogFile=\"/var/log/ispconfig/httpd/".$data['new']['domain']."/access.log\"\n";
$content .= "SiteDomain=\"".$data['new']['domain']."\"\n";
$content .= "HostAliases=\"www.".$data['new']['domain']." localhost 127.0.0.1\"\n";
if (isset($include_file)) {
$app->system->file_put_contents($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', $content);
$app->log('Created AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', LOGLEVEL_DEBUG);
} else {
$app->log("No awstats base config found. Either awstats.conf or awstats.model.conf must exist in ".$awstats_conf_dir.".", LOGLEVEL_WARN);
}
if(is_file($data['new']['document_root']."/" . $web_folder . "/stats/index.html")) $app->system->unlink($data['new']['document_root']."/" . $web_folder . "/stats/index.html");
if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
$app->system->copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master", $data['new']['document_root']."/" . $web_folder . "/stats/index.php");
$app->system->copy("/usr/local/ispconfig/server/conf/awstats_index.php.master", $data['new']['document_root']."/" . $web_folder . "/stats/index.php");
//* Delete the awstats configuration file
private function awstats_delete ($data, $web_config) {
$awstats_conf_dir = $web_config['awstats_conf_dir'];
if ( @is_file($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf') ) {
$app->system->unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');