Marius Cramer
2014-01-23 ccdf702f14e6b15449a1fa93fb4be815e592028e
server/plugins-available/apache2_plugin.inc.php
@@ -88,6 +88,95 @@
      $app->plugins->registerEvent('ftp_user_delete', $this->plugin_name, 'ftp_user_delete');
      $app->plugins->registerAction('php_ini_changed', $this->plugin_name, 'php_ini_changed');
   }
   // check for php.ini changes
   // Handle php.ini changes
   function php_ini_changed($event_name, $data) {
      global $app, $conf;
      $app->uses('getconf');
      $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
      $fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
      /* $data contains an array with these keys:
         * file -> full path of changed php_ini
         * mode -> web_domain php modes to change (mod, fast-cgi, php-fpm or '' for all except 'mod')
         * php_version -> php ini path that changed (additional php versions)
         */
      $qrystr = "SELECT * FROM web_domain WHERE custom_php_ini != ''";
      if($data['mode'] == 'mod') {
         $qrystr .= " AND php = 'mod'";
      } elseif($data['mode'] == 'fast-cgi') {
         $qrystr .= " AND php = 'fast-cgi'";
         if($data['php_version']) {
            $qrystr .= " AND fastcgi_php_version LIKE '%:" . $app->db->quote($data['php_version']) . "'";
         }
      } elseif($data['mode'] == 'php-fpm') {
         $qrystr .= " AND php = 'php-fpm'";
         if($data['php_version']) {
            $qrystr .= " AND fastcgi_php_version LIKE '%:" . $app->db->quote($data['php_version']) . ":%'";
         }
      } else {
         $qrystr .= " AND php != 'mod' AND php != 'fast-cgi'";
      }
      //** Get all the webs
      $web_domains = $app->db->queryAllRecords($qrystr);
      foreach($web_domains as $web_data) {
         $custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$web_data['system_user'];
         $web_folder = 'web';
         if($web_data['type'] == 'vhostsubdomain') {
            $web_folder = $web_data['web_folder'];
            $custom_php_ini_dir .= '_' . $web_folder;
         }
         if(!is_dir($web_config['website_basedir'].'/conf')) $app->system->mkdir($web_config['website_basedir'].'/conf');
         if(!is_dir($custom_php_ini_dir)) $app->system->mkdir($custom_php_ini_dir);
         $php_ini_content = '';
         if($web_data['php'] == 'mod') {
            $master_php_ini_path = $web_config['php_ini_path_apache'];
         } else {
            if($web_data['php'] == 'fast-cgi' && file_exists($fastcgi_config["fastcgi_phpini_path"])) {
               $master_php_ini_path = $fastcgi_config["fastcgi_phpini_path"];
            } else {
               $master_php_ini_path = $web_config['php_ini_path_cgi'];
            }
         }
         if($master_php_ini_path != '' && substr($master_php_ini_path, -7) == 'php.ini' && is_file($master_php_ini_path)) {
            $php_ini_content .= $app->system->file_get_contents($master_php_ini_path)."\n";
         }
         $php_ini_content .= str_replace("\r", '', trim($web_data['custom_php_ini']));
         $app->system->file_put_contents($custom_php_ini_dir.'/php.ini', $php_ini_content);
         $app->log('Info: rewrote custom php.ini for web ' . $web_data['domain_id'] . ' (' . $web_data['domain'] . ').', LOGLEVEL_DEBUG);
      }
      if(count($web_domains) > 0) {
         //* We do not check the apache config here - we only changed the php.ini
         //* 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;
         }
         $app->log('Info: rewrote all php.ini and reloading apache now.', LOGLEVEL_DEBUG);
         if($apache_chrooted) {
            $app->services->restartServiceDelayed('httpd', 'restart');
         } else {
            // request a httpd reload when all records have been processed
            $app->services->restartServiceDelayed('httpd', 'reload');
         }
      } else {
         $app->log('Info: No webs affected by php.ini change.', LOGLEVEL_DEBUG);
      }
   }
   // Handle the creation of SSL certificates
@@ -930,6 +1019,7 @@
      }
      $tpl->setVar($vhost_data);
      $tpl->setVar('apache_version', $app->system->getapacheversion());
      // Rewrite rules
      $rewrite_rules = array();
@@ -1136,6 +1226,7 @@
         $fcgi_tpl = new tpl();
         $fcgi_tpl->newTemplate('php-fcgi-starter.master');
         $fcgi_tpl->setVar('apache_version', $app->system->getapacheversion());
         // Support for multiple PHP versions (FastCGI)
         if(trim($data['new']['fastcgi_php_version']) != ''){
@@ -1275,6 +1366,7 @@
         $cgi_tpl = new tpl();
         $cgi_tpl->newTemplate('php-cgi-starter.master');
         $cgi_tpl->setVar('apache_version', $app->system->getapacheversion());
         // This works because PHP "rewrites" a symlink to the physical path
         $php_open_basedir = ($data['new']['php_open_basedir'] == '')?$data['new']['document_root']:$data['new']['php_open_basedir'];
@@ -1346,9 +1438,11 @@
      //* Add vhost for IPv6 IP
      if($data['new']['ipv6_address'] != '') {
         if ($conf['serverconfig']['web']['vhost_rewrite_v6'] == 'y') {
            if (isset($conf['serverconfig']['server']['v6_prefix']) && $conf['serverconfig']['server']['v6_prefix'] <> '') {
               $explode_v6prefix=explode(':', $conf['serverconfig']['server']['v6_prefix']);
         //* rewrite ipv6 on mirrors
         /* chang $conf to $web_config */
         if ($web_config['serverconfig']['web']['vhost_rewrite_v6'] == 'y') {
            if (isset($web_config['serverconfig']['server']['v6_prefix']) && $web_config['serverconfig']['server']['v6_prefix'] <> '') {
               $explode_v6prefix=explode(':', $web_config['serverconfig']['server']['v6_prefix']);
               $explode_v6=explode(':', $data['new']['ipv6_address']);
               for ( $i = 0; $i <= count($explode_v6prefix)-3; $i++ ) {
@@ -1893,6 +1987,34 @@
            $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 directory, if necessary
            $mount_backup = true;
            $server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
            if($server_config['backup_dir'] != '' && $server_config['backup_delete'] == 'y') {
               if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
                  if(!$app->system->is_mounted($backup_dir)){
                     exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
                     sleep(1);
                     if(!$app->system->is_mounted($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 = ".$conf['server_id']." AND parent_domain_id = ".$data_old['domain_id']." AND filename LIKE 'web".$data_old['domain_id']."_%'";
                  $app->db->query($sql);
                  if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
                  $app->log('Deleted the web backup files', LOGLEVEL_DEBUG);
               }
            }
         }
      }
      if($data['old']['type'] != 'vhost') $app->system->web_folder_protection($data['old']['document_root'], true);
   }
@@ -1909,6 +2031,7 @@
      $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 = '.$conf['server_id']." AND virtualhost = 'y'");
      $records_out= array();
@@ -2535,13 +2658,22 @@
         }
         $content = '';
         $content .= "Include \"".$awstats_conf_dir."/awstats.conf\"\n";
         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 .= "Include \"".$include_file."\"\n";
         $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";
         $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);
         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");
@@ -2609,7 +2741,8 @@
      $app->load('tpl');
      $tpl = new tpl();
      $tpl->newTemplate('php_fpm_pool.conf.master');
      $tpl->setVar('apache_version', $app->system->getapacheversion());
      if($data['new']['php_fpm_use_socket'] == 'y'){
         $use_tcp = 0;
         $use_socket = 1;
@@ -2628,6 +2761,7 @@
      $tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id'] - 1);
      $tpl->setVar('fpm_user', $data['new']['system_user']);
      $tpl->setVar('fpm_group', $data['new']['system_group']);
      $tpl->setVar('fpm_domain', $data['new']['domain']);
      $tpl->setVar('pm', $data['new']['pm']);
      $tpl->setVar('pm_max_children', $data['new']['pm_max_children']);
      $tpl->setVar('pm_start_servers', $data['new']['pm_start_servers']);